github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/integration/shared/isolated/space_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     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("space command", func() {
    16  	var (
    17  		orgName   string
    18  		spaceName string
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		orgName = helpers.NewOrgName()
    23  		spaceName = helpers.NewSpaceName()
    24  	})
    25  
    26  	Describe("help", func() {
    27  		When("--help flag is set", func() {
    28  			It("Displays command usage to output", func() {
    29  				session := helpers.CF("space", "--help")
    30  				Eventually(session).Should(Say("NAME:"))
    31  				Eventually(session).Should(Say("space - Show space info"))
    32  				Eventually(session).Should(Say("USAGE:"))
    33  				Eventually(session).Should(Say(`cf space SPACE \[--guid\] \[--security-group-rules\]`))
    34  				Eventually(session).Should(Say("OPTIONS:"))
    35  				Eventually(session).Should(Say(`--guid\s+Retrieve and display the given space's guid\.  All other output for the space is suppressed\.`))
    36  				Eventually(session).Should(Say(`--security-group-rules\s+Retrieve the rules for all the security groups associated with the space\.`))
    37  				Eventually(session).Should(Say("SEE ALSO:"))
    38  				Eventually(session).Should(Say("set-space-isolation-segment, space-quota, space-users"))
    39  				Eventually(session).Should(Exit(0))
    40  			})
    41  		})
    42  	})
    43  
    44  	When("the environment is not setup correctly", func() {
    45  		It("fails with the appropriate errors", func() {
    46  			helpers.CheckEnvironmentTargetedCorrectly(true, false, ReadOnlyOrg, "space", "space-name")
    47  		})
    48  	})
    49  
    50  	When("the environment is set up correctly", func() {
    51  		BeforeEach(func() {
    52  			helpers.LoginCF()
    53  			helpers.SetupCF(orgName, spaceName)
    54  		})
    55  
    56  		AfterEach(func() {
    57  			helpers.QuickDeleteOrg(orgName)
    58  			helpers.ClearTarget()
    59  		})
    60  
    61  		When("the space does not exist", func() {
    62  			It("displays not found and exits 1", func() {
    63  				badSpaceName := fmt.Sprintf("%s-1", spaceName)
    64  				session := helpers.CF("space", badSpaceName)
    65  				userName, _ := helpers.GetCredentials()
    66  				Eventually(session).Should(Say(`Getting info for space %s in org %s as %s\.\.\.`, badSpaceName, orgName, userName))
    67  				Eventually(session).Should(Say("FAILED"))
    68  				Eventually(session.Err).Should(Say("Space '%s' not found.", badSpaceName))
    69  				Eventually(session).Should(Exit(1))
    70  			})
    71  		})
    72  
    73  		When("the space exists", func() {
    74  			When("the --guid flag is used", func() {
    75  				It("displays the space guid", func() {
    76  					session := helpers.CF("space", "--guid", spaceName)
    77  					Eventually(session).Should(Say(`[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}`))
    78  					Eventually(session).Should(Exit(0))
    79  				})
    80  			})
    81  
    82  			When("the --guid flag is not used", func() {
    83  				var (
    84  					securityGroup0      helpers.SecurityGroup
    85  					securityGroup1      helpers.SecurityGroup
    86  					securityGroupName2  string
    87  					securityGroupRules2 *os.File
    88  					err                 error
    89  				)
    90  
    91  				BeforeEach(func() {
    92  					securityGroup0 = helpers.NewSecurityGroup(helpers.NewSecurityGroupName("0"), "tcp", "4.3.2.1/24", "80,443", "foo security group")
    93  					securityGroup0.Create()
    94  					Eventually(helpers.CF("bind-security-group", securityGroup0.Name, orgName, spaceName, "--lifecycle", "staging")).Should(Exit(0))
    95  
    96  					securityGroup1 = helpers.NewSecurityGroup(helpers.NewSecurityGroupName("1"), "tcp", "1.2.3.4/24", "80,443", "some security group")
    97  					securityGroup1.Create()
    98  					Eventually(helpers.CF("bind-security-group", securityGroup1.Name, orgName, spaceName)).Should(Exit(0))
    99  
   100  					securityGroupName2 = helpers.NewSecurityGroupName("2")
   101  					securityGroupRules2, err = ioutil.TempFile("", "security-group-rules")
   102  					Expect(err).ToNot(HaveOccurred())
   103  
   104  					securityGroupRules2.Write([]byte(`
   105  						[
   106  							{
   107  								"protocol": "udp",
   108  								"destination": "92.0.0.1/24",
   109  								"ports": "80,443",
   110  								"description": "some other other security group"
   111  							},
   112  							{
   113  								"protocol": "tcp",
   114  								"destination": "5.7.9.11/24",
   115  								"ports": "80,443",
   116  								"description": "some other security group"
   117  							}
   118  						]
   119  					`))
   120  
   121  					Eventually(helpers.CF("create-security-group", securityGroupName2, securityGroupRules2.Name())).Should(Exit(0))
   122  					os.Remove(securityGroupRules2.Name())
   123  
   124  					Eventually(helpers.CF("bind-security-group", securityGroupName2, orgName, spaceName)).Should(Exit(0))
   125  					Eventually(helpers.CF("bind-security-group", securityGroupName2, orgName, spaceName, "--lifecycle", "staging")).Should(Exit(0))
   126  				})
   127  
   128  				When("no flags are used", func() {
   129  					var (
   130  						appName              string
   131  						spaceQuotaName       string
   132  						serviceInstance      string
   133  						isolationSegmentName string
   134  					)
   135  
   136  					BeforeEach(func() {
   137  						appName = helpers.PrefixedRandomName("app")
   138  						helpers.WithHelloWorldApp(func(appDir string) {
   139  							Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack")).Should(Exit(0))
   140  						})
   141  						serviceInstance = helpers.PrefixedRandomName("si")
   142  						Eventually(helpers.CF("create-user-provided-service", serviceInstance, "-p", "{}")).Should(Exit(0))
   143  						Eventually(helpers.CF("bind-service", appName, serviceInstance)).Should(Exit(0))
   144  						spaceQuotaName = helpers.PrefixedRandomName("space-quota")
   145  						Eventually(helpers.CF("create-space-quota", spaceQuotaName)).Should(Exit(0))
   146  						Eventually(helpers.CF("set-space-quota", spaceName, spaceQuotaName)).Should(Exit(0))
   147  
   148  						Eventually(helpers.CF("bind-security-group", securityGroup1.Name, orgName, spaceName)).Should(Exit(0))
   149  					})
   150  
   151  					It("displays a table with space name, org, apps, services, space quota and security groups", func() {
   152  						session := helpers.CF("space", spaceName)
   153  						userName, _ := helpers.GetCredentials()
   154  						Eventually(session).Should(Say(`Getting info for space %s in org %s as %s\.\.\.`, spaceName, orgName, userName))
   155  
   156  						Eventually(session).Should(Say(`name:\s+%s`, spaceName))
   157  						Eventually(session).Should(Say(`org:\s+%s`, orgName))
   158  						Eventually(session).Should(Say(`apps:\s+%s`, appName))
   159  						Eventually(session).Should(Say(`services:\s+%s`, serviceInstance))
   160  						Eventually(session).Should(Say(`space quota:\s+%s`, spaceQuotaName))
   161  						Eventually(session).Should(Say(`running security groups:\s+.*%s,.* %s`, securityGroup1.Name, securityGroupName2))
   162  						Eventually(session).Should(Say(`staging security groups:\s+.*%s,.* %s`, securityGroup0.Name, securityGroupName2))
   163  						Eventually(session).Should(Exit(0))
   164  					})
   165  
   166  					When("isolations segments are enabled", func() {
   167  						BeforeEach(func() {
   168  							isolationSegmentName = helpers.NewIsolationSegmentName()
   169  							Eventually(helpers.CF("create-isolation-segment", isolationSegmentName)).Should(Exit(0))
   170  							Eventually(helpers.CF("enable-org-isolation", orgName, isolationSegmentName)).Should(Exit(0))
   171  							Eventually(helpers.CF("set-space-isolation-segment", spaceName, isolationSegmentName)).Should(Exit(0))
   172  						})
   173  
   174  						It("displays the isolation segment in the table", func() {
   175  							session := helpers.CF("space", spaceName)
   176  							userName, _ := helpers.GetCredentials()
   177  							Eventually(session).Should(Say(`Getting info for space %s in org %s as %s\.\.\.`, spaceName, orgName, userName))
   178  
   179  							Eventually(session).Should(Say(`isolation segment:\s+%s`, isolationSegmentName))
   180  						})
   181  					})
   182  				})
   183  
   184  				When("the space does not have an isolation segment and its org has a default isolation segment", func() {
   185  					var orgIsolationSegmentName string
   186  
   187  					BeforeEach(func() {
   188  						orgIsolationSegmentName = helpers.NewIsolationSegmentName()
   189  						Eventually(helpers.CF("create-isolation-segment", orgIsolationSegmentName)).Should(Exit(0))
   190  						Eventually(helpers.CF("enable-org-isolation", orgName, orgIsolationSegmentName)).Should(Exit(0))
   191  						Eventually(helpers.CF("set-org-default-isolation-segment", orgName, orgIsolationSegmentName)).Should(Exit(0))
   192  					})
   193  
   194  					It("shows the org default isolation segment", func() {
   195  						session := helpers.CF("space", spaceName)
   196  						Eventually(session).Should(Say(`isolation segment:\s+%s`, orgIsolationSegmentName))
   197  						Eventually(session).Should(Exit(0))
   198  					})
   199  				})
   200  
   201  				When("the security group rules flag is used", func() {
   202  					It("displays the space information as well as all security group rules", func() {
   203  						session := helpers.CF("space", "--security-group-rules", spaceName)
   204  						userName, _ := helpers.GetCredentials()
   205  						Eventually(session).Should(Say(`Getting info for space %s in org %s as %s\.\.\.`, spaceName, orgName, userName))
   206  
   207  						Eventually(session).Should(Say("name:"))
   208  						Eventually(session).Should(Say("org:"))
   209  						Eventually(session).Should(Say("apps:"))
   210  						Eventually(session).Should(Say("services:"))
   211  						Eventually(session).Should(Say("space quota:"))
   212  						Eventually(session).Should(Say("running security groups:"))
   213  						Eventually(session).Should(Say("staging security groups:"))
   214  						Eventually(session).Should(Say("\n\n"))
   215  
   216  						Eventually(session).Should(Say(`security group\s+destination\s+ports\s+protocol\s+lifecycle\s+description`))
   217  						Eventually(session).Should(Say(`#\d+\s+%s\s+4.3.2.1/24\s+80,443\s+tcp\s+staging\s+foo security group`, securityGroup0.Name))
   218  						Eventually(session).Should(Say(`#\d+\s+%s\s+1.2.3.4/24\s+80,443\s+tcp\s+running\s+some security group`, securityGroup1.Name))
   219  						Eventually(session).Should(Say(`#\d+\s+%s\s+5.7.9.11/24\s+80,443\s+tcp\s+running\s+some other security group`, securityGroupName2))
   220  						Eventually(session).Should(Say(`\s+%s\s+5.7.9.11/24\s+80,443\s+tcp\s+staging\s+some other security group`, securityGroupName2))
   221  						Eventually(session).Should(Say(`\s+%s\s+92.0.0.1/24\s+80,443\s+udp\s+running\s+some other other security group`, securityGroupName2))
   222  						Eventually(session).Should(Say(`\s+%s\s+92.0.0.1/24\s+80,443\s+udp\s+staging\s+some other other security group`, securityGroupName2))
   223  						Eventually(session).Should(Exit(0))
   224  					})
   225  				})
   226  			})
   227  		})
   228  	})
   229  })