github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/integration/shared/isolated/remove_network_policy_command_test.go (about)

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