ssh.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 node
  14. import (
  15. "fmt"
  16. "strings"
  17. "k8s.io/kubernetes/test/e2e/framework"
  18. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  19. e2essh "k8s.io/kubernetes/test/e2e/framework/ssh"
  20. "github.com/onsi/ginkgo"
  21. )
  22. const maxNodes = 100
  23. var _ = SIGDescribe("SSH", func() {
  24. f := framework.NewDefaultFramework("ssh")
  25. ginkgo.BeforeEach(func() {
  26. // When adding more providers here, also implement their functionality in e2essh.GetSigner(...).
  27. e2eskipper.SkipUnlessProviderIs(framework.ProvidersWithSSH...)
  28. // This test SSH's into the node for which it needs the $HOME/.ssh/id_rsa key to be present. So
  29. // we should skip if the environment does not have the key (not all CI systems support this use case)
  30. e2eskipper.SkipUnlessSSHKeyPresent()
  31. })
  32. ginkgo.It("should SSH to all nodes and run commands", func() {
  33. // Get all nodes' external IPs.
  34. ginkgo.By("Getting all nodes' SSH-able IP addresses")
  35. hosts, err := e2essh.NodeSSHHosts(f.ClientSet)
  36. if err != nil {
  37. framework.Failf("Error getting node hostnames: %v", err)
  38. }
  39. testCases := []struct {
  40. cmd string
  41. checkStdout bool
  42. expectedStdout string
  43. expectedStderr string
  44. expectedCode int
  45. expectedError error
  46. }{
  47. // Keep this test first - this variant runs on all nodes.
  48. {`echo "Hello from $(whoami)@$(hostname)"`, false, "", "", 0, nil},
  49. {`echo "foo" | grep "bar"`, true, "", "", 1, nil},
  50. {`echo "stdout" && echo "stderr" >&2 && exit 7`, true, "stdout", "stderr", 7, nil},
  51. }
  52. for i, testCase := range testCases {
  53. // Only run the first testcase against max 100 nodes. Run
  54. // the rest against the first node we find only, since
  55. // they're basically testing SSH semantics (and we don't
  56. // need to do that against each host in the cluster).
  57. nodes := len(hosts)
  58. if i > 0 {
  59. nodes = 1
  60. } else if nodes > maxNodes {
  61. nodes = maxNodes
  62. }
  63. testhosts := hosts[:nodes]
  64. ginkgo.By(fmt.Sprintf("SSH'ing to %d nodes and running %s", len(testhosts), testCase.cmd))
  65. for _, host := range testhosts {
  66. result, err := e2essh.SSH(testCase.cmd, host, framework.TestContext.Provider)
  67. stdout, stderr := strings.TrimSpace(result.Stdout), strings.TrimSpace(result.Stderr)
  68. if err != testCase.expectedError {
  69. framework.Failf("Ran %s on %s, got error %v, expected %v", testCase.cmd, host, err, testCase.expectedError)
  70. }
  71. if testCase.checkStdout && stdout != testCase.expectedStdout {
  72. framework.Failf("Ran %s on %s, got stdout '%s', expected '%s'", testCase.cmd, host, stdout, testCase.expectedStdout)
  73. }
  74. if stderr != testCase.expectedStderr {
  75. framework.Failf("Ran %s on %s, got stderr '%s', expected '%s'", testCase.cmd, host, stderr, testCase.expectedStderr)
  76. }
  77. if result.Code != testCase.expectedCode {
  78. framework.Failf("Ran %s on %s, got exit code %d, expected %d", testCase.cmd, host, result.Code, testCase.expectedCode)
  79. }
  80. // Show stdout, stderr for logging purposes.
  81. if len(stdout) > 0 {
  82. framework.Logf("Got stdout from %s: %s", host, strings.TrimSpace(stdout))
  83. }
  84. if len(stderr) > 0 {
  85. framework.Logf("Got stderr from %s: %s", host, strings.TrimSpace(stderr))
  86. }
  87. }
  88. }
  89. // Quickly test that SSH itself errors correctly.
  90. ginkgo.By("SSH'ing to a nonexistent host")
  91. if _, err = e2essh.SSH(`echo "hello"`, "i.do.not.exist", framework.TestContext.Provider); err == nil {
  92. framework.Failf("Expected error trying to SSH to nonexistent host.")
  93. }
  94. })
  95. })