Skip to content

Commit 6ba3510

Browse files
authored
Merge pull request #542 from Random-Liu/add-hostname-test
Add hostname test.
2 parents ad1e8a4 + e24c79d commit 6ba3510

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

pkg/validate/consts.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ var (
205205
hostNetWebServerImage string
206206
getDNSConfigCmd []string
207207
getDNSConfigContent []string
208+
getHostnameCmd []string
208209

209210
// Linux defaults
210211
getDNSConfigLinuxCmd = []string{"cat", resolvConfigPath}
@@ -213,6 +214,7 @@ var (
213214
"search " + defaultDNSSearch,
214215
"options " + defaultDNSOption,
215216
}
217+
getHostnameLinuxCmd = []string{"hostname"}
216218

217219
// Windows defaults
218220
// Windows doesn't support ndots options.
@@ -222,6 +224,7 @@ var (
222224
"DNS Servers . . . . . . . . . . . : " + defaultDNSServer,
223225
"DNS Suffix Search List. . . . . . : " + defaultDNSSearch,
224226
}
227+
getHostnameWindowsCmd = []string{"powershell", "/c", "$env:computername"}
225228
)
226229

227230
var _ = framework.AddBeforeSuiteCallback(func() {
@@ -230,11 +233,13 @@ var _ = framework.AddBeforeSuiteCallback(func() {
230233
hostNetWebServerImage = hostNetWebServerLinuxImage
231234
getDNSConfigCmd = getDNSConfigLinuxCmd
232235
getDNSConfigContent = getDNSConfigLinuxContent
236+
getHostnameCmd = getHostnameLinuxCmd
233237
} else {
234238
webServerImage = webServerWindowsImage
235239
hostNetWebServerImage = hostNetWebServerWindowsImage
236240
getDNSConfigCmd = getDNSConfigWindowsCmd
237241
getDNSConfigContent = getDNSConfigWindowsContent
242+
getHostnameCmd = getHostnameWindowsCmd
238243
}
239244
})
240245

pkg/validate/container.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ var _ = framework.KubeDescribe("Container", func() {
268268
}, 5*time.Second, time.Second).Should(Equal(oldLength), "old container log should not change")
269269
})
270270
})
271-
272271
})
273272

274273
// containerFound returns whether containers is found.

pkg/validate/networking.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package validate
1919
import (
2020
"net/http"
2121
"strconv"
22+
"strings"
2223
"time"
2324

2425
"github.com/kubernetes-sigs/cri-tools/pkg/framework"
@@ -66,6 +67,22 @@ var _ = framework.KubeDescribe("Networking", func() {
6667
checkDNSConfig(rc, containerID, expectedContent)
6768
})
6869

70+
It("runtime should support set hostname [Conformance]", func() {
71+
By("create a PodSandbox with hostname")
72+
var podConfig *runtimeapi.PodSandboxConfig
73+
const testHostname = "test-hostname"
74+
podID, podConfig = createPodSandWithHostname(rc, testHostname)
75+
76+
By("create container")
77+
containerID := framework.CreateDefaultContainer(rc, ic, podID, podConfig, "container-for-hostname-test-")
78+
79+
By("start container")
80+
startContainer(rc, containerID)
81+
82+
By("check hostname")
83+
checkHostname(rc, containerID, testHostname)
84+
})
85+
6986
It("runtime should support port mapping with only container port [Conformance]", func() {
7087
By("create a PodSandbox with container port port mapping")
7188
var podConfig *runtimeapi.PodSandboxConfig
@@ -109,6 +126,21 @@ var _ = framework.KubeDescribe("Networking", func() {
109126
})
110127
})
111128

129+
// createPodSandWithHostname create a PodSandbox with hostname.
130+
func createPodSandWithHostname(c internalapi.RuntimeService, hostname string) (string, *runtimeapi.PodSandboxConfig) {
131+
podSandboxName := "create-PodSandbox-with-hostname" + framework.NewUUID()
132+
uid := framework.DefaultUIDPrefix + framework.NewUUID()
133+
namespace := framework.DefaultNamespacePrefix + framework.NewUUID()
134+
config := &runtimeapi.PodSandboxConfig{
135+
Metadata: framework.BuildPodSandboxMetadata(podSandboxName, uid, namespace, framework.DefaultAttempt),
136+
Hostname: hostname,
137+
Labels: framework.DefaultPodLabels,
138+
}
139+
140+
podID := framework.RunPodSandbox(c, config)
141+
return podID, config
142+
}
143+
112144
// createPodSandWithDNSConfig create a PodSandbox with DNS config.
113145
func createPodSandWithDNSConfig(c internalapi.RuntimeService) (string, *runtimeapi.PodSandboxConfig) {
114146
podSandboxName := "create-PodSandbox-with-DNS-config" + framework.NewUUID()
@@ -152,6 +184,16 @@ func createPodSandboxWithPortMapping(c internalapi.RuntimeService, portMappings
152184
return podID, config
153185
}
154186

187+
// checkHostname checks the container hostname.
188+
func checkHostname(c internalapi.RuntimeService, containerID string, hostname string) {
189+
By("get the current hostname via execSync")
190+
stdout, stderr, err := c.ExecSync(containerID, getHostnameCmd, time.Duration(defaultExecSyncTimeout)*time.Second)
191+
framework.ExpectNoError(err, "failed to execSync in container %q", containerID)
192+
Expect(strings.EqualFold(strings.TrimSpace(string(stdout)), hostname)).To(BeTrue())
193+
Expect(stderr).To(BeNil(), "The stderr should be nil.")
194+
framework.Logf("check hostname succeed")
195+
}
196+
155197
// checkDNSConfig checks the content of /etc/resolv.conf.
156198
func checkDNSConfig(c internalapi.RuntimeService, containerID string, expectedContent []string) {
157199
By("get the current dns config via execSync")

0 commit comments

Comments
 (0)