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