github.com/sleungcy/cli@v7.1.0+incompatible/integration/v7/isolated/enable_ssh_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  
     6  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
     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("enable-ssh command", func() {
    16  	var (
    17  		orgName   string
    18  		spaceName string
    19  		appName   string
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		orgName = helpers.NewOrgName()
    24  		spaceName = helpers.NewSpaceName()
    25  		appName = helpers.PrefixedRandomName("app")
    26  	})
    27  
    28  	Describe("help", func() {
    29  		When("--help flag is set", func() {
    30  			It("appears in cf help -a", func() {
    31  				session := helpers.CF("help", "-a")
    32  				Eventually(session).Should(Exit(0))
    33  				Expect(session).To(HaveCommandInCategoryWithDescription("enable-ssh", "APPS", "Enable ssh for the application"))
    34  			})
    35  
    36  			It("displays command usage to output", func() {
    37  				session := helpers.CF("enable-ssh", "--help")
    38  
    39  				Eventually(session).Should(Say("NAME:"))
    40  				Eventually(session).Should(Say("enable-ssh - Enable ssh for the application"))
    41  				Eventually(session).Should(Say("USAGE:"))
    42  				Eventually(session).Should(Say("cf enable-ssh APP_NAME"))
    43  				Eventually(session).Should(Say("SEE ALSO:"))
    44  				Eventually(session).Should(Say("allow-space-ssh, space-ssh-allowed, ssh, ssh-enabled"))
    45  				Eventually(session).Should(Exit(0))
    46  			})
    47  		})
    48  	})
    49  
    50  	When("the app name is not provided", func() {
    51  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    52  			session := helpers.CF("enable-ssh")
    53  
    54  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided"))
    55  			Eventually(session).Should(Say("NAME:"))
    56  			Eventually(session).Should(Exit(1))
    57  		})
    58  	})
    59  
    60  	When("the environment is not setup correctly", func() {
    61  		It("fails with the appropriate errors", func() {
    62  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "enable-ssh", appName)
    63  		})
    64  	})
    65  
    66  	When("the environment is set up correctly", func() {
    67  		var userName string
    68  
    69  		BeforeEach(func() {
    70  			helpers.SetupCF(orgName, spaceName)
    71  			userName, _ = helpers.GetCredentials()
    72  		})
    73  		AfterEach(func() {
    74  			helpers.QuickDeleteOrg(orgName)
    75  		})
    76  
    77  		When("the app does not exist", func() {
    78  			It("displays app not found and exits 1", func() {
    79  				invalidAppName := "invalid-app-name"
    80  				session := helpers.CF("enable-ssh", invalidAppName)
    81  
    82  				Eventually(session).Should(Say(`Enabling ssh support for app %s as %s\.\.\.`, invalidAppName, userName))
    83  				Eventually(session.Err).Should(Say("App '%s' not found", invalidAppName))
    84  				Eventually(session).Should(Say("FAILED"))
    85  				Eventually(session).Should(Exit(1))
    86  			})
    87  		})
    88  
    89  		When("the app exists", func() {
    90  			// Consider using cf curl /v3/apps
    91  			BeforeEach(func() {
    92  				helpers.WithHelloWorldApp(func(appDir string) {
    93  					Eventually(helpers.CF("push", appName, "-p", appDir, "--no-start")).Should(Exit(0))
    94  				})
    95  			})
    96  
    97  			When("when ssh has not been enabled yet", func() {
    98  				It("enables ssh for the app", func() {
    99  					session := helpers.CF("enable-ssh", appName)
   100  
   101  					Eventually(session).Should(Say(`Enabling ssh support for app %s as %s\.\.\.`, appName, userName))
   102  
   103  					Eventually(session).Should(Say("OK"))
   104  					Eventually(session).Should(Exit(0))
   105  
   106  					session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/ssh_enabled", helpers.AppGUID(appName)))
   107  					Eventually(session).Should(Say(`"enabled": %s`, "true"))
   108  					Eventually(session).Should(Exit(0))
   109  				})
   110  			})
   111  
   112  			When("ssh was previously enabled for the app", func() {
   113  				BeforeEach(func() {
   114  					Eventually(helpers.CF("enable-ssh", appName)).Should(Exit(0))
   115  				})
   116  
   117  				It("informs the user and exits 0", func() {
   118  					session := helpers.CF("enable-ssh", appName)
   119  
   120  					Eventually(session).Should(Say(`Enabling ssh support for app %s as %s\.\.\.`, appName, userName))
   121  
   122  					Eventually(session).Should(Say("ssh support for app '%s' is already enabled.", appName))
   123  					Eventually(session).Should(Say("OK"))
   124  
   125  					session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/ssh_enabled", helpers.AppGUID(appName)))
   126  					Eventually(session).Should(Say(`"enabled": %s`, "true"))
   127  					Eventually(session).Should(Exit(0))
   128  				})
   129  			})
   130  
   131  			When("ssh is disabled space-wide", func() {
   132  				BeforeEach(func() {
   133  					Eventually(helpers.CF("disallow-space-ssh", spaceName)).Should(Exit(0))
   134  				})
   135  
   136  				It("informs the user", func() {
   137  					session := helpers.CF("enable-ssh", appName)
   138  
   139  					Eventually(session).Should(Say(`Enabling ssh support for app %s as %s\.\.\.`, appName, userName))
   140  
   141  					Eventually(session).Should(Say("OK"))
   142  					Eventually(session).Should(Say("TIP: Ensure ssh is also enabled on the space and global level."))
   143  					Eventually(session).Should(Exit(0))
   144  				})
   145  			})
   146  
   147  			// We're not testing the failure case when ssh is globally disabled;
   148  			// It would require us to redeploy CAPI with the cloud_controller_ng job property
   149  			// cc.allow_app_ssh_access set to false, which is time-consuming & brittle
   150  			// https://github.com/cloudfoundry/capi-release/blob/8d08628fecf116a6a1512d8ad6413414b092fba8/jobs/cloud_controller_ng/spec#L754-L756
   151  
   152  		})
   153  	})
   154  })