PageRenderTime 92ms CodeModel.GetById 24ms RepoModel.GetById 2ms app.codeStats 1ms

/test/e2e/kubectl/kubectl.go

https://gitlab.com/unofficial-mirrors/kubernetes
Go | 1135 lines | 885 code | 128 blank | 122 comment | 97 complexity | 5fb6bb25f198a448710a303c0f6fdccd MD5 | raw file
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // OWNER = sig/cli
  14. package kubectl
  15. import (
  16. "bytes"
  17. "context"
  18. "encoding/json"
  19. "fmt"
  20. "io"
  21. "io/ioutil"
  22. "log"
  23. "mime/multipart"
  24. "net"
  25. "net/http"
  26. "net/http/httptest"
  27. "os"
  28. "os/exec"
  29. "path"
  30. "path/filepath"
  31. "regexp"
  32. "sort"
  33. "strconv"
  34. "strings"
  35. "text/template"
  36. "time"
  37. "github.com/elazarl/goproxy"
  38. "github.com/ghodss/yaml"
  39. "k8s.io/api/core/v1"
  40. rbacv1beta1 "k8s.io/api/rbac/v1beta1"
  41. apierrs "k8s.io/apimachinery/pkg/api/errors"
  42. "k8s.io/apimachinery/pkg/api/resource"
  43. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  44. "k8s.io/apimachinery/pkg/labels"
  45. "k8s.io/apimachinery/pkg/runtime/schema"
  46. utilnet "k8s.io/apimachinery/pkg/util/net"
  47. "k8s.io/apimachinery/pkg/util/uuid"
  48. "k8s.io/apimachinery/pkg/util/wait"
  49. "k8s.io/apiserver/pkg/authentication/serviceaccount"
  50. genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
  51. clientset "k8s.io/client-go/kubernetes"
  52. "k8s.io/kubernetes/pkg/controller"
  53. "k8s.io/kubernetes/test/e2e/framework"
  54. "k8s.io/kubernetes/test/e2e/generated"
  55. "k8s.io/kubernetes/test/e2e/scheduling"
  56. testutils "k8s.io/kubernetes/test/utils"
  57. uexec "k8s.io/utils/exec"
  58. . "github.com/onsi/ginkgo"
  59. . "github.com/onsi/gomega"
  60. "k8s.io/kubernetes/pkg/kubectl/polymorphichelpers"
  61. imageutils "k8s.io/kubernetes/test/utils/image"
  62. )
  63. const (
  64. updateDemoSelector = "name=update-demo"
  65. guestbookStartupTimeout = 10 * time.Minute
  66. guestbookResponseTimeout = 3 * time.Minute
  67. simplePodSelector = "name=nginx"
  68. simplePodName = "nginx"
  69. nginxDefaultOutput = "Welcome to nginx!"
  70. simplePodPort = 80
  71. pausePodSelector = "name=pause"
  72. pausePodName = "pause"
  73. runJobTimeout = 5 * time.Minute
  74. kubeCtlManifestPath = "test/e2e/testing-manifests/kubectl"
  75. redisControllerFilename = "redis-master-controller.json.in"
  76. redisServiceFilename = "redis-master-service.json"
  77. nginxDeployment1Filename = "nginx-deployment1.yaml.in"
  78. nginxDeployment2Filename = "nginx-deployment2.yaml.in"
  79. nginxDeployment3Filename = "nginx-deployment3.yaml.in"
  80. )
  81. var (
  82. nautilusImage = imageutils.GetE2EImage(imageutils.Nautilus)
  83. kittenImage = imageutils.GetE2EImage(imageutils.Kitten)
  84. redisImage = imageutils.GetE2EImage(imageutils.Redis)
  85. nginxImage = imageutils.GetE2EImage(imageutils.NginxSlim)
  86. busyboxImage = "busybox"
  87. )
  88. var testImages = struct {
  89. GBFrontendImage string
  90. PauseImage string
  91. NginxSlimImage string
  92. NginxSlimNewImage string
  93. RedisImage string
  94. GBRedisSlaveImage string
  95. NautilusImage string
  96. KittenImage string
  97. }{
  98. imageutils.GetE2EImage(imageutils.GBFrontend),
  99. imageutils.GetE2EImage(imageutils.Pause),
  100. imageutils.GetE2EImage(imageutils.NginxSlim),
  101. imageutils.GetE2EImage(imageutils.NginxSlimNew),
  102. imageutils.GetE2EImage(imageutils.Redis),
  103. imageutils.GetE2EImage(imageutils.GBRedisSlave),
  104. imageutils.GetE2EImage(imageutils.Nautilus),
  105. imageutils.GetE2EImage(imageutils.Kitten),
  106. }
  107. var (
  108. proxyRegexp = regexp.MustCompile("Starting to serve on 127.0.0.1:([0-9]+)")
  109. CronJobGroupVersionResourceAlpha = schema.GroupVersionResource{Group: "batch", Version: "v2alpha1", Resource: "cronjobs"}
  110. CronJobGroupVersionResourceBeta = schema.GroupVersionResource{Group: "batch", Version: "v1beta1", Resource: "cronjobs"}
  111. )
  112. // Stops everything from filePath from namespace ns and checks if everything matching selectors from the given namespace is correctly stopped.
  113. // Aware of the kubectl example files map.
  114. func cleanupKubectlInputs(fileContents string, ns string, selectors ...string) {
  115. By("using delete to clean up resources")
  116. var nsArg string
  117. if ns != "" {
  118. nsArg = fmt.Sprintf("--namespace=%s", ns)
  119. }
  120. // support backward compatibility : file paths or raw json - since we are removing file path
  121. // dependencies from this test.
  122. framework.RunKubectlOrDieInput(fileContents, "delete", "--grace-period=0", "--force", "-f", "-", nsArg)
  123. framework.AssertCleanup(ns, selectors...)
  124. }
  125. func substituteImageName(content string) string {
  126. contentWithImageName := new(bytes.Buffer)
  127. tmpl, err := template.New("imagemanifest").Parse(content)
  128. if err != nil {
  129. framework.Failf("Failed Parse the template:", err)
  130. }
  131. err = tmpl.Execute(contentWithImageName, testImages)
  132. if err != nil {
  133. framework.Failf("Failed executing template:", err)
  134. }
  135. return contentWithImageName.String()
  136. }
  137. func readTestFileOrDie(file string) []byte {
  138. return generated.ReadOrDie(path.Join(kubeCtlManifestPath, file))
  139. }
  140. func runKubectlRetryOrDie(args ...string) string {
  141. var err error
  142. var output string
  143. for i := 0; i < 5; i++ {
  144. output, err = framework.RunKubectl(args...)
  145. if err == nil || (!strings.Contains(err.Error(), genericregistry.OptimisticLockErrorMsg) && !strings.Contains(err.Error(), "Operation cannot be fulfilled")) {
  146. break
  147. }
  148. time.Sleep(time.Second)
  149. }
  150. // Expect no errors to be present after retries are finished
  151. // Copied from framework #ExecOrDie
  152. framework.Logf("stdout: %q", output)
  153. Expect(err).NotTo(HaveOccurred())
  154. return output
  155. }
  156. // duplicated setup to avoid polluting "normal" clients with alpha features which confuses the generated clients
  157. var _ = SIGDescribe("Kubectl alpha client", func() {
  158. defer GinkgoRecover()
  159. f := framework.NewDefaultFramework("kubectl")
  160. var c clientset.Interface
  161. var ns string
  162. BeforeEach(func() {
  163. c = f.ClientSet
  164. ns = f.Namespace.Name
  165. })
  166. // Customized Wait / ForEach wrapper for this test. These demonstrate the
  167. framework.KubeDescribe("Kubectl run CronJob", func() {
  168. var nsFlag string
  169. var cjName string
  170. BeforeEach(func() {
  171. nsFlag = fmt.Sprintf("--namespace=%v", ns)
  172. cjName = "e2e-test-echo-cronjob-alpha"
  173. })
  174. AfterEach(func() {
  175. framework.RunKubectlOrDie("delete", "cronjobs", cjName, nsFlag)
  176. })
  177. It("should create a CronJob", func() {
  178. framework.SkipIfMissingResource(f.DynamicClient, CronJobGroupVersionResourceAlpha, f.Namespace.Name)
  179. schedule := "*/5 * * * ?"
  180. framework.RunKubectlOrDie("run", cjName, "--restart=OnFailure", "--generator=cronjob/v2alpha1",
  181. "--schedule="+schedule, "--image="+busyboxImage, nsFlag)
  182. By("verifying the CronJob " + cjName + " was created")
  183. sj, err := c.BatchV1beta1().CronJobs(ns).Get(cjName, metav1.GetOptions{})
  184. if err != nil {
  185. framework.Failf("Failed getting CronJob %s: %v", cjName, err)
  186. }
  187. if sj.Spec.Schedule != schedule {
  188. framework.Failf("Failed creating a CronJob with correct schedule %s", schedule)
  189. }
  190. containers := sj.Spec.JobTemplate.Spec.Template.Spec.Containers
  191. if containers == nil || len(containers) != 1 || containers[0].Image != busyboxImage {
  192. framework.Failf("Failed creating CronJob %s for 1 pod with expected image %s: %#v", cjName, busyboxImage, containers)
  193. }
  194. if sj.Spec.JobTemplate.Spec.Template.Spec.RestartPolicy != v1.RestartPolicyOnFailure {
  195. framework.Failf("Failed creating a CronJob with correct restart policy for --restart=OnFailure")
  196. }
  197. })
  198. })
  199. })
  200. var _ = SIGDescribe("Kubectl client", func() {
  201. defer GinkgoRecover()
  202. f := framework.NewDefaultFramework("kubectl")
  203. // Reusable cluster state function. This won't be adversely affected by lazy initialization of framework.
  204. clusterState := func() *framework.ClusterVerification {
  205. return f.NewClusterVerification(
  206. f.Namespace,
  207. framework.PodStateVerification{
  208. Selectors: map[string]string{"app": "redis"},
  209. ValidPhases: []v1.PodPhase{v1.PodRunning /*v1.PodPending*/},
  210. })
  211. }
  212. forEachPod := func(podFunc func(p v1.Pod)) {
  213. clusterState().ForEach(podFunc)
  214. }
  215. var c clientset.Interface
  216. var ns string
  217. BeforeEach(func() {
  218. c = f.ClientSet
  219. ns = f.Namespace.Name
  220. })
  221. // Customized Wait / ForEach wrapper for this test. These demonstrate the
  222. // idiomatic way to wrap the ClusterVerification structs for syntactic sugar in large
  223. // test files.
  224. // Print debug info if atLeast Pods are not found before the timeout
  225. waitForOrFailWithDebug := func(atLeast int) {
  226. pods, err := clusterState().WaitFor(atLeast, framework.PodStartTimeout)
  227. if err != nil || len(pods) < atLeast {
  228. // TODO: Generalize integrating debug info into these tests so we always get debug info when we need it
  229. framework.DumpAllNamespaceInfo(f.ClientSet, ns)
  230. framework.Failf("Verified %v of %v pods , error : %v", len(pods), atLeast, err)
  231. }
  232. }
  233. framework.KubeDescribe("Update Demo", func() {
  234. var nautilus, kitten string
  235. BeforeEach(func() {
  236. updateDemoRoot := "test/fixtures/doc-yaml/user-guide/update-demo"
  237. nautilus = substituteImageName(string(generated.ReadOrDie(filepath.Join(updateDemoRoot, "nautilus-rc.yaml.in"))))
  238. kitten = substituteImageName(string(generated.ReadOrDie(filepath.Join(updateDemoRoot, "kitten-rc.yaml.in"))))
  239. })
  240. /*
  241. Release : v1.9
  242. Testname: Kubectl, replication controller
  243. Description: Create a Pod and a container with a given image. Configure replication controller to run 2 replicas. The number of running instances of the Pod MUST equal the number of replicas set on the replication controller which is 2.
  244. */
  245. framework.ConformanceIt("should create and stop a replication controller ", func() {
  246. defer cleanupKubectlInputs(nautilus, ns, updateDemoSelector)
  247. By("creating a replication controller")
  248. framework.RunKubectlOrDieInput(nautilus, "create", "-f", "-", fmt.Sprintf("--namespace=%v", ns))
  249. framework.ValidateController(c, nautilusImage, 2, "update-demo", updateDemoSelector, getUDData("nautilus.jpg", ns), ns)
  250. })
  251. /*
  252. Release : v1.9
  253. Testname: Kubectl, scale replication controller
  254. Description: Create a Pod and a container with a given image. Configure replication controller to run 2 replicas. The number of running instances of the Pod MUST equal the number of replicas set on the replication controller which is 2. Update the replicaset to 1. Number of running instances of the Pod MUST be 1. Update the replicaset to 2. Number of running instances of the Pod MUST be 2.
  255. */
  256. framework.ConformanceIt("should scale a replication controller ", func() {
  257. defer cleanupKubectlInputs(nautilus, ns, updateDemoSelector)
  258. By("creating a replication controller")
  259. framework.RunKubectlOrDieInput(nautilus, "create", "-f", "-", fmt.Sprintf("--namespace=%v", ns))
  260. framework.ValidateController(c, nautilusImage, 2, "update-demo", updateDemoSelector, getUDData("nautilus.jpg", ns), ns)
  261. By("scaling down the replication controller")
  262. framework.RunKubectlOrDie("scale", "rc", "update-demo-nautilus", "--replicas=1", "--timeout=5m", fmt.Sprintf("--namespace=%v", ns))
  263. framework.ValidateController(c, nautilusImage, 1, "update-demo", updateDemoSelector, getUDData("nautilus.jpg", ns), ns)
  264. By("scaling up the replication controller")
  265. framework.RunKubectlOrDie("scale", "rc", "update-demo-nautilus", "--replicas=2", "--timeout=5m", fmt.Sprintf("--namespace=%v", ns))
  266. framework.ValidateController(c, nautilusImage, 2, "update-demo", updateDemoSelector, getUDData("nautilus.jpg", ns), ns)
  267. })
  268. /*
  269. Release : v1.9
  270. Testname: Kubectl, rolling update replication controller
  271. Description: Create a Pod and a container with a given image. Configure replication controller to run 2 replicas. The number of running instances of the Pod MUST equal the number of replicas set on the replication controller which is 2. Run a rolling update to run a different version of the container. All running instances SHOULD now be running the newer version of the container as part of the rolling update.
  272. */
  273. framework.ConformanceIt("should do a rolling update of a replication controller ", func() {
  274. By("creating the initial replication controller")
  275. framework.RunKubectlOrDieInput(string(nautilus[:]), "create", "-f", "-", fmt.Sprintf("--namespace=%v", ns))
  276. framework.ValidateController(c, nautilusImage, 2, "update-demo", updateDemoSelector, getUDData("nautilus.jpg", ns), ns)
  277. By("rolling-update to new replication controller")
  278. framework.RunKubectlOrDieInput(string(kitten[:]), "rolling-update", "update-demo-nautilus", "--update-period=1s", "-f", "-", fmt.Sprintf("--namespace=%v", ns))
  279. framework.ValidateController(c, kittenImage, 2, "update-demo", updateDemoSelector, getUDData("kitten.jpg", ns), ns)
  280. // Everything will hopefully be cleaned up when the namespace is deleted.
  281. })
  282. })
  283. framework.KubeDescribe("Guestbook application", func() {
  284. forEachGBFile := func(run func(s string)) {
  285. guestbookRoot := "test/e2e/testing-manifests/guestbook"
  286. for _, gbAppFile := range []string{
  287. "redis-slave-service.yaml",
  288. "redis-master-service.yaml",
  289. "frontend-service.yaml",
  290. "frontend-deployment.yaml.in",
  291. "redis-master-deployment.yaml.in",
  292. "redis-slave-deployment.yaml.in",
  293. } {
  294. contents := substituteImageName(string(generated.ReadOrDie(filepath.Join(guestbookRoot, gbAppFile))))
  295. run(contents)
  296. }
  297. }
  298. /*
  299. Release : v1.9
  300. Testname: Kubectl, guestbook application
  301. Description: Create Guestbook application that contains redis server, 2 instances of redis slave, frontend application, frontend service and redis master service and redis slave service. Using frontend service, the test will write an entry into the guestbook application which will store the entry into the backend redis database. Application flow MUST work as expected and the data written MUST be available to read.
  302. */
  303. framework.ConformanceIt("should create and stop a working application ", func() {
  304. defer forEachGBFile(func(contents string) {
  305. cleanupKubectlInputs(contents, ns)
  306. })
  307. By("creating all guestbook components")
  308. forEachGBFile(func(contents string) {
  309. framework.Logf(contents)
  310. framework.RunKubectlOrDieInput(contents, "create", "-f", "-", fmt.Sprintf("--namespace=%v", ns))
  311. })
  312. By("validating guestbook app")
  313. validateGuestbookApp(c, ns)
  314. })
  315. })
  316. framework.KubeDescribe("Simple pod", func() {
  317. podYaml := substituteImageName(string(readTestFileOrDie("pod-with-readiness-probe.yaml.in")))
  318. BeforeEach(func() {
  319. By(fmt.Sprintf("creating the pod from %v", podYaml))
  320. framework.RunKubectlOrDieInput(podYaml, "create", "-f", "-", fmt.Sprintf("--namespace=%v", ns))
  321. Expect(framework.CheckPodsRunningReady(c, ns, []string{simplePodName}, framework.PodStartTimeout)).To(BeTrue())
  322. })
  323. AfterEach(func() {
  324. cleanupKubectlInputs(podYaml, ns, simplePodSelector)
  325. })
  326. It("should support exec", func() {
  327. By("executing a command in the container")
  328. execOutput := framework.RunKubectlOrDie("exec", fmt.Sprintf("--namespace=%v", ns), simplePodName, "echo", "running", "in", "container")
  329. if e, a := "running in container", strings.TrimSpace(execOutput); e != a {
  330. framework.Failf("Unexpected kubectl exec output. Wanted %q, got %q", e, a)
  331. }
  332. By("executing a very long command in the container")
  333. veryLongData := make([]rune, 20000)
  334. for i := 0; i < len(veryLongData); i++ {
  335. veryLongData[i] = 'a'
  336. }
  337. execOutput = framework.RunKubectlOrDie("exec", fmt.Sprintf("--namespace=%v", ns), simplePodName, "echo", string(veryLongData))
  338. Expect(string(veryLongData)).To(Equal(strings.TrimSpace(execOutput)), "Unexpected kubectl exec output")
  339. By("executing a command in the container with noninteractive stdin")
  340. execOutput = framework.NewKubectlCommand("exec", fmt.Sprintf("--namespace=%v", ns), "-i", simplePodName, "cat").
  341. WithStdinData("abcd1234").
  342. ExecOrDie()
  343. if e, a := "abcd1234", execOutput; e != a {
  344. framework.Failf("Unexpected kubectl exec output. Wanted %q, got %q", e, a)
  345. }
  346. // pretend that we're a user in an interactive shell
  347. r, closer, err := newBlockingReader("echo hi\nexit\n")
  348. if err != nil {
  349. framework.Failf("Error creating blocking reader: %v", err)
  350. }
  351. // NOTE this is solely for test cleanup!
  352. defer closer.Close()
  353. By("executing a command in the container with pseudo-interactive stdin")
  354. execOutput = framework.NewKubectlCommand("exec", fmt.Sprintf("--namespace=%v", ns), "-i", simplePodName, "bash").
  355. WithStdinReader(r).
  356. ExecOrDie()
  357. if e, a := "hi", strings.TrimSpace(execOutput); e != a {
  358. framework.Failf("Unexpected kubectl exec output. Wanted %q, got %q", e, a)
  359. }
  360. })
  361. It("should support exec through an HTTP proxy", func() {
  362. // Fail if the variable isn't set
  363. if framework.TestContext.Host == "" {
  364. framework.Failf("--host variable must be set to the full URI to the api server on e2e run.")
  365. }
  366. By("Starting goproxy")
  367. testSrv, proxyLogs := startLocalProxy()
  368. defer testSrv.Close()
  369. proxyAddr := testSrv.URL
  370. for _, proxyVar := range []string{"https_proxy", "HTTPS_PROXY"} {
  371. proxyLogs.Reset()
  372. By("Running kubectl via an HTTP proxy using " + proxyVar)
  373. output := framework.NewKubectlCommand(fmt.Sprintf("--namespace=%s", ns), "exec", "nginx", "echo", "running", "in", "container").
  374. WithEnv(append(os.Environ(), fmt.Sprintf("%s=%s", proxyVar, proxyAddr))).
  375. ExecOrDie()
  376. // Verify we got the normal output captured by the exec server
  377. expectedExecOutput := "running in container\n"
  378. if output != expectedExecOutput {
  379. framework.Failf("Unexpected kubectl exec output. Wanted %q, got %q", expectedExecOutput, output)
  380. }
  381. // Verify the proxy server logs saw the connection
  382. expectedProxyLog := fmt.Sprintf("Accepting CONNECT to %s", strings.TrimRight(strings.TrimLeft(framework.TestContext.Host, "https://"), "/api"))
  383. proxyLog := proxyLogs.String()
  384. if !strings.Contains(proxyLog, expectedProxyLog) {
  385. framework.Failf("Missing expected log result on proxy server for %s. Expected: %q, got %q", proxyVar, expectedProxyLog, proxyLog)
  386. }
  387. }
  388. })
  389. It("should support exec through kubectl proxy", func() {
  390. // Fail if the variable isn't set
  391. if framework.TestContext.Host == "" {
  392. framework.Failf("--host variable must be set to the full URI to the api server on e2e run.")
  393. }
  394. By("Starting kubectl proxy")
  395. port, proxyCmd, err := startProxyServer()
  396. framework.ExpectNoError(err)
  397. defer framework.TryKill(proxyCmd)
  398. //proxyLogs.Reset()
  399. host := fmt.Sprintf("--server=http://127.0.0.1:%d", port)
  400. By("Running kubectl via kubectl proxy using " + host)
  401. output := framework.NewKubectlCommand(
  402. host, fmt.Sprintf("--namespace=%s", ns),
  403. "exec", "nginx", "echo", "running", "in", "container",
  404. ).ExecOrDie()
  405. // Verify we got the normal output captured by the exec server
  406. expectedExecOutput := "running in container\n"
  407. if output != expectedExecOutput {
  408. framework.Failf("Unexpected kubectl exec output. Wanted %q, got %q", expectedExecOutput, output)
  409. }
  410. })
  411. It("should return command exit codes", func() {
  412. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  413. By("execing into a container with a successful command")
  414. _, err := framework.NewKubectlCommand(nsFlag, "exec", "nginx", "--", "/bin/sh", "-c", "exit 0").Exec()
  415. framework.ExpectNoError(err)
  416. By("execing into a container with a failing command")
  417. _, err = framework.NewKubectlCommand(nsFlag, "exec", "nginx", "--", "/bin/sh", "-c", "exit 42").Exec()
  418. ee, ok := err.(uexec.ExitError)
  419. Expect(ok).To(Equal(true))
  420. Expect(ee.ExitStatus()).To(Equal(42))
  421. By("running a successful command")
  422. _, err = framework.NewKubectlCommand(nsFlag, "run", "-i", "--image="+busyboxImage, "--restart=Never", "success", "--", "/bin/sh", "-c", "exit 0").Exec()
  423. framework.ExpectNoError(err)
  424. By("running a failing command")
  425. _, err = framework.NewKubectlCommand(nsFlag, "run", "-i", "--image="+busyboxImage, "--restart=Never", "failure-1", "--", "/bin/sh", "-c", "exit 42").Exec()
  426. ee, ok = err.(uexec.ExitError)
  427. Expect(ok).To(Equal(true))
  428. Expect(ee.ExitStatus()).To(Equal(42))
  429. By("running a failing command without --restart=Never")
  430. _, err = framework.NewKubectlCommand(nsFlag, "run", "-i", "--image="+busyboxImage, "--restart=OnFailure", "failure-2", "--", "/bin/sh", "-c", "cat && exit 42").
  431. WithStdinData("abcd1234").
  432. Exec()
  433. framework.ExpectNoError(err)
  434. By("running a failing command without --restart=Never, but with --rm")
  435. _, err = framework.NewKubectlCommand(nsFlag, "run", "-i", "--image="+busyboxImage, "--restart=OnFailure", "--rm", "failure-3", "--", "/bin/sh", "-c", "cat && exit 42").
  436. WithStdinData("abcd1234").
  437. Exec()
  438. framework.ExpectNoError(err)
  439. framework.WaitForPodToDisappear(f.ClientSet, ns, "failure-3", labels.Everything(), 2*time.Second, wait.ForeverTestTimeout)
  440. By("running a failing command with --leave-stdin-open")
  441. _, err = framework.NewKubectlCommand(nsFlag, "run", "-i", "--image="+busyboxImage, "--restart=Never", "failure-4", "--leave-stdin-open", "--", "/bin/sh", "-c", "exit 42").
  442. WithStdinData("abcd1234").
  443. Exec()
  444. framework.ExpectNoError(err)
  445. })
  446. It("should support inline execution and attach", func() {
  447. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  448. By("executing a command with run and attach with stdin")
  449. runOutput := framework.NewKubectlCommand(nsFlag, "run", "run-test", "--image="+busyboxImage, "--restart=OnFailure", "--attach=true", "--stdin", "--", "sh", "-c", "cat && echo 'stdin closed'").
  450. WithStdinData("abcd1234").
  451. ExecOrDie()
  452. Expect(runOutput).To(ContainSubstring("abcd1234"))
  453. Expect(runOutput).To(ContainSubstring("stdin closed"))
  454. Expect(c.BatchV1().Jobs(ns).Delete("run-test", nil)).To(BeNil())
  455. By("executing a command with run and attach without stdin")
  456. runOutput = framework.NewKubectlCommand(fmt.Sprintf("--namespace=%v", ns), "run", "run-test-2", "--image="+busyboxImage, "--restart=OnFailure", "--attach=true", "--leave-stdin-open=true", "--", "sh", "-c", "cat && echo 'stdin closed'").
  457. WithStdinData("abcd1234").
  458. ExecOrDie()
  459. Expect(runOutput).ToNot(ContainSubstring("abcd1234"))
  460. Expect(runOutput).To(ContainSubstring("stdin closed"))
  461. Expect(c.BatchV1().Jobs(ns).Delete("run-test-2", nil)).To(BeNil())
  462. By("executing a command with run and attach with stdin with open stdin should remain running")
  463. runOutput = framework.NewKubectlCommand(nsFlag, "run", "run-test-3", "--image="+busyboxImage, "--restart=OnFailure", "--attach=true", "--leave-stdin-open=true", "--stdin", "--", "sh", "-c", "cat && echo 'stdin closed'").
  464. WithStdinData("abcd1234\n").
  465. ExecOrDie()
  466. Expect(runOutput).ToNot(ContainSubstring("stdin closed"))
  467. g := func(pods []*v1.Pod) sort.Interface { return sort.Reverse(controller.ActivePods(pods)) }
  468. runTestPod, _, err := polymorphichelpers.GetFirstPod(f.InternalClientset.Core(), ns, "run=run-test-3", 1*time.Minute, g)
  469. if err != nil {
  470. os.Exit(1)
  471. }
  472. if !framework.CheckPodsRunningReady(c, ns, []string{runTestPod.Name}, time.Minute) {
  473. framework.Failf("Pod %q of Job %q should still be running", runTestPod.Name, "run-test-3")
  474. }
  475. // NOTE: we cannot guarantee our output showed up in the container logs before stdin was closed, so we have
  476. // to loop test.
  477. err = wait.PollImmediate(time.Second, time.Minute, func() (bool, error) {
  478. if !framework.CheckPodsRunningReady(c, ns, []string{runTestPod.Name}, 1*time.Second) {
  479. framework.Failf("Pod %q of Job %q should still be running", runTestPod.Name, "run-test-3")
  480. }
  481. logOutput := framework.RunKubectlOrDie(nsFlag, "logs", runTestPod.Name)
  482. Expect(logOutput).ToNot(ContainSubstring("stdin closed"))
  483. return strings.Contains(logOutput, "abcd1234"), nil
  484. })
  485. if err != nil {
  486. os.Exit(1)
  487. }
  488. Expect(err).To(BeNil())
  489. Expect(c.BatchV1().Jobs(ns).Delete("run-test-3", nil)).To(BeNil())
  490. })
  491. It("should support port-forward", func() {
  492. By("forwarding the container port to a local port")
  493. cmd := runPortForward(ns, simplePodName, simplePodPort)
  494. defer cmd.Stop()
  495. By("curling local port output")
  496. localAddr := fmt.Sprintf("http://localhost:%d", cmd.port)
  497. body, err := curl(localAddr)
  498. framework.Logf("got: %s", body)
  499. if err != nil {
  500. framework.Failf("Failed http.Get of forwarded port (%s): %v", localAddr, err)
  501. }
  502. if !strings.Contains(body, nginxDefaultOutput) {
  503. framework.Failf("Container port output missing expected value. Wanted:'%s', got: %s", nginxDefaultOutput, body)
  504. }
  505. })
  506. It("should handle in-cluster config", func() {
  507. By("adding rbac permissions")
  508. // grant the view permission widely to allow inspection of the `invalid` namespace and the default namespace
  509. framework.BindClusterRole(f.ClientSet.RbacV1beta1(), "view", f.Namespace.Name,
  510. rbacv1beta1.Subject{Kind: rbacv1beta1.ServiceAccountKind, Namespace: f.Namespace.Name, Name: "default"})
  511. err := framework.WaitForAuthorizationUpdate(f.ClientSet.AuthorizationV1beta1(),
  512. serviceaccount.MakeUsername(f.Namespace.Name, "default"),
  513. f.Namespace.Name, "list", schema.GroupResource{Resource: "pods"}, true)
  514. framework.ExpectNoError(err)
  515. By("overriding icc with values provided by flags")
  516. kubectlPath := framework.TestContext.KubectlPath
  517. // we need the actual kubectl binary, not the script wrapper
  518. kubectlPathNormalizer := exec.Command("which", kubectlPath)
  519. if strings.HasSuffix(kubectlPath, "kubectl.sh") {
  520. kubectlPathNormalizer = exec.Command(kubectlPath, "path")
  521. }
  522. kubectlPathNormalized, err := kubectlPathNormalizer.Output()
  523. framework.ExpectNoError(err)
  524. kubectlPath = strings.TrimSpace(string(kubectlPathNormalized))
  525. inClusterHost := strings.TrimSpace(framework.RunHostCmdOrDie(ns, simplePodName, "printenv KUBERNETES_SERVICE_HOST"))
  526. inClusterPort := strings.TrimSpace(framework.RunHostCmdOrDie(ns, simplePodName, "printenv KUBERNETES_SERVICE_PORT"))
  527. framework.Logf("copying %s to the %s pod", kubectlPath, simplePodName)
  528. framework.RunKubectlOrDie("cp", kubectlPath, ns+"/"+simplePodName+":/tmp/")
  529. // Build a kubeconfig file that will make use of the injected ca and token,
  530. // but point at the DNS host and the default namespace
  531. tmpDir, err := ioutil.TempDir("", "icc-override")
  532. overrideKubeconfigName := "icc-override.kubeconfig"
  533. framework.ExpectNoError(err)
  534. defer func() { os.Remove(tmpDir) }()
  535. framework.ExpectNoError(ioutil.WriteFile(filepath.Join(tmpDir, overrideKubeconfigName), []byte(`
  536. kind: Config
  537. apiVersion: v1
  538. clusters:
  539. - cluster:
  540. api-version: v1
  541. server: https://kubernetes.default.svc:443
  542. certificate-authority: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
  543. name: kubeconfig-cluster
  544. contexts:
  545. - context:
  546. cluster: kubeconfig-cluster
  547. namespace: default
  548. user: kubeconfig-user
  549. name: kubeconfig-context
  550. current-context: kubeconfig-context
  551. users:
  552. - name: kubeconfig-user
  553. user:
  554. tokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
  555. `), os.FileMode(0755)))
  556. framework.Logf("copying override kubeconfig to the %s pod", simplePodName)
  557. framework.RunKubectlOrDie("cp", filepath.Join(tmpDir, overrideKubeconfigName), ns+"/"+simplePodName+":/tmp/")
  558. framework.ExpectNoError(ioutil.WriteFile(filepath.Join(tmpDir, "invalid-configmap-with-namespace.yaml"), []byte(`
  559. kind: ConfigMap
  560. apiVersion: v1
  561. metadata:
  562. name: "configmap with namespace and invalid name"
  563. namespace: configmap-namespace
  564. `), os.FileMode(0755)))
  565. framework.ExpectNoError(ioutil.WriteFile(filepath.Join(tmpDir, "invalid-configmap-without-namespace.yaml"), []byte(`
  566. kind: ConfigMap
  567. apiVersion: v1
  568. metadata:
  569. name: "configmap without namespace and invalid name"
  570. `), os.FileMode(0755)))
  571. framework.Logf("copying configmap manifests to the %s pod", simplePodName)
  572. framework.RunKubectlOrDie("cp", filepath.Join(tmpDir, "invalid-configmap-with-namespace.yaml"), ns+"/"+simplePodName+":/tmp/")
  573. framework.RunKubectlOrDie("cp", filepath.Join(tmpDir, "invalid-configmap-without-namespace.yaml"), ns+"/"+simplePodName+":/tmp/")
  574. By("getting pods with in-cluster configs")
  575. execOutput := framework.RunHostCmdOrDie(ns, simplePodName, "/tmp/kubectl get pods --v=7 2>&1")
  576. Expect(execOutput).To(MatchRegexp("nginx +1/1 +Running"))
  577. Expect(execOutput).To(ContainSubstring("Using in-cluster namespace"))
  578. Expect(execOutput).To(ContainSubstring("Using in-cluster configuration"))
  579. By("creating an object containing a namespace with in-cluster config")
  580. _, err = framework.RunHostCmd(ns, simplePodName, "/tmp/kubectl create -f /tmp/invalid-configmap-with-namespace.yaml --v=7 2>&1")
  581. Expect(err).To(ContainSubstring("Using in-cluster namespace"))
  582. Expect(err).To(ContainSubstring("Using in-cluster configuration"))
  583. Expect(err).To(ContainSubstring(fmt.Sprintf("POST https://%s:%s/api/v1/namespaces/configmap-namespace/configmaps", inClusterHost, inClusterPort)))
  584. By("creating an object not containing a namespace with in-cluster config")
  585. _, err = framework.RunHostCmd(ns, simplePodName, "/tmp/kubectl create -f /tmp/invalid-configmap-without-namespace.yaml --v=7 2>&1")
  586. Expect(err).To(ContainSubstring("Using in-cluster namespace"))
  587. Expect(err).To(ContainSubstring("Using in-cluster configuration"))
  588. Expect(err).To(ContainSubstring(fmt.Sprintf("POST https://%s:%s/api/v1/namespaces/%s/configmaps", inClusterHost, inClusterPort, f.Namespace.Name)))
  589. By("trying to use kubectl with invalid token")
  590. _, err = framework.RunHostCmd(ns, simplePodName, "/tmp/kubectl get pods --token=invalid --v=7 2>&1")
  591. framework.Logf("got err %v", err)
  592. Expect(err).To(HaveOccurred())
  593. Expect(err).To(ContainSubstring("Using in-cluster namespace"))
  594. Expect(err).To(ContainSubstring("Using in-cluster configuration"))
  595. Expect(err).To(ContainSubstring("Authorization: Bearer invalid"))
  596. Expect(err).To(ContainSubstring("Response Status: 401 Unauthorized"))
  597. By("trying to use kubectl with invalid server")
  598. _, err = framework.RunHostCmd(ns, simplePodName, "/tmp/kubectl get pods --server=invalid --v=6 2>&1")
  599. framework.Logf("got err %v", err)
  600. Expect(err).To(HaveOccurred())
  601. Expect(err).To(ContainSubstring("Unable to connect to the server"))
  602. Expect(err).To(ContainSubstring("GET http://invalid/api"))
  603. By("trying to use kubectl with invalid namespace")
  604. execOutput = framework.RunHostCmdOrDie(ns, simplePodName, "/tmp/kubectl get pods --namespace=invalid --v=6 2>&1")
  605. Expect(execOutput).To(ContainSubstring("No resources found"))
  606. Expect(execOutput).ToNot(ContainSubstring("Using in-cluster namespace"))
  607. Expect(execOutput).To(ContainSubstring("Using in-cluster configuration"))
  608. Expect(execOutput).To(MatchRegexp(fmt.Sprintf("GET http[s]?://%s:%s/api/v1/namespaces/invalid/pods", inClusterHost, inClusterPort)))
  609. By("trying to use kubectl with kubeconfig")
  610. execOutput = framework.RunHostCmdOrDie(ns, simplePodName, "/tmp/kubectl get pods --kubeconfig=/tmp/"+overrideKubeconfigName+" --v=6 2>&1")
  611. Expect(execOutput).ToNot(ContainSubstring("Using in-cluster namespace"))
  612. Expect(execOutput).ToNot(ContainSubstring("Using in-cluster configuration"))
  613. Expect(execOutput).To(ContainSubstring("GET https://kubernetes.default.svc:443/api/v1/namespaces/default/pods"))
  614. })
  615. })
  616. framework.KubeDescribe("Kubectl api-versions", func() {
  617. /*
  618. Release : v1.9
  619. Testname: Kubectl, check version v1
  620. Description: Run kubectl to get api versions, output MUST contain returned versions with ‘v1’ listed.
  621. */
  622. framework.ConformanceIt("should check if v1 is in available api versions ", func() {
  623. By("validating api versions")
  624. output := framework.RunKubectlOrDie("api-versions")
  625. if !strings.Contains(output, "v1") {
  626. framework.Failf("No v1 in kubectl api-versions")
  627. }
  628. })
  629. })
  630. framework.KubeDescribe("Kubectl apply", func() {
  631. It("should apply a new configuration to an existing RC", func() {
  632. controllerJson := substituteImageName(string(readTestFileOrDie(redisControllerFilename)))
  633. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  634. By("creating Redis RC")
  635. framework.RunKubectlOrDieInput(controllerJson, "create", "-f", "-", nsFlag)
  636. By("applying a modified configuration")
  637. stdin := modifyReplicationControllerConfiguration(controllerJson)
  638. framework.NewKubectlCommand("apply", "-f", "-", nsFlag).
  639. WithStdinReader(stdin).
  640. ExecOrDie()
  641. By("checking the result")
  642. forEachReplicationController(c, ns, "app", "redis", validateReplicationControllerConfiguration)
  643. })
  644. It("should reuse port when apply to an existing SVC", func() {
  645. serviceJson := readTestFileOrDie(redisServiceFilename)
  646. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  647. By("creating Redis SVC")
  648. framework.RunKubectlOrDieInput(string(serviceJson[:]), "create", "-f", "-", nsFlag)
  649. By("getting the original port")
  650. originalNodePort := framework.RunKubectlOrDie("get", "service", "redis-master", nsFlag, "-o", "jsonpath={.spec.ports[0].port}")
  651. By("applying the same configuration")
  652. framework.RunKubectlOrDieInput(string(serviceJson[:]), "apply", "-f", "-", nsFlag)
  653. By("getting the port after applying configuration")
  654. currentNodePort := framework.RunKubectlOrDie("get", "service", "redis-master", nsFlag, "-o", "jsonpath={.spec.ports[0].port}")
  655. By("checking the result")
  656. if originalNodePort != currentNodePort {
  657. framework.Failf("port should keep the same")
  658. }
  659. })
  660. It("apply set/view last-applied", func() {
  661. deployment1Yaml := substituteImageName(string(readTestFileOrDie(nginxDeployment1Filename)))
  662. deployment2Yaml := substituteImageName(string(readTestFileOrDie(nginxDeployment2Filename)))
  663. deployment3Yaml := substituteImageName(string(readTestFileOrDie(nginxDeployment3Filename)))
  664. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  665. By("deployment replicas number is 2")
  666. framework.RunKubectlOrDieInput(deployment1Yaml, "apply", "-f", "-", nsFlag)
  667. By("check the last-applied matches expectations annotations")
  668. output := framework.RunKubectlOrDieInput(deployment1Yaml, "apply", "view-last-applied", "-f", "-", nsFlag, "-o", "json")
  669. requiredString := "\"replicas\": 2"
  670. if !strings.Contains(output, requiredString) {
  671. framework.Failf("Missing %s in kubectl view-last-applied", requiredString)
  672. }
  673. By("apply file doesn't have replicas")
  674. framework.RunKubectlOrDieInput(deployment2Yaml, "apply", "set-last-applied", "-f", "-", nsFlag)
  675. By("check last-applied has been updated, annotations doesn't replicas")
  676. output = framework.RunKubectlOrDieInput(deployment1Yaml, "apply", "view-last-applied", "-f", "-", nsFlag, "-o", "json")
  677. requiredString = "\"replicas\": 2"
  678. if strings.Contains(output, requiredString) {
  679. framework.Failf("Missing %s in kubectl view-last-applied", requiredString)
  680. }
  681. By("scale set replicas to 3")
  682. nginxDeploy := "nginx-deployment"
  683. framework.RunKubectlOrDie("scale", "deployment", nginxDeploy, "--replicas=3", nsFlag)
  684. By("apply file doesn't have replicas but image changed")
  685. framework.RunKubectlOrDieInput(deployment3Yaml, "apply", "-f", "-", nsFlag)
  686. By("verify replicas still is 3 and image has been updated")
  687. output = framework.RunKubectlOrDieInput(deployment3Yaml, "get", "-f", "-", nsFlag, "-o", "json")
  688. requiredItems := []string{"\"replicas\": 3", imageutils.GetE2EImage(imageutils.NginxSlim)}
  689. for _, item := range requiredItems {
  690. if !strings.Contains(output, item) {
  691. framework.Failf("Missing %s in kubectl apply", item)
  692. }
  693. }
  694. })
  695. })
  696. framework.KubeDescribe("Kubectl cluster-info", func() {
  697. /*
  698. Release : v1.9
  699. Testname: Kubectl, cluster info
  700. Description: Call kubectl to get cluster-info, output MUST contain cluster-info returned and Kubernetes Master SHOULD be running.
  701. */
  702. framework.ConformanceIt("should check if Kubernetes master services is included in cluster-info ", func() {
  703. By("validating cluster-info")
  704. output := framework.RunKubectlOrDie("cluster-info")
  705. // Can't check exact strings due to terminal control commands (colors)
  706. requiredItems := []string{"Kubernetes master", "is running at"}
  707. if framework.ProviderIs("gce", "gke") {
  708. requiredItems = append(requiredItems, "Heapster")
  709. }
  710. for _, item := range requiredItems {
  711. if !strings.Contains(output, item) {
  712. framework.Failf("Missing %s in kubectl cluster-info", item)
  713. }
  714. }
  715. })
  716. })
  717. framework.KubeDescribe("Kubectl describe", func() {
  718. /*
  719. Release : v1.9
  720. Testname: Kubectl, describe pod or rc
  721. Description: Deploy a redis controller and a redis service. Kubectl describe pods SHOULD return the name, namespace, labels, state and other information as expected. Kubectl describe on rc, service, node and namespace SHOULD also return proper information.
  722. */
  723. framework.ConformanceIt("should check if kubectl describe prints relevant information for rc and pods ", func() {
  724. kv, err := framework.KubectlVersion()
  725. Expect(err).NotTo(HaveOccurred())
  726. framework.SkipUnlessServerVersionGTE(kv, c.Discovery())
  727. controllerJson := substituteImageName(string(readTestFileOrDie(redisControllerFilename)))
  728. serviceJson := readTestFileOrDie(redisServiceFilename)
  729. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  730. framework.RunKubectlOrDieInput(controllerJson, "create", "-f", "-", nsFlag)
  731. framework.RunKubectlOrDieInput(string(serviceJson[:]), "create", "-f", "-", nsFlag)
  732. By("Waiting for Redis master to start.")
  733. waitForOrFailWithDebug(1)
  734. // Pod
  735. forEachPod(func(pod v1.Pod) {
  736. output := framework.RunKubectlOrDie("describe", "pod", pod.Name, nsFlag)
  737. requiredStrings := [][]string{
  738. {"Name:", "redis-master-"},
  739. {"Namespace:", ns},
  740. {"Node:"},
  741. {"Labels:", "app=redis"},
  742. {"role=master"},
  743. {"Annotations:"},
  744. {"Status:", "Running"},
  745. {"IP:"},
  746. {"Controlled By:", "ReplicationController/redis-master"},
  747. {"Image:", redisImage},
  748. {"State:", "Running"},
  749. {"QoS Class:", "BestEffort"},
  750. }
  751. checkOutput(output, requiredStrings)
  752. })
  753. // Rc
  754. requiredStrings := [][]string{
  755. {"Name:", "redis-master"},
  756. {"Namespace:", ns},
  757. {"Selector:", "app=redis,role=master"},
  758. {"Labels:", "app=redis"},
  759. {"role=master"},
  760. {"Annotations:"},
  761. {"Replicas:", "1 current", "1 desired"},
  762. {"Pods Status:", "1 Running", "0 Waiting", "0 Succeeded", "0 Failed"},
  763. {"Pod Template:"},
  764. {"Image:", redisImage},
  765. {"Events:"}}
  766. checkKubectlOutputWithRetry(requiredStrings, "describe", "rc", "redis-master", nsFlag)
  767. // Service
  768. output := framework.RunKubectlOrDie("describe", "service", "redis-master", nsFlag)
  769. requiredStrings = [][]string{
  770. {"Name:", "redis-master"},
  771. {"Namespace:", ns},
  772. {"Labels:", "app=redis"},
  773. {"role=master"},
  774. {"Annotations:"},
  775. {"Selector:", "app=redis", "role=master"},
  776. {"Type:", "ClusterIP"},
  777. {"IP:"},
  778. {"Port:", "<unset>", "6379/TCP"},
  779. {"Endpoints:"},
  780. {"Session Affinity:", "None"}}
  781. checkOutput(output, requiredStrings)
  782. // Node
  783. // It should be OK to list unschedulable Nodes here.
  784. nodes, err := c.CoreV1().Nodes().List(metav1.ListOptions{})
  785. Expect(err).NotTo(HaveOccurred())
  786. node := nodes.Items[0]
  787. output = framework.RunKubectlOrDie("describe", "node", node.Name)
  788. requiredStrings = [][]string{
  789. {"Name:", node.Name},
  790. {"Labels:"},
  791. {"Annotations:"},
  792. {"CreationTimestamp:"},
  793. {"Conditions:"},
  794. {"Type", "Status", "LastHeartbeatTime", "LastTransitionTime", "Reason", "Message"},
  795. {"Addresses:"},
  796. {"Capacity:"},
  797. {"Version:"},
  798. {"Kernel Version:"},
  799. {"OS Image:"},
  800. {"Container Runtime Version:"},
  801. {"Kubelet Version:"},
  802. {"Kube-Proxy Version:"},
  803. {"Pods:"}}
  804. checkOutput(output, requiredStrings)
  805. // Namespace
  806. output = framework.RunKubectlOrDie("describe", "namespace", ns)
  807. requiredStrings = [][]string{
  808. {"Name:", ns},
  809. {"Labels:"},
  810. {"Annotations:"},
  811. {"Status:", "Active"}}
  812. checkOutput(output, requiredStrings)
  813. // Quota and limitrange are skipped for now.
  814. })
  815. })
  816. framework.KubeDescribe("Kubectl expose", func() {
  817. /*
  818. Release : v1.9
  819. Testname: Kubectl, create service, replication controller
  820. Description: Create a Pod running redis master listening to port 6379. Using kubectl expose the redis master replication controllers at port 1234. Validate that the replication controller is listening on port 1234 and the target port is set to 6379, port that redis master is listening. Using kubectl expose the redis master as a service at port 2345. The service MUST be listening on port 2345 and the target port is set to 6379, port that redis master is listening.
  821. */
  822. framework.ConformanceIt("should create services for rc ", func() {
  823. controllerJson := substituteImageName(string(readTestFileOrDie(redisControllerFilename)))
  824. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  825. redisPort := 6379
  826. By("creating Redis RC")
  827. framework.Logf("namespace %v", ns)
  828. framework.RunKubectlOrDieInput(controllerJson, "create", "-f", "-", nsFlag)
  829. // It may take a while for the pods to get registered in some cases, wait to be sure.
  830. By("Waiting for Redis master to start.")
  831. waitForOrFailWithDebug(1)
  832. forEachPod(func(pod v1.Pod) {
  833. framework.Logf("wait on redis-master startup in %v ", ns)
  834. framework.LookForStringInLog(ns, pod.Name, "redis-master", "The server is now ready to accept connections", framework.PodStartTimeout)
  835. })
  836. validateService := func(name string, servicePort int, timeout time.Duration) {
  837. err := wait.Poll(framework.Poll, timeout, func() (bool, error) {
  838. endpoints, err := c.CoreV1().Endpoints(ns).Get(name, metav1.GetOptions{})
  839. if err != nil {
  840. // log the real error
  841. framework.Logf("Get endpoints failed (interval %v): %v", framework.Poll, err)
  842. // if the error is API not found or could not find default credentials or TLS handshake timeout, try again
  843. if apierrs.IsNotFound(err) ||
  844. apierrs.IsUnauthorized(err) ||
  845. apierrs.IsServerTimeout(err) {
  846. err = nil
  847. }
  848. return false, err
  849. }
  850. uidToPort := framework.GetContainerPortsByPodUID(endpoints)
  851. if len(uidToPort) == 0 {
  852. framework.Logf("No endpoint found, retrying")
  853. return false, nil
  854. }
  855. if len(uidToPort) > 1 {
  856. framework.Failf("Too many endpoints found")
  857. }
  858. for _, port := range uidToPort {
  859. if port[0] != redisPort {
  860. framework.Failf("Wrong endpoint port: %d", port[0])
  861. }
  862. }
  863. return true, nil
  864. })
  865. Expect(err).NotTo(HaveOccurred())
  866. service, err := c.CoreV1().Services(ns).Get(name, metav1.GetOptions{})
  867. Expect(err).NotTo(HaveOccurred())
  868. if len(service.Spec.Ports) != 1 {
  869. framework.Failf("1 port is expected")
  870. }
  871. port := service.Spec.Ports[0]
  872. if port.Port != int32(servicePort) {
  873. framework.Failf("Wrong service port: %d", port.Port)
  874. }
  875. if port.TargetPort.IntValue() != redisPort {
  876. framework.Failf("Wrong target port: %d", port.TargetPort.IntValue())
  877. }
  878. }
  879. By("exposing RC")
  880. framework.RunKubectlOrDie("expose", "rc", "redis-master", "--name=rm2", "--port=1234", fmt.Sprintf("--target-port=%d", redisPort), nsFlag)
  881. framework.WaitForService(c, ns, "rm2", true, framework.Poll, framework.ServiceStartTimeout)
  882. validateService("rm2", 1234, framework.ServiceStartTimeout)
  883. By("exposing service")
  884. framework.RunKubectlOrDie("expose", "service", "rm2", "--name=rm3", "--port=2345", fmt.Sprintf("--target-port=%d", redisPort), nsFlag)
  885. framework.WaitForService(c, ns, "rm3", true, framework.Poll, framework.ServiceStartTimeout)
  886. validateService("rm3", 2345, framework.ServiceStartTimeout)
  887. })
  888. })
  889. framework.KubeDescribe("Kubectl label", func() {
  890. podYaml := substituteImageName(string(readTestFileOrDie("pause-pod.yaml.in")))
  891. var nsFlag string
  892. BeforeEach(func() {
  893. By("creating the pod")
  894. nsFlag = fmt.Sprintf("--namespace=%v", ns)
  895. framework.RunKubectlOrDieInput(podYaml, "create", "-f", "-", nsFlag)
  896. Expect(framework.CheckPodsRunningReady(c, ns, []string{pausePodName}, framework.PodStartTimeout)).To(BeTrue())
  897. })
  898. AfterEach(func() {
  899. cleanupKubectlInputs(podYaml, ns, pausePodSelector)
  900. })
  901. /*
  902. Release : v1.9
  903. Testname: Kubectl, label update
  904. Description: When a Pod is running, update a Label using ‘kubectl label’ command. The label MUST be created in the Pod. A ‘kubectl get pod’ with -l option on the container MUST verify that the label can be read back. Use ‘kubectl label label-’ to remove the label. Kubetctl get pod’ with -l option SHOULD no list the deleted label as the label is removed.
  905. */
  906. framework.ConformanceIt("should update the label on a resource ", func() {
  907. labelName := "testing-label"
  908. labelValue := "testing-label-value"
  909. By("adding the label " + labelName + " with value " + labelValue + " to a pod")
  910. framework.RunKubectlOrDie("label", "pods", pausePodName, labelName+"="+labelValue, nsFlag)
  911. By("verifying the pod has the label " + labelName + " with the value " + labelValue)
  912. output := framework.RunKubectlOrDie("get", "pod", pausePodName, "-L", labelName, nsFlag)
  913. if !strings.Contains(output, labelValue) {
  914. framework.Failf("Failed updating label " + labelName + " to the pod " + pausePodName)
  915. }
  916. By("removing the label " + labelName + " of a pod")
  917. framework.RunKubectlOrDie("label", "pods", pausePodName, labelName+"-", nsFlag)
  918. By("verifying the pod doesn't have the label " + labelName)
  919. output = framework.RunKubectlOrDie("get", "pod", pausePodName, "-L", labelName, nsFlag)
  920. if strings.Contains(output, labelValue) {
  921. framework.Failf("Failed removing label " + labelName + " of the pod " + pausePodName)
  922. }
  923. })
  924. })
  925. framework.KubeDescribe("Kubectl logs", func() {
  926. var nsFlag string
  927. rc := substituteImageName(string(readTestFileOrDie(redisControllerFilename)))
  928. containerName := "redis-master"
  929. BeforeEach(func() {
  930. By("creating an rc")
  931. nsFlag = fmt.Sprintf("--namespace=%v", ns)
  932. framework.RunKubectlOrDieInput(rc, "create", "-f", "-", nsFlag)
  933. })
  934. AfterEach(func() {
  935. cleanupKubectlInputs(rc, ns, simplePodSelector)
  936. })
  937. /*
  938. Release : v1.9
  939. Testname: Kubectl, logs
  940. Description: When a Pod is running then it MUST generate logs.
  941. Starting a Pod should have a log line indicating the the server is running and ready to accept connections. Also log command options MUST work as expected and described below.
  942. ‘kubectl log -tail=1’ should generate a output of one line, the last line in the log.
  943. ‘kubectl --limit-bytes=1’ should generate a single byte output.
  944. ‘kubectl --tail=1 --timestamp should genrate one line with timestamp in RFC3339 format
  945. ‘kubectl --since=1s’ should output logs that are only 1 second older from now
  946. ‘kubectl --since=24h’ should output logs that are only 1 day older from now
  947. */
  948. framework.ConformanceIt("should be able to retrieve and filter logs ", func() {
  949. // Split("something\n", "\n") returns ["something", ""], so
  950. // strip trailing newline first
  951. lines := func(out string) []string {
  952. return strings.Split(strings.TrimRight(out, "\n"), "\n")
  953. }
  954. By("Waiting for Redis master to start.")
  955. waitForOrFailWithDebug(1)
  956. forEachPod(func(pod v1.Pod) {
  957. By("checking for a matching strings")
  958. _, err := framework.LookForStringInLog(ns, pod.Name, containerName, "The server is now ready to accept connections", framework.PodStartTimeout)
  959. Expect(err).NotTo(HaveOccurred())
  960. By("limiting log lines")
  961. out := framework.RunKubectlOrDie("log", pod.Name, containerName, nsFlag, "--tail=1")
  962. Expect(len(out)).NotTo(BeZero())
  963. Expect(len(lines(out))).To(Equal(1))
  964. By("limiting log bytes")
  965. out = framework.RunKubectlOrDie("log", pod.Name, containerName, nsFlag, "--limit-bytes=1")
  966. Expect(len(lines(out))).To(Equal(1))
  967. Expect(len(out)).To(Equal(1))
  968. By("exposing timestamps")
  969. out = framework.RunKubectlOrDie("log", pod.Name, containerName, nsFlag, "--tail=1", "--timestamps")
  970. l := lines(out)
  971. Expect(len(l)).To(Equal(1))
  972. words := strings.Split(l[0], " ")
  973. Expect(len(words)).To(BeNumerically(">", 1))
  974. if _, err := time.Parse(time.RFC3339Nano, words[0]); err != nil {
  975. if _, err := time.Parse(time.RFC3339, words[0]); err != nil {
  976. framework.Failf("expected %q to be RFC3339 or RFC3339Nano", words[0])
  977. }
  978. }
  979. By("restricting to a time range")
  980. // Note: we must wait at least two seconds,
  981. // because the granularity is only 1 second and
  982. // it could end up rounding the wrong way.
  983. time.Sleep(2500 * time.Millisecond) // ensure that startup logs on the node are seen as older than 1s
  984. recent_out := framework.RunKubectlOrDie("log", pod.Name, containerName, nsFlag, "--since=1s")
  985. recent := len(strings.Split(recent_out, "\n"))
  986. older_out := framework.RunKubectlOrDie("log", pod.Name, containerName, nsFlag, "--since=24h")
  987. older := len(strings.Split(older_out, "\n"))
  988. Expect(recent).To(BeNumerically("<", older), "expected recent(%v) to be less than older(%v)\nrecent lines:\n%v\nolder lines:\n%v\n", recent, older, recent_out, older_out)
  989. })
  990. })
  991. })
  992. framework.KubeDescribe("Kubectl patch", func() {
  993. /*
  994. Release : v1.9
  995. Testname: Kubectl, patch to annotate
  996. Description: Start running a redis master and a replication controller. When the pod is running, using ‘kubectl patch’ command add annotations. The annotation MUST be added to running pods and SHOULD be able to read added annotations from each of the Pods running under the replication controller.
  997. */
  998. framework.ConformanceIt("should add annotations for pods in rc ", func() {
  999. controllerJson := substituteImageName(string(readTestFileOrDie(redisControllerFilename)))
  1000. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  1001. By("creating Redis RC")
  1002. framework.RunKubectlOrDieInput(controllerJson, "create", "-f", "-", nsFlag)
  1003. By("Waiting for Redis master to start.")
  1004. waitForOrFailWi