Skip to content

Commit 4416495

Browse files
committed
add e2e tests
1 parent cb80821 commit 4416495

File tree

7 files changed

+157
-24
lines changed

7 files changed

+157
-24
lines changed

test/e2e/cli.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/e2e/cli_test.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

test/e2e/clu2adv_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package e2e_test
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/mongodb-labs/atlas-cli-plugin-terraform/test/e2e"
8+
"github.com/spf13/afero"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestClu2AdvParams(t *testing.T) {
14+
cwd, err := os.Getwd()
15+
require.NoError(t, err)
16+
var (
17+
prefix = cwd + "/testdata/"
18+
fileIn = prefix + "clu2adv.in.tf"
19+
fileOut = prefix + "clu2adv.out.tf"
20+
fileExpected = prefix + "clu2adv.expected.tf"
21+
fileUnexisting = prefix + "clu2adv.unexisting.tf"
22+
fs = afero.NewOsFs()
23+
)
24+
tests := map[string]struct {
25+
expectedErrContains string
26+
assertFunc func(t *testing.T)
27+
args []string
28+
}{
29+
"no params": {
30+
expectedErrContains: "required flag(s) \"file\", \"output\" not set",
31+
},
32+
"no input file": {
33+
args: []string{"--output", fileOut},
34+
expectedErrContains: "required flag(s) \"file\" not set",
35+
},
36+
"no output file": {
37+
args: []string{"--file", fileIn},
38+
expectedErrContains: "required flag(s) \"output\" not set",
39+
},
40+
"unexisting input file": {
41+
args: []string{"--file", fileUnexisting, "--output", fileOut},
42+
expectedErrContains: "file must exist: " + fileUnexisting,
43+
},
44+
"existing output file without replaceOutput flag": {
45+
args: []string{"--file", fileIn, "--output", fileExpected},
46+
expectedErrContains: "file must not exist: " + fileExpected,
47+
},
48+
"basic use": {
49+
args: []string{"--file", fileIn, "--output", fileOut},
50+
assertFunc: func(t *testing.T) { e2e.CompareFiles(t, fs, fileOut, fileExpected) },
51+
},
52+
}
53+
for name, tc := range tests {
54+
t.Run(name, func(t *testing.T) {
55+
resp, err := e2e.RunClu2Adv(tc.args...)
56+
assert.Equal(t, tc.expectedErrContains == "", err == nil)
57+
if err == nil {
58+
assert.Empty(t, resp)
59+
if tc.assertFunc != nil {
60+
tc.assertFunc(t)
61+
}
62+
} else {
63+
assert.Contains(t, resp, tc.expectedErrContains)
64+
}
65+
_ = fs.Remove(fileOut) // Ensure the output file does not exist in case it was generated in some test case
66+
})
67+
}
68+
}

test/e2e/e2e_helper.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package e2e
2+
3+
import (
4+
"os/exec"
5+
"testing"
6+
7+
"github.com/spf13/afero"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func RunTF(args ...string) (string, error) {
13+
args = append([]string{"tf"}, args...)
14+
cmd := exec.Command("atlas", args...)
15+
resp, err := cmd.CombinedOutput()
16+
return string(resp), err
17+
}
18+
19+
func RunClu2Adv(args ...string) (string, error) {
20+
args = append([]string{"clu2adv"}, args...)
21+
return RunTF(args...)
22+
}
23+
24+
func CompareFiles(t *testing.T, fs afero.Fs, file1, file2 string) {
25+
t.Helper()
26+
data1, err1 := afero.ReadFile(fs, file1)
27+
require.NoError(t, err1)
28+
data2, err2 := afero.ReadFile(fs, file2)
29+
require.NoError(t, err2)
30+
assert.Equal(t, string(data1), string(data2))
31+
}

test/e2e/run_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package e2e_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/mongodb-labs/atlas-cli-plugin-terraform/test/e2e"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestRunTF(t *testing.T) {
11+
resp, err := e2e.RunTF()
12+
require.NoError(t, err, resp)
13+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
resource "mongodbatlas_advanced_cluster" "cluster" {
2+
project_id = var.project_id
3+
name = "cluster"
4+
cluster_type = "REPLICASET"
5+
replication_specs = [
6+
{
7+
region_configs = [
8+
{
9+
provider_name = "AWS"
10+
region_name = "US_EAST_1"
11+
priority = 7
12+
electable_specs = {
13+
node_count = 3
14+
instance_size = "M10"
15+
}
16+
}
17+
]
18+
}
19+
]
20+
tags = {
21+
environment = "dev"
22+
}
23+
24+
# Generated by atlas-cli-plugin-terraform.
25+
# Please confirm that all references to this resource are updated.
26+
}

test/e2e/testdata/clu2adv.in.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
resource "mongodbatlas_cluster" "cluster" {
2+
project_id = var.project_id
3+
name = "cluster"
4+
cluster_type = "REPLICASET"
5+
provider_name = "AWS"
6+
provider_instance_size_name = "M10"
7+
replication_specs {
8+
num_shards = 1
9+
regions_config {
10+
region_name = "US_EAST_1"
11+
electable_nodes = 3
12+
priority = 7
13+
}
14+
}
15+
tags {
16+
key = "environment"
17+
value = "dev"
18+
}
19+
}

0 commit comments

Comments
 (0)