Skip to content

Commit 68058ce

Browse files
committed
helm: add flex support
1 parent b5957b6 commit 68058ce

File tree

5 files changed

+131
-1
lines changed

5 files changed

+131
-1
lines changed

test/helm/flex_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package helm
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
9+
akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1"
10+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1/common"
11+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/cmd"
12+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/decoder"
13+
)
14+
15+
func TestFlexSpec(t *testing.T) {
16+
output := cmd.RunCommand(t, "helm", "template", "--values=flex_values.yaml", "../../helm-charts/charts/atlas-deployment")
17+
objects := decoder.DecodeAll(t, output)
18+
19+
var gotDeployment *akov2.AtlasDeployment
20+
for _, obj := range objects {
21+
if d, ok := obj.(*akov2.AtlasDeployment); ok {
22+
if gotDeployment != nil {
23+
t.Errorf("Expect one deployment only but also got: %v", d)
24+
continue
25+
}
26+
gotDeployment = d
27+
}
28+
}
29+
30+
// ignore
31+
gotDeployment.Kind = ""
32+
gotDeployment.APIVersion = ""
33+
gotDeployment.Labels = nil
34+
35+
wantDeployment := &akov2.AtlasDeployment{
36+
ObjectMeta: metav1.ObjectMeta{
37+
Name: "flex-instance",
38+
Namespace: "default",
39+
},
40+
Spec: akov2.AtlasDeploymentSpec{
41+
FlexSpec: &akov2.FlexSpec{
42+
Name: "flex-instance",
43+
ProviderSettings: &akov2.FlexProviderSettings{
44+
BackingProviderName: "AWS",
45+
RegionName: "US_EAST_1",
46+
},
47+
},
48+
ProjectDualReference: akov2.ProjectDualReference{
49+
ProjectRef: &common.ResourceRefNamespaced{
50+
Name: "release-name-my-project",
51+
},
52+
ExternalProjectRef: nil,
53+
ConnectionSecret: nil,
54+
},
55+
},
56+
}
57+
58+
require.Equal(t, wantDeployment, gotDeployment)
59+
}

test/helm/flex_values.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
deployments:
2+
- flexSpec:
3+
name: flex-instance
4+
providerSettings:
5+
backingProviderName: AWS
6+
regionName: US_EAST_1

test/helper/cmd/cmd.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"os/exec"
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
// RunCommand executes the given command with the given arguments
13+
// and returns the resulting stdout and stderr as an io.Reader.
14+
//
15+
// If the command fails to run, the given test is being failed immediately.
16+
func RunCommand(t *testing.T, name string, args ...string) io.Reader {
17+
var result bytes.Buffer
18+
cmd := exec.Command(name, args...)
19+
cmd.Stdout = &result
20+
cmd.Stderr = &result
21+
require.NoError(t, cmd.Run())
22+
return &result
23+
}

test/helper/decoder/decoder.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package decoder
2+
3+
import (
4+
"bufio"
5+
"io"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
"k8s.io/apimachinery/pkg/runtime"
10+
"k8s.io/apimachinery/pkg/runtime/serializer"
11+
"k8s.io/apimachinery/pkg/util/yaml"
12+
"k8s.io/client-go/kubernetes/scheme"
13+
14+
akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1"
15+
)
16+
17+
// DecodeAll decodes runtime objects using the core Kubernetes scheme
18+
// and the AKO scheme.
19+
//
20+
// If scheme registration or decoding fails, the given test fails immediately.
21+
func DecodeAll(t *testing.T, from io.Reader) []runtime.Object {
22+
s := runtime.NewScheme()
23+
require.NoError(t, scheme.AddToScheme(s))
24+
require.NoError(t, akov2.AddToScheme(s))
25+
26+
decoder := serializer.NewCodecFactory(s).UniversalDeserializer()
27+
reader := yaml.NewYAMLReader(bufio.NewReader(from))
28+
29+
var result []runtime.Object
30+
for {
31+
buf, err := reader.Read()
32+
if err == io.EOF {
33+
break
34+
}
35+
require.NoError(t, err)
36+
obj, _, err := decoder.Decode(buf, nil, nil)
37+
require.NoError(t, err)
38+
result = append(result, obj)
39+
}
40+
41+
return result
42+
}

0 commit comments

Comments
 (0)