|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package windows |
| 18 | + |
| 19 | +import ( |
| 20 | + "regexp" |
| 21 | + "strings" |
| 22 | + |
| 23 | + v1 "k8s.io/api/core/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/kubernetes/test/e2e/framework" |
| 26 | + e2elog "k8s.io/kubernetes/test/e2e/framework/log" |
| 27 | + imageutils "k8s.io/kubernetes/test/utils/image" |
| 28 | + |
| 29 | + "github.com/onsi/ginkgo" |
| 30 | +) |
| 31 | + |
| 32 | +const dnsTestPodHostName = "dns-querier-1" |
| 33 | +const dnsTestServiceName = "dns-test-service" |
| 34 | + |
| 35 | +var _ = SIGDescribe("DNS", func() { |
| 36 | + |
| 37 | + ginkgo.BeforeEach(func() { |
| 38 | + framework.SkipUnlessNodeOSDistroIs("windows") |
| 39 | + }) |
| 40 | + |
| 41 | + f := framework.NewDefaultFramework("dns") |
| 42 | + |
| 43 | + ginkgo.It("should support configurable pod DNS servers", func() { |
| 44 | + ginkgo.By("Preparing a test DNS service with injected DNS names...") |
| 45 | + testInjectedIP := "1.1.1.1" |
| 46 | + testSearchPath := "resolv.conf.local" |
| 47 | + |
| 48 | + ginkgo.By("Creating a pod with dnsPolicy=None and customized dnsConfig...") |
| 49 | + testUtilsPod := generateDNSUtilsPod() |
| 50 | + testUtilsPod.Spec.DNSPolicy = v1.DNSNone |
| 51 | + testUtilsPod.Spec.DNSConfig = &v1.PodDNSConfig{ |
| 52 | + Nameservers: []string{testInjectedIP}, |
| 53 | + Searches: []string{testSearchPath}, |
| 54 | + } |
| 55 | + testUtilsPod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(testUtilsPod) |
| 56 | + framework.ExpectNoError(err) |
| 57 | + e2elog.Logf("Created pod %v", testUtilsPod) |
| 58 | + defer func() { |
| 59 | + e2elog.Logf("Deleting pod %s...", testUtilsPod.Name) |
| 60 | + if err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(testUtilsPod.Name, metav1.NewDeleteOptions(0)); err != nil { |
| 61 | + e2elog.Failf("Failed to delete pod %s: %v", testUtilsPod.Name, err) |
| 62 | + } |
| 63 | + }() |
| 64 | + framework.ExpectNoError(f.WaitForPodRunning(testUtilsPod.Name), "failed to wait for pod %s to be running", testUtilsPod.Name) |
| 65 | + |
| 66 | + ginkgo.By("Verifying customized DNS option is configured on pod...") |
| 67 | + cmd := []string{"ipconfig", "/all"} |
| 68 | + stdout, _, err := f.ExecWithOptions(framework.ExecOptions{ |
| 69 | + Command: cmd, |
| 70 | + Namespace: f.Namespace.Name, |
| 71 | + PodName: testUtilsPod.Name, |
| 72 | + ContainerName: "util", |
| 73 | + CaptureStdout: true, |
| 74 | + CaptureStderr: true, |
| 75 | + }) |
| 76 | + framework.ExpectNoError(err) |
| 77 | + |
| 78 | + e2elog.Logf("ipconfig /all:\n%s", stdout) |
| 79 | + dnsRegex, err := regexp.Compile(`DNS Servers[\s*.]*:(\s*[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})+`) |
| 80 | + |
| 81 | + if dnsRegex.MatchString(stdout) { |
| 82 | + match := dnsRegex.FindString(stdout) |
| 83 | + |
| 84 | + if !strings.Contains(match, testInjectedIP) { |
| 85 | + e2elog.Failf("customized DNS options not found in ipconfig /all, got: %s", match) |
| 86 | + } |
| 87 | + } else { |
| 88 | + e2elog.Failf("cannot find DNS server info in ipconfig /all output: \n%s", stdout) |
| 89 | + } |
| 90 | + // TODO: Add more test cases for other DNSPolicies. |
| 91 | + }) |
| 92 | +}) |
| 93 | + |
| 94 | +func generateDNSUtilsPod() *v1.Pod { |
| 95 | + return &v1.Pod{ |
| 96 | + TypeMeta: metav1.TypeMeta{ |
| 97 | + Kind: "Pod", |
| 98 | + }, |
| 99 | + ObjectMeta: metav1.ObjectMeta{ |
| 100 | + GenerateName: "e2e-dns-utils-", |
| 101 | + }, |
| 102 | + Spec: v1.PodSpec{ |
| 103 | + Containers: []v1.Container{ |
| 104 | + { |
| 105 | + Name: "util", |
| 106 | + Image: imageutils.GetE2EImage(imageutils.Dnsutils), |
| 107 | + Command: []string{"sleep", "10000"}, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + } |
| 112 | +} |
0 commit comments