PageRenderTime 54ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/test/e2e/kubectl.go

https://gitlab.com/CORP-RESELLER/kubernetes
Go | 1275 lines | 1040 code | 155 blank | 80 comment | 213 complexity | f6dd4a1ac8412df934ee5ea88a0b7215 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. package e2e
  14. import (
  15. "bytes"
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "io"
  20. "io/ioutil"
  21. "log"
  22. "mime/multipart"
  23. "net"
  24. "net/http"
  25. "net/http/httptest"
  26. "os"
  27. "os/exec"
  28. "path"
  29. "path/filepath"
  30. "regexp"
  31. "sort"
  32. "strconv"
  33. "strings"
  34. "time"
  35. "github.com/elazarl/goproxy"
  36. "github.com/ghodss/yaml"
  37. "k8s.io/kubernetes/pkg/api"
  38. "k8s.io/kubernetes/pkg/api/annotations"
  39. apierrs "k8s.io/kubernetes/pkg/api/errors"
  40. "k8s.io/kubernetes/pkg/api/resource"
  41. "k8s.io/kubernetes/pkg/api/unversioned"
  42. client "k8s.io/kubernetes/pkg/client/unversioned"
  43. "k8s.io/kubernetes/pkg/controller"
  44. "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  45. "k8s.io/kubernetes/pkg/labels"
  46. "k8s.io/kubernetes/pkg/registry/generic/registry"
  47. utilnet "k8s.io/kubernetes/pkg/util/net"
  48. "k8s.io/kubernetes/pkg/util/uuid"
  49. "k8s.io/kubernetes/pkg/util/wait"
  50. "k8s.io/kubernetes/pkg/version"
  51. "k8s.io/kubernetes/test/e2e/framework"
  52. . "github.com/onsi/ginkgo"
  53. . "github.com/onsi/gomega"
  54. )
  55. const (
  56. nautilusImage = "gcr.io/google_containers/update-demo:nautilus"
  57. kittenImage = "gcr.io/google_containers/update-demo:kitten"
  58. updateDemoSelector = "name=update-demo"
  59. updateDemoContainer = "update-demo"
  60. frontendSelector = "app=guestbook,tier=frontend"
  61. redisMasterSelector = "app=redis,role=master"
  62. redisSlaveSelector = "app=redis,role=slave"
  63. goproxyContainer = "goproxy"
  64. goproxyPodSelector = "name=goproxy"
  65. netexecContainer = "netexec"
  66. netexecPodSelector = "name=netexec"
  67. kubectlProxyPort = 8011
  68. guestbookStartupTimeout = 10 * time.Minute
  69. guestbookResponseTimeout = 3 * time.Minute
  70. simplePodSelector = "name=nginx"
  71. simplePodName = "nginx"
  72. nginxDefaultOutput = "Welcome to nginx!"
  73. simplePodPort = 80
  74. pausePodSelector = "name=pause"
  75. pausePodName = "pause"
  76. runJobTimeout = 5 * time.Minute
  77. busyboxImage = "gcr.io/google_containers/busybox:1.24"
  78. nginxImage = "gcr.io/google_containers/nginx-slim:0.7"
  79. kubeCtlManifestPath = "test/e2e/testing-manifests/kubectl"
  80. redisControllerFilename = "redis-master-controller.json"
  81. redisServiceFilename = "redis-master-service.json"
  82. )
  83. var (
  84. proxyRegexp = regexp.MustCompile("Starting to serve on 127.0.0.1:([0-9]+)")
  85. // Extended pod logging options were introduced in #13780 (v1.1.0) so we don't expect tests
  86. // that rely on extended pod logging options to work on clusters before that.
  87. //
  88. // TODO(ihmccreery): remove once we don't care about v1.0 anymore, (tentatively in v1.3).
  89. extendedPodLogFilterVersion = version.MustParse("v1.1.0")
  90. // NodePorts were made optional in #12831 (v1.1.0) so we don't expect tests that used to
  91. // require NodePorts but no longer include them to work on clusters before that.
  92. //
  93. // TODO(ihmccreery): remove once we don't care about v1.0 anymore, (tentatively in v1.3).
  94. nodePortsOptionalVersion = version.MustParse("v1.1.0")
  95. // Jobs were introduced in v1.1, so we don't expect tests that rely on jobs to work on
  96. // clusters before that.
  97. //
  98. // TODO(ihmccreery): remove once we don't care about v1.0 anymore, (tentatively in v1.3).
  99. jobsVersion = version.MustParse("v1.1.0")
  100. // Deployments were introduced by default in v1.2, so we don't expect tests that rely on
  101. // deployments to work on clusters before that.
  102. //
  103. // TODO(ihmccreery): remove once we don't care about v1.1 anymore, (tentatively in v1.4).
  104. deploymentsVersion = version.MustParse("v1.2.0-alpha.7.726")
  105. // Pod probe parameters were introduced in #15967 (v1.2) so we dont expect tests that use
  106. // these probe parameters to work on clusters before that.
  107. //
  108. // TODO(ihmccreery): remove once we don't care about v1.1 anymore, (tentatively in v1.4).
  109. podProbeParametersVersion = version.MustParse("v1.2.0-alpha.4")
  110. )
  111. // Stops everything from filePath from namespace ns and checks if everything matching selectors from the given namespace is correctly stopped.
  112. // Aware of the kubectl example files map.
  113. func cleanupKubectlInputs(fileContents string, ns string, selectors ...string) {
  114. By("using delete to clean up resources")
  115. var nsArg string
  116. if ns != "" {
  117. nsArg = fmt.Sprintf("--namespace=%s", ns)
  118. }
  119. // support backward compatibility : file paths or raw json - since we are removing file path
  120. // dependencies from this test.
  121. framework.RunKubectlOrDieInput(fileContents, "delete", "--grace-period=0", "-f", "-", nsArg)
  122. framework.AssertCleanup(ns, selectors...)
  123. }
  124. func readTestFileOrDie(file string) []byte {
  125. return framework.ReadOrDie(path.Join(kubeCtlManifestPath, file))
  126. }
  127. func runKubectlRetryOrDie(args ...string) string {
  128. var err error
  129. var output string
  130. for i := 0; i < 3; i++ {
  131. output, err = framework.RunKubectl(args...)
  132. if err == nil || !strings.Contains(err.Error(), registry.OptimisticLockErrorMsg) {
  133. break
  134. }
  135. time.Sleep(time.Second)
  136. }
  137. // Expect no errors to be present after retries are finished
  138. // Copied from framework #ExecOrDie
  139. framework.Logf("stdout: %q", output)
  140. Expect(err).NotTo(HaveOccurred())
  141. return output
  142. }
  143. var _ = framework.KubeDescribe("Kubectl client", func() {
  144. defer GinkgoRecover()
  145. f := framework.NewDefaultFramework("kubectl")
  146. // Reustable cluster state function. This won't be adversly affected by lazy initialization of framework.
  147. clusterState := func() *framework.ClusterVerification {
  148. return f.NewClusterVerification(
  149. framework.PodStateVerification{
  150. Selectors: map[string]string{"app": "redis"},
  151. ValidPhases: []api.PodPhase{api.PodRunning /*api.PodPending*/},
  152. })
  153. }
  154. forEachPod := func(podFunc func(p api.Pod)) {
  155. clusterState().ForEach(podFunc)
  156. }
  157. var c *client.Client
  158. var ns string
  159. BeforeEach(func() {
  160. c = f.Client
  161. ns = f.Namespace.Name
  162. })
  163. // Customized Wait / ForEach wrapper for this test. These demonstrate the
  164. // idiomatic way to wrap the ClusterVerification structs for syntactic sugar in large
  165. // test files.
  166. // Print debug info if atLeast Pods are not found before the timeout
  167. waitForOrFailWithDebug := func(atLeast int) {
  168. pods, err := clusterState().WaitFor(atLeast, framework.PodStartTimeout)
  169. if err != nil || len(pods) < atLeast {
  170. // TODO: Generalize integrating debug info into these tests so we always get debug info when we need it
  171. framework.DumpAllNamespaceInfo(c, ns)
  172. framework.Failf("Verified %v of %v pods , error : %v", len(pods), atLeast, err)
  173. }
  174. }
  175. framework.KubeDescribe("Update Demo", func() {
  176. var nautilus, kitten []byte
  177. BeforeEach(func() {
  178. updateDemoRoot := "docs/user-guide/update-demo"
  179. nautilus = framework.ReadOrDie(filepath.Join(updateDemoRoot, "nautilus-rc.yaml"))
  180. kitten = framework.ReadOrDie(filepath.Join(updateDemoRoot, "kitten-rc.yaml"))
  181. })
  182. It("should create and stop a replication controller [Conformance]", func() {
  183. defer cleanupKubectlInputs(string(nautilus), ns, updateDemoSelector)
  184. By("creating a replication controller")
  185. framework.RunKubectlOrDieInput(string(nautilus[:]), "create", "-f", "-", fmt.Sprintf("--namespace=%v", ns))
  186. framework.ValidateController(c, nautilusImage, 2, "update-demo", updateDemoSelector, getUDData("nautilus.jpg", ns), ns)
  187. })
  188. It("should scale a replication controller [Conformance]", func() {
  189. defer cleanupKubectlInputs(string(nautilus[:]), ns, updateDemoSelector)
  190. By("creating a replication controller")
  191. framework.RunKubectlOrDieInput(string(nautilus[:]), "create", "-f", "-", fmt.Sprintf("--namespace=%v", ns))
  192. framework.ValidateController(c, nautilusImage, 2, "update-demo", updateDemoSelector, getUDData("nautilus.jpg", ns), ns)
  193. By("scaling down the replication controller")
  194. framework.RunKubectlOrDie("scale", "rc", "update-demo-nautilus", "--replicas=1", "--timeout=5m", fmt.Sprintf("--namespace=%v", ns))
  195. framework.ValidateController(c, nautilusImage, 1, "update-demo", updateDemoSelector, getUDData("nautilus.jpg", ns), ns)
  196. By("scaling up the replication controller")
  197. framework.RunKubectlOrDie("scale", "rc", "update-demo-nautilus", "--replicas=2", "--timeout=5m", fmt.Sprintf("--namespace=%v", ns))
  198. framework.ValidateController(c, nautilusImage, 2, "update-demo", updateDemoSelector, getUDData("nautilus.jpg", ns), ns)
  199. })
  200. It("should do a rolling update of a replication controller [Conformance]", func() {
  201. By("creating the initial replication controller")
  202. framework.RunKubectlOrDieInput(string(nautilus[:]), "create", "-f", "-", fmt.Sprintf("--namespace=%v", ns))
  203. framework.ValidateController(c, nautilusImage, 2, "update-demo", updateDemoSelector, getUDData("nautilus.jpg", ns), ns)
  204. By("rolling-update to new replication controller")
  205. framework.RunKubectlOrDieInput(string(kitten[:]), "rolling-update", "update-demo-nautilus", "--update-period=1s", "-f", "-", fmt.Sprintf("--namespace=%v", ns))
  206. framework.ValidateController(c, kittenImage, 2, "update-demo", updateDemoSelector, getUDData("kitten.jpg", ns), ns)
  207. // Everything will hopefully be cleaned up when the namespace is deleted.
  208. })
  209. })
  210. framework.KubeDescribe("Guestbook application", func() {
  211. forEachGBFile := func(run func(s string)) {
  212. for _, gbAppFile := range []string{
  213. "examples/guestbook/frontend-deployment.yaml",
  214. "examples/guestbook/frontend-service.yaml",
  215. "examples/guestbook/redis-master-deployment.yaml",
  216. "examples/guestbook/redis-master-service.yaml",
  217. "examples/guestbook/redis-slave-deployment.yaml",
  218. "examples/guestbook/redis-slave-service.yaml",
  219. } {
  220. contents := framework.ReadOrDie(gbAppFile)
  221. run(string(contents))
  222. }
  223. }
  224. It("should create and stop a working application [Conformance]", func() {
  225. framework.SkipUnlessServerVersionGTE(deploymentsVersion, c)
  226. defer forEachGBFile(func(contents string) {
  227. cleanupKubectlInputs(contents, ns)
  228. })
  229. By("creating all guestbook components")
  230. forEachGBFile(func(contents string) {
  231. framework.Logf(contents)
  232. framework.RunKubectlOrDieInput(contents, "create", "-f", "-", fmt.Sprintf("--namespace=%v", ns))
  233. })
  234. By("validating guestbook app")
  235. validateGuestbookApp(c, ns)
  236. })
  237. })
  238. framework.KubeDescribe("Simple pod", func() {
  239. var podPath []byte
  240. BeforeEach(func() {
  241. podPath = framework.ReadOrDie(path.Join(kubeCtlManifestPath, "pod-with-readiness-probe.yaml"))
  242. By(fmt.Sprintf("creating the pod from %v", string(podPath)))
  243. framework.RunKubectlOrDieInput(string(podPath[:]), "create", "-f", "-", fmt.Sprintf("--namespace=%v", ns))
  244. Expect(framework.CheckPodsRunningReady(c, ns, []string{simplePodName}, framework.PodStartTimeout)).To(BeTrue())
  245. })
  246. AfterEach(func() {
  247. cleanupKubectlInputs(string(podPath[:]), ns, simplePodSelector)
  248. })
  249. It("should support exec", func() {
  250. By("executing a command in the container")
  251. execOutput := framework.RunKubectlOrDie("exec", fmt.Sprintf("--namespace=%v", ns), simplePodName, "echo", "running", "in", "container")
  252. if e, a := "running in container", strings.TrimSpace(execOutput); e != a {
  253. framework.Failf("Unexpected kubectl exec output. Wanted %q, got %q", e, a)
  254. }
  255. By("executing a command in the container with noninteractive stdin")
  256. execOutput = framework.NewKubectlCommand("exec", fmt.Sprintf("--namespace=%v", ns), "-i", simplePodName, "cat").
  257. WithStdinData("abcd1234").
  258. ExecOrDie()
  259. if e, a := "abcd1234", execOutput; e != a {
  260. framework.Failf("Unexpected kubectl exec output. Wanted %q, got %q", e, a)
  261. }
  262. // pretend that we're a user in an interactive shell
  263. r, closer, err := newBlockingReader("echo hi\nexit\n")
  264. if err != nil {
  265. framework.Failf("Error creating blocking reader: %v", err)
  266. }
  267. // NOTE this is solely for test cleanup!
  268. defer closer.Close()
  269. By("executing a command in the container with pseudo-interactive stdin")
  270. execOutput = framework.NewKubectlCommand("exec", fmt.Sprintf("--namespace=%v", ns), "-i", simplePodName, "bash").
  271. WithStdinReader(r).
  272. ExecOrDie()
  273. if e, a := "hi", strings.TrimSpace(execOutput); e != a {
  274. framework.Failf("Unexpected kubectl exec output. Wanted %q, got %q", e, a)
  275. }
  276. })
  277. It("should support exec through an HTTP proxy", func() {
  278. // Note: We are skipping local since we want to verify an apiserver with HTTPS.
  279. // At this time local only supports plain HTTP.
  280. framework.SkipIfProviderIs("local")
  281. // Fail if the variable isn't set
  282. if framework.TestContext.Host == "" {
  283. framework.Failf("--host variable must be set to the full URI to the api server on e2e run.")
  284. }
  285. By("Starting goproxy")
  286. testSrv, proxyLogs := startLocalProxy()
  287. defer testSrv.Close()
  288. proxyAddr := testSrv.URL
  289. for _, proxyVar := range []string{"https_proxy", "HTTPS_PROXY"} {
  290. proxyLogs.Reset()
  291. By("Running kubectl via an HTTP proxy using " + proxyVar)
  292. output := framework.NewKubectlCommand(fmt.Sprintf("--namespace=%s", ns), "exec", "nginx", "echo", "running", "in", "container").
  293. WithEnv(append(os.Environ(), fmt.Sprintf("%s=%s", proxyVar, proxyAddr))).
  294. ExecOrDie()
  295. // Verify we got the normal output captured by the exec server
  296. expectedExecOutput := "running in container\n"
  297. if output != expectedExecOutput {
  298. framework.Failf("Unexpected kubectl exec output. Wanted %q, got %q", expectedExecOutput, output)
  299. }
  300. // Verify the proxy server logs saw the connection
  301. expectedProxyLog := fmt.Sprintf("Accepting CONNECT to %s", strings.TrimRight(strings.TrimLeft(framework.TestContext.Host, "https://"), "/api"))
  302. proxyLog := proxyLogs.String()
  303. if !strings.Contains(proxyLog, expectedProxyLog) {
  304. framework.Failf("Missing expected log result on proxy server for %s. Expected: %q, got %q", proxyVar, expectedProxyLog, proxyLog)
  305. }
  306. }
  307. })
  308. It("should support inline execution and attach", func() {
  309. framework.SkipIfContainerRuntimeIs("rkt") // #23335
  310. framework.SkipUnlessServerVersionGTE(jobsVersion, c)
  311. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  312. By("executing a command with run and attach with stdin")
  313. runOutput := framework.NewKubectlCommand(nsFlag, "run", "run-test", "--image="+busyboxImage, "--restart=OnFailure", "--attach=true", "--stdin", "--", "sh", "-c", "cat && echo 'stdin closed'").
  314. WithStdinData("abcd1234").
  315. ExecOrDie()
  316. Expect(runOutput).To(ContainSubstring("abcd1234"))
  317. Expect(runOutput).To(ContainSubstring("stdin closed"))
  318. Expect(c.Extensions().Jobs(ns).Delete("run-test", nil)).To(BeNil())
  319. By("executing a command with run and attach without stdin")
  320. 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'").
  321. WithStdinData("abcd1234").
  322. ExecOrDie()
  323. Expect(runOutput).ToNot(ContainSubstring("abcd1234"))
  324. Expect(runOutput).To(ContainSubstring("stdin closed"))
  325. Expect(c.Extensions().Jobs(ns).Delete("run-test-2", nil)).To(BeNil())
  326. By("executing a command with run and attach with stdin with open stdin should remain running")
  327. 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'").
  328. WithStdinData("abcd1234\n").
  329. ExecOrDie()
  330. Expect(runOutput).ToNot(ContainSubstring("stdin closed"))
  331. f := func(pods []*api.Pod) sort.Interface { return sort.Reverse(controller.ActivePods(pods)) }
  332. runTestPod, _, err := util.GetFirstPod(c, ns, labels.SelectorFromSet(map[string]string{"run": "run-test-3"}), 1*time.Minute, f)
  333. if err != nil {
  334. os.Exit(1)
  335. }
  336. if !framework.CheckPodsRunningReady(c, ns, []string{runTestPod.Name}, time.Minute) {
  337. framework.Failf("Pod %q of Job %q should still be running", runTestPod.Name, "run-test-3")
  338. }
  339. // NOTE: we cannot guarantee our output showed up in the container logs before stdin was closed, so we have
  340. // to loop test.
  341. err = wait.PollImmediate(time.Second, time.Minute, func() (bool, error) {
  342. if !framework.CheckPodsRunningReady(c, ns, []string{runTestPod.Name}, 1*time.Second) {
  343. framework.Failf("Pod %q of Job %q should still be running", runTestPod.Name, "run-test-3")
  344. }
  345. logOutput := framework.RunKubectlOrDie(nsFlag, "logs", runTestPod.Name)
  346. Expect(logOutput).ToNot(ContainSubstring("stdin closed"))
  347. return strings.Contains(logOutput, "abcd1234"), nil
  348. })
  349. if err != nil {
  350. os.Exit(1)
  351. }
  352. Expect(err).To(BeNil())
  353. Expect(c.Extensions().Jobs(ns).Delete("run-test-3", nil)).To(BeNil())
  354. })
  355. It("should support port-forward", func() {
  356. By("forwarding the container port to a local port")
  357. cmd := runPortForward(ns, simplePodName, simplePodPort)
  358. defer cmd.Stop()
  359. By("curling local port output")
  360. localAddr := fmt.Sprintf("http://localhost:%d", cmd.port)
  361. body, err := curl(localAddr)
  362. framework.Logf("got: %s", body)
  363. if err != nil {
  364. framework.Failf("Failed http.Get of forwarded port (%s): %v", localAddr, err)
  365. }
  366. if !strings.Contains(body, nginxDefaultOutput) {
  367. framework.Failf("Container port output missing expected value. Wanted:'%s', got: %s", nginxDefaultOutput, body)
  368. }
  369. })
  370. })
  371. framework.KubeDescribe("Kubectl api-versions", func() {
  372. It("should check if v1 is in available api versions [Conformance]", func() {
  373. By("validating api verions")
  374. output := framework.RunKubectlOrDie("api-versions")
  375. if !strings.Contains(output, "v1") {
  376. framework.Failf("No v1 in kubectl api-versions")
  377. }
  378. })
  379. })
  380. framework.KubeDescribe("Kubectl apply", func() {
  381. It("should apply a new configuration to an existing RC", func() {
  382. controllerJson := readTestFileOrDie(redisControllerFilename)
  383. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  384. By("creating Redis RC")
  385. framework.RunKubectlOrDieInput(string(controllerJson), "create", "-f", "-", nsFlag)
  386. By("applying a modified configuration")
  387. stdin := modifyReplicationControllerConfiguration(string(controllerJson))
  388. framework.NewKubectlCommand("apply", "-f", "-", nsFlag).
  389. WithStdinReader(stdin).
  390. ExecOrDie()
  391. By("checking the result")
  392. forEachReplicationController(c, ns, "app", "redis", validateReplicationControllerConfiguration)
  393. })
  394. It("should reuse nodePort when apply to an existing SVC", func() {
  395. serviceJson := readTestFileOrDie(redisServiceFilename)
  396. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  397. By("creating Redis SVC")
  398. framework.RunKubectlOrDieInput(string(serviceJson[:]), "create", "-f", "-", nsFlag)
  399. By("getting the original nodePort")
  400. originalNodePort := framework.RunKubectlOrDie("get", "service", "redis-master", nsFlag, "-o", "jsonpath={.spec.ports[0].nodePort}")
  401. By("applying the same configuration")
  402. framework.RunKubectlOrDieInput(string(serviceJson[:]), "apply", "-f", "-", nsFlag)
  403. By("getting the nodePort after applying configuration")
  404. currentNodePort := framework.RunKubectlOrDie("get", "service", "redis-master", nsFlag, "-o", "jsonpath={.spec.ports[0].nodePort}")
  405. By("checking the result")
  406. if originalNodePort != currentNodePort {
  407. framework.Failf("nodePort should keep the same")
  408. }
  409. })
  410. })
  411. framework.KubeDescribe("Kubectl cluster-info", func() {
  412. It("should check if Kubernetes master services is included in cluster-info [Conformance]", func() {
  413. By("validating cluster-info")
  414. output := framework.RunKubectlOrDie("cluster-info")
  415. // Can't check exact strings due to terminal control commands (colors)
  416. requiredItems := []string{"Kubernetes master", "is running at"}
  417. if framework.ProviderIs("gce", "gke") {
  418. requiredItems = append(requiredItems, "KubeDNS", "Heapster")
  419. }
  420. for _, item := range requiredItems {
  421. if !strings.Contains(output, item) {
  422. framework.Failf("Missing %s in kubectl cluster-info", item)
  423. }
  424. }
  425. })
  426. })
  427. framework.KubeDescribe("Kubectl describe", func() {
  428. It("should check if kubectl describe prints relevant information for rc and pods [Conformance]", func() {
  429. framework.SkipUnlessServerVersionGTE(nodePortsOptionalVersion, c)
  430. controllerJson := readTestFileOrDie(redisControllerFilename)
  431. serviceJson := readTestFileOrDie(redisServiceFilename)
  432. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  433. framework.RunKubectlOrDieInput(string(controllerJson[:]), "create", "-f", "-", nsFlag)
  434. framework.RunKubectlOrDieInput(string(serviceJson[:]), "create", "-f", "-", nsFlag)
  435. By("Waiting for Redis master to start.")
  436. waitForOrFailWithDebug(1)
  437. // Pod
  438. forEachPod(func(pod api.Pod) {
  439. output := framework.RunKubectlOrDie("describe", "pod", pod.Name, nsFlag)
  440. requiredStrings := [][]string{
  441. {"Name:", "redis-master-"},
  442. {"Namespace:", ns},
  443. {"Node:"},
  444. {"Labels:", "app=redis"},
  445. {"role=master"},
  446. {"Status:", "Running"},
  447. {"IP:"},
  448. {"Controllers:", "ReplicationController/redis-master"},
  449. {"Image:", redisImage},
  450. {"State:", "Running"},
  451. {"QoS Class:", "BestEffort"},
  452. }
  453. checkOutput(output, requiredStrings)
  454. })
  455. // Rc
  456. output := framework.RunKubectlOrDie("describe", "rc", "redis-master", nsFlag)
  457. requiredStrings := [][]string{
  458. {"Name:", "redis-master"},
  459. {"Namespace:", ns},
  460. {"Image(s):", redisImage},
  461. {"Selector:", "app=redis,role=master"},
  462. {"Labels:", "app=redis"},
  463. {"role=master"},
  464. {"Replicas:", "1 current", "1 desired"},
  465. {"Pods Status:", "1 Running", "0 Waiting", "0 Succeeded", "0 Failed"},
  466. // {"Events:"} would ordinarily go in the list
  467. // here, but in some rare circumstances the
  468. // events are delayed, and instead kubectl
  469. // prints "No events." This string will match
  470. // either way.
  471. {"vents"}}
  472. checkOutput(output, requiredStrings)
  473. // Service
  474. output = framework.RunKubectlOrDie("describe", "service", "redis-master", nsFlag)
  475. requiredStrings = [][]string{
  476. {"Name:", "redis-master"},
  477. {"Namespace:", ns},
  478. {"Labels:", "app=redis"},
  479. {"role=master"},
  480. {"Selector:", "app=redis", "role=master"},
  481. {"Type:", "ClusterIP"},
  482. {"IP:"},
  483. {"Port:", "<unset>", "6379/TCP"},
  484. {"Endpoints:"},
  485. {"Session Affinity:", "None"}}
  486. checkOutput(output, requiredStrings)
  487. // Node
  488. // It should be OK to list unschedulable Nodes here.
  489. nodes, err := c.Nodes().List(api.ListOptions{})
  490. Expect(err).NotTo(HaveOccurred())
  491. node := nodes.Items[0]
  492. output = framework.RunKubectlOrDie("describe", "node", node.Name)
  493. requiredStrings = [][]string{
  494. {"Name:", node.Name},
  495. {"Labels:"},
  496. {"CreationTimestamp:"},
  497. {"Conditions:"},
  498. {"Type", "Status", "LastHeartbeatTime", "LastTransitionTime", "Reason", "Message"},
  499. {"Addresses:"},
  500. {"Capacity:"},
  501. {"Version:"},
  502. {"Kernel Version:"},
  503. {"OS Image:"},
  504. {"Container Runtime Version:"},
  505. {"Kubelet Version:"},
  506. {"Kube-Proxy Version:"},
  507. {"Pods:"}}
  508. checkOutput(output, requiredStrings)
  509. // Namespace
  510. output = framework.RunKubectlOrDie("describe", "namespace", ns)
  511. requiredStrings = [][]string{
  512. {"Name:", ns},
  513. {"Labels:"},
  514. {"Status:", "Active"}}
  515. checkOutput(output, requiredStrings)
  516. // Quota and limitrange are skipped for now.
  517. })
  518. })
  519. framework.KubeDescribe("Kubectl expose", func() {
  520. It("should create services for rc [Conformance]", func() {
  521. controllerJson := readTestFileOrDie(redisControllerFilename)
  522. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  523. redisPort := 6379
  524. By("creating Redis RC")
  525. framework.Logf("namespace %v", ns)
  526. framework.RunKubectlOrDieInput(string(controllerJson[:]), "create", "-f", "-", nsFlag)
  527. // It may take a while for the pods to get registered in some cases, wait to be sure.
  528. By("Waiting for Redis master to start.")
  529. waitForOrFailWithDebug(1)
  530. forEachPod(func(pod api.Pod) {
  531. framework.Logf("wait on redis-master startup in %v ", ns)
  532. framework.LookForStringInLog(ns, pod.Name, "redis-master", "The server is now ready to accept connections", framework.PodStartTimeout)
  533. })
  534. validateService := func(name string, servicePort int, timeout time.Duration) {
  535. err := wait.Poll(framework.Poll, timeout, func() (bool, error) {
  536. endpoints, err := c.Endpoints(ns).Get(name)
  537. if err != nil {
  538. if apierrs.IsNotFound(err) {
  539. err = nil
  540. }
  541. framework.Logf("Get endpoints failed (interval %v): %v", framework.Poll, err)
  542. return false, err
  543. }
  544. uidToPort := getContainerPortsByPodUID(endpoints)
  545. if len(uidToPort) == 0 {
  546. framework.Logf("No endpoint found, retrying")
  547. return false, nil
  548. }
  549. if len(uidToPort) > 1 {
  550. Fail("Too many endpoints found")
  551. }
  552. for _, port := range uidToPort {
  553. if port[0] != redisPort {
  554. framework.Failf("Wrong endpoint port: %d", port[0])
  555. }
  556. }
  557. return true, nil
  558. })
  559. Expect(err).NotTo(HaveOccurred())
  560. service, err := c.Services(ns).Get(name)
  561. Expect(err).NotTo(HaveOccurred())
  562. if len(service.Spec.Ports) != 1 {
  563. framework.Failf("1 port is expected")
  564. }
  565. port := service.Spec.Ports[0]
  566. if port.Port != int32(servicePort) {
  567. framework.Failf("Wrong service port: %d", port.Port)
  568. }
  569. if port.TargetPort.IntValue() != redisPort {
  570. framework.Failf("Wrong target port: %d")
  571. }
  572. }
  573. By("exposing RC")
  574. framework.RunKubectlOrDie("expose", "rc", "redis-master", "--name=rm2", "--port=1234", fmt.Sprintf("--target-port=%d", redisPort), nsFlag)
  575. framework.WaitForService(c, ns, "rm2", true, framework.Poll, framework.ServiceStartTimeout)
  576. validateService("rm2", 1234, framework.ServiceStartTimeout)
  577. By("exposing service")
  578. framework.RunKubectlOrDie("expose", "service", "rm2", "--name=rm3", "--port=2345", fmt.Sprintf("--target-port=%d", redisPort), nsFlag)
  579. framework.WaitForService(c, ns, "rm3", true, framework.Poll, framework.ServiceStartTimeout)
  580. validateService("rm3", 2345, framework.ServiceStartTimeout)
  581. })
  582. })
  583. framework.KubeDescribe("Kubectl label", func() {
  584. var pod []byte
  585. var nsFlag string
  586. BeforeEach(func() {
  587. pod = readTestFileOrDie("pause-pod.yaml")
  588. By("creating the pod")
  589. nsFlag = fmt.Sprintf("--namespace=%v", ns)
  590. framework.RunKubectlOrDieInput(string(pod), "create", "-f", "-", nsFlag)
  591. Expect(framework.CheckPodsRunningReady(c, ns, []string{pausePodName}, framework.PodStartTimeout)).To(BeTrue())
  592. })
  593. AfterEach(func() {
  594. cleanupKubectlInputs(string(pod[:]), ns, pausePodSelector)
  595. })
  596. It("should update the label on a resource [Conformance]", func() {
  597. labelName := "testing-label"
  598. labelValue := "testing-label-value"
  599. By("adding the label " + labelName + " with value " + labelValue + " to a pod")
  600. framework.RunKubectlOrDie("label", "pods", pausePodName, labelName+"="+labelValue, nsFlag)
  601. By("verifying the pod has the label " + labelName + " with the value " + labelValue)
  602. output := framework.RunKubectlOrDie("get", "pod", pausePodName, "-L", labelName, nsFlag)
  603. if !strings.Contains(output, labelValue) {
  604. framework.Failf("Failed updating label " + labelName + " to the pod " + pausePodName)
  605. }
  606. By("removing the label " + labelName + " of a pod")
  607. framework.RunKubectlOrDie("label", "pods", pausePodName, labelName+"-", nsFlag)
  608. By("verifying the pod doesn't have the label " + labelName)
  609. output = framework.RunKubectlOrDie("get", "pod", pausePodName, "-L", labelName, nsFlag)
  610. if strings.Contains(output, labelValue) {
  611. framework.Failf("Failed removing label " + labelName + " of the pod " + pausePodName)
  612. }
  613. })
  614. })
  615. framework.KubeDescribe("Kubectl logs", func() {
  616. var rc []byte
  617. var nsFlag string
  618. containerName := "redis-master"
  619. BeforeEach(func() {
  620. rc = readTestFileOrDie(redisControllerFilename)
  621. By("creating an rc")
  622. nsFlag = fmt.Sprintf("--namespace=%v", ns)
  623. framework.RunKubectlOrDieInput(string(rc[:]), "create", "-f", "-", nsFlag)
  624. })
  625. AfterEach(func() {
  626. cleanupKubectlInputs(string(rc[:]), ns, simplePodSelector)
  627. })
  628. It("should be able to retrieve and filter logs [Conformance]", func() {
  629. framework.SkipUnlessServerVersionGTE(extendedPodLogFilterVersion, c)
  630. // Split("something\n", "\n") returns ["something", ""], so
  631. // strip trailing newline first
  632. lines := func(out string) []string {
  633. return strings.Split(strings.TrimRight(out, "\n"), "\n")
  634. }
  635. By("Waiting for Redis master to start.")
  636. waitForOrFailWithDebug(1)
  637. forEachPod(func(pod api.Pod) {
  638. By("checking for a matching strings")
  639. _, err := framework.LookForStringInLog(ns, pod.Name, containerName, "The server is now ready to accept connections", framework.PodStartTimeout)
  640. Expect(err).NotTo(HaveOccurred())
  641. By("limiting log lines")
  642. out := framework.RunKubectlOrDie("log", pod.Name, containerName, nsFlag, "--tail=1")
  643. Expect(len(out)).NotTo(BeZero())
  644. Expect(len(lines(out))).To(Equal(1))
  645. By("limiting log bytes")
  646. out = framework.RunKubectlOrDie("log", pod.Name, containerName, nsFlag, "--limit-bytes=1")
  647. Expect(len(lines(out))).To(Equal(1))
  648. Expect(len(out)).To(Equal(1))
  649. By("exposing timestamps")
  650. out = framework.RunKubectlOrDie("log", pod.Name, containerName, nsFlag, "--tail=1", "--timestamps")
  651. l := lines(out)
  652. Expect(len(l)).To(Equal(1))
  653. words := strings.Split(l[0], " ")
  654. Expect(len(words)).To(BeNumerically(">", 1))
  655. if _, err := time.Parse(time.RFC3339Nano, words[0]); err != nil {
  656. if _, err := time.Parse(time.RFC3339, words[0]); err != nil {
  657. framework.Failf("expected %q to be RFC3339 or RFC3339Nano", words[0])
  658. }
  659. }
  660. By("restricting to a time range")
  661. // Note: we must wait at least two seconds,
  662. // because the granularity is only 1 second and
  663. // it could end up rounding the wrong way.
  664. time.Sleep(2500 * time.Millisecond) // ensure that startup logs on the node are seen as older than 1s
  665. recent_out := framework.RunKubectlOrDie("log", pod.Name, containerName, nsFlag, "--since=1s")
  666. recent := len(strings.Split(recent_out, "\n"))
  667. older_out := framework.RunKubectlOrDie("log", pod.Name, containerName, nsFlag, "--since=24h")
  668. older := len(strings.Split(older_out, "\n"))
  669. 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)
  670. })
  671. })
  672. })
  673. framework.KubeDescribe("Kubectl patch", func() {
  674. It("should add annotations for pods in rc [Conformance]", func() {
  675. controllerJson := readTestFileOrDie(redisControllerFilename)
  676. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  677. By("creating Redis RC")
  678. framework.RunKubectlOrDieInput(string(controllerJson[:]), "create", "-f", "-", nsFlag)
  679. By("Waiting for Redis master to start.")
  680. waitForOrFailWithDebug(1)
  681. By("patching all pods")
  682. forEachPod(func(pod api.Pod) {
  683. framework.RunKubectlOrDie("patch", "pod", pod.Name, nsFlag, "-p", "{\"metadata\":{\"annotations\":{\"x\":\"y\"}}}")
  684. })
  685. By("checking annotations")
  686. forEachPod(func(pod api.Pod) {
  687. found := false
  688. for key, val := range pod.Annotations {
  689. if key == "x" && val == "y" {
  690. found = true
  691. }
  692. }
  693. if !found {
  694. framework.Failf("Added annotation not found")
  695. }
  696. })
  697. })
  698. })
  699. framework.KubeDescribe("Kubectl version", func() {
  700. It("should check is all data is printed [Conformance]", func() {
  701. version := framework.RunKubectlOrDie("version")
  702. requiredItems := []string{"Client Version:", "Server Version:", "Major:", "Minor:", "GitCommit:"}
  703. for _, item := range requiredItems {
  704. if !strings.Contains(version, item) {
  705. framework.Failf("Required item %s not found in %s", item, version)
  706. }
  707. }
  708. })
  709. })
  710. framework.KubeDescribe("Kubectl run default", func() {
  711. var nsFlag string
  712. var name string
  713. var cleanUp func()
  714. BeforeEach(func() {
  715. nsFlag = fmt.Sprintf("--namespace=%v", ns)
  716. gte, err := framework.ServerVersionGTE(deploymentsVersion, c)
  717. if err != nil {
  718. framework.Failf("Failed to get server version: %v", err)
  719. }
  720. if gte {
  721. name = "e2e-test-nginx-deployment"
  722. cleanUp = func() { framework.RunKubectlOrDie("delete", "deployment", name, nsFlag) }
  723. } else {
  724. name = "e2e-test-nginx-rc"
  725. cleanUp = func() { framework.RunKubectlOrDie("delete", "rc", name, nsFlag) }
  726. }
  727. })
  728. AfterEach(func() {
  729. cleanUp()
  730. })
  731. It("should create an rc or deployment from an image [Conformance]", func() {
  732. By("running the image " + nginxImage)
  733. framework.RunKubectlOrDie("run", name, "--image="+nginxImage, nsFlag)
  734. By("verifying the pod controlled by " + name + " gets created")
  735. label := labels.SelectorFromSet(labels.Set(map[string]string{"run": name}))
  736. podlist, err := framework.WaitForPodsWithLabel(c, ns, label)
  737. if err != nil {
  738. framework.Failf("Failed getting pod controlled by %s: %v", name, err)
  739. }
  740. pods := podlist.Items
  741. if pods == nil || len(pods) != 1 || len(pods[0].Spec.Containers) != 1 || pods[0].Spec.Containers[0].Image != nginxImage {
  742. framework.RunKubectlOrDie("get", "pods", "-L", "run", nsFlag)
  743. framework.Failf("Failed creating 1 pod with expected image %s. Number of pods = %v", nginxImage, len(pods))
  744. }
  745. })
  746. })
  747. framework.KubeDescribe("Kubectl run rc", func() {
  748. var nsFlag string
  749. var rcName string
  750. BeforeEach(func() {
  751. nsFlag = fmt.Sprintf("--namespace=%v", ns)
  752. rcName = "e2e-test-nginx-rc"
  753. })
  754. AfterEach(func() {
  755. framework.RunKubectlOrDie("delete", "rc", rcName, nsFlag)
  756. })
  757. It("should create an rc from an image [Conformance]", func() {
  758. By("running the image " + nginxImage)
  759. framework.RunKubectlOrDie("run", rcName, "--image="+nginxImage, "--generator=run/v1", nsFlag)
  760. By("verifying the rc " + rcName + " was created")
  761. rc, err := c.ReplicationControllers(ns).Get(rcName)
  762. if err != nil {
  763. framework.Failf("Failed getting rc %s: %v", rcName, err)
  764. }
  765. containers := rc.Spec.Template.Spec.Containers
  766. if containers == nil || len(containers) != 1 || containers[0].Image != nginxImage {
  767. framework.Failf("Failed creating rc %s for 1 pod with expected image %s", rcName, nginxImage)
  768. }
  769. By("verifying the pod controlled by rc " + rcName + " was created")
  770. label := labels.SelectorFromSet(labels.Set(map[string]string{"run": rcName}))
  771. podlist, err := framework.WaitForPodsWithLabel(c, ns, label)
  772. if err != nil {
  773. framework.Failf("Failed getting pod controlled by rc %s: %v", rcName, err)
  774. }
  775. pods := podlist.Items
  776. if pods == nil || len(pods) != 1 || len(pods[0].Spec.Containers) != 1 || pods[0].Spec.Containers[0].Image != nginxImage {
  777. framework.RunKubectlOrDie("get", "pods", "-L", "run", nsFlag)
  778. framework.Failf("Failed creating 1 pod with expected image %s. Number of pods = %v", nginxImage, len(pods))
  779. }
  780. By("confirm that you can get logs from an rc")
  781. podNames := []string{}
  782. for _, pod := range pods {
  783. podNames = append(podNames, pod.Name)
  784. }
  785. if !framework.CheckPodsRunningReady(c, ns, podNames, framework.PodStartTimeout) {
  786. framework.Failf("Pods for rc %s were not ready", rcName)
  787. }
  788. _, err = framework.RunKubectl("logs", "rc/"+rcName, nsFlag)
  789. // a non-nil error is fine as long as we actually found a pod.
  790. if err != nil && !strings.Contains(err.Error(), " in pod ") {
  791. framework.Failf("Failed getting logs by rc %s: %v", rcName, err)
  792. }
  793. })
  794. })
  795. framework.KubeDescribe("Kubectl rolling-update", func() {
  796. var nsFlag string
  797. var rcName string
  798. var c *client.Client
  799. BeforeEach(func() {
  800. c = f.Client
  801. nsFlag = fmt.Sprintf("--namespace=%v", ns)
  802. rcName = "e2e-test-nginx-rc"
  803. })
  804. AfterEach(func() {
  805. framework.RunKubectlOrDie("delete", "rc", rcName, nsFlag)
  806. })
  807. It("should support rolling-update to same image [Conformance]", func() {
  808. By("running the image " + nginxImage)
  809. framework.RunKubectlOrDie("run", rcName, "--image="+nginxImage, "--generator=run/v1", nsFlag)
  810. By("verifying the rc " + rcName + " was created")
  811. rc, err := c.ReplicationControllers(ns).Get(rcName)
  812. if err != nil {
  813. framework.Failf("Failed getting rc %s: %v", rcName, err)
  814. }
  815. containers := rc.Spec.Template.Spec.Containers
  816. if containers == nil || len(containers) != 1 || containers[0].Image != nginxImage {
  817. framework.Failf("Failed creating rc %s for 1 pod with expected image %s", rcName, nginxImage)
  818. }
  819. framework.WaitForRCToStabilize(c, ns, rcName, framework.PodStartTimeout)
  820. By("rolling-update to same image controller")
  821. runKubectlRetryOrDie("rolling-update", rcName, "--update-period=1s", "--image="+nginxImage, "--image-pull-policy="+string(api.PullIfNotPresent), nsFlag)
  822. framework.ValidateController(c, nginxImage, 1, rcName, "run="+rcName, noOpValidatorFn, ns)
  823. })
  824. })
  825. framework.KubeDescribe("Kubectl run deployment", func() {
  826. var nsFlag string
  827. var dName string
  828. BeforeEach(func() {
  829. nsFlag = fmt.Sprintf("--namespace=%v", ns)
  830. dName = "e2e-test-nginx-deployment"
  831. })
  832. AfterEach(func() {
  833. framework.RunKubectlOrDie("delete", "deployment", dName, nsFlag)
  834. })
  835. It("should create a deployment from an image [Conformance]", func() {
  836. framework.SkipUnlessServerVersionGTE(deploymentsVersion, c)
  837. By("running the image " + nginxImage)
  838. framework.RunKubectlOrDie("run", dName, "--image="+nginxImage, "--generator=deployment/v1beta1", nsFlag)
  839. By("verifying the deployment " + dName + " was created")
  840. d, err := c.Extensions().Deployments(ns).Get(dName)
  841. if err != nil {
  842. framework.Failf("Failed getting deployment %s: %v", dName, err)
  843. }
  844. containers := d.Spec.Template.Spec.Containers
  845. if containers == nil || len(containers) != 1 || containers[0].Image != nginxImage {
  846. framework.Failf("Failed creating deployment %s for 1 pod with expected image %s", dName, nginxImage)
  847. }
  848. By("verifying the pod controlled by deployment " + dName + " was created")
  849. label := labels.SelectorFromSet(labels.Set(map[string]string{"run": dName}))
  850. podlist, err := framework.WaitForPodsWithLabel(c, ns, label)
  851. if err != nil {
  852. framework.Failf("Failed getting pod controlled by deployment %s: %v", dName, err)
  853. }
  854. pods := podlist.Items
  855. if pods == nil || len(pods) != 1 || len(pods[0].Spec.Containers) != 1 || pods[0].Spec.Containers[0].Image != nginxImage {
  856. framework.RunKubectlOrDie("get", "pods", "-L", "run", nsFlag)
  857. framework.Failf("Failed creating 1 pod with expected image %s. Number of pods = %v", nginxImage, len(pods))
  858. }
  859. })
  860. })
  861. framework.KubeDescribe("Kubectl run job", func() {
  862. var nsFlag string
  863. var jobName string
  864. BeforeEach(func() {
  865. nsFlag = fmt.Sprintf("--namespace=%v", ns)
  866. jobName = "e2e-test-nginx-job"
  867. })
  868. AfterEach(func() {
  869. framework.RunKubectlOrDie("delete", "jobs", jobName, nsFlag)
  870. })
  871. It("should create a job from an image when restart is OnFailure [Conformance]", func() {
  872. framework.SkipUnlessServerVersionGTE(jobsVersion, c)
  873. By("running the image " + nginxImage)
  874. framework.RunKubectlOrDie("run", jobName, "--restart=OnFailure", "--generator=job/v1", "--image="+nginxImage, nsFlag)
  875. By("verifying the job " + jobName + " was created")
  876. job, err := c.Extensions().Jobs(ns).Get(jobName)
  877. if err != nil {
  878. framework.Failf("Failed getting job %s: %v", jobName, err)
  879. }
  880. containers := job.Spec.Template.Spec.Containers
  881. if containers == nil || len(containers) != 1 || containers[0].Image != nginxImage {
  882. framework.Failf("Failed creating job %s for 1 pod with expected image %s", jobName, nginxImage)
  883. }
  884. if job.Spec.Template.Spec.RestartPolicy != api.RestartPolicyOnFailure {
  885. framework.Failf("Failed creating a job with correct restart policy for --restart=OnFailure")
  886. }
  887. })
  888. })
  889. framework.KubeDescribe("Kubectl run pod", func() {
  890. var nsFlag string
  891. var podName string
  892. BeforeEach(func() {
  893. nsFlag = fmt.Sprintf("--namespace=%v", ns)
  894. podName = "e2e-test-nginx-pod"
  895. })
  896. AfterEach(func() {
  897. framework.RunKubectlOrDie("delete", "pods", podName, nsFlag)
  898. })
  899. It("should create a pod from an image when restart is Never [Conformance]", func() {
  900. framework.SkipUnlessServerVersionGTE(jobsVersion, c)
  901. By("running the image " + nginxImage)
  902. framework.RunKubectlOrDie("run", podName, "--restart=Never", "--generator=run-pod/v1", "--image="+nginxImage, nsFlag)
  903. By("verifying the pod " + podName + " was created")
  904. pod, err := c.Pods(ns).Get(podName)
  905. if err != nil {
  906. framework.Failf("Failed getting pod %s: %v", podName, err)
  907. }
  908. containers := pod.Spec.Containers
  909. if containers == nil || len(containers) != 1 || containers[0].Image != nginxImage {
  910. framework.Failf("Failed creating pod %s with expected image %s", podName, nginxImage)
  911. }
  912. if pod.Spec.RestartPolicy != api.RestartPolicyNever {
  913. framework.Failf("Failed creating a pod with correct restart policy for --restart=Never")
  914. }
  915. })
  916. })
  917. framework.KubeDescribe("Kubectl replace", func() {
  918. var nsFlag string
  919. var podName string
  920. BeforeEach(func() {
  921. nsFlag = fmt.Sprintf("--namespace=%v", ns)
  922. podName = "e2e-test-nginx-pod"
  923. })
  924. AfterEach(func() {
  925. framework.RunKubectlOrDie("delete", "pods", podName, nsFlag)
  926. })
  927. It("should update a single-container pod's image [Conformance]", func() {
  928. framework.SkipUnlessServerVersionGTE(jobsVersion, c)
  929. By("running the image " + nginxImage)
  930. framework.RunKubectlOrDie("run", podName, "--generator=run-pod/v1", "--image="+nginxImage, "--labels=run="+podName, nsFlag)
  931. By("verifying the pod " + podName + " is running")
  932. label := labels.SelectorFromSet(labels.Set(map[string]string{"run": podName}))
  933. err := framework.WaitForPodsWithLabelRunning(c, ns, label)
  934. if err != nil {
  935. framework.Failf("Failed getting pod %s: %v", podName, err)
  936. }
  937. By("verifying the pod " + podName + " was created")
  938. podJson := framework.RunKubectlOrDie("get", "pod", podName, nsFlag, "-o", "json")
  939. if !strings.Contains(podJson, podName) {
  940. framework.Failf("Failed to find pod %s in [%s]", podName, podJson)
  941. }
  942. By("replace the image in the pod")
  943. podJson = strings.Replace(podJson, nginxImage, busyboxImage, 1)
  944. framework.RunKubectlOrDieInput(podJson, "replace", "-f", "-", nsFlag)
  945. By("verifying the pod " + podName + " has the right image " + busyboxImage)
  946. pod, err := c.Pods(ns).Get(podName)
  947. if err != nil {
  948. framework.Failf("Failed getting deployment %s: %v", podName, err)
  949. }
  950. containers := pod.Spec.Containers
  951. if containers == nil || len(containers) != 1 || containers[0].Image != busyboxImage {
  952. framework.Failf("Failed creating pod with expected image %s", busyboxImage)
  953. }
  954. })
  955. })
  956. framework.KubeDescribe("Kubectl run --rm job", func() {
  957. jobName := "e2e-test-rm-busybox-job"
  958. It("should create a job from an image, then delete the job [Conformance]", func() {
  959. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  960. // The rkt runtime doesn't support attach, see #23335
  961. framework.SkipIfContainerRuntimeIs("rkt")
  962. framework.SkipUnlessServerVersionGTE(jobsVersion, c)
  963. By("executing a command with run --rm and attach with stdin")
  964. t := time.NewTimer(runJobTimeout)
  965. defer t.Stop()
  966. runOutput := framework.NewKubectlCommand(nsFlag, "run", jobName, "--image="+busyboxImage, "--rm=true", "--generator=job/v1", "--restart=OnFailure", "--attach=true", "--stdin", "--", "sh", "-c", "cat && echo 'stdin closed'").
  967. WithStdinData("abcd1234").
  968. WithTimeout(t.C).
  969. ExecOrDie()
  970. Expect(runOutput).To(ContainSubstring("abcd1234"))
  971. Expect(runOutput).To(ContainSubstring("stdin closed"))
  972. By("verifying the job " + jobName + " was deleted")
  973. _, err := c.Extensions().Jobs(ns).Get(jobName)
  974. Expect(err).To(HaveOccurred())
  975. Expect(apierrs.IsNotFound(err)).To(BeTrue())
  976. })
  977. })
  978. framework.KubeDescribe("Proxy server", func() {
  979. // TODO: test proxy options (static, prefix, etc)
  980. It("should support proxy with --port 0 [Conformance]", func() {
  981. By("starting the proxy server")
  982. port, cmd, err := startProxyServer()
  983. if cmd != nil {
  984. defer framework.TryKill(cmd)
  985. }
  986. if err != nil {
  987. framework.Failf("Failed to start proxy server: %v", err)
  988. }
  989. By("curling proxy /api/ output")
  990. localAddr := fmt.Sprintf("http://localhost:%d/api/", port)
  991. apiVersions, err := getAPIVersions(localAddr)
  992. if err != nil {
  993. framework.Failf("Expected at least one supported apiversion, got error %v", err)
  994. }
  995. if len(apiVersions.Versions) < 1 {
  996. framework.Failf("Expected at least one supported apiversion, got %v", apiVersions)
  997. }
  998. })
  999. It("should support --unix-socket=/path [Conformance]", func() {
  1000. By("Starting the proxy")
  1001. tmpdir, err := ioutil.TempDir("", "kubectl-proxy-unix")
  1002. if err != nil {
  1003. framework.Failf("Failed to create temporary directory: %v", err)
  1004. }
  1005. path := filepath.Join(tmpdir, "test")
  1006. defer os.Remove(path)
  1007. defer os.Remove(tmpdir)
  1008. cmd := framework.KubectlCmd("proxy", fmt.Sprintf("--unix-socket=%s", path))
  1009. stdout, stderr, err := framework.StartCmdAndStreamOutput(cmd)
  1010. if err != nil {
  1011. framework.Failf("Failed to start kubectl command: %v", err)
  1012. }
  1013. defer stdout.Close()
  1014. defer stderr.Close()
  1015. defer framework.TryKill(cmd)
  1016. buf := make([]byte, 128)
  1017. if _, err = stdout.Read(buf); err != nil {
  1018. framework.Failf("Expected output from kubectl proxy: %v", err)
  1019. }
  1020. By("retrieving proxy /api/ output")
  1021. _, err = curlUnix("http://unused/api", path)
  1022. if err != nil {
  1023. framework.Failf("Failed get of /api at %s: %v", path, err)
  1024. }
  1025. })
  1026. })
  1027. framework.KubeDescribe("Kubectl taint", func() {
  1028. It("should update the taint on a node", func() {
  1029. taintName := fmt.Sprintf("kubernetes.io/e2e-taint-key-%s", string(uuid.NewUUID()))
  1030. taintValue := "testing-taint-value"
  1031. taintEffect := fmt.Sprintf("%s", api.TaintEffectNoSchedule)
  1032. nodes, err := c.Nodes().List(api.ListOptions{})
  1033. Expect(err).NotTo(HaveOccurred())
  1034. node := nodes.Items[0]
  1035. nodeName := node.Name
  1036. By("adding the taint " + taintName + " with value " + taintValue + " and taint effect " + taintEffect + " to a node")
  1037. framework.RunKubectlOrDie("taint", "nodes", nodeName, taintName+"="+taintValue+":"+taintEffect)
  1038. By("verifying the node has the taint " + taintName + " with the value " + taintValue)
  1039. output := framework.RunKubectlOrDie("describe", "node", nodeName)
  1040. requiredStrings := [][]string{
  1041. {"Name:", nodeName},
  1042. {"Taints:"},
  1043. {taintName + "=" + taintValue + ":" + taintEffect},
  1044. }
  1045. checkOutput(output, requiredStrings)
  1046. By("removing the taint " + taintName + " of a node")
  1047. framework.RunKubectlOrDie("taint", "nodes", nodeName, taintName+"-")
  1048. By("verifying the node doesn't have the taint " + taintName)
  1049. output = framework.RunKubectlOrDie("describe", "node", nodeName)
  1050. if strings.Contains(output, taintName) {
  1051. framework.Failf("Failed removing taint " + taintName + " of the node " + nodeName)
  1052. }
  1053. })
  1054. })
  1055. framework.KubeDescribe("Kubectl create quota", func() {
  1056. It("should create a quota without scopes", func() {
  1057. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  1058. quotaName := "million"
  1059. By("calling kubectl quota")
  1060. framework.RunKubectlOrDie("create", "quota", quotaName, "--hard=pods=1000000,services=1000000", nsFlag)
  1061. By("verifying that the quota was created")
  1062. quota, err := c.ResourceQuotas(ns).Get(quotaName)
  1063. if err != nil {
  1064. framework.Failf("Failed getting quota %s: %v", quotaName, err)
  1065. }
  1066. if len(quota.Spec.Scopes) != 0 {
  1067. framework.Failf("Expected empty scopes, got %v", quota.Spec.Scopes)
  1068. }
  1069. if len(quota.Spec.Hard) != 2 {
  1070. framework.Failf("Expected two resources, got %v", quota.Spec.Hard)
  1071. }
  1072. r, found := quota.Spec.Hard[api.ResourcePods]
  1073. if expected := resource.MustParse("1000000"); !found || (&r).Cmp(expected) != 0 {
  1074. framework.Failf("Expected pods=1000000, got %v", r)
  1075. }
  1076. r, found = quota.Spec.Hard[api.ResourceServices]
  1077. if expected := resource.MustParse("1000000"); !found || (&r).Cmp(expected) != 0 {
  1078. framework.Failf("Expected services=1000000, got %v", r)
  1079. }
  1080. })
  1081. It("should create a quota with scopes", func() {
  1082. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  1083. quotaName := "scopes"
  1084. By("calling kubectl quota")
  1085. framework.RunKubectlOrDie("create", "quota", quotaName, "--hard=pods=1000000", "--scopes=BestEffort,NotTerminating", nsFlag)
  1086. By("verifying that the quota was created")
  1087. quota, err := c.ResourceQuotas(ns).Get(quotaName)
  1088. if err != nil {
  1089. framework.Failf("Failed getting quota %s: %v", quotaName, err)
  1090. }
  1091. if len(quota.Spec.Scopes) != 2 {
  1092. framework.Failf("Expected two scopes, got %v", quota.Spec.Scopes)
  1093. }
  1094. scopes := make(map[api.ResourceQuotaScope]struct{})
  1095. for _, scope := range quota.Spec.Scopes {
  1096. scopes[scope] = struct{}{}
  1097. }
  1098. if _, found := scopes[api.ResourceQuotaScopeBestEffort]; !found {
  1099. framework.Failf("Expected BestEffort scope, got %v", quota.Spec.Scopes)
  1100. }
  1101. if _, found := scopes[api.ResourceQuotaScopeNotTerminating]; !found {
  1102. framework.Failf("Expected NotTerminating scope, got %v", quota.Spec.Scopes)
  1103. }
  1104. })
  1105. It("should reject quota with invalid scopes", func() {
  1106. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  1107. quotaName := "scopes"
  1108. By("calling kubectl quota")
  1109. out, err := framework.RunKubectl("create", "quota", quotaName, "--hard=hard=pods=1000000", "--scopes=Foo", nsFlag)
  1110. if err == nil {
  1111. framework.Failf("Expected kubectl to fail, but it succeeded: %s", out)
  1112. }
  1113. })
  1114. })
  1115. })
  1116. // Checks whether the output split by line contains the required elements.
  1117. func checkOutput(out