github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/integration/isolated/remove_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("remove-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("remove-network-policy", "--help")
    20  				Eventually(session).Should(Say("NAME:"))
    21  				Eventually(session).Should(Say("remove-network-policy - Remove network traffic policy of an app"))
    22  				Eventually(session).Should(Say("USAGE:"))
    23  				Eventually(session).Should(Say(regexp.QuoteMeta("cf remove-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 remove-network-policy frontend --destination-app backend --protocol tcp --port 8081"))
    26  				Eventually(session).Should(Say("   cf remove-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 that destination app is connected with"))
    30  				Eventually(session).Should(Say("   --protocol             Protocol that apps are connected with"))
    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("remove-network-policy", "some-app", "--destination-app", "some-other-app", "--port", "8080", "--protocol", "tcp")
    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("remove-network-policy", "some-app", "--destination-app", "some-app", "--protocol", "tcp", "--port", "8080")
    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("remove-network-policy", "some-app", "--destination-app", "some-other-app", "--port", "8080", "--protocol", "tcp")
    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("remove-network-policy", "some-app", "--destination-app", "some-other-app", "--port", "8080", "--protocol", "tcp")
    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("remove-network-policy", "some-app", "--destination-app", "some-other-app", "--port", "8080", "--protocol", "tcp")
   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  			BeforeEach(func() {
   139  				session := helpers.CF("add-network-policy", appName, "--destination-app", appName)
   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+tcp\\s+8080[^-]", appName, appName))
   151  				Eventually(session).Should(Exit(0))
   152  			})
   153  
   154  			It("can remove a policy", func() {
   155  				session := helpers.CF("remove-network-policy", appName, "--destination-app", appName, "--port", "8080", "--protocol", "tcp")
   156  
   157  				username, _ := helpers.GetCredentials()
   158  				Eventually(session).Should(Say(`Removing network policy for app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
   159  				Eventually(session).Should(Say("OK"))
   160  				Eventually(session).Should(Exit(0))
   161  
   162  				session = helpers.CF("network-policies")
   163  				Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
   164  				Consistently(session).ShouldNot(Say("OK"))
   165  				Eventually(session).Should(Say("source\\s+destination\\s+protocol\\s+ports"))
   166  				Eventually(session).ShouldNot(Say("%s\\s+%s\\s+tcp\\s+8080[^-]", appName, appName))
   167  				Eventually(session).Should(Exit(0))
   168  			})
   169  
   170  			Context("when the protocol is not provided", func() {
   171  				It("returns a helpful message", func() {
   172  					session := helpers.CF("remove-network-policy", appName, "--destination-app", appName, "--port", "8080")
   173  					Eventually(session.Err).Should(Say("Incorrect Usage: the required flag `--protocol' was not specified"))
   174  					Eventually(session).Should(Say("NAME:"))
   175  					Eventually(session).Should(Say("remove-network-policy - Remove network traffic policy of an app"))
   176  					Eventually(session).Should(Exit(1))
   177  				})
   178  			})
   179  
   180  			Context("when the port is not provided", func() {
   181  				It("returns a helpful message", func() {
   182  					session := helpers.CF("remove-network-policy", appName, "--destination-app", appName, "--protocol", "tcp")
   183  					Eventually(session.Err).Should(Say("Incorrect Usage: the required flag `--port' was not specified"))
   184  					Eventually(session).Should(Say("NAME:"))
   185  					Eventually(session).Should(Say("remove-network-policy - Remove network traffic policy of an app"))
   186  					Eventually(session).Should(Exit(1))
   187  				})
   188  			})
   189  
   190  			Context("when the policy does not exist", func() {
   191  				It("returns a helpful message and exits 0", func() {
   192  					session := helpers.CF("remove-network-policy", appName, "--destination-app", appName, "--port", "8081", "--protocol", "udp")
   193  					username, _ := helpers.GetCredentials()
   194  					Eventually(session).Should(Say(`Removing network policy for app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
   195  					Eventually(session).Should(Say("Policy does not exist."))
   196  					Eventually(session).Should(Say("OK"))
   197  					Eventually(session).Should(Exit(0))
   198  				})
   199  
   200  			})
   201  		})
   202  
   203  		Context("when the source app does not exist", func() {
   204  			It("returns an error", func() {
   205  				session := helpers.CF("remove-network-policy", "pineapple", "--destination-app", appName, "--port", "8080", "--protocol", "tcp")
   206  
   207  				username, _ := helpers.GetCredentials()
   208  				Eventually(session).Should(Say(`Removing network policy for app pineapple in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
   209  				Eventually(session.Err).Should(Say("App pineapple not found"))
   210  				Eventually(session).Should(Say("FAILED"))
   211  				Eventually(session).Should(Exit(1))
   212  			})
   213  		})
   214  
   215  		Context("when the dest app does not exist", func() {
   216  			It("returns an error", func() {
   217  				session := helpers.CF("remove-network-policy", appName, "--destination-app", "pineapple", "--port", "8080", "--protocol", "tcp")
   218  
   219  				username, _ := helpers.GetCredentials()
   220  				Eventually(session).Should(Say(`Removing network policy for app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
   221  				Eventually(session.Err).Should(Say("App pineapple not found"))
   222  				Eventually(session).Should(Say("FAILED"))
   223  				Eventually(session).Should(Exit(1))
   224  			})
   225  		})
   226  	})
   227  })