-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathksv_test.go
121 lines (111 loc) · 2.42 KB
/
ksv_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"strings"
"testing"
)
var encodedYamlData = `
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: YWRtaW4=
password: MWYyZDFlMmU2N2Rm
`
var decodedYamlData = `
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: admin
password: 1f2d1e2e67df
`
func TestEncode(t *testing.T) {
s, err := encodeToBase64(strings.NewReader(decodedYamlData))
if err != nil {
t.Error("Can't parse yaml doc")
}
if s.APIVersion != "v1" {
t.Error("Invalid API version")
}
if s.Kind != "Secret" {
t.Error("Invalid Kind")
}
if s.Data["username"] != "YWRtaW4=" {
t.Error("Invalid username value")
}
if s.Data["password"] != "MWYyZDFlMmU2N2Rm" {
t.Error("Invalid password value")
}
if len(s.StringData) != 0 {
t.Error("StringData should be empty")
}
}
func TestDecode(t *testing.T) {
s, err := decodeFromBase64(strings.NewReader(encodedYamlData), false)
if err != nil {
t.Error("Can't parse yaml doc")
}
if s.APIVersion != "v1" {
t.Error("Invalid API version")
}
if s.Kind != "Secret" {
t.Error("Invalid Kind")
}
if s.Data["username"] != "admin" {
t.Error("Invalid username value")
}
if s.Data["password"] != "1f2d1e2e67df" {
t.Error("Invalid password value")
}
if len(s.StringData) != 0 {
t.Error("StringData should be empty")
}
}
func TestDecodeToStringData(t *testing.T) {
s, err := decodeFromBase64(strings.NewReader(encodedYamlData), true)
if err != nil {
t.Error("Can't parse yaml doc")
}
if s.APIVersion != "v1" {
t.Error("Invalid API version")
}
if s.Kind != "Secret" {
t.Error("Invalid Kind")
}
if len(s.Data) != 0 {
t.Error("Data should be empty")
}
if len(s.StringData) != 2 {
t.Error("Not enough keys in stringData")
}
if s.StringData["username"] != "admin" {
t.Error("Invalid username value")
}
if s.StringData["password"] != "1f2d1e2e67df" {
t.Error("Invalid password value")
}
secretYaml, err := secretToYamlString(s)
if err != nil {
t.Error("Error converting secret to yaml")
}
if !strings.Contains(secretYaml, "stringData") {
t.Error("Doesn't contain the stringData key")
}
}
func TestAdd(t *testing.T) {
s, err := addKey(strings.NewReader(encodedYamlData), "foo", "bar")
if err != nil {
t.Error("Can't parse yaml doc")
}
sobj, err := decodeFromBase64(strings.NewReader(s), false)
if err != nil {
t.Error("Can't decode")
}
if sobj.Data["foo"] != "bar" {
t.Error("foo != bar")
}
}