@@ -19,6 +19,7 @@ package validate
1919import (
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.
113145func 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.
156198func checkDNSConfig (c internalapi.RuntimeService , containerID string , expectedContent []string ) {
157199 By ("get the current dns config via execSync" )
0 commit comments