Skip to content

Commit 4451410

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request kubernetes#37647 from bowei/priv-pod-e2e
Automatic merge from submit-queue (batch tested with PRs 37692, 37785, 37647, 37941, 37856) Remove extraneous curl, pods, etc from privileged pod test
2 parents 8256fa1 + 67fec72 commit 4451410

File tree

1 file changed

+52
-83
lines changed

1 file changed

+52
-83
lines changed

test/e2e/common/privileged.go

Lines changed: 52 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -17,129 +17,98 @@ limitations under the License.
1717
package common
1818

1919
import (
20-
"encoding/json"
2120
"fmt"
22-
"net/url"
2321

2422
. "github.com/onsi/ginkgo"
2523
. "github.com/onsi/gomega"
2624
"k8s.io/kubernetes/pkg/api/v1"
2725
"k8s.io/kubernetes/test/e2e/framework"
2826
)
2927

30-
const (
31-
privilegedPodName = "privileged-pod"
32-
privilegedContainerName = "privileged-container"
33-
privilegedHttpPort = 8080
34-
privilegedUdpPort = 8081
35-
notPrivilegedHttpPort = 9090
36-
notPrivilegedUdpPort = 9091
37-
notPrivilegedContainerName = "not-privileged-container"
38-
privilegedContainerImage = "gcr.io/google_containers/netexec:1.7"
39-
privilegedCommand = "ip link add dummy1 type dummy"
40-
)
41-
4228
type PrivilegedPodTestConfig struct {
43-
privilegedPod *v1.Pod
44-
f *framework.Framework
45-
hostExecPod *v1.Pod
29+
f *framework.Framework
30+
31+
privilegedPod string
32+
privilegedContainer string
33+
notPrivilegedContainer string
34+
35+
pod *v1.Pod
4636
}
4737

4838
var _ = framework.KubeDescribe("PrivilegedPod", func() {
49-
f := framework.NewDefaultFramework("e2e-privilegedpod")
5039
config := &PrivilegedPodTestConfig{
51-
f: f,
40+
f: framework.NewDefaultFramework("e2e-privileged-pod"),
41+
privilegedPod: "privileged-pod",
42+
privilegedContainer: "privileged-container",
43+
notPrivilegedContainer: "not-privileged-container",
5244
}
53-
It("should test privileged pod", func() {
54-
By("Creating a hostexec pod")
55-
config.createHostExecPod()
5645

57-
By("Creating a privileged pod")
58-
config.createPrivilegedPod()
46+
It("should enable privileged commands", func() {
47+
By("Creating a pod with a privileged container")
48+
config.createPods()
5949

60-
By("Executing privileged command on privileged container")
61-
config.runPrivilegedCommandOnPrivilegedContainer()
50+
By("Executing in the privileged container")
51+
config.run(config.privilegedContainer, true)
6252

63-
By("Executing privileged command on non-privileged container")
64-
config.runPrivilegedCommandOnNonPrivilegedContainer()
53+
By("Executing in the non-privileged container")
54+
config.run(config.notPrivilegedContainer, false)
6555
})
6656
})
6757

68-
func (config *PrivilegedPodTestConfig) runPrivilegedCommandOnPrivilegedContainer() {
69-
outputMap := config.dialFromContainer(config.privilegedPod.Status.PodIP, privilegedHttpPort)
70-
if len(outputMap["error"]) > 0 {
71-
framework.Failf("Privileged command failed unexpectedly on privileged container, output:%v", outputMap)
72-
}
73-
}
74-
75-
func (config *PrivilegedPodTestConfig) runPrivilegedCommandOnNonPrivilegedContainer() {
76-
outputMap := config.dialFromContainer(config.privilegedPod.Status.PodIP, notPrivilegedHttpPort)
77-
if len(outputMap["error"]) == 0 {
78-
framework.Failf("Privileged command should have failed on non-privileged container, output:%v", outputMap)
58+
func (c *PrivilegedPodTestConfig) run(containerName string, expectSuccess bool) {
59+
cmd := []string{"ip", "link", "add", "dummy1", "type", "dummy"}
60+
reverseCmd := []string{"ip", "link", "del", "dummy1"}
61+
62+
stdout, stderr, err := c.f.ExecCommandInContainerWithFullOutput(
63+
c.privilegedPod, containerName, cmd...)
64+
msg := fmt.Sprintf("cmd %v, stdout %q, stderr %q", cmd, stdout, stderr)
65+
66+
if expectSuccess {
67+
Expect(err).NotTo(HaveOccurred(), msg)
68+
// We need to clean up the dummy link that was created, as it
69+
// leaks out into the node level -- yuck.
70+
_, _, err := c.f.ExecCommandInContainerWithFullOutput(
71+
c.privilegedPod, containerName, reverseCmd...)
72+
Expect(err).NotTo(HaveOccurred(),
73+
fmt.Sprintf("could not remove dummy1 link: %v", err))
74+
} else {
75+
Expect(err).To(HaveOccurred(), msg)
7976
}
8077
}
8178

82-
func (config *PrivilegedPodTestConfig) dialFromContainer(containerIP string, containerHttpPort int) map[string]string {
83-
v := url.Values{}
84-
v.Set("shellCommand", "ip link add dummy1 type dummy")
85-
cmd := fmt.Sprintf("curl -q 'http://%s:%d/shell?%s'",
86-
containerIP,
87-
containerHttpPort,
88-
v.Encode())
89-
90-
By(fmt.Sprintf("Exec-ing into container over http. Running command:%s", cmd))
91-
stdout := config.f.ExecShellInPod(config.hostExecPod.Name, cmd)
92-
var output map[string]string
93-
err := json.Unmarshal([]byte(stdout), &output)
94-
Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("Could not unmarshal curl response: %s", stdout))
95-
framework.Logf("Deserialized output is %v", stdout)
96-
return output
97-
}
98-
99-
func (config *PrivilegedPodTestConfig) createPrivilegedPodSpec() *v1.Pod {
79+
func (c *PrivilegedPodTestConfig) createPodsSpec() *v1.Pod {
10080
isPrivileged := true
10181
notPrivileged := false
102-
pod := &v1.Pod{
82+
83+
const image = "gcr.io/google_containers/busybox:1.24"
84+
85+
return &v1.Pod{
10386
ObjectMeta: v1.ObjectMeta{
104-
Name: privilegedPodName,
105-
Namespace: config.f.Namespace.Name,
87+
Name: c.privilegedPod,
88+
Namespace: c.f.Namespace.Name,
10689
},
10790
Spec: v1.PodSpec{
10891
Containers: []v1.Container{
10992
{
110-
Name: privilegedContainerName,
111-
Image: privilegedContainerImage,
93+
Name: c.privilegedContainer,
94+
Image: image,
11295
ImagePullPolicy: v1.PullIfNotPresent,
11396
SecurityContext: &v1.SecurityContext{Privileged: &isPrivileged},
114-
Command: []string{
115-
"/netexec",
116-
fmt.Sprintf("--http-port=%d", privilegedHttpPort),
117-
fmt.Sprintf("--udp-port=%d", privilegedUdpPort),
118-
},
97+
Command: []string{"/bin/sleep", "10000"},
11998
},
12099
{
121-
Name: notPrivilegedContainerName,
122-
Image: privilegedContainerImage,
100+
Name: c.notPrivilegedContainer,
101+
Image: image,
123102
ImagePullPolicy: v1.PullIfNotPresent,
124103
SecurityContext: &v1.SecurityContext{Privileged: &notPrivileged},
125-
Command: []string{
126-
"/netexec",
127-
fmt.Sprintf("--http-port=%d", notPrivilegedHttpPort),
128-
fmt.Sprintf("--udp-port=%d", notPrivilegedUdpPort),
129-
},
104+
Command: []string{"/bin/sleep", "10000"},
130105
},
131106
},
132107
},
133108
}
134-
return pod
135-
}
136-
137-
func (config *PrivilegedPodTestConfig) createHostExecPod() {
138-
podSpec := framework.NewHostExecPodSpec(config.f.Namespace.Name, "hostexec")
139-
config.hostExecPod = config.f.PodClient().CreateSync(podSpec)
140109
}
141110

142-
func (config *PrivilegedPodTestConfig) createPrivilegedPod() {
143-
podSpec := config.createPrivilegedPodSpec()
144-
config.privilegedPod = config.f.PodClient().CreateSync(podSpec)
111+
func (c *PrivilegedPodTestConfig) createPods() {
112+
podSpec := c.createPodsSpec()
113+
c.pod = c.f.PodClient().CreateSync(podSpec)
145114
}

0 commit comments

Comments
 (0)