ssh.go 3.6 KB

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