This repository has been archived by the owner on Jun 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main_test.go
98 lines (89 loc) · 3.45 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"testing"
"github.com/rendon/testcli"
"github.com/stretchr/testify/assert"
)
var xTerrafileBinaryPath string
var workingDirectory string
func init() {
var err error
workingDirectory, err = os.Getwd()
if err != nil {
panic(err)
}
xTerrafileBinaryPath = workingDirectory + "/xterrafile"
}
func TestTerraformWithTerrafilePath(t *testing.T) {
folder, back := setup(t)
defer back()
testcli.Run(xTerrafileBinaryPath, "-f", fmt.Sprint(folder, "/Terrafile.test"), "install")
if !testcli.Success() {
t.Fatalf("Expected to succeed, but failed: %q with message: %q", testcli.Error(), testcli.Stderr())
}
// Assert output
for _, output := range []string{
"Removing all modules in vendor/modules",
"[terrafile-test-local] Copying from ./test/module",
"[terrafile-test-path] Fetching git::https://github.com/terraform-digitalocean-modules/terraform-digitalocean-droplet.git?ref=v0.1.7",
"[terrafile-test-commit] Fetching git::https://github.com/terraform-digitalocean-modules/terraform-digitalocean-droplet.git?ref=2e6b9729f3f6ea3ef5190bac0b0e1544a01fd80f",
"[terrafile-test-https] Fetching git::https://github.com/terraform-digitalocean-modules/terraform-digitalocean-droplet.git",
"[terrafile-test-branch] Fetching git::ssh://[email protected]/terraform-digitalocean-modules/terraform-digitalocean-droplet.git?ref=branch_test",
"[terrafile-test-tag] Fetching git::ssh://[email protected]/terraform-digitalocean-modules/terraform-digitalocean-droplet.git?ref=v0.1.7",
"[terrafile-test-registry] Found module version 0.1.7 at registry.terraform.io",
"[terrafile-test-registry] Fetching git::https://github.com/terraform-digitalocean-modules/terraform-digitalocean-droplet?ref=v0.1.7",
} {
assert.Contains(t, testcli.Stdout(), output)
}
// Assert files exist
for _, moduleName := range []string{
"terrafile-test-registry",
"terrafile-test-https",
"terrafile-test-tag",
"terrafile-test-branch",
"terrafile-test-commit",
"terrafile-test-path",
"terrafile-test-local",
} {
assert.DirExists(t, path.Join(workingDirectory, "vendor/modules", moduleName))
}
}
func setup(t *testing.T) (current string, back func()) {
folder, err := ioutil.TempDir("", "")
assert.NoError(t, err)
createTerrafile(t, folder)
return folder, func() {
assert.NoError(t, os.RemoveAll(folder))
}
}
func createFile(t *testing.T, filename string, contents string) {
assert.NoError(t, ioutil.WriteFile(filename, []byte(contents), 0644))
}
func createTerrafile(t *testing.T, folder string) {
var yaml = `terrafile-test-registry:
source: "terraform-digitalocean-modules/droplet/digitalocean"
version: "0.1.7"
terrafile-test-https:
source: "https://github.com/terraform-digitalocean-modules/terraform-digitalocean-droplet.git"
terrafile-test-tag:
source: "[email protected]:terraform-digitalocean-modules/terraform-digitalocean-droplet.git"
version: "v0.1.7"
terrafile-test-branch:
source: "[email protected]:terraform-digitalocean-modules/terraform-digitalocean-droplet.git"
version: "branch_test"
terrafile-test-commit:
source: "https://github.com/terraform-digitalocean-modules/terraform-digitalocean-droplet.git"
version: "2e6b9729f3f6ea3ef5190bac0b0e1544a01fd80f"
terrafile-test-path:
source: "https://github.com/terraform-digitalocean-modules/terraform-digitalocean-droplet.git"
version: "v0.1.7"
path: "examples/simple"
terrafile-test-local:
source: "./test/module"
`
createFile(t, path.Join(folder, "Terrafile.test"), yaml)
}