Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hostname test. #542

Merged
merged 1 commit into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/validate/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ var (
hostNetWebServerImage string
getDNSConfigCmd []string
getDNSConfigContent []string
getHostnameCmd []string

// Linux defaults
getDNSConfigLinuxCmd = []string{"cat", resolvConfigPath}
Expand All @@ -213,6 +214,7 @@ var (
"search " + defaultDNSSearch,
"options " + defaultDNSOption,
}
getHostnameLinuxCmd = []string{"hostname"}

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

var _ = framework.AddBeforeSuiteCallback(func() {
Expand All @@ -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
}
})

Expand Down
1 change: 0 additions & 1 deletion pkg/validate/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
42 changes: 42 additions & 0 deletions pkg/validate/networking.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package validate
import (
"net/http"
"strconv"
"strings"
"time"

"github.com/kubernetes-sigs/cri-tools/pkg/framework"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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")
Expand Down