github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/integration/isolated/remove_network_policy_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"regexp"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
     7  	"code.cloudfoundry.org/cli/integration/helpers"
     8  
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gbytes"
    12  	. "github.com/onsi/gomega/gexec"
    13  	. "github.com/onsi/gomega/ghttp"
    14  )
    15  
    16  var _ = Describe("remove-network-policy command", func() {
    17  	BeforeEach(func() {
    18  		helpers.SkipIfVersionLessThan(ccversion.MinVersionNetworkingV3)
    19  	})
    20  
    21  	Describe("help", func() {
    22  		Context("when --help flag is set", func() {
    23  			It("Displays command usage to output", func() {
    24  				session := helpers.CF("remove-network-policy", "--help")
    25  				Eventually(session).Should(Say("NAME:"))
    26  				Eventually(session).Should(Say("remove-network-policy - Remove network traffic policy of an app"))
    27  				Eventually(session).Should(Say("USAGE:"))
    28  				Eventually(session).Should(Say(regexp.QuoteMeta("cf remove-network-policy SOURCE_APP --destination-app DESTINATION_APP --protocol (tcp | udp) --port RANGE")))
    29  				Eventually(session).Should(Say("EXAMPLES:"))
    30  				Eventually(session).Should(Say("   cf remove-network-policy frontend --destination-app backend --protocol tcp --port 8081"))
    31  				Eventually(session).Should(Say("   cf remove-network-policy frontend --destination-app backend --protocol tcp --port 8080-8090"))
    32  				Eventually(session).Should(Say("OPTIONS:"))
    33  				Eventually(session).Should(Say("   --destination-app      Name of app to connect to"))
    34  				Eventually(session).Should(Say("   --port                 Port or range of ports that destination app is connected with"))
    35  				Eventually(session).Should(Say("   --protocol             Protocol that apps are connected with"))
    36  				Eventually(session).Should(Say("SEE ALSO:"))
    37  				Eventually(session).Should(Say("   apps, network-policies"))
    38  				Eventually(session).Should(Exit(0))
    39  			})
    40  		})
    41  	})
    42  
    43  	Context("when the environment is not setup correctly", func() {
    44  		It("fails with the appropriate errors", func() {
    45  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "remove-network-policy", "some-app", "--destination-app", "some-other-app", "--port", "8080", "--protocol", "tcp")
    46  		})
    47  
    48  		Context("when the v3 api does not exist", func() {
    49  			var server *Server
    50  
    51  			BeforeEach(func() {
    52  				server = helpers.StartAndTargetServerWithoutV3API()
    53  			})
    54  
    55  			AfterEach(func() {
    56  				server.Close()
    57  			})
    58  
    59  			It("fails with no networking api error message", func() {
    60  				session := helpers.CF("remove-network-policy", "some-app", "--destination-app", "some-app", "--protocol", "tcp", "--port", "8080")
    61  				Eventually(session).Should(Say("FAILED"))
    62  				Eventually(session.Err).Should(Say("This command requires Network Policy API V1. Your targeted endpoint does not expose it."))
    63  				Eventually(session).Should(Exit(1))
    64  			})
    65  		})
    66  	})
    67  
    68  	Context("when the org and space are properly targetted", func() {
    69  		var (
    70  			orgName   string
    71  			spaceName string
    72  			appName   string
    73  		)
    74  
    75  		BeforeEach(func() {
    76  			orgName = helpers.NewOrgName()
    77  			spaceName = helpers.NewSpaceName()
    78  			appName = helpers.PrefixedRandomName("app")
    79  
    80  			helpers.SetupCF(orgName, spaceName)
    81  
    82  			helpers.WithHelloWorldApp(func(appDir string) {
    83  				Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack", "--no-start")).Should(Exit(0))
    84  			})
    85  		})
    86  
    87  		AfterEach(func() {
    88  			helpers.QuickDeleteOrg(orgName)
    89  		})
    90  
    91  		Context("when an app exists", func() {
    92  			BeforeEach(func() {
    93  				session := helpers.CF("add-network-policy", appName, "--destination-app", appName)
    94  
    95  				username, _ := helpers.GetCredentials()
    96  				Eventually(session).Should(Say(`Adding network policy to app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
    97  				Eventually(session).Should(Say("OK"))
    98  				Eventually(session).Should(Exit(0))
    99  
   100  				session = helpers.CF("network-policies")
   101  				Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
   102  				Consistently(session).ShouldNot(Say("OK"))
   103  				Eventually(session).Should(Say("source\\s+destination\\s+protocol\\s+ports"))
   104  				Eventually(session).Should(Say("%s\\s+%s\\s+tcp\\s+8080[^-]", appName, appName))
   105  				Eventually(session).Should(Exit(0))
   106  			})
   107  
   108  			It("can remove a policy", func() {
   109  				session := helpers.CF("remove-network-policy", appName, "--destination-app", appName, "--port", "8080", "--protocol", "tcp")
   110  
   111  				username, _ := helpers.GetCredentials()
   112  				Eventually(session).Should(Say(`Removing network policy for app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
   113  				Eventually(session).Should(Say("OK"))
   114  				Eventually(session).Should(Exit(0))
   115  
   116  				session = helpers.CF("network-policies")
   117  				Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
   118  				Consistently(session).ShouldNot(Say("OK"))
   119  				Eventually(session).Should(Say("source\\s+destination\\s+protocol\\s+ports"))
   120  				Eventually(session).ShouldNot(Say("%s\\s+%s\\s+tcp\\s+8080[^-]", appName, appName))
   121  				Eventually(session).Should(Exit(0))
   122  			})
   123  
   124  			Context("when the protocol is not provided", func() {
   125  				It("returns a helpful message", func() {
   126  					session := helpers.CF("remove-network-policy", appName, "--destination-app", appName, "--port", "8080")
   127  					Eventually(session.Err).Should(Say("Incorrect Usage: the required flag `--protocol' was not specified"))
   128  					Eventually(session).Should(Say("NAME:"))
   129  					Eventually(session).Should(Say("remove-network-policy - Remove network traffic policy of an app"))
   130  					Eventually(session).Should(Exit(1))
   131  				})
   132  			})
   133  
   134  			Context("when the port is not provided", func() {
   135  				It("returns a helpful message", func() {
   136  					session := helpers.CF("remove-network-policy", appName, "--destination-app", appName, "--protocol", "tcp")
   137  					Eventually(session.Err).Should(Say("Incorrect Usage: the required flag `--port' was not specified"))
   138  					Eventually(session).Should(Say("NAME:"))
   139  					Eventually(session).Should(Say("remove-network-policy - Remove network traffic policy of an app"))
   140  					Eventually(session).Should(Exit(1))
   141  				})
   142  			})
   143  
   144  			Context("when the policy does not exist", func() {
   145  				It("returns a helpful message and exits 0", func() {
   146  					session := helpers.CF("remove-network-policy", appName, "--destination-app", appName, "--port", "8081", "--protocol", "udp")
   147  					username, _ := helpers.GetCredentials()
   148  					Eventually(session).Should(Say(`Removing network policy for app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
   149  					Eventually(session).Should(Say("Policy does not exist."))
   150  					Eventually(session).Should(Say("OK"))
   151  					Eventually(session).Should(Exit(0))
   152  				})
   153  
   154  			})
   155  		})
   156  
   157  		Context("when the source app does not exist", func() {
   158  			It("returns an error", func() {
   159  				session := helpers.CF("remove-network-policy", "pineapple", "--destination-app", appName, "--port", "8080", "--protocol", "tcp")
   160  
   161  				username, _ := helpers.GetCredentials()
   162  				Eventually(session).Should(Say(`Removing network policy for app pineapple in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
   163  				Eventually(session.Err).Should(Say("App pineapple not found"))
   164  				Eventually(session).Should(Say("FAILED"))
   165  				Eventually(session).Should(Exit(1))
   166  			})
   167  		})
   168  
   169  		Context("when the dest app does not exist", func() {
   170  			It("returns an error", func() {
   171  				session := helpers.CF("remove-network-policy", appName, "--destination-app", "pineapple", "--port", "8080", "--protocol", "tcp")
   172  
   173  				username, _ := helpers.GetCredentials()
   174  				Eventually(session).Should(Say(`Removing network policy for app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
   175  				Eventually(session.Err).Should(Say("App pineapple not found"))
   176  				Eventually(session).Should(Say("FAILED"))
   177  				Eventually(session).Should(Exit(1))
   178  			})
   179  		})
   180  	})
   181  })