This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathsecrets_and_cfgmap_test.go
224 lines (189 loc) · 6.59 KB
/
secrets_and_cfgmap_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package tests_test
import (
"flag"
"time"
expect "github.com/google/goexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/pborman/uuid"
tests "kubevirt.io/kubevirt-ansible/tests/framework"
"kubevirt.io/kubevirt/pkg/config"
"kubevirt.io/kubevirt/pkg/kubecli"
ktests "kubevirt.io/kubevirt/tests"
)
var _ = Describe("[rfe_id:384][crit:medium][vendor:[email protected]][level:component]Config", func() {
flag.Parse()
virtClient, err := kubecli.GetKubevirtClient()
ktests.PanicOnError(err)
BeforeEach(func() {
ktests.BeforeTestCleanup()
})
Context("With a Secret and a ConfigMap defined", func() {
Context("With a single volume", func() {
var (
configMapName string
configMapPath string
secretName string
secretPath string
)
BeforeEach(func() {
configMapName = "configmap-" + uuid.NewRandom().String()
configMapPath = config.GetConfigMapSourcePath(configMapName + "-disk")
secretName = "secret-" + uuid.NewRandom().String()
secretPath = config.GetSecretSourcePath(secretName + "-disk")
config_data := map[string]string{
"config1": "value1",
"config2": "value2",
"config3": "value3",
}
secret_data := map[string]string{
"user": "admin",
"password": "redhat",
}
ktests.CreateConfigMap(configMapName, config_data)
ktests.CreateSecret(secretName, secret_data)
})
AfterEach(func() {
ktests.DeleteConfigMap(configMapName)
ktests.DeleteSecret(secretName)
})
It("[test_id:786]Should be that cfgMap and secret fs layout same for the pod and vmi", func() {
expectedOutput_cfgMap := "value1value2value3"
expectedOutput_Secret := "adminredhat"
By("Running VMI")
vmi := ktests.NewRandomVMIWithEphemeralDiskAndUserdataHighMemory(
ktests.ContainerDiskFor(
ktests.ContainerDiskFedora), "#!/bin/bash\necho \"fedora\" | passwd fedora --stdin\n")
ktests.AddConfigMapDisk(vmi, configMapName)
ktests.AddSecretDisk(vmi, secretName)
ktests.RunVMIAndExpectLaunch(vmi, false, 90)
By("Checking if ConfigMap has been attached to the pod")
vmiPod := ktests.GetRunningPodByVirtualMachineInstance(vmi, tests.NamespaceTestDefault)
podOutput_cfgMap, err := ktests.ExecuteCommandOnPod(
virtClient,
vmiPod,
vmiPod.Spec.Containers[1].Name,
[]string{"cat",
configMapPath + "/config1",
configMapPath + "/config2",
configMapPath + "/config3",
},
)
Expect(err).To(BeNil())
Expect(podOutput_cfgMap).To(Equal(expectedOutput_cfgMap))
By("Checking mounted ConfigMap image")
expecter, err := tests.LoggedInFedoraExpecter(vmi.Name, tests.NamespaceTestDefault, 360, false)
Expect(err).ToNot(HaveOccurred())
defer expecter.Close()
_, err = expecter.ExpectBatch([]expect.Batcher{
// mount ConfigMap image
&expect.BSnd{S: "sudo su -\n"},
&expect.BExp{R: "#"},
&expect.BSnd{S: "mount /dev/sda /mnt\n"},
&expect.BSnd{S: "echo $?\n"},
&expect.BExp{R: "0"},
&expect.BSnd{S: "cat /mnt/config1 /mnt/config2 /mnt/config3\n"},
&expect.BExp{R: expectedOutput_cfgMap},
}, 200*time.Second)
Expect(err).ToNot(HaveOccurred())
By("Checking if Secret has also been attached to the same pod")
podOutput_Secret, err := ktests.ExecuteCommandOnPod(
virtClient,
vmiPod,
vmiPod.Spec.Containers[1].Name,
[]string{"cat",
secretPath + "/user",
secretPath + "/password",
},
)
Expect(err).To(BeNil())
Expect(podOutput_Secret).To(Equal(expectedOutput_Secret))
By("Checking mounted secret image")
_, err = expecter.ExpectBatch([]expect.Batcher{
// mount Secret image
&expect.BSnd{S: "mount /dev/sdb /mnt\n"},
&expect.BSnd{S: "echo $?\n"},
&expect.BExp{R: "0"},
&expect.BSnd{S: "cat /mnt/user /mnt/password\n"},
&expect.BExp{R: expectedOutput_Secret},
}, 200*time.Second)
Expect(err).ToNot(HaveOccurred())
})
})
})
Context("With SSH Keys as a Secret defined", func() {
Context("With a single volume", func() {
var (
secretName string
secretPath string
)
var bitSize int = 2048
privateKey, _ := tests.GeneratePrivateKey(bitSize)
publicKeyBytes, _ := tests.GeneratePublicKey(&privateKey.PublicKey)
privateKeyBytes := tests.EncodePrivateKeyToPEM(privateKey)
BeforeEach(func() {
secretName = "secret-" + uuid.NewRandom().String()
secretPath = config.GetSecretSourcePath(secretName + "-disk")
data := map[string]string{
"ssh-privatekey": string(privateKeyBytes),
"ssh-publickey": string(publicKeyBytes),
}
ktests.CreateSecret(secretName, data)
})
AfterEach(func() {
ktests.DeleteSecret(secretName)
})
It("[test_id:778]Should be the fs layout the same for a pod and vmi", func() {
expectedPrivateKey := string(privateKeyBytes)
expectedPublicKey := string(publicKeyBytes)
By("Running VMI")
vmi := ktests.NewRandomVMIWithEphemeralDiskAndUserdataHighMemory(
ktests.ContainerDiskFor(
ktests.ContainerDiskFedora), "#!/bin/bash\necho \"fedora\" | passwd fedora --stdin\n")
ktests.AddSecretDisk(vmi, secretName)
ktests.RunVMIAndExpectLaunch(vmi, false, 90)
By("Checking if Secret has been attached to the pod")
vmiPod := ktests.GetRunningPodByVirtualMachineInstance(vmi, tests.NamespaceTestDefault)
podOutput1, err := ktests.ExecuteCommandOnPod(
virtClient,
vmiPod,
vmiPod.Spec.Containers[1].Name,
[]string{"cat",
secretPath + "/ssh-privatekey",
},
)
Expect(err).To(BeNil())
Expect(podOutput1).To(Equal(expectedPrivateKey))
podOutput2, err := ktests.ExecuteCommandOnPod(
virtClient,
vmiPod,
vmiPod.Spec.Containers[1].Name,
[]string{"cat",
secretPath + "/ssh-publickey",
},
)
Expect(err).To(BeNil())
Expect(podOutput2).To(Equal(expectedPublicKey))
By("Checking mounted secrets sshkeys image")
expecter, err := tests.LoggedInFedoraExpecter(vmi.Name, tests.NamespaceTestDefault, 360, false)
Expect(err).ToNot(HaveOccurred())
defer expecter.Close()
_, err = expecter.ExpectBatch([]expect.Batcher{
// mount iso Secret image
&expect.BSnd{S: "sudo su -\n"},
&expect.BExp{R: "#"},
&expect.BSnd{S: "mount /dev/sda /mnt\n"},
&expect.BSnd{S: "echo $?\n"},
&expect.BExp{R: "0"},
&expect.BSnd{S: "grep \"PRIVATE KEY\" /mnt/ssh-privatekey\n"},
&expect.BSnd{S: "echo $?\n"},
&expect.BExp{R: "0"},
&expect.BSnd{S: "grep ssh-rsa /mnt/ssh-publickey\n"},
&expect.BSnd{S: "echo $?\n"},
&expect.BExp{R: "0"},
}, 200*time.Second)
Expect(err).ToNot(HaveOccurred())
})
})
})
})