github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/integration/isolated/ssh_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"code.cloudfoundry.org/cli/integration/helpers"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/gbytes"
    13  	. "github.com/onsi/gomega/gexec"
    14  )
    15  
    16  var _ = Describe("ssh command", func() {
    17  	Describe("help", func() {
    18  		Context("when --help flag is provided", func() {
    19  			It("displays command usage to output", func() {
    20  				session := helpers.CF("ssh", "--help")
    21  				Eventually(session).Should(Say(`NAME:`))
    22  				Eventually(session).Should(Say(`ssh - SSH to an application container instance`))
    23  				Eventually(session).Should(Say(`USAGE:`))
    24  				Eventually(session).Should(Say(`cf ssh APP_NAME \[-i INDEX\] \[-c COMMAND\]\.\.\. \[-L \[BIND_ADDRESS:\]PORT:HOST:HOST_PORT\] \[--skip-host-validation\] \[--skip-remote-execution\] \[--disable-pseudo-tty \| --force-pseudo-tty \| --request-pseudo-tty\]`))
    25  				Eventually(session).Should(Say(`--app-instance-index, -i\s+Application instance index \(Default: 0\)`))
    26  				Eventually(session).Should(Say(`--command, -c\s+Command to run\. This flag can be defined more than once\.`))
    27  				Eventually(session).Should(Say(`--disable-pseudo-tty, -T\s+Disable pseudo-tty allocation`))
    28  				Eventually(session).Should(Say(`--force-pseudo-tty\s+Force pseudo-tty allocation`))
    29  				Eventually(session).Should(Say(`-L\s+Local port forward specification\. This flag can be defined more than once\.`))
    30  				Eventually(session).Should(Say(`--request-pseudo-tty, -t\s+Request pseudo-tty allocation`))
    31  				Eventually(session).Should(Say(`--skip-host-validation, -k\s+Skip host key validation`))
    32  				Eventually(session).Should(Say(`--skip-remote-execution, -N\s+Do not execute a remote command`))
    33  				Eventually(session).Should(Say(`SEE ALSO:`))
    34  				Eventually(session).Should(Say(`allow-space-ssh, enable-ssh, space-ssh-allowed, ssh-code, ssh-enabled`))
    35  				Eventually(session).Should(Exit(0))
    36  			})
    37  		})
    38  	})
    39  
    40  	Context("when an application with multiple instances has been pushed", func() {
    41  		var (
    42  			appName          string
    43  			appDirForCleanup string
    44  			domainName       string
    45  			orgName          string
    46  			spaceName        string
    47  		)
    48  
    49  		BeforeEach(func() {
    50  			helpers.LogoutCF()
    51  
    52  			orgName = helpers.NewOrgName()
    53  			spaceName = helpers.NewSpaceName()
    54  
    55  			setupCF(orgName, spaceName)
    56  			appName = helpers.PrefixedRandomName("app")
    57  			domainName = defaultSharedDomain()
    58  			helpers.WithHelloWorldApp(func(appDir string) {
    59  				manifestContents := []byte(fmt.Sprintf(`
    60  ---
    61  applications:
    62  - name: %s
    63    memory: 128M
    64    instances: 2
    65    disk_quota: 128M
    66    routes:
    67    - route: %s.%s
    68  `, appName, appName, domainName))
    69  				manifestPath := filepath.Join(appDir, "manifest.yml")
    70  				err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
    71  				Expect(err).ToNot(HaveOccurred())
    72  
    73  				// Create manifest
    74  				Eventually(helpers.CF("push", appName, "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack", "--random-route")).Should(Exit(0))
    75  				appDirForCleanup = appDir
    76  			})
    77  		})
    78  
    79  		AfterEach(func() {
    80  			Eventually(helpers.CF("delete", appName, "-f", "-r")).Should(Exit(0))
    81  			Expect(os.RemoveAll(appDirForCleanup)).NotTo(HaveOccurred())
    82  			helpers.QuickDeleteOrg(orgName)
    83  		})
    84  
    85  		Context("when the app index is specified", func() {
    86  			Context("when it is negative", func() {
    87  				It("throws an error and informs the user that the app instance index cannot be negative", func() {
    88  					session := helpers.CF("ssh", appName, "-i", "-1")
    89  					Eventually(session).Should(Say("FAILED"))
    90  					Eventually(session).Should(Say("The application instance index cannot be negative"))
    91  					Eventually(session).Should(Exit(1))
    92  				})
    93  			})
    94  
    95  			Context("when the app index exceeds the last valid index", func() {
    96  				It("throws an error and informs the user that the specified application does not exist", func() {
    97  					session := helpers.CF("ssh", appName, "-i", "2")
    98  					Eventually(session).Should(Say("FAILED"))
    99  					Eventually(session).Should(Say("The specified application instance does not exist"))
   100  					Eventually(session).Should(Exit(1))
   101  				})
   102  			})
   103  
   104  			Context("when it is a valid index", func() {
   105  				It("does not throw any error", func() {
   106  					buffer := NewBuffer()
   107  					buffer.Write([]byte("exit\n"))
   108  					session := helpers.CFWithStdin(buffer, "ssh", appName, "-i", "0")
   109  					Eventually(session).Should(Exit(0))
   110  				})
   111  			})
   112  		})
   113  	})
   114  })