github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/integration/v7/isolated/ssh_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"time"
     7  
     8  	"code.cloudfoundry.org/cli/integration/helpers"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gbytes"
    12  	. "github.com/onsi/gomega/gexec"
    13  )
    14  
    15  var _ = Describe("ssh command", func() {
    16  	var (
    17  		appName   string
    18  		orgName   string
    19  		spaceName string
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		appName = helpers.PrefixedRandomName("app")
    24  		orgName = helpers.NewOrgName()
    25  		spaceName = helpers.NewSpaceName()
    26  	})
    27  
    28  	When("--help flag is set", func() {
    29  		It("Displays command usage to output", func() {
    30  			session := helpers.CF("ssh", "--help")
    31  
    32  			Eventually(session).Should(Say(`NAME:`))
    33  			Eventually(session).Should(Say(`ssh - SSH to an application container instance`))
    34  			Eventually(session).Should(Say(`USAGE:`))
    35  			Eventually(session).Should(Say(`cf ssh APP_NAME \[--process PROCESS\] \[-i INDEX\] \[-c COMMAND\]...\n`))
    36  			Eventually(session).Should(Say(`\[-L \[BIND_ADDRESS:\]LOCAL_PORT:REMOTE_HOST:REMOTE_PORT\]\.\.\. \[--skip-remote-execution\]`))
    37  			Eventually(session).Should(Say(`\[--disable-pseudo-tty \| --force-pseudo-tty \| --request-pseudo-tty\] \[--skip-host-validation\]`))
    38  			Eventually(session).Should(Say(`OPTIONS:`))
    39  			Eventually(session).Should(Say(`--app-instance-index, -i\s+App process instance index \(Default: 0\)`))
    40  			Eventually(session).Should(Say(`--command, -c\s+Command to run`))
    41  			Eventually(session).Should(Say(`--disable-pseudo-tty, -T\s+Disable pseudo-tty allocation`))
    42  			Eventually(session).Should(Say(`--force-pseudo-tty\s+Force pseudo-tty allocation`))
    43  			Eventually(session).Should(Say(`-L\s+Local port forward specification`))
    44  			Eventually(session).Should(Say(`--process\s+App process name \(Default: web\)`))
    45  			Eventually(session).Should(Say(`--request-pseudo-tty, -t\s+Request pseudo-tty allocation`))
    46  			Eventually(session).Should(Say(`--skip-host-validation, -k\s+Skip host key validation\. Not recommended!`))
    47  			Eventually(session).Should(Say(`--skip-remote-execution, -N\s+Do not execute a remote command`))
    48  			Eventually(session).Should(Say(`ENVIRONMENT:`))
    49  			Eventually(session).Should(Say(`all_proxy=\s+Specify a proxy server to enable proxying for all requests`))
    50  			Eventually(session).Should(Say(`SEE ALSO:`))
    51  			Eventually(session).Should(Say(`allow-space-ssh, enable-ssh, space-ssh-allowed, ssh-code, ssh-enabled`))
    52  			Eventually(session).Should(Exit(0))
    53  		})
    54  	})
    55  
    56  	When("the app name is not provided", func() {
    57  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    58  			session := helpers.CF("ssh")
    59  
    60  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided"))
    61  			Eventually(session).Should(Say("NAME:"))
    62  			Eventually(session).Should(Exit(1))
    63  		})
    64  	})
    65  
    66  	When("the environment is not setup correctly", func() {
    67  		It("fails with the appropriate errors", func() {
    68  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "ssh", appName)
    69  		})
    70  	})
    71  
    72  	When("the environment is setup correctly", func() {
    73  		BeforeEach(func() {
    74  			helpers.SetupCF(orgName, spaceName)
    75  		})
    76  
    77  		AfterEach(func() {
    78  			helpers.QuickDeleteOrg(orgName)
    79  		})
    80  
    81  		When("the app does not exist", func() {
    82  			It("it displays the app does not exist", func() {
    83  				session := helpers.CF("ssh", appName)
    84  				Eventually(session).Should(Say("FAILED"))
    85  				Eventually(session.Err).Should(Say("App %s not found", appName))
    86  				Eventually(session).Should(Exit(1))
    87  			})
    88  		})
    89  
    90  		When("the app exists", func() {
    91  			BeforeEach(func() {
    92  				helpers.WithProcfileApp(func(appDir string) {
    93  					Eventually(helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "push", appName)).Should(Exit(0))
    94  				})
    95  			})
    96  
    97  			Context("TTY Options", func() {
    98  				// * The columns specify the various TTY flags passed to cf ssh
    99  				//   (--disable-pseudo-tty, --force-pseudo-tty, --request-pseudo-tty).
   100  				// * The rows specify what kind of shell you’re running "cf ssh" from. To
   101  				//   simulate an interactive shell, simply use your terminal as always.
   102  				//   To simulate a non-interactive shell, append "<< EOF <new-line>
   103  				//   <command-to-execute-on-remote-host> <new-line> EOF" to your command
   104  				// * The values (yes/no) determine whether a TTY session should be
   105  				//   allocated on the remote host. Verify by running "TTY" on remote host.
   106  				//
   107  				//               TTY Option -> | Default(auto) | Disable | Force | Request
   108  				// Shell_Type__________________|_______________|_________|_______|_____________
   109  				// interactive                 | Yes           | No      | Yes   | Yes
   110  				// non-interactive             | No            | No      | No    | No
   111  				// interactive w/ commands     | No            | No      | Yes   | Yes
   112  				// non-interactive w/ commands | No            | No      | Yes   | No
   113  
   114  				When("the running session is interactive", func() {
   115  					// This should be tested manually (launching an interactive shell in code is hard)
   116  				})
   117  
   118  				When("the running session is non-interactive", func() {
   119  					When("providing commands to run on the remote host", func() {
   120  						When("using default tty option (auto)", func() {
   121  							It("the remote shell is not TTY", func() {
   122  								// we echo hello because a successful ssh call returns the status
   123  								session := helpers.CF("ssh", appName, "-c tty;", "-c echo hello")
   124  								Eventually(session).Should(Say("not a tty"))
   125  								Eventually(session).Should(Exit(0))
   126  							})
   127  						})
   128  
   129  						When("disable-pseudo-tty is specified", func() {
   130  							It("the remote shell is not TTY", func() {
   131  								session := helpers.CF("ssh", appName, "--disable-pseudo-tty", "-c tty;", "-c echo hello")
   132  								Eventually(session).Should(Say("not a tty"))
   133  								Eventually(session).Should(Exit(0))
   134  							})
   135  						})
   136  
   137  						When("force-pseudo-tty is specified", func() {
   138  							It("the remote shell is TTY", func() {
   139  								session := helpers.CF("ssh", appName, "--force-pseudo-tty", "-c tty;", "-c echo hello")
   140  								Eventually(session).ShouldNot(Say("not a tty"))
   141  								Eventually(session).Should(Say("/dev/*"))
   142  								Eventually(session).Should(Exit(0))
   143  							})
   144  						})
   145  
   146  						When("request-pseudo-tty is specified", func() {
   147  							It("the remote shell is not TTY", func() {
   148  								session := helpers.CF("ssh", appName, "--request-pseudo-tty", "-c tty;", "-c echo hello")
   149  								Eventually(session).Should(Say("not a tty"))
   150  								Eventually(session).Should(Exit(0))
   151  							})
   152  						})
   153  					})
   154  
   155  					When("not providing commands as args", func() {
   156  						var buffer *Buffer
   157  
   158  						BeforeEach(func() {
   159  							buffer = NewBuffer()
   160  						})
   161  
   162  						When("using default tty option (auto)", func() {
   163  							It("the remote shell is not TTY", func() {
   164  								buffer.Write([]byte("tty\n"))
   165  								buffer.Write([]byte("echo hello\n"))
   166  								buffer.Write([]byte("exit\n"))
   167  								session := helpers.CFWithStdin(buffer, "ssh", appName)
   168  								Eventually(session).Should(Say("not a tty"))
   169  								Eventually(session).Should(Exit(0))
   170  							})
   171  						})
   172  
   173  						When("disable-pseudo-tty is specified", func() {
   174  							It("the remote shell is not TTY", func() {
   175  								buffer.Write([]byte("tty\n"))
   176  								buffer.Write([]byte("echo hello\n"))
   177  								buffer.Write([]byte("exit\n"))
   178  								session := helpers.CFWithStdin(buffer, "ssh", appName, "--disable-pseudo-tty")
   179  								Eventually(session).Should(Say("not a tty"))
   180  								Eventually(session).Should(Exit(0))
   181  							})
   182  						})
   183  
   184  						When("force-pseudo-tty is specified", func() {
   185  							It("the remote shell is TTY", func() {
   186  								buffer.Write([]byte("tty\n"))
   187  								buffer.Write([]byte("echo hello\n"))
   188  								buffer.Write([]byte("exit\n"))
   189  								session := helpers.CFWithStdin(buffer, "ssh", appName, "--force-pseudo-tty")
   190  								Eventually(session).ShouldNot(Say("not a tty"))
   191  								Eventually(session).Should(Say("/dev/*"))
   192  								Eventually(session).Should(Exit(0))
   193  							})
   194  						})
   195  
   196  						When("request-pseudo-tty is specified", func() {
   197  							It("the remote shell is TTY", func() {
   198  								buffer.Write([]byte("tty\n"))
   199  								buffer.Write([]byte("echo hello\n"))
   200  								buffer.Write([]byte("exit\n"))
   201  								session := helpers.CFWithStdin(buffer, "ssh", appName, "--request-pseudo-tty")
   202  								Eventually(session).Should(Say("not a tty"))
   203  								Eventually(session).Should(Exit(0))
   204  							})
   205  						})
   206  					})
   207  				})
   208  			})
   209  
   210  			It("ssh's to the process 'web', index '0'", func() {
   211  				session := helpers.CF("ssh", appName, "-c", "ps aux;", "-c", "env")
   212  				// To verify we ssh'd into the web process we examine processes
   213  				// that were launched that are unique to that process
   214  				Eventually(session).Should(Say("vcap.*ruby"))
   215  				Eventually(session).Should(Say("INSTANCE_INDEX=0"))
   216  				Eventually(session).Should(Exit(0))
   217  			})
   218  
   219  			When("commands to run are specified", func() {
   220  				It("ssh's to the default container and runs the commands", func() {
   221  					session := helpers.CF("ssh", appName, "-c", "ls;", "-c", "echo $USER")
   222  					Eventually(session).Should(Say("app"))
   223  					Eventually(session).Should(Say("deps"))
   224  					Eventually(session).Should(Say("logs"))
   225  					Eventually(session).Should(Say("vcap"))
   226  					Eventually(session).Should(Exit(0))
   227  				})
   228  			})
   229  
   230  			When("the application hasn't started", func() {
   231  				BeforeEach(func() {
   232  					session := helpers.CF("v3-stop", appName)
   233  					Eventually(session).Should(Exit(0))
   234  				})
   235  
   236  				It("prints an error message", func() {
   237  					session := helpers.CF("ssh", appName)
   238  					Eventually(session).Should(Say("FAILED"))
   239  					Eventually(session.Err).Should(Say(fmt.Sprintf("Application '%s' is not in the STARTED state", appName)))
   240  					Eventually(session).Should(Exit(1))
   241  				})
   242  			})
   243  
   244  			When("the remote command exits with a different status code", func() {
   245  				It("exits with that status code", func() {
   246  					session := helpers.CF("ssh", appName, "-c", "asdf")
   247  					Eventually(session).Should(Exit(127))
   248  				})
   249  			})
   250  
   251  			When("port forwarding is used", func() {
   252  				var port int
   253  
   254  				BeforeEach(func() {
   255  					port = 55500 + GinkgoParallelNode()
   256  				})
   257  
   258  				It("configures local port to connect to the app port", func() {
   259  					session := helpers.CF("ssh", appName, "-N", "-L", fmt.Sprintf("%d:localhost:8080", port))
   260  
   261  					time.Sleep(35 * time.Second) // Need to wait a few seconds for pipes to connect.
   262  					response, err := http.Get(fmt.Sprintf("http://localhost:%d/", port))
   263  					Expect(err).ToNot(HaveOccurred())
   264  					defer response.Body.Close()
   265  
   266  					Eventually(BufferReader(response.Body)).Should(Say("WEBrick"))
   267  
   268  					session.Kill()
   269  					Eventually(session).Should(Exit())
   270  				})
   271  			})
   272  
   273  			When("a process is specified", func() {
   274  				When("the process does not exist", func() {
   275  					It("displays the process does not exist", func() {
   276  						session := helpers.CF("ssh", appName, "--process", "fake-process")
   277  						Eventually(session).Should(Say("FAILED"))
   278  						Eventually(session.Err).Should(Say("Process fake-process not found"))
   279  						Eventually(session).Should(Exit(1))
   280  					})
   281  				})
   282  
   283  				When("the process exists", func() {
   284  					BeforeEach(func() {
   285  						Eventually(helpers.CF("scale", appName, "--process", "console", "-i", "1")).Should(Exit(0))
   286  					})
   287  
   288  					It("ssh's to the process's default index", func() {
   289  						session := helpers.CF("ssh", appName, "--process", "console", "-c", "ps aux;", "-c", "env")
   290  						Eventually(session).Should(Say("vcap.*irb"))
   291  						Eventually(session).Should(Say("INSTANCE_INDEX=0"))
   292  						Eventually(session).Should(Exit(0))
   293  					})
   294  
   295  					When("the index is specified", func() {
   296  						When("the index does not exist", func() {
   297  							It("returns an instance not found error", func() {
   298  								session := helpers.CF("ssh", appName, "--process", "console", "-i", "1", "-c", "ps aux;", "-c", "env")
   299  								Eventually(session).Should(Say("FAILED"))
   300  								Eventually(session.Err).Should(Say("Instance %d of process console not found", 1))
   301  								Eventually(session).Should(Exit(1))
   302  							})
   303  						})
   304  
   305  						When("the index exists", func() {
   306  							It("ssh's to the provided index", func() {
   307  								session := helpers.CF("ssh", appName, "--process", "console", "-i", "0", "-c", "ps aux;", "-c", "env")
   308  								Eventually(session).Should(Say("vcap.*irb"))
   309  								Eventually(session).Should(Say("INSTANCE_INDEX=0"))
   310  								Eventually(session).Should(Exit(0))
   311  							})
   312  						})
   313  					})
   314  				})
   315  			})
   316  
   317  			When("a user isn't authorized", func() {
   318  				var (
   319  					newUser string
   320  					newPass string
   321  				)
   322  
   323  				BeforeEach(func() {
   324  					newUser = helpers.NewUsername()
   325  					newPass = helpers.NewPassword()
   326  
   327  					Eventually(helpers.CF("create-user", newUser, newPass)).Should(Exit(0))
   328  					Eventually(helpers.CF("set-space-role", newUser, orgName, spaceName, "SpaceAuditor")).Should(Exit(0))
   329  					env := map[string]string{
   330  						"CF_USERNAME": newUser,
   331  						"CF_PASSWORD": newPass,
   332  					}
   333  					Eventually(helpers.CFWithEnv(env, "auth")).Should(Exit(0))
   334  					helpers.TargetOrgAndSpace(orgName, spaceName)
   335  				})
   336  
   337  				AfterEach(func() {
   338  					helpers.LoginCF()
   339  				})
   340  
   341  				It("returns an error", func() {
   342  					session := helpers.CF("ssh", appName)
   343  
   344  					Eventually(session.Err).Should(Say("Error opening SSH connection: You are not authorized to perform the requested action."))
   345  					Eventually(session).Should(Exit(1))
   346  				})
   347  			})
   348  		})
   349  	})
   350  })