From e24c79d7a791ad0770a25b32f673672804c48c71 Mon Sep 17 00:00:00 2001 From: Lantao Liu Date: Tue, 24 Sep 2019 15:37:07 -0700 Subject: [PATCH] Add hostname test. Signed-off-by: Lantao Liu --- pkg/validate/consts.go | 5 +++++ pkg/validate/container.go | 1 - pkg/validate/networking.go | 42 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/pkg/validate/consts.go b/pkg/validate/consts.go index 3f175a28fb..be71701773 100644 --- a/pkg/validate/consts.go +++ b/pkg/validate/consts.go @@ -205,6 +205,7 @@ var ( hostNetWebServerImage string getDNSConfigCmd []string getDNSConfigContent []string + getHostnameCmd []string // Linux defaults getDNSConfigLinuxCmd = []string{"cat", resolvConfigPath} @@ -213,6 +214,7 @@ var ( "search " + defaultDNSSearch, "options " + defaultDNSOption, } + getHostnameLinuxCmd = []string{"hostname"} // Windows defaults // Windows doesn't support ndots options. @@ -222,6 +224,7 @@ var ( "DNS Servers . . . . . . . . . . . : " + defaultDNSServer, "DNS Suffix Search List. . . . . . : " + defaultDNSSearch, } + getHostnameWindowsCmd = []string{"powershell", "/c", "$env:computername"} ) var _ = framework.AddBeforeSuiteCallback(func() { @@ -230,11 +233,13 @@ var _ = framework.AddBeforeSuiteCallback(func() { hostNetWebServerImage = hostNetWebServerLinuxImage getDNSConfigCmd = getDNSConfigLinuxCmd getDNSConfigContent = getDNSConfigLinuxContent + getHostnameCmd = getHostnameLinuxCmd } else { webServerImage = webServerWindowsImage hostNetWebServerImage = hostNetWebServerWindowsImage getDNSConfigCmd = getDNSConfigWindowsCmd getDNSConfigContent = getDNSConfigWindowsContent + getHostnameCmd = getHostnameWindowsCmd } }) diff --git a/pkg/validate/container.go b/pkg/validate/container.go index 0f218a9262..c149a39d0d 100644 --- a/pkg/validate/container.go +++ b/pkg/validate/container.go @@ -268,7 +268,6 @@ var _ = framework.KubeDescribe("Container", func() { }, 5*time.Second, time.Second).Should(Equal(oldLength), "old container log should not change") }) }) - }) // containerFound returns whether containers is found. diff --git a/pkg/validate/networking.go b/pkg/validate/networking.go index b6cd614cb4..27d4ba7e62 100644 --- a/pkg/validate/networking.go +++ b/pkg/validate/networking.go @@ -19,6 +19,7 @@ package validate import ( "net/http" "strconv" + "strings" "time" "github.com/kubernetes-sigs/cri-tools/pkg/framework" @@ -66,6 +67,22 @@ var _ = framework.KubeDescribe("Networking", func() { checkDNSConfig(rc, containerID, expectedContent) }) + It("runtime should support set hostname [Conformance]", func() { + By("create a PodSandbox with hostname") + var podConfig *runtimeapi.PodSandboxConfig + const testHostname = "test-hostname" + podID, podConfig = createPodSandWithHostname(rc, testHostname) + + By("create container") + containerID := framework.CreateDefaultContainer(rc, ic, podID, podConfig, "container-for-hostname-test-") + + By("start container") + startContainer(rc, containerID) + + By("check hostname") + checkHostname(rc, containerID, testHostname) + }) + It("runtime should support port mapping with only container port [Conformance]", func() { By("create a PodSandbox with container port port mapping") var podConfig *runtimeapi.PodSandboxConfig @@ -109,6 +126,21 @@ var _ = framework.KubeDescribe("Networking", func() { }) }) +// createPodSandWithHostname create a PodSandbox with hostname. +func createPodSandWithHostname(c internalapi.RuntimeService, hostname string) (string, *runtimeapi.PodSandboxConfig) { + podSandboxName := "create-PodSandbox-with-hostname" + framework.NewUUID() + uid := framework.DefaultUIDPrefix + framework.NewUUID() + namespace := framework.DefaultNamespacePrefix + framework.NewUUID() + config := &runtimeapi.PodSandboxConfig{ + Metadata: framework.BuildPodSandboxMetadata(podSandboxName, uid, namespace, framework.DefaultAttempt), + Hostname: hostname, + Labels: framework.DefaultPodLabels, + } + + podID := framework.RunPodSandbox(c, config) + return podID, config +} + // createPodSandWithDNSConfig create a PodSandbox with DNS config. func createPodSandWithDNSConfig(c internalapi.RuntimeService) (string, *runtimeapi.PodSandboxConfig) { podSandboxName := "create-PodSandbox-with-DNS-config" + framework.NewUUID() @@ -152,6 +184,16 @@ func createPodSandboxWithPortMapping(c internalapi.RuntimeService, portMappings return podID, config } +// checkHostname checks the container hostname. +func checkHostname(c internalapi.RuntimeService, containerID string, hostname string) { + By("get the current hostname via execSync") + stdout, stderr, err := c.ExecSync(containerID, getHostnameCmd, time.Duration(defaultExecSyncTimeout)*time.Second) + framework.ExpectNoError(err, "failed to execSync in container %q", containerID) + Expect(strings.EqualFold(strings.TrimSpace(string(stdout)), hostname)).To(BeTrue()) + Expect(stderr).To(BeNil(), "The stderr should be nil.") + framework.Logf("check hostname succeed") +} + // checkDNSConfig checks the content of /etc/resolv.conf. func checkDNSConfig(c internalapi.RuntimeService, containerID string, expectedContent []string) { By("get the current dns config via execSync")