Skip to content

Commit 74ae6d1

Browse files
authored
Merge pull request kubernetes#124428 from yashsingh74/yashsingh/hostname-test
Added a testcase to check hostname and hostNetwork
2 parents f138590 + 091a05b commit 74ae6d1

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

test/e2e/network/dns.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import (
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/util/wait"
2828
"k8s.io/kubernetes/test/e2e/framework"
29+
e2enode "k8s.io/kubernetes/test/e2e/framework/node"
2930
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
31+
e2eoutput "k8s.io/kubernetes/test/e2e/framework/pod/output"
3032
e2eservice "k8s.io/kubernetes/test/e2e/framework/service"
3133
e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
3234
"k8s.io/kubernetes/test/e2e/network/common"
@@ -642,4 +644,51 @@ var _ = common.SIGDescribe("DNS HostNetwork", func() {
642644
validateDNSResults(ctx, f, pod, append(wheezyFileNames, jessieFileNames...))
643645
})
644646

647+
// https://issues.k8s.io/67019
648+
ginkgo.It("spec.Hostname field is not silently ignored and is used for hostname for a Pod", func(ctx context.Context) {
649+
ginkgo.By("Creating a pod by setting a hostname")
650+
651+
testAgnhostPod := e2epod.NewAgnhostPod(f.Namespace.Name, "test-dns-hostname", nil, nil, nil)
652+
testAgnhostPod.Spec.Hostname = dnsTestPodHostName
653+
testAgnhostPod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(ctx, testAgnhostPod, metav1.CreateOptions{})
654+
framework.ExpectNoError(err, "failed to created pod: %s", testAgnhostPod.Name)
655+
656+
err = e2epod.WaitTimeoutForPodReadyInNamespace(ctx, f.ClientSet, testAgnhostPod.Name, f.Namespace.Name, framework.PodStartTimeout)
657+
framework.ExpectNoError(err, "failed to wait for pod %s to be running", testAgnhostPod.Name)
658+
659+
stdout, err := e2eoutput.RunHostCmd(testAgnhostPod.Namespace, testAgnhostPod.Name, "hostname")
660+
framework.ExpectNoError(err, "failed to run command hostname: %s", stdout)
661+
hostname := strings.TrimSpace(stdout)
662+
if testAgnhostPod.Spec.Hostname != hostname {
663+
framework.Failf("expected hostname: %s, got: %s", testAgnhostPod.Spec.Hostname, hostname)
664+
}
665+
})
666+
667+
// https://issues.k8s.io/67019
668+
ginkgo.It("spec.Hostname field is silently ignored and the node hostname is used when hostNetwork is set to true for a Pod", func(ctx context.Context) {
669+
ginkgo.By("Creating a pod by setting a hostNetwork to true")
670+
671+
testAgnhostPod := e2epod.NewAgnhostPod(f.Namespace.Name, "test-dns-hostnetwork", nil, nil, nil)
672+
testAgnhostPod.Spec.Hostname = dnsTestPodHostName
673+
testAgnhostPod.Spec.HostNetwork = true
674+
node, err := e2enode.GetRandomReadySchedulableNode(ctx, f.ClientSet)
675+
framework.ExpectNoError(err)
676+
nodeSelection := e2epod.NodeSelection{}
677+
e2epod.SetAffinity(&nodeSelection, node.Name)
678+
e2epod.SetNodeSelection(&testAgnhostPod.Spec, nodeSelection)
679+
680+
testAgnhostPod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(ctx, testAgnhostPod, metav1.CreateOptions{})
681+
framework.ExpectNoError(err, "failed to created pod: %s", testAgnhostPod.Name)
682+
683+
err = e2epod.WaitTimeoutForPodReadyInNamespace(ctx, f.ClientSet, testAgnhostPod.Name, f.Namespace.Name, framework.PodStartTimeout)
684+
framework.ExpectNoError(err, "failed to wait for pod %s to be running", testAgnhostPod.Name)
685+
686+
stdout, err := e2eoutput.RunHostCmd(testAgnhostPod.Namespace, testAgnhostPod.Name, "hostname")
687+
framework.ExpectNoError(err, "failed to run command hostname: %s", stdout)
688+
hostname := strings.TrimSpace(stdout)
689+
if node.Name != hostname {
690+
framework.Failf("expected hostname: %s, got: %s", node.Name, hostname)
691+
}
692+
})
693+
645694
})

0 commit comments

Comments
 (0)