github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/integration/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  		Context("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  			})
    40  		})
    41  	})
    42  
    43  	Context("when the environment is not setup correctly", func() {
    44  		Context("when no API endpoint is set", func() {
    45  			BeforeEach(func() {
    46  				helpers.UnsetAPI()
    47  			})
    48  
    49  			It("fails with no API endpoint set message", func() {
    50  				session := helpers.CF("space", "some-space")
    51  				Eventually(session.Out).Should(Say("FAILED"))
    52  				Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint."))
    53  				Eventually(session).Should(Exit(1))
    54  			})
    55  		})
    56  
    57  		Context("when not logged in", func() {
    58  			BeforeEach(func() {
    59  				helpers.LogoutCF()
    60  			})
    61  
    62  			It("fails with not logged in message", func() {
    63  				session := helpers.CF("space", "some-space")
    64  				Eventually(session.Out).Should(Say("FAILED"))
    65  				Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in."))
    66  				Eventually(session).Should(Exit(1))
    67  			})
    68  		})
    69  
    70  		Context("when no organization is targeted", func() {
    71  			BeforeEach(func() {
    72  				helpers.LoginCF()
    73  			})
    74  
    75  			AfterEach(func() {
    76  				helpers.LogoutCF()
    77  			})
    78  
    79  			It("fails with no organization targeted message and exits 1", func() {
    80  				session := helpers.CF("space", spaceName)
    81  				_, _ = helpers.GetCredentials()
    82  				Eventually(session.Out).Should(Say("FAILED"))
    83  				Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org."))
    84  				Eventually(session).Should(Exit(1))
    85  			})
    86  		})
    87  	})
    88  
    89  	Context("when the environment is set up correctly", func() {
    90  		BeforeEach(func() {
    91  			helpers.LoginCF()
    92  			setupCF(orgName, spaceName)
    93  		})
    94  
    95  		AfterEach(func() {
    96  			helpers.ClearTarget()
    97  		})
    98  
    99  		Context("when the space does not exist", func() {
   100  			It("displays not found and exits 1", func() {
   101  				badSpaceName := fmt.Sprintf("%s-1", spaceName)
   102  				session := helpers.CF("space", badSpaceName)
   103  				userName, _ := helpers.GetCredentials()
   104  				Eventually(session.Out).Should(Say("Getting info for space %s in org %s as %s...", badSpaceName, orgName, userName))
   105  				Eventually(session.Out).Should(Say("FAILED"))
   106  				Eventually(session.Err).Should(Say("Space '%s' not found.", badSpaceName))
   107  				Eventually(session).Should(Exit(1))
   108  			})
   109  		})
   110  
   111  		Context("when the space exists", func() {
   112  			Context("when the --guid flag is used", func() {
   113  				It("displays the space guid", func() {
   114  					session := helpers.CF("space", "--guid", spaceName)
   115  					Eventually(session.Out).Should(Say("[\\da-f]{8}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{12}"))
   116  					Eventually(session).Should(Exit(0))
   117  				})
   118  			})
   119  
   120  			Context("when the --guid flag is not used", func() {
   121  				var (
   122  					securityGroup0 helpers.SecurityGroup
   123  					securityGroup1 helpers.SecurityGroup
   124  					// securityGroup2      helpers.SecurityGroup
   125  					securityGroupName2  string
   126  					securityGroupRules2 *os.File
   127  					err                 error
   128  				)
   129  
   130  				BeforeEach(func() {
   131  					securityGroup0 = helpers.NewSecurityGroup(helpers.PrefixedRandomName("INTEGRATION-SEC-GROUP-0"), "tcp", "4.3.2.1/24", "80,443", "foo security group")
   132  					securityGroup0.Create()
   133  					Eventually(helpers.CF("bind-security-group", securityGroup0.Name, orgName, spaceName, "--lifecycle", "staging")).Should(Exit(0))
   134  
   135  					securityGroup1 = helpers.NewSecurityGroup(helpers.PrefixedRandomName("INTEGRATION-SEC-GROUP-1"), "tcp", "1.2.3.4/24", "80,443", "some security group")
   136  					securityGroup1.Create()
   137  					Eventually(helpers.CF("bind-security-group", securityGroup1.Name, orgName, spaceName)).Should(Exit(0))
   138  
   139  					securityGroupName2 = helpers.PrefixedRandomName("INTEGRATION-SEC-GROUP-2")
   140  					securityGroupRules2, err = ioutil.TempFile("", "security-group-rules")
   141  					Expect(err).ToNot(HaveOccurred())
   142  
   143  					securityGroupRules2.Write([]byte(`
   144  						[
   145  							{
   146  								"protocol": "udp",
   147  								"destination": "92.0.0.1/24",
   148  								"ports": "80,443",
   149  								"description": "some other other security group"
   150  							},
   151  							{
   152  								"protocol": "tcp",
   153  								"destination": "5.7.9.11/24",
   154  								"ports": "80,443",
   155  								"description": "some other security group"
   156  							}
   157  						]
   158  					`))
   159  
   160  					Eventually(helpers.CF("create-security-group", securityGroupName2, securityGroupRules2.Name())).Should(Exit(0))
   161  					Eventually(helpers.CF("bind-security-group", securityGroupName2, orgName, spaceName)).Should(Exit(0))
   162  					Eventually(helpers.CF("bind-security-group", securityGroupName2, orgName, spaceName, "--lifecycle", "staging")).Should(Exit(0))
   163  				})
   164  
   165  				AfterEach(func() {
   166  					securityGroup1.Delete()
   167  					Eventually(helpers.CF("delete-security-group", securityGroupName2, "-f")).Should(Exit(0))
   168  					os.Remove(securityGroupRules2.Name())
   169  				})
   170  
   171  				Context("when no flags are used", func() {
   172  					var (
   173  						appName              string
   174  						spaceQuotaName       string
   175  						serviceInstance      string
   176  						isolationSegmentName string
   177  					)
   178  
   179  					BeforeEach(func() {
   180  						appName = helpers.PrefixedRandomName("app")
   181  						helpers.WithHelloWorldApp(func(appDir string) {
   182  							Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack")).Should(Exit(0))
   183  						})
   184  						serviceInstance = helpers.PrefixedRandomName("si")
   185  						Eventually(helpers.CF("create-user-provided-service", serviceInstance, "-p", "{}")).Should(Exit(0))
   186  						Eventually(helpers.CF("bind-service", appName, serviceInstance)).Should(Exit(0))
   187  						spaceQuotaName = helpers.PrefixedRandomName("space-quota")
   188  						Eventually(helpers.CF("create-space-quota", spaceQuotaName)).Should(Exit(0))
   189  						Eventually(helpers.CF("set-space-quota", spaceName, spaceQuotaName)).Should(Exit(0))
   190  						isolationSegmentName = helpers.IsolationSegmentName()
   191  						Eventually(helpers.CF("create-isolation-segment", isolationSegmentName)).Should(Exit(0))
   192  						Eventually(helpers.CF("enable-org-isolation", orgName, isolationSegmentName)).Should(Exit(0))
   193  						Eventually(helpers.CF("set-space-isolation-segment", spaceName, isolationSegmentName)).Should(Exit(0))
   194  
   195  						Eventually(helpers.CF("bind-security-group", securityGroup1.Name, orgName, spaceName)).Should(Exit(0))
   196  					})
   197  
   198  					It("displays a table with space name, org, apps, services, isolation segment, space quota and security groups", func() {
   199  						session := helpers.CF("space", spaceName)
   200  						userName, _ := helpers.GetCredentials()
   201  						Eventually(session.Out).Should(Say("Getting info for space %s in org %s as %s...", spaceName, orgName, userName))
   202  
   203  						Eventually(session.Out).Should(Say("name:\\s+%s", spaceName))
   204  						Eventually(session.Out).Should(Say("org:\\s+%s", orgName))
   205  						Eventually(session.Out).Should(Say("apps:\\s+%s", appName))
   206  						Eventually(session.Out).Should(Say("services:\\s+%s", serviceInstance))
   207  						Eventually(session.Out).Should(Say("isolation segment:\\s+%s", isolationSegmentName))
   208  						Eventually(session.Out).Should(Say("space quota:\\s+%s", spaceQuotaName))
   209  						Eventually(session.Out).Should(Say("running security groups:\\s+.*%s,.* %s", securityGroup1.Name, securityGroupName2))
   210  						Eventually(session.Out).Should(Say("staging security groups:\\s+.*%s,.* %s", securityGroup0.Name, securityGroupName2))
   211  					})
   212  				})
   213  
   214  				Context("when the space does not have an isolation segment and its org has a default isolation segment", func() {
   215  					var orgIsolationSegmentName string
   216  
   217  					BeforeEach(func() {
   218  						orgIsolationSegmentName = helpers.IsolationSegmentName()
   219  						Eventually(helpers.CF("create-isolation-segment", orgIsolationSegmentName)).Should(Exit(0))
   220  						Eventually(helpers.CF("enable-org-isolation", orgName, orgIsolationSegmentName)).Should(Exit(0))
   221  						orgIsolationSegmentGUID := helpers.GetIsolationSegmentGUID(orgIsolationSegmentName)
   222  						orgGUID := helpers.GetOrgGUID(orgName)
   223  
   224  						Eventually(helpers.CF("curl", "-X", "PATCH",
   225  							fmt.Sprintf("/v3/organizations/%s/relationships/default_isolation_segment", orgGUID),
   226  							"-d", fmt.Sprintf(`{"data":{"guid":"%s"}`, orgIsolationSegmentGUID))).Should(Exit(0))
   227  					})
   228  
   229  					It("shows the org default isolation segment", func() {
   230  						session := helpers.CF("space", spaceName)
   231  						Eventually(session.Out).Should(Say("isolation segment:\\s+%s", orgIsolationSegmentName))
   232  						Eventually(session).Should(Exit(0))
   233  					})
   234  				})
   235  
   236  				Context("when the security group rules flag is used", func() {
   237  					It("displays the space information as well as all security group rules", func() {
   238  						session := helpers.CF("space", "--security-group-rules", spaceName)
   239  						userName, _ := helpers.GetCredentials()
   240  						Eventually(session.Out).Should(Say("Getting info for space %s in org %s as %s...", spaceName, orgName, userName))
   241  
   242  						Eventually(session.Out).Should(Say("name:"))
   243  						Eventually(session.Out).Should(Say("org:"))
   244  						Eventually(session.Out).Should(Say("apps:"))
   245  						Eventually(session.Out).Should(Say("services:"))
   246  						Eventually(session.Out).Should(Say("isolation segment:"))
   247  						Eventually(session.Out).Should(Say("space quota:"))
   248  						Eventually(session.Out).Should(Say("running security groups:"))
   249  						Eventually(session.Out).Should(Say("staging security groups:"))
   250  						Eventually(session.Out).Should(Say("\n\n"))
   251  
   252  						Eventually(session.Out).Should(Say("security group\\s+destination\\s+ports\\s+protocol\\s+lifecycle\\s+description"))
   253  						Eventually(session.Out).Should(Say("#\\d+\\s+%s\\s+4.3.2.1/24\\s+80,443\\s+tcp\\s+staging\\s+foo security group", securityGroup0.Name))
   254  						Eventually(session.Out).Should(Say("#\\d+\\s+%s\\s+1.2.3.4/24\\s+80,443\\s+tcp\\s+running\\s+some security group", securityGroup1.Name))
   255  						Eventually(session.Out).Should(Say("#\\d+\\s+%s\\s+5.7.9.11/24\\s+80,443\\s+tcp\\s+running\\s+some other security group", securityGroupName2))
   256  						Eventually(session.Out).Should(Say("\\s+%s\\s+5.7.9.11/24\\s+80,443\\s+tcp\\s+staging\\s+some other security group", securityGroupName2))
   257  						Eventually(session.Out).Should(Say("\\s+%s\\s+92.0.0.1/24\\s+80,443\\s+udp\\s+running\\s+some other other security group", securityGroupName2))
   258  						Eventually(session.Out).Should(Say("\\s+%s\\s+92.0.0.1/24\\s+80,443\\s+udp\\s+staging\\s+some other other security group", securityGroupName2))
   259  					})
   260  				})
   261  			})
   262  		})
   263  	})
   264  })