mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-21 09:57:52 +00:00
e2e test for dns example
This commit is contained in:
@@ -18,6 +18,8 @@ package e2e
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -34,8 +36,17 @@ import (
|
||||
const (
|
||||
podListTimeout = time.Minute
|
||||
serverStartTimeout = podStartTimeout + 3*time.Minute
|
||||
dnsReadyTimeout = time.Minute
|
||||
)
|
||||
|
||||
const queryDnsPythonTemplate string = `
|
||||
import socket
|
||||
try:
|
||||
socket.gethostbyname('%s')
|
||||
print 'ok'
|
||||
except:
|
||||
print 'err'`
|
||||
|
||||
var _ = Describe("Examples e2e", func() {
|
||||
var c *client.Client
|
||||
var ns string
|
||||
@@ -412,6 +423,103 @@ var _ = Describe("Examples e2e", func() {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Describe("[Example]ClusterDns", func() {
|
||||
It("should create pod that uses dns", func() {
|
||||
mkpath := func(file string) string {
|
||||
return filepath.Join(testContext.RepoRoot, "examples/cluster-dns", file)
|
||||
}
|
||||
|
||||
// contrary to the example, this test does not use contexts, for simplicity
|
||||
// namespaces are passed directly.
|
||||
// Also, for simplicity, we don't use yamls with namespaces, but we
|
||||
// create testing namespaces instead.
|
||||
|
||||
backendRcYaml := mkpath("dns-backend-rc.yaml")
|
||||
backendRcName := "dns-backend"
|
||||
backendSvcYaml := mkpath("dns-backend-service.yaml")
|
||||
backendSvcName := "dns-backend"
|
||||
backendPodName := "dns-backend"
|
||||
frontendPodYaml := mkpath("dns-frontend-pod.yaml")
|
||||
frontendPodName := "dns-frontend"
|
||||
frontendPodContainerName := "dns-frontend"
|
||||
|
||||
podOutput := "Hello World!"
|
||||
|
||||
// we need two namespaces anyway, so let's forget about
|
||||
// the one created in BeforeEach and create two new ones.
|
||||
namespaces := []*api.Namespace{nil, nil}
|
||||
for i := range namespaces {
|
||||
var err error
|
||||
namespaces[i], err = createTestingNS(fmt.Sprintf("dnsexample%d", i), c)
|
||||
if namespaces[i] != nil {
|
||||
defer c.Namespaces().Delete(namespaces[i].Name)
|
||||
}
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
|
||||
for _, ns := range namespaces {
|
||||
runKubectl("create", "-f", backendRcYaml, getNsCmdFlag(ns))
|
||||
}
|
||||
|
||||
for _, ns := range namespaces {
|
||||
runKubectl("create", "-f", backendSvcYaml, getNsCmdFlag(ns))
|
||||
}
|
||||
|
||||
// wait for objects
|
||||
for _, ns := range namespaces {
|
||||
waitForRCPodsRunning(c, ns.Name, backendRcName)
|
||||
waitForService(c, ns.Name, backendSvcName, true, poll, serviceStartTimeout)
|
||||
}
|
||||
// it is not enough that pods are running because they may be set to running, but
|
||||
// the application itself may have not been initialized. Just query the application.
|
||||
for _, ns := range namespaces {
|
||||
label := labels.SelectorFromSet(labels.Set(map[string]string{"name": backendRcName}))
|
||||
pods, err := c.Pods(ns.Name).List(label, fields.Everything())
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
err = podsResponding(c, ns.Name, backendPodName, false, pods)
|
||||
Expect(err).NotTo(HaveOccurred(), "waiting for all pods to respond")
|
||||
Logf("found %d backend pods responding in namespace %s", len(pods.Items), ns.Name)
|
||||
|
||||
err = serviceResponding(c, ns.Name, backendSvcName)
|
||||
Expect(err).NotTo(HaveOccurred(), "waiting for the service to respond")
|
||||
}
|
||||
|
||||
// Now another tricky part:
|
||||
// It may happen that the service name is not yet in DNS.
|
||||
// So if we start our pod, it will fail. We must make sure
|
||||
// the name is already resolvable. So let's try to query DNS from
|
||||
// the pod we have, until we find our service name.
|
||||
// This complicated code may be removed if the pod itself retried after
|
||||
// dns error or timeout.
|
||||
// This code is probably unnecessary, but let's stay on the safe side.
|
||||
label := labels.SelectorFromSet(labels.Set(map[string]string{"name": backendPodName}))
|
||||
pods, err := c.Pods(namespaces[0].Name).List(label, fields.Everything())
|
||||
|
||||
if err != nil || pods == nil || len(pods.Items) == 0 {
|
||||
Failf("no running pods found")
|
||||
}
|
||||
podName := pods.Items[0].Name
|
||||
|
||||
queryDns := fmt.Sprintf(queryDnsPythonTemplate, backendSvcName+"."+namespaces[0].Name)
|
||||
_, err = lookForStringInPodExec(namespaces[0].Name, podName, []string{"python", "-c", queryDns}, "ok", dnsReadyTimeout)
|
||||
Expect(err).NotTo(HaveOccurred(), "waiting for output from pod exec")
|
||||
|
||||
updatedPodYaml := prepareResourceWithReplacedString(frontendPodYaml, "dns-backend.development.cluster.local", fmt.Sprintf("dns-backend.%s.cluster.local", namespaces[0].Name))
|
||||
|
||||
// create a pod in each namespace
|
||||
for _, ns := range namespaces {
|
||||
newKubectlCommand("create", "-f", "-", getNsCmdFlag(ns)).withStdinData(updatedPodYaml).exec()
|
||||
}
|
||||
// remember that we cannot wait for the pods to be running because our pods terminate by themselves.
|
||||
|
||||
// wait for pods to print their result
|
||||
for _, ns := range namespaces {
|
||||
_, err := lookForStringInLog(ns.Name, frontendPodName, frontendPodContainerName, podOutput, podStartTimeout)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
func makeHttpRequestToService(c *client.Client, ns, service, path string) (string, error) {
|
||||
@@ -426,6 +534,21 @@ func makeHttpRequestToService(c *client.Client, ns, service, path string) (strin
|
||||
return string(result), err
|
||||
}
|
||||
|
||||
func getNsCmdFlag(ns *api.Namespace) string {
|
||||
return fmt.Sprintf("--namespace=%v", ns.Name)
|
||||
}
|
||||
|
||||
// pass enough context with the 'old' parameter so that it replaces what your really intended.
|
||||
func prepareResourceWithReplacedString(inputFile, old, new string) string {
|
||||
f, err := os.Open(inputFile)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
defer f.Close()
|
||||
data, err := ioutil.ReadAll(f)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
podYaml := strings.Replace(string(data), old, new, 1)
|
||||
return podYaml
|
||||
}
|
||||
|
||||
func forEachPod(c *client.Client, ns, selectorKey, selectorValue string, fn func(api.Pod)) {
|
||||
var pods *api.PodList
|
||||
var err error
|
||||
@@ -458,6 +581,15 @@ func lookForStringInFile(ns, podName, container, file, expectedString string, ti
|
||||
})
|
||||
}
|
||||
|
||||
func lookForStringInPodExec(ns, podName string, command []string, expectedString string, timeout time.Duration) (result string, err error) {
|
||||
return lookForString(expectedString, timeout, func() string {
|
||||
// use the first container
|
||||
args := []string{"exec", podName, fmt.Sprintf("--namespace=%v", ns), "--"}
|
||||
args = append(args, command...)
|
||||
return runKubectl(args...)
|
||||
})
|
||||
}
|
||||
|
||||
// Looks for the given string in the output of fn, repeatedly calling fn until
|
||||
// the timeout is reached or the string is found. Returns last log and possibly
|
||||
// error if the string was not found.
|
||||
@@ -468,6 +600,6 @@ func lookForString(expectedString string, timeout time.Duration, fn func() strin
|
||||
return
|
||||
}
|
||||
}
|
||||
err = fmt.Errorf("Failed to find \"%s\"", expectedString)
|
||||
err = fmt.Errorf("Failed to find \"%s\", last result: \"%s\"", expectedString, result)
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user