Skip to content

Commit 1a88228

Browse files
authored
Merge pull request #1234 from saschagrunert/dedup
Dedup test code and activate linter
2 parents e010ebf + 5b33775 commit 1a88228

File tree

3 files changed

+41
-102
lines changed

3 files changed

+41
-102
lines changed

.golangci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ linters:
66
disable-all: true
77
enable:
88
- depguard
9+
- dupl
910
- durationcheck
1011
- forcetypeassert
1112
- gofmt
@@ -22,13 +23,12 @@ linters:
2223
- promlinter
2324
- tagliatelle
2425
- typecheck
25-
- wastedassign
2626
- unused
27+
- wastedassign
2728
# - asciicheck
2829
# - bodyclose
2930
# - cyclop
3031
# - dogsled
31-
# - dupl
3232
# - errcheck
3333
# - errorlint
3434
# - exhaustive

pkg/validate/container_linux.go

Lines changed: 23 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ var _ = framework.KubeDescribe("Container Mount Propagation", func() {
5959
rc.RemovePodSandbox(context.TODO(), podID)
6060
})
6161

62-
It("mount with 'rprivate' should not support propagation", func() {
62+
testMountPropagation := func(propagation runtimeapi.MountPropagation) {
6363
By("create host path and flag file")
64-
mntSource, propagationSrcDir, propagationMntPoint, clearHostPath := createHostPathForMountPropagation(podID, runtimeapi.MountPropagation_PROPAGATION_PRIVATE)
64+
mntSource, propagationSrcDir, propagationMntPoint, clearHostPath := createHostPathForMountPropagation(podID, propagation)
6565
defer clearHostPath() // clean up the TempDir
6666

6767
By("create container with volume")
68-
containerID := createMountPropagationContainer(rc, ic, "mount-propagation-test-", podID, podConfig, mntSource, runtimeapi.MountPropagation_PROPAGATION_PRIVATE)
68+
containerID := createMountPropagationContainer(rc, ic, "mount-propagation-test-", podID, podConfig, mntSource, propagation)
6969

7070
By("test start container with volume")
7171
testStartContainer(rc, containerID)
@@ -76,7 +76,13 @@ var _ = framework.KubeDescribe("Container Mount Propagation", func() {
7676
By("check whether propagationMntPoint contains file or dir in container")
7777
command := []string{"ls", "-A", propagationMntPoint}
7878
output := execSyncContainer(rc, containerID, command)
79-
Expect(len(output)).To(BeZero(), "len(output) should be zero.")
79+
80+
switch propagation {
81+
case runtimeapi.MountPropagation_PROPAGATION_PRIVATE:
82+
Expect(len(output)).To(BeZero(), "len(output) should be zero.")
83+
case runtimeapi.MountPropagation_PROPAGATION_BIDIRECTIONAL, runtimeapi.MountPropagation_PROPAGATION_HOST_TO_CONTAINER:
84+
Expect(len(output)).NotTo(BeZero(), "len(output) should not be zero.")
85+
}
8086

8187
By("create a directory named containerMntPoint as a mount point in container")
8288
containerMntPoint := path.Join(mntSource, "containerMntPoint")
@@ -90,75 +96,25 @@ var _ = framework.KubeDescribe("Container Mount Propagation", func() {
9096
By("check whether containerMntPoint contains file or dir in host")
9197
fileInfo, err := ioutil.ReadDir(containerMntPoint)
9298
framework.ExpectNoError(err, "failed to ReadDir %q in Host", containerMntPoint)
93-
Expect(len(fileInfo)).To(BeZero(), "len(fileInfo) should be zero.")
94-
})
95-
96-
It("mount with 'rshared' should support propagation from host to container and vice versa", func() {
97-
By("create host path and flag file")
98-
mntSource, propagationSrcDir, propagationMntPoint, clearHostPath := createHostPathForMountPropagation(podID, runtimeapi.MountPropagation_PROPAGATION_BIDIRECTIONAL)
99-
defer clearHostPath() // clean up the TempDir
100-
101-
By("create container with volume")
102-
containerID := createMountPropagationContainer(rc, ic, "mount-propagation-test-", podID, podConfig, mntSource, runtimeapi.MountPropagation_PROPAGATION_BIDIRECTIONAL)
103-
104-
By("test start container with volume")
105-
testStartContainer(rc, containerID)
106-
107-
By("create a propatation mount point in host")
108-
createPropagationMountPoint(propagationSrcDir, propagationMntPoint)
10999

110-
By("check whether propagationMntPoint contains file or dir in container")
111-
command := []string{"ls", "-A", propagationMntPoint}
112-
output := execSyncContainer(rc, containerID, command)
113-
Expect(len(output)).NotTo(BeZero(), "len(output) should not be zero.")
114-
115-
By("create a directory named containerMntPoint as a mount point in container")
116-
containerMntPoint := path.Join(mntSource, "containerMntPoint")
117-
command = []string{"sh", "-c", "mkdir -p " + containerMntPoint}
118-
execSyncContainer(rc, containerID, command)
100+
switch propagation {
101+
case runtimeapi.MountPropagation_PROPAGATION_PRIVATE, runtimeapi.MountPropagation_PROPAGATION_HOST_TO_CONTAINER:
102+
Expect(len(fileInfo)).To(BeZero(), "len(fileInfo) should be zero.")
103+
case runtimeapi.MountPropagation_PROPAGATION_BIDIRECTIONAL:
104+
Expect(len(fileInfo)).NotTo(BeZero(), "len(fileInfo) should not be zero.")
105+
}
106+
}
119107

120-
By("mount /etc to the mount point in container")
121-
command = []string{"sh", "-c", "mount --bind /etc " + containerMntPoint}
122-
execSyncContainer(rc, containerID, command)
108+
It("mount with 'rprivate' should not support propagation", func() {
109+
testMountPropagation(runtimeapi.MountPropagation_PROPAGATION_PRIVATE)
110+
})
123111

124-
By("check whether containerMntPoint contains file or dir in host")
125-
fileInfo, err := ioutil.ReadDir(containerMntPoint)
126-
framework.ExpectNoError(err, "failed to ReadDir %q in Host", containerMntPoint)
127-
Expect(len(fileInfo)).NotTo(BeZero(), "len(fileInfo) should not be zero.")
112+
It("mount with 'rshared' should support propagation from host to container and vice versa", func() {
113+
testMountPropagation(runtimeapi.MountPropagation_PROPAGATION_BIDIRECTIONAL)
128114
})
129115

130116
It("mount with 'rslave' should support propagation from host to container", func() {
131-
By("create host path and flag file")
132-
mntSource, propagationSrcDir, propagationMntPoint, clearHostPath := createHostPathForMountPropagation(podID, runtimeapi.MountPropagation_PROPAGATION_HOST_TO_CONTAINER)
133-
defer clearHostPath() // clean up the TempDir
134-
135-
By("create container with volume")
136-
containerID := createMountPropagationContainer(rc, ic, "mount-propagation-test-", podID, podConfig, mntSource, runtimeapi.MountPropagation_PROPAGATION_HOST_TO_CONTAINER)
137-
138-
By("test start container with volume")
139-
testStartContainer(rc, containerID)
140-
141-
By("create a propatation mount point in host")
142-
createPropagationMountPoint(propagationSrcDir, propagationMntPoint)
143-
144-
By("check whether propagationMntPoint contains file or dir in container")
145-
command := []string{"ls", "-A", propagationMntPoint}
146-
output := execSyncContainer(rc, containerID, command)
147-
Expect(len(output)).NotTo(BeZero(), "len(output) should not be zero.")
148-
149-
By("create a directory named containerMntPoint as a mount point in container")
150-
containerMntPoint := path.Join(mntSource, "containerMntPoint")
151-
command = []string{"sh", "-c", "mkdir -p " + containerMntPoint}
152-
execSyncContainer(rc, containerID, command)
153-
154-
By("mount /etc to the mount point in container")
155-
command = []string{"sh", "-c", "mount --bind /etc " + containerMntPoint}
156-
execSyncContainer(rc, containerID, command)
157-
158-
By("check whether containerMntPoint contains file or dir in host")
159-
fileInfo, err := ioutil.ReadDir(containerMntPoint)
160-
framework.ExpectNoError(err, "failed to ReadDir %q in Host", containerMntPoint)
161-
Expect(len(fileInfo)).To(BeZero(), "len(fileInfo) should be zero.")
117+
testMountPropagation(runtimeapi.MountPropagation_PROPAGATION_HOST_TO_CONTAINER)
162118
})
163119
})
164120
})

pkg/validate/security_context_linux.go

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,17 @@ var _ = framework.KubeDescribe("Security Context", func() {
126126

127127
})
128128

129-
It("runtime should support HostIpc is true", func() {
129+
testHostIPC := func(mode runtimeapi.NamespaceMode) {
130130
By("create shared memory segment on the host")
131131
out, err := exec.Command("ipcmk", "-M", "1048576").Output()
132132
framework.ExpectNoError(err, "failed to execute ipcmk -M 1048576")
133133
rawID := strings.TrimSpace(string(out))
134134
segmentID := strings.TrimPrefix(rawID, "Shared memory id: ")
135135

136-
By("create podSandbox for security context HostIPC is true")
136+
By("create podSandbox for security context HostIPC is " + mode.String())
137137
namespaceOption := &runtimeapi.NamespaceOption{
138138
Pid: runtimeapi.NamespaceMode_POD,
139-
Ipc: runtimeapi.NamespaceMode_NODE,
139+
Ipc: mode,
140140
Network: runtimeapi.NamespaceMode_POD,
141141
}
142142
podID, podConfig = createNamespacePodSandbox(rc, namespaceOption, podSandboxName, "")
@@ -152,42 +152,25 @@ var _ = framework.KubeDescribe("Security Context", func() {
152152
return getContainerStatus(rc, containerID).State
153153
}, time.Minute, time.Second*4).Should(Equal(runtimeapi.ContainerState_CONTAINER_RUNNING))
154154

155-
By("check if the shared memory segment is included in the container")
155+
By("check if the shared memory segment is (not) included in the container")
156156
command := []string{"ipcs", "-m"}
157157
o := execSyncContainer(rc, containerID, command)
158-
Expect(o).To(ContainSubstring(segmentID), "The shared memory segment should be included in the container")
159-
})
160158

161-
It("runtime should support HostIpc is false", func() {
162-
By("create shared memory segment on the host")
163-
out, err := exec.Command("ipcmk", "-M", "1048576").Output()
164-
framework.ExpectNoError(err, "failed to execute ipcmk -M 1048576")
165-
rawID := strings.TrimSpace(string(out))
166-
segmentID := strings.TrimPrefix(rawID, "Shared memory id: ")
167-
168-
By("create podSandbox for security context HostIpc is false")
169-
namespaceOption := &runtimeapi.NamespaceOption{
170-
Pid: runtimeapi.NamespaceMode_POD,
171-
Ipc: runtimeapi.NamespaceMode_POD,
172-
Network: runtimeapi.NamespaceMode_POD,
159+
const substr = "The shared memory segment should be included in the container"
160+
switch mode {
161+
case runtimeapi.NamespaceMode_NODE:
162+
Expect(o).To(ContainSubstring(segmentID), substr)
163+
case runtimeapi.NamespaceMode_POD:
164+
Expect(o).NotTo(ContainSubstring(segmentID), substr)
173165
}
174-
podID, podConfig = createNamespacePodSandbox(rc, namespaceOption, podSandboxName, "")
175-
176-
By("create a default container with namespace")
177-
prefix := "namespace-container-"
178-
containerName := prefix + framework.NewUUID()
179-
containerID, _, _ := createNamespaceContainer(rc, ic, podID, podConfig, containerName, framework.TestContext.TestImageList.DefaultTestContainerImage, namespaceOption, pauseCmd, "")
166+
}
180167

181-
By("start container")
182-
startContainer(rc, containerID)
183-
Eventually(func() runtimeapi.ContainerState {
184-
return getContainerStatus(rc, containerID).State
185-
}, time.Minute, time.Second*4).Should(Equal(runtimeapi.ContainerState_CONTAINER_RUNNING))
168+
It("runtime should support HostIpc is true", func() {
169+
testHostIPC(runtimeapi.NamespaceMode_NODE)
170+
})
186171

187-
By("check if the shared memory segment is not included in the container")
188-
command := []string{"ipcs", "-m"}
189-
o := execSyncContainer(rc, containerID, command)
190-
Expect(o).NotTo(ContainSubstring(segmentID), "The shared memory segment should be included in the container")
172+
It("runtime should support HostIpc is false", func() {
173+
testHostIPC(runtimeapi.NamespaceMode_POD)
191174
})
192175

193176
It("runtime should support PodPID", func() {

0 commit comments

Comments
 (0)