github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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  		It("fails with the appropriate errors", func() {
    40  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "remove-network-policy", "some-app", "--destination-app", "some-other-app", "--port", "8080", "--protocol", "tcp")
    41  		})
    42  
    43  		Context("when the v3 api does not exist", func() {
    44  			var server *Server
    45  
    46  			BeforeEach(func() {
    47  				server = helpers.StartAndTargetServerWithoutV3API()
    48  			})
    49  
    50  			AfterEach(func() {
    51  				server.Close()
    52  			})
    53  
    54  			It("fails with no networking api error message", func() {
    55  				session := helpers.CF("remove-network-policy", "some-app", "--destination-app", "some-app", "--protocol", "tcp", "--port", "8080")
    56  				Eventually(session).Should(Say("FAILED"))
    57  				Eventually(session.Err).Should(Say("This command requires Network Policy API V1. Your targeted endpoint does not expose it."))
    58  				Eventually(session).Should(Exit(1))
    59  			})
    60  		})
    61  	})
    62  
    63  	Context("when the org and space are properly targetted", func() {
    64  		var (
    65  			orgName   string
    66  			spaceName string
    67  			appName   string
    68  		)
    69  
    70  		BeforeEach(func() {
    71  			orgName = helpers.NewOrgName()
    72  			spaceName = helpers.NewSpaceName()
    73  			appName = helpers.PrefixedRandomName("app")
    74  
    75  			setupCF(orgName, spaceName)
    76  
    77  			helpers.WithHelloWorldApp(func(appDir string) {
    78  				Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack", "--no-start")).Should(Exit(0))
    79  			})
    80  		})
    81  
    82  		AfterEach(func() {
    83  			helpers.QuickDeleteOrg(orgName)
    84  		})
    85  
    86  		Context("when an app exists", func() {
    87  			BeforeEach(func() {
    88  				session := helpers.CF("add-network-policy", appName, "--destination-app", appName)
    89  
    90  				username, _ := helpers.GetCredentials()
    91  				Eventually(session).Should(Say(`Adding network policy to app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
    92  				Eventually(session).Should(Say("OK"))
    93  				Eventually(session).Should(Exit(0))
    94  
    95  				session = helpers.CF("network-policies")
    96  				Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
    97  				Consistently(session).ShouldNot(Say("OK"))
    98  				Eventually(session).Should(Say("source\\s+destination\\s+protocol\\s+ports"))
    99  				Eventually(session).Should(Say("%s\\s+%s\\s+tcp\\s+8080[^-]", appName, appName))
   100  				Eventually(session).Should(Exit(0))
   101  			})
   102  
   103  			It("can remove a policy", func() {
   104  				session := helpers.CF("remove-network-policy", appName, "--destination-app", appName, "--port", "8080", "--protocol", "tcp")
   105  
   106  				username, _ := helpers.GetCredentials()
   107  				Eventually(session).Should(Say(`Removing network policy for app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
   108  				Eventually(session).Should(Say("OK"))
   109  				Eventually(session).Should(Exit(0))
   110  
   111  				session = helpers.CF("network-policies")
   112  				Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
   113  				Consistently(session).ShouldNot(Say("OK"))
   114  				Eventually(session).Should(Say("source\\s+destination\\s+protocol\\s+ports"))
   115  				Eventually(session).ShouldNot(Say("%s\\s+%s\\s+tcp\\s+8080[^-]", appName, appName))
   116  				Eventually(session).Should(Exit(0))
   117  			})
   118  
   119  			Context("when the protocol is not provided", func() {
   120  				It("returns a helpful message", func() {
   121  					session := helpers.CF("remove-network-policy", appName, "--destination-app", appName, "--port", "8080")
   122  					Eventually(session.Err).Should(Say("Incorrect Usage: the required flag `--protocol' was not specified"))
   123  					Eventually(session).Should(Say("NAME:"))
   124  					Eventually(session).Should(Say("remove-network-policy - Remove network traffic policy of an app"))
   125  					Eventually(session).Should(Exit(1))
   126  				})
   127  			})
   128  
   129  			Context("when the port is not provided", func() {
   130  				It("returns a helpful message", func() {
   131  					session := helpers.CF("remove-network-policy", appName, "--destination-app", appName, "--protocol", "tcp")
   132  					Eventually(session.Err).Should(Say("Incorrect Usage: the required flag `--port' was not specified"))
   133  					Eventually(session).Should(Say("NAME:"))
   134  					Eventually(session).Should(Say("remove-network-policy - Remove network traffic policy of an app"))
   135  					Eventually(session).Should(Exit(1))
   136  				})
   137  			})
   138  
   139  			Context("when the policy does not exist", func() {
   140  				It("returns a helpful message and exits 0", func() {
   141  					session := helpers.CF("remove-network-policy", appName, "--destination-app", appName, "--port", "8081", "--protocol", "udp")
   142  					username, _ := helpers.GetCredentials()
   143  					Eventually(session).Should(Say(`Removing network policy for app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
   144  					Eventually(session).Should(Say("Policy does not exist."))
   145  					Eventually(session).Should(Say("OK"))
   146  					Eventually(session).Should(Exit(0))
   147  				})
   148  
   149  			})
   150  		})
   151  
   152  		Context("when the source app does not exist", func() {
   153  			It("returns an error", func() {
   154  				session := helpers.CF("remove-network-policy", "pineapple", "--destination-app", appName, "--port", "8080", "--protocol", "tcp")
   155  
   156  				username, _ := helpers.GetCredentials()
   157  				Eventually(session).Should(Say(`Removing network policy for app pineapple in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
   158  				Eventually(session.Err).Should(Say("App pineapple not found"))
   159  				Eventually(session).Should(Say("FAILED"))
   160  				Eventually(session).Should(Exit(1))
   161  			})
   162  		})
   163  
   164  		Context("when the dest app does not exist", func() {
   165  			It("returns an error", func() {
   166  				session := helpers.CF("remove-network-policy", appName, "--destination-app", "pineapple", "--port", "8080", "--protocol", "tcp")
   167  
   168  				username, _ := helpers.GetCredentials()
   169  				Eventually(session).Should(Say(`Removing network policy for app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
   170  				Eventually(session.Err).Should(Say("App pineapple not found"))
   171  				Eventually(session).Should(Say("FAILED"))
   172  				Eventually(session).Should(Exit(1))
   173  			})
   174  		})
   175  	})
   176  })