-
Notifications
You must be signed in to change notification settings - Fork 2
/
secrets_test.go
100 lines (87 loc) · 2.46 KB
/
secrets_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
99
100
package secrets_test
import (
"encoding/json"
"io/ioutil"
"testing"
"github.com/ijustfool/docker-secrets"
)
const (
secretDir = "test-data"
userVal = "root"
passVal = "myPass"
)
type testSecrets struct {
User string
Password string `mapstructure:"pass"`
}
func TestNewDockerSecrets(t *testing.T) {
_, err := secrets.NewDockerSecrets(secretDir)
if err != nil {
t.Errorf("NewDockerSecrets(): %v", err)
return
}
}
func TestDockerSecrets_GetAll(t *testing.T) {
dockerSecrets, _ := secrets.NewDockerSecrets(secretDir)
allSecrets := dockerSecrets.GetAll()
if _, ok := allSecrets["user"]; !ok {
t.Errorf("allSecrets[\"user\"] not exsist, expected: `%v`", userVal)
}
if _, ok := allSecrets["pass"]; !ok {
t.Errorf("allSecrets[\"pass\"] not exsist, expected: `%v`", passVal)
}
}
func TestDockerSecrets_Get(t *testing.T) {
dockerSecrets, _ := secrets.NewDockerSecrets(secretDir)
checkKey(t, dockerSecrets, "user", userVal)
checkKey(t, dockerSecrets, "pass", passVal)
}
func TestDockerSecrets_Unmarshal(t *testing.T) {
dockerSecrets, _ := secrets.NewDockerSecrets(secretDir)
testSecrets := testSecrets{}
err := dockerSecrets.Unmarshal(&testSecrets)
if err != nil {
t.Errorf("dockerSecrets.Unmarshal(): %v", err)
return
}
if testSecrets.User != userVal {
t.Errorf("testSecrets.User = `%v`, expected: `%v`", testSecrets.User, userVal)
}
if testSecrets.Password != passVal {
t.Errorf("testSecrets.Password = `%v`, expected: `%v`", testSecrets.Password, passVal)
}
}
func TestReplaceInFile(t *testing.T) {
b, err := ioutil.ReadFile(secretDir + "/config.json")
if err != nil {
t.Errorf("ReplaceInFile(): %v", err)
return
}
b, err = secrets.ReplaceInFile(b)
if err != nil {
t.Errorf("ReplaceInFile(): %v", err)
return
}
testSecrets := testSecrets{}
err = json.Unmarshal(b, &testSecrets)
if err != nil {
t.Errorf("ReplaceInFile(): %v", err)
return
}
if testSecrets.User != userVal {
t.Errorf("testSecrets.User = `%v`, expected: `%v`", testSecrets.User, userVal)
}
if testSecrets.Password != passVal {
t.Errorf("testSecrets.Password = `%v`, expected: `%v`", testSecrets.Password, passVal)
}
}
func checkKey(t *testing.T, dockerSecrets *secrets.DockerSecrets, key, expectedValue string) {
value, err := dockerSecrets.Get(key)
if err != nil {
t.Errorf("dockerSecrets.Get(\"%s\"): %s", key, err.Error())
return
}
if expectedValue != value {
t.Errorf("dockerSecrets.Get(\"%s\") = `%s`, expected: `%s`", key, value, expectedValue)
}
}