github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/integration/v6/isolated/ssh_command_test.go (about)

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