github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/integration/isolated/bind_security_group_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"code.cloudfoundry.org/cli/integration/helpers"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  	. "github.com/onsi/gomega/gbytes"
    10  	. "github.com/onsi/gomega/gexec"
    11  	. "github.com/onsi/gomega/ghttp"
    12  )
    13  
    14  var _ = Describe("bind-security-group command", func() {
    15  	var (
    16  		orgName      string
    17  		secGroupName string
    18  		someOrgName  string
    19  		spaceName1   string
    20  		spaceName2   string
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		orgName = helpers.NewOrgName()
    25  		secGroupName = helpers.NewSecGroupName()
    26  		someOrgName = helpers.NewOrgName()
    27  		spaceName1 = helpers.NewSpaceName()
    28  		spaceName2 = helpers.NewSpaceName()
    29  
    30  		helpers.LoginCF()
    31  	})
    32  
    33  	Describe("help", func() {
    34  		Context("when --help flag is set", func() {
    35  			It("Displays command usage to output", func() {
    36  				session := helpers.CF("bind-security-group", "--help")
    37  				Eventually(session.Out).Should(Say("NAME:"))
    38  				Eventually(session.Out).Should(Say("\\s+bind-security-group - Bind a security group to a particular space, or all existing spaces of an org"))
    39  				Eventually(session.Out).Should(Say("USAGE:"))
    40  				Eventually(session.Out).Should(Say("\\s+cf bind-security-group SECURITY_GROUP ORG \\[SPACE\\] \\[--lifecycle \\(running \\| staging\\)\\]"))
    41  				Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\."))
    42  				Eventually(session.Out).Should(Say("OPTIONS:"))
    43  				Eventually(session.Out).Should(Say("\\s+--lifecycle      Lifecycle phase the group applies to \\(Default: running\\)"))
    44  				Eventually(session.Out).Should(Say("SEE ALSO:"))
    45  				Eventually(session.Out).Should(Say("\\s+apps, bind-running-security-group, bind-staging-security-group, restart, security-groups"))
    46  				Eventually(session).Should(Exit(0))
    47  			})
    48  		})
    49  	})
    50  
    51  	Context("when the lifecycle flag is invalid", func() {
    52  		It("outputs a message and usage", func() {
    53  			session := helpers.CF("bind-security-group", secGroupName, someOrgName, "--lifecycle", "invalid")
    54  			Eventually(session.Err).Should(Say("Incorrect Usage: Invalid value `invalid' for option `--lifecycle'. Allowed values are: running or staging"))
    55  			Eventually(session.Out).Should(Say("USAGE:"))
    56  			Eventually(session).Should(Exit(1))
    57  		})
    58  	})
    59  
    60  	Context("when the lifecycle flag has no argument", func() {
    61  		It("outputs a message and usage", func() {
    62  			session := helpers.CF("bind-security-group", secGroupName, someOrgName, "--lifecycle")
    63  			Eventually(session.Err).Should(Say("Incorrect Usage: expected argument for flag `--lifecycle'"))
    64  			Eventually(session.Out).Should(Say("USAGE:"))
    65  			Eventually(session).Should(Exit(1))
    66  		})
    67  	})
    68  
    69  	Context("when the environment is not setup correctly", func() {
    70  		Context("when no API endpoint is set", func() {
    71  			BeforeEach(func() {
    72  				helpers.UnsetAPI()
    73  			})
    74  
    75  			It("fails with no API endpoint set message", func() {
    76  				session := helpers.CF("bind-security-group", secGroupName, someOrgName)
    77  				Eventually(session.Out).Should(Say("FAILED"))
    78  				Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint."))
    79  				Eventually(session).Should(Exit(1))
    80  			})
    81  		})
    82  
    83  		Context("when not logged in", func() {
    84  			BeforeEach(func() {
    85  				helpers.LogoutCF()
    86  			})
    87  
    88  			It("fails with not logged in message", func() {
    89  				session := helpers.CF("bind-security-group", secGroupName, someOrgName)
    90  				Eventually(session.Out).Should(Say("FAILED"))
    91  				Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in."))
    92  				Eventually(session).Should(Exit(1))
    93  			})
    94  		})
    95  	})
    96  
    97  	Context("when the server's API version is too low", func() {
    98  		var server *Server
    99  
   100  		BeforeEach(func() {
   101  			server = NewTLSServer()
   102  			server.AppendHandlers(
   103  				CombineHandlers(
   104  					VerifyRequest(http.MethodGet, "/v2/info"),
   105  					RespondWith(http.StatusOK, `{"api_version":"2.34.0"}`),
   106  				),
   107  				CombineHandlers(
   108  					VerifyRequest(http.MethodGet, "/v2/info"),
   109  					RespondWith(http.StatusOK, `{"api_version":"2.34.0"}`),
   110  				),
   111  			)
   112  			Eventually(helpers.CF("api", server.URL(), "--skip-ssl-validation")).Should(Exit(0))
   113  		})
   114  
   115  		AfterEach(func() {
   116  			server.Close()
   117  		})
   118  
   119  		It("reports an error with a minimum-version message", func() {
   120  			session := helpers.CF("bind-security-group", secGroupName, orgName, spaceName1, "--lifecycle", "staging")
   121  
   122  			Eventually(session.Err).Should(Say("Lifecycle value 'staging' requires CF API version 2\\.68\\.0\\ or higher. Your target is 2\\.34\\.0\\."))
   123  			Eventually(session).Should(Exit(1))
   124  		})
   125  	})
   126  
   127  	Context("when the input is invalid", func() {
   128  		Context("when the security group is not provided", func() {
   129  			It("fails with an incorrect usage message and displays help", func() {
   130  				session := helpers.CF("bind-security-group")
   131  				Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `SECURITY_GROUP` and `ORG` were not provided"))
   132  				Eventually(session.Out).Should(Say("USAGE:"))
   133  				Eventually(session).Should(Exit(1))
   134  			})
   135  		})
   136  
   137  		Context("when the org is not provided", func() {
   138  			It("fails with an incorrect usage message and displays help", func() {
   139  				session := helpers.CF("bind-security-group", secGroupName)
   140  				Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `ORG` was not provided"))
   141  				Eventually(session.Out).Should(Say("USAGE:"))
   142  				Eventually(session).Should(Exit(1))
   143  			})
   144  		})
   145  	})
   146  
   147  	Context("when the security group doesn't exist", func() {
   148  		It("fails with a security group not found message", func() {
   149  			session := helpers.CF("bind-security-group", "some-security-group-that-doesn't-exist", someOrgName)
   150  			Eventually(session.Err).Should(Say("Security group 'some-security-group-that-doesn't-exist' not found."))
   151  			Eventually(session.Out).Should(Say("FAILED"))
   152  			Eventually(session).Should(Exit(1))
   153  		})
   154  	})
   155  
   156  	Context("when the security group exists", func() {
   157  		var someSecurityGroup helpers.SecurityGroup
   158  
   159  		BeforeEach(func() {
   160  			someSecurityGroup = helpers.NewSecurityGroup(secGroupName, "tcp", "0.0.0.0/0", "53", "")
   161  			someSecurityGroup.Create()
   162  		})
   163  
   164  		AfterEach(func() {
   165  			someSecurityGroup.Delete()
   166  		})
   167  
   168  		Context("when the org doesn't exist", func() {
   169  			It("fails with an org not found message", func() {
   170  				session := helpers.CF("bind-security-group", secGroupName, someOrgName)
   171  				Eventually(session.Err).Should(Say("Organization '%s' not found.", someOrgName))
   172  				Eventually(session.Out).Should(Say("FAILED"))
   173  				Eventually(session).Should(Exit(1))
   174  			})
   175  		})
   176  
   177  		Context("when the org exists", func() {
   178  			BeforeEach(func() {
   179  				helpers.CreateOrg(orgName)
   180  				helpers.TargetOrg(orgName)
   181  			})
   182  
   183  			Context("when the space doesn't exist", func() {
   184  				It("fails with a space not found message", func() {
   185  					session := helpers.CF("bind-security-group", secGroupName, orgName, "space-doesnt-exist")
   186  					Eventually(session.Err).Should(Say("Space 'space-doesnt-exist' not found."))
   187  					Eventually(session.Out).Should(Say("FAILED"))
   188  					Eventually(session).Should(Exit(1))
   189  				})
   190  			})
   191  
   192  			Context("when there are no spaces in this org", func() {
   193  				It("does not bind the security group to any space", func() {
   194  					session := helpers.CF("bind-security-group", secGroupName, orgName)
   195  					Consistently(session.Out).ShouldNot(Say("Assigning security group"))
   196  					Consistently(session.Out).ShouldNot(Say("OK"))
   197  					Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\."))
   198  					Eventually(session).Should(Exit(0))
   199  				})
   200  			})
   201  
   202  			Context("when there are spaces in this org", func() {
   203  				BeforeEach(func() {
   204  					helpers.CreateSpace(spaceName1)
   205  					helpers.CreateSpace(spaceName2)
   206  				})
   207  
   208  				Context("when the lifecycle flag is not set", func() {
   209  					Context("when binding to all spaces in an org", func() {
   210  						It("binds the security group to each space", func() {
   211  							session := helpers.CF("bind-security-group", secGroupName, orgName)
   212  							userName, _ := helpers.GetCredentials()
   213  							Eventually(session.Out).Should(Say("Assigning security group %s to space INTEGRATION-SPACE.* in org %s as %s\\.\\.\\.", secGroupName, orgName, userName))
   214  							Eventually(session.Out).Should(Say("OK"))
   215  							Eventually(session.Out).Should(Say("Assigning security group %s to space INTEGRATION-SPACE.* in org %s as %s\\.\\.\\.", secGroupName, orgName, userName))
   216  							Eventually(session.Out).Should(Say("OK"))
   217  							Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\."))
   218  							Eventually(session).Should(Exit(0))
   219  						})
   220  					})
   221  				})
   222  
   223  				Context("when binding to a particular space", func() {
   224  					It("binds the security group to the space", func() {
   225  						session := helpers.CF("bind-security-group", secGroupName, orgName, spaceName1)
   226  						userName, _ := helpers.GetCredentials()
   227  						Eventually(session.Out).Should(Say("Assigning security group %s to space %s in org %s as %s\\.\\.\\.", secGroupName, spaceName1, orgName, userName))
   228  						Eventually(session.Out).Should(Say("OK"))
   229  						Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\."))
   230  						Eventually(session).Should(Exit(0))
   231  					})
   232  				})
   233  
   234  				Context("when the lifecycle flag is running", func() {
   235  					Context("when binding to a particular space", func() {
   236  						It("binds the security group to the space", func() {
   237  							session := helpers.CF("bind-security-group", secGroupName, orgName, spaceName1, "--lifecycle", "running")
   238  							userName, _ := helpers.GetCredentials()
   239  							Eventually(session.Out).Should(Say("Assigning security group %s to space %s in org %s as %s\\.\\.\\.", secGroupName, spaceName1, orgName, userName))
   240  							Eventually(session.Out).Should(Say("OK"))
   241  							Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\."))
   242  							Eventually(session).Should(Exit(0))
   243  						})
   244  					})
   245  				})
   246  
   247  				Context("when the lifecycle flag is staging", func() {
   248  					Context("when binding to all spaces in an org", func() {
   249  						It("binds the security group to each space", func() {
   250  							session := helpers.CF("bind-security-group", secGroupName, orgName, "--lifecycle", "staging")
   251  							userName, _ := helpers.GetCredentials()
   252  							Eventually(session.Out).Should(Say("Assigning security group %s to space INTEGRATION-SPACE.* in org %s as %s\\.\\.\\.", secGroupName, orgName, userName))
   253  							Eventually(session.Out).Should(Say("OK"))
   254  							Eventually(session.Out).Should(Say("Assigning security group %s to space INTEGRATION-SPACE.* in org %s as %s\\.\\.\\.", secGroupName, orgName, userName))
   255  							Eventually(session.Out).Should(Say("OK"))
   256  							Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\."))
   257  							Eventually(session).Should(Exit(0))
   258  						})
   259  					})
   260  
   261  					Context("when binding to a particular space", func() {
   262  						It("binds the security group to the space", func() {
   263  							session := helpers.CF("bind-security-group", secGroupName, orgName, spaceName1, "--lifecycle", "staging")
   264  							userName, _ := helpers.GetCredentials()
   265  							Eventually(session.Out).Should(Say("Assigning security group %s to space %s in org %s as %s\\.\\.\\.", secGroupName, spaceName1, orgName, userName))
   266  							Eventually(session.Out).Should(Say("OK"))
   267  							Eventually(session.Out).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\."))
   268  							Eventually(session).Should(Exit(0))
   269  						})
   270  					})
   271  				})
   272  			})
   273  		})
   274  	})
   275  })