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