Skip to content

Commit 4230ede

Browse files
author
Otávio Fernandes
committed
End-to-end testing.
1 parent 46a74db commit 4230ede

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

test/e2e/vault_handler_test.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package e2e
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"testing"
7+
8+
vh "github.com/otaviof/vault-handler/pkg/vault-handler"
9+
log "github.com/sirupsen/logrus"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
var manifestFiles = []string{"../mock/manifest-1.yaml", "../mock/manifest-2.yaml"}
14+
15+
var config = &vh.Config{
16+
VaultAddr: "http://127.0.0.1:8200",
17+
InputDir: "../mock/input-dir",
18+
OutputDir: "/tmp",
19+
VaultRoleID: os.Getenv("VAULT_HANDLER_VAULT_ROLE_ID"),
20+
VaultSecretID: os.Getenv("VAULT_HANDLER_VAULT_SECRET_ID"),
21+
}
22+
23+
func TestVaultHandler(t *testing.T) {
24+
log.SetLevel(log.TraceLevel)
25+
26+
cleanUp(t)
27+
28+
t.Run("DRY-RUN upload", uploadDryRun)
29+
t.Run("upload", upload)
30+
t.Run("DRY-RUN download", downloadDryRun)
31+
t.Run("download", download)
32+
t.Run("compare", compare)
33+
}
34+
35+
func loopOverManifests(t *testing.T, fn func(t *testing.T, manifest *vh.Manifest)) {
36+
for _, manifestFile := range manifestFiles {
37+
manifest, err := vh.NewManifest(manifestFile)
38+
assert.Nil(t, err)
39+
fn(t, manifest)
40+
}
41+
}
42+
43+
func loopOverGroupSecrets(
44+
t *testing.T, manifest *vh.Manifest, fn func(t *testing.T, group string, data *vh.SecretData),
45+
) {
46+
for group, secrets := range manifest.Secrets {
47+
for _, data := range secrets.Data {
48+
fn(t, group, &data)
49+
}
50+
}
51+
}
52+
53+
func readFile(t *testing.T, path string) []byte {
54+
fileBytes, err := ioutil.ReadFile(path)
55+
assert.Nil(t, err)
56+
return fileBytes
57+
}
58+
59+
func cleanUp(t *testing.T) {
60+
loopOverManifests(t, func(t *testing.T, manifest *vh.Manifest) {
61+
loopOverGroupSecrets(t, manifest, func(t *testing.T, group string, data *vh.SecretData) {
62+
file := vh.NewFile(group, data, nil)
63+
path := file.FilePath(config.OutputDir)
64+
t.Logf("Excluding file: '%s'", path)
65+
_ = os.Remove(path)
66+
})
67+
})
68+
}
69+
70+
func spinUpNewHandler(t *testing.T, dryRun bool) *vh.Handler {
71+
config.DryRun = dryRun
72+
handler, err := vh.NewHandler(config)
73+
assert.Nil(t, err)
74+
75+
err = handler.Authenticate()
76+
assert.Nil(t, err)
77+
78+
return handler
79+
}
80+
81+
func uploadDryRun(t *testing.T) {
82+
handler := spinUpNewHandler(t, true)
83+
84+
loopOverManifests(t, func(t *testing.T, manifest *vh.Manifest) {
85+
err := handler.Upload(manifest)
86+
assert.Nil(t, err)
87+
})
88+
}
89+
90+
func upload(t *testing.T) {
91+
handler := spinUpNewHandler(t, false)
92+
93+
loopOverManifests(t, func(t *testing.T, manifest *vh.Manifest) {
94+
err := handler.Upload(manifest)
95+
assert.Nil(t, err)
96+
})
97+
}
98+
99+
func downloadDryRun(t *testing.T) {
100+
handler := spinUpNewHandler(t, true)
101+
102+
loopOverManifests(t, func(t *testing.T, manifest *vh.Manifest) {
103+
err := handler.Download(manifest)
104+
assert.Nil(t, err)
105+
})
106+
}
107+
108+
func download(t *testing.T) {
109+
handler := spinUpNewHandler(t, false)
110+
111+
loopOverManifests(t, func(t *testing.T, manifest *vh.Manifest) {
112+
err := handler.Download(manifest)
113+
assert.Nil(t, err)
114+
})
115+
}
116+
117+
func compare(t *testing.T) {
118+
loopOverManifests(t, func(t *testing.T, manifest *vh.Manifest) {
119+
loopOverGroupSecrets(t, manifest, func(t *testing.T, group string, data *vh.SecretData) {
120+
file := vh.NewFile(group, data, nil)
121+
pathIn := file.FilePath(config.InputDir)
122+
pathOut := file.FilePath(config.OutputDir)
123+
124+
assert.FileExists(t, pathOut)
125+
t.Logf("Comparing files: '%s' vs. '%s'", pathIn, pathOut)
126+
assert.Equal(t, string(readFile(t, pathIn)), string(readFile(t, pathOut)))
127+
})
128+
})
129+
}

0 commit comments

Comments
 (0)