github.com/sleungcy-sap/cli@v7.1.0+incompatible/integration/v7/isolated/remove_network_policy_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  
     7  	"code.cloudfoundry.org/cli/integration/helpers"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/gbytes"
    11  	. "github.com/onsi/gomega/gexec"
    12  )
    13  
    14  var _ = Describe("remove-network-policy command", func() {
    15  	Describe("help", func() {
    16  		When("--help flag is set", func() {
    17  			It("Displays command usage to output", func() {
    18  				session := helpers.CF("remove-network-policy", "--help")
    19  				Eventually(session).Should(Say("NAME:"))
    20  				Eventually(session).Should(Say("remove-network-policy - Remove network traffic policy of an app"))
    21  				Eventually(session).Should(Say("USAGE:"))
    22  				Eventually(session).Should(Say(regexp.QuoteMeta("cf remove-network-policy SOURCE_APP DESTINATION_APP [-s DESTINATION_SPACE_NAME [-o DESTINATION_ORG_NAME]] --protocol (tcp | udp) --port RANGE")))
    23  				Eventually(session).Should(Say("EXAMPLES:"))
    24  				Eventually(session).Should(Say("   cf remove-network-policy frontend backend --protocol tcp --port 8081"))
    25  				Eventually(session).Should(Say("   cf remove-network-policy frontend backend -s backend-space -o backend-org --protocol tcp --port 8080-8090"))
    26  				Eventually(session).Should(Say("OPTIONS:"))
    27  				Eventually(session).Should(Say("   --port          Port or range of ports that destination app is connected with"))
    28  				Eventually(session).Should(Say("   --protocol      Protocol that apps are connected with"))
    29  				Eventually(session).Should(Say(`   -o              The org of the destination app \(Default: targeted org\)`))
    30  				Eventually(session).Should(Say(`   -s              The space of the destination app \(Default: targeted space\)`))
    31  				Eventually(session).Should(Say("SEE ALSO:"))
    32  				Eventually(session).Should(Say("   add-network-policy, apps, network-policies"))
    33  				Eventually(session).Should(Exit(0))
    34  			})
    35  		})
    36  	})
    37  
    38  	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", "some-other-app", "--port", "8080", "--protocol", "tcp")
    41  		})
    42  	})
    43  
    44  	When("the org and space are properly targetted", func() {
    45  		var (
    46  			orgName   string
    47  			spaceName string
    48  			appName   string
    49  		)
    50  
    51  		BeforeEach(func() {
    52  			orgName = helpers.NewOrgName()
    53  			spaceName = helpers.NewSpaceName()
    54  			appName = helpers.PrefixedRandomName("app")
    55  
    56  			helpers.SetupCF(orgName, spaceName)
    57  
    58  			helpers.WithHelloWorldApp(func(appDir string) {
    59  				Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack", "--no-start")).Should(Exit(0))
    60  			})
    61  		})
    62  
    63  		AfterEach(func() {
    64  			helpers.QuickDeleteOrg(orgName)
    65  		})
    66  
    67  		When("an app exists", func() {
    68  			BeforeEach(func() {
    69  				session := helpers.CF("add-network-policy", appName, appName)
    70  
    71  				username, _ := helpers.GetCredentials()
    72  				Eventually(session).Should(Say(`Adding network policy from app %s to app %s in org %s / space %s as %s\.\.\.`, appName, appName, orgName, spaceName, username))
    73  				Eventually(session).Should(Say("OK"))
    74  				Eventually(session).Should(Exit(0))
    75  
    76  				session = helpers.CF("network-policies")
    77  				Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
    78  				Consistently(session).ShouldNot(Say("OK"))
    79  				Eventually(session).Should(Say(`source\s+destination\s+protocol\s+ports\s+destination space\s+destination org`))
    80  				Eventually(session).Should(Say(`%s\s+%s\s+tcp\s+8080\s+%s\s+%s`, appName, appName, spaceName, orgName))
    81  				Eventually(session).Should(Exit(0))
    82  			})
    83  
    84  			It("can remove a policy", func() {
    85  				session := helpers.CF("remove-network-policy", appName, appName, "--port", "8080", "--protocol", "tcp")
    86  
    87  				username, _ := helpers.GetCredentials()
    88  				Eventually(session).Should(Say(`Removing network policy from app %s to app %s in org %s / space %s as %s\.\.\.`, appName, appName, orgName, spaceName, username))
    89  				Eventually(session).Should(Say("OK"))
    90  				Eventually(session).Should(Exit(0))
    91  
    92  				session = helpers.CF("network-policies")
    93  				Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
    94  				Consistently(session).ShouldNot(Say("OK"))
    95  				Eventually(session).Should(Say(`source\s+destination\s+protocol\s+ports\s+destination space\s+destination org`))
    96  				Eventually(session).ShouldNot(Say(`%s\s+%s\s+tcp\s+8080\s+%s\s+%s`, appName, appName, spaceName, orgName))
    97  				Eventually(session).Should(Exit(0))
    98  			})
    99  
   100  			When("an org and space is provided for destination app", func() {
   101  				var (
   102  					sourceOrg     string
   103  					sourceSpace   string
   104  					sourceApp     string
   105  					sourceAppGUID string
   106  				)
   107  
   108  				BeforeEach(func() {
   109  					sourceOrg = helpers.NewOrgName()
   110  					sourceSpace = helpers.NewSpaceName()
   111  					sourceApp = helpers.PrefixedRandomName("sourceapp")
   112  
   113  					helpers.SetupCF(sourceOrg, sourceSpace)
   114  
   115  					helpers.WithHelloWorldApp(func(appDir string) {
   116  						Eventually(helpers.CF("push", sourceApp, "-p", appDir, "-b", "staticfile_buildpack", "--no-start")).Should(Exit(0))
   117  					})
   118  
   119  					sourceAppGUID = helpers.AppGUID(sourceApp)
   120  
   121  					session := helpers.CF("add-network-policy", sourceApp, appName, "-o", orgName, "-s", spaceName)
   122  					username, _ := helpers.GetCredentials()
   123  					Eventually(session).Should(Say(`Adding network policy from app %s in org %s / space %s to app %s in org %s / space %s as %s\.\.\.`, sourceApp, sourceOrg, sourceSpace, appName, orgName, spaceName, username))
   124  					Eventually(session).Should(Say("OK"))
   125  					Eventually(session).Should(Exit(0))
   126  
   127  					session = helpers.CF("network-policies")
   128  					Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, sourceOrg, sourceSpace, username))
   129  					Eventually(session).Should(Say(`source\s+destination\s+protocol\s+ports\s+destination space\s+destination org`))
   130  					Eventually(session).Should(Say(`%s\s+%s\s+tcp\s+8080\s+%s\s+%s`, sourceApp, appName, spaceName, orgName))
   131  					Eventually(session).Should(Exit(0))
   132  				})
   133  
   134  				It("can remove a policy", func() {
   135  					session := helpers.CF("remove-network-policy", sourceApp, appName, "-o", orgName, "-s", spaceName, "--port", "8080", "--protocol", "tcp")
   136  
   137  					username, _ := helpers.GetCredentials()
   138  					Eventually(session).Should(Say(`Removing network policy from app %s in org %s / space %s to app %s in org %s / space %s as %s\.\.\.`, sourceApp, sourceOrg, sourceSpace, appName, orgName, spaceName, username))
   139  					Eventually(session).Should(Say("OK"))
   140  					Eventually(session).Should(Exit(0))
   141  
   142  					session = helpers.CF("curl", fmt.Sprintf("/networking/v1/external/policies?id=%s", sourceAppGUID))
   143  					Eventually(session).Should(Exit(0))
   144  					Expect(string(session.Out.Contents())).To(MatchJSON(`{ "total_policies": 0, "policies": [] }`))
   145  				})
   146  			})
   147  
   148  			When("the protocol is not provided", func() {
   149  				It("returns a helpful message", func() {
   150  					session := helpers.CF("remove-network-policy", appName, appName, "--port", "8080")
   151  					Eventually(session.Err).Should(Say("Incorrect Usage: the required flag `--protocol' was not specified"))
   152  					Eventually(session).Should(Say("NAME:"))
   153  					Eventually(session).Should(Say("remove-network-policy - Remove network traffic policy of an app"))
   154  					Eventually(session).Should(Exit(1))
   155  				})
   156  			})
   157  
   158  			When("the port is not provided", func() {
   159  				It("returns a helpful message", func() {
   160  					session := helpers.CF("remove-network-policy", appName, appName, "--protocol", "tcp")
   161  					Eventually(session.Err).Should(Say("Incorrect Usage: the required flag `--port' was not specified"))
   162  					Eventually(session).Should(Say("NAME:"))
   163  					Eventually(session).Should(Say("remove-network-policy - Remove network traffic policy of an app"))
   164  					Eventually(session).Should(Exit(1))
   165  				})
   166  			})
   167  
   168  			When("the policy does not exist", func() {
   169  				It("returns a helpful message and exits 0", func() {
   170  					session := helpers.CF("remove-network-policy", appName, appName, "--port", "8081", "--protocol", "udp")
   171  					username, _ := helpers.GetCredentials()
   172  					Eventually(session).Should(Say(`Removing network policy from app %s to app %s in org %s / space %s as %s\.\.\.`, appName, appName, orgName, spaceName, username))
   173  					Eventually(session).Should(Say("Policy does not exist."))
   174  					Eventually(session).Should(Say("OK"))
   175  					Eventually(session).Should(Exit(0))
   176  				})
   177  
   178  			})
   179  		})
   180  
   181  		When("the source app does not exist", func() {
   182  			It("returns an error", func() {
   183  				session := helpers.CF("remove-network-policy", "pineapple", appName, "--port", "8080", "--protocol", "tcp")
   184  
   185  				username, _ := helpers.GetCredentials()
   186  				Eventually(session).Should(Say(`Removing network policy from app pineapple to app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
   187  				Eventually(session.Err).Should(Say("App 'pineapple' not found"))
   188  				Eventually(session).Should(Say("FAILED"))
   189  				Eventually(session).Should(Exit(1))
   190  			})
   191  		})
   192  
   193  		When("the dest app does not exist", func() {
   194  			It("returns an error", func() {
   195  				session := helpers.CF("remove-network-policy", appName, "pineapple", "--port", "8080", "--protocol", "tcp")
   196  
   197  				username, _ := helpers.GetCredentials()
   198  				Eventually(session).Should(Say(`Removing network policy from app %s to app pineapple in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
   199  				Eventually(session.Err).Should(Say("App 'pineapple' not found"))
   200  				Eventually(session).Should(Say("FAILED"))
   201  				Eventually(session).Should(Exit(1))
   202  			})
   203  		})
   204  	})
   205  })