github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/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(Exit(0))
   108  
   109  					bytes := session.Out.Contents()
   110  
   111  					actualEnablementValue := helpers.GetsEnablementValue(bytes)
   112  					Expect(actualEnablementValue).To(Equal(true))
   113  				})
   114  			})
   115  
   116  			When("ssh was previously enabled for the app", func() {
   117  				BeforeEach(func() {
   118  					Eventually(helpers.CF("enable-ssh", appName)).Should(Exit(0))
   119  				})
   120  
   121  				It("informs the user and exits 0", func() {
   122  					session := helpers.CF("enable-ssh", appName)
   123  
   124  					Eventually(session).Should(Say(`Enabling ssh support for app %s as %s\.\.\.`, appName, userName))
   125  
   126  					Eventually(session).Should(Say("ssh support for app '%s' is already enabled.", appName))
   127  					Eventually(session).Should(Say("OK"))
   128  					Eventually(session).Should(Say("An app restart may be required for the change to take effect."))
   129  
   130  					session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/ssh_enabled", helpers.AppGUID(appName)))
   131  					Eventually(session).Should(Exit(0))
   132  
   133  					bytes := session.Out.Contents()
   134  
   135  					actualEnablementValue := helpers.GetsEnablementValue(bytes)
   136  					Expect(actualEnablementValue).To(Equal(true))
   137  				})
   138  			})
   139  
   140  			When("ssh is disabled space-wide", func() {
   141  				BeforeEach(func() {
   142  					Eventually(helpers.CF("disallow-space-ssh", spaceName)).Should(Exit(0))
   143  				})
   144  
   145  				It("informs the user", func() {
   146  					session := helpers.CF("enable-ssh", appName)
   147  
   148  					Eventually(session).Should(Say(`Enabling ssh support for app %s as %s\.\.\.`, appName, userName))
   149  
   150  					Eventually(session).Should(Say("OK"))
   151  					Eventually(session).Should(Say("TIP: Ensure ssh is also enabled on the space and global level."))
   152  					Eventually(session).Should(Exit(0))
   153  				})
   154  			})
   155  
   156  			// We're not testing the failure case when ssh is globally disabled;
   157  			// It would require us to redeploy CAPI with the cloud_controller_ng job property
   158  			// cc.allow_app_ssh_access set to false, which is time-consuming & brittle
   159  			// https://github.com/cloudfoundry/capi-release/blob/8d08628fecf116a6a1512d8ad6413414b092fba8/jobs/cloud_controller_ng/spec#L754-L756
   160  
   161  		})
   162  	})
   163  })