github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/integration/isolated/add_network_policy_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"regexp"
     5  
     6  	"code.cloudfoundry.org/cli/integration/helpers"
     7  
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/gbytes"
    11  	. "github.com/onsi/gomega/gexec"
    12  	. "github.com/onsi/gomega/ghttp"
    13  )
    14  
    15  var _ = Describe("add-network-policy command", func() {
    16  	Describe("help", func() {
    17  		Context("when --help flag is set", func() {
    18  			It("Displays command usage to output", func() {
    19  				session := helpers.CF("add-network-policy", "--help")
    20  				Eventually(session).Should(Say("NAME:"))
    21  				Eventually(session).Should(Say("add-network-policy - Create policy to allow direct network traffic from one app to another"))
    22  				Eventually(session).Should(Say("USAGE:"))
    23  				Eventually(session).Should(Say(regexp.QuoteMeta("cf add-network-policy SOURCE_APP --destination-app DESTINATION_APP [(--protocol (tcp | udp) --port RANGE)]")))
    24  				Eventually(session).Should(Say("EXAMPLES:"))
    25  				Eventually(session).Should(Say("   cf add-network-policy frontend --destination-app backend --protocol tcp --port 8081"))
    26  				Eventually(session).Should(Say("   cf add-network-policy frontend --destination-app backend --protocol tcp --port 8080-8090"))
    27  				Eventually(session).Should(Say("OPTIONS:"))
    28  				Eventually(session).Should(Say("   --destination-app      Name of app to connect to"))
    29  				Eventually(session).Should(Say("   --port                 Port or range of ports for connection to destination app \\(Default: 8080\\)"))
    30  				Eventually(session).Should(Say("   --protocol             Protocol to connect apps with \\(Default: tcp\\)"))
    31  				Eventually(session).Should(Say("SEE ALSO:"))
    32  				Eventually(session).Should(Say("   apps, network-policies"))
    33  				Eventually(session).Should(Exit(0))
    34  			})
    35  		})
    36  	})
    37  
    38  	Context("when the environment is not setup correctly", func() {
    39  		Context("when no API endpoint is set", func() {
    40  			BeforeEach(func() {
    41  				helpers.UnsetAPI()
    42  			})
    43  
    44  			It("fails with no API endpoint set message", func() {
    45  				session := helpers.CF("add-network-policy", "some-app", "--destination-app", "some-other-app")
    46  				Eventually(session).Should(Say("FAILED"))
    47  				Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint."))
    48  				Eventually(session).Should(Exit(1))
    49  			})
    50  		})
    51  
    52  		Context("when the v3 api does not exist", func() {
    53  			var server *Server
    54  
    55  			BeforeEach(func() {
    56  				server = helpers.StartAndTargetServerWithoutV3API()
    57  			})
    58  
    59  			AfterEach(func() {
    60  				server.Close()
    61  			})
    62  
    63  			It("fails with no networking api error message", func() {
    64  				session := helpers.CF("add-network-policy", "some-app", "--destination-app", "some-other-app")
    65  				Eventually(session).Should(Say("FAILED"))
    66  				Eventually(session.Err).Should(Say("This command requires Network Policy API V1. Your targeted endpoint does not expose it."))
    67  				Eventually(session).Should(Exit(1))
    68  			})
    69  		})
    70  
    71  		Context("when not logged in", func() {
    72  			BeforeEach(func() {
    73  				helpers.LogoutCF()
    74  			})
    75  
    76  			It("fails with not logged in message", func() {
    77  				session := helpers.CF("add-network-policy", "some-app", "--destination-app", "some-other-app")
    78  				Eventually(session).Should(Say("FAILED"))
    79  				Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in."))
    80  				Eventually(session).Should(Exit(1))
    81  			})
    82  		})
    83  
    84  		Context("when there is no org and space set", func() {
    85  			BeforeEach(func() {
    86  				helpers.LogoutCF()
    87  				helpers.LoginCF()
    88  			})
    89  
    90  			It("fails with no targeted org error message", func() {
    91  				session := helpers.CF("add-network-policy", "some-app", "--destination-app", "some-other-app")
    92  				Eventually(session).Should(Say("FAILED"))
    93  				Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org."))
    94  				Eventually(session).Should(Exit(1))
    95  			})
    96  		})
    97  
    98  		Context("when there is no space set", func() {
    99  			BeforeEach(func() {
   100  				helpers.LogoutCF()
   101  				helpers.LoginCF()
   102  				helpers.TargetOrg(ReadOnlyOrg)
   103  			})
   104  
   105  			It("fails with no targeted space error message", func() {
   106  				session := helpers.CF("add-network-policy", "some-app", "--destination-app", "some-other-app")
   107  				Eventually(session).Should(Say("FAILED"))
   108  				Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space."))
   109  				Eventually(session).Should(Exit(1))
   110  			})
   111  		})
   112  	})
   113  
   114  	Context("when the org and space are properly targetted", func() {
   115  		var (
   116  			orgName   string
   117  			spaceName string
   118  			appName   string
   119  		)
   120  
   121  		BeforeEach(func() {
   122  			orgName = helpers.NewOrgName()
   123  			spaceName = helpers.NewSpaceName()
   124  			appName = helpers.PrefixedRandomName("app")
   125  
   126  			setupCF(orgName, spaceName)
   127  
   128  			helpers.WithHelloWorldApp(func(appDir string) {
   129  				Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack", "--no-start")).Should(Exit(0))
   130  			})
   131  		})
   132  
   133  		AfterEach(func() {
   134  			helpers.QuickDeleteOrg(orgName)
   135  		})
   136  
   137  		Context("when an app exists", func() {
   138  			It("creates a policy", func() {
   139  				session := helpers.CF("add-network-policy", appName, "--destination-app", appName, "--port", "8080-8090", "--protocol", "udp")
   140  
   141  				username, _ := helpers.GetCredentials()
   142  				Eventually(session).Should(Say(`Adding network policy to app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
   143  				Eventually(session).Should(Say("OK"))
   144  				Eventually(session).Should(Exit(0))
   145  
   146  				session = helpers.CF("network-policies")
   147  				Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
   148  				Consistently(session).ShouldNot(Say("OK"))
   149  				Eventually(session).Should(Say("source\\s+destination\\s+protocol\\s+ports"))
   150  				Eventually(session).Should(Say("%s\\s+%s\\s+udp\\s+8080-8090", appName, appName))
   151  				Eventually(session).Should(Exit(0))
   152  			})
   153  
   154  			Context("when port and protocol are not specified", func() {
   155  				It("creates a policy with the default values", func() {
   156  					session := helpers.CF("add-network-policy", appName, "--destination-app", appName)
   157  
   158  					username, _ := helpers.GetCredentials()
   159  					Eventually(session).Should(Say(`Adding network policy to app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
   160  					Eventually(session).Should(Say("OK"))
   161  					Eventually(session).Should(Exit(0))
   162  
   163  					session = helpers.CF("network-policies")
   164  					Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
   165  					Consistently(session).ShouldNot(Say("OK"))
   166  					Eventually(session).Should(Say("source\\s+destination\\s+protocol\\s+ports"))
   167  					Eventually(session).Should(Say("%s\\s+%s\\s+tcp\\s+8080[^-]", appName, appName))
   168  					Eventually(session).Should(Exit(0))
   169  				})
   170  			})
   171  		})
   172  
   173  		Context("when the source app does not exist", func() {
   174  			It("returns an error", func() {
   175  				session := helpers.CF("add-network-policy", "pineapple", "--destination-app", appName)
   176  
   177  				username, _ := helpers.GetCredentials()
   178  				Eventually(session).Should(Say(`Adding network policy to app pineapple in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
   179  				Eventually(session.Err).Should(Say("App pineapple not found"))
   180  				Eventually(session).Should(Say("FAILED"))
   181  				Eventually(session).Should(Exit(1))
   182  			})
   183  		})
   184  
   185  		Context("when the dest app does not exist", func() {
   186  			It("returns an error", func() {
   187  				session := helpers.CF("add-network-policy", appName, "--destination-app", "pineapple")
   188  
   189  				username, _ := helpers.GetCredentials()
   190  				Eventually(session).Should(Say(`Adding network policy to app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
   191  				Eventually(session.Err).Should(Say("App pineapple not found"))
   192  				Eventually(session).Should(Say("FAILED"))
   193  				Eventually(session).Should(Exit(1))
   194  			})
   195  		})
   196  
   197  		Context("when port is specified but protocol is not", func() {
   198  			It("returns an error", func() {
   199  				session := helpers.CF("add-network-policy", appName, "--destination-app", appName, "--port", "8080")
   200  
   201  				Eventually(session.Err).Should(Say("Incorrect Usage: --protocol and --port flags must be specified together"))
   202  				Eventually(session).Should(Say("FAILED"))
   203  				Eventually(session).Should(Say("NAME:"))
   204  				Eventually(session).Should(Exit(1))
   205  			})
   206  		})
   207  
   208  		Context("when protocol is specified but port is not", func() {
   209  			It("returns an error", func() {
   210  				session := helpers.CF("add-network-policy", appName, "--destination-app", appName, "--protocol", "tcp")
   211  
   212  				Eventually(session.Err).Should(Say("Incorrect Usage: --protocol and --port flags must be specified together"))
   213  				Eventually(session).Should(Say("FAILED"))
   214  				Eventually(session).Should(Say("NAME:"))
   215  				Eventually(session).Should(Exit(1))
   216  			})
   217  		})
   218  	})
   219  })