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