github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+incompatible/integration/shared/isolated/add_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("add-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("add-network-policy", "--help")
    24  				Eventually(session).Should(Say("NAME:"))
    25  				Eventually(session).Should(Say("add-network-policy - Create policy to allow direct network traffic from one app to another"))
    26  				Eventually(session).Should(Say("USAGE:"))
    27  				Eventually(session).Should(Say(regexp.QuoteMeta("cf add-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 add-network-policy frontend --destination-app backend --protocol tcp --port 8081"))
    30  				Eventually(session).Should(Say("   cf add-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 for connection to destination app \\(Default: 8080\\)"))
    34  				Eventually(session).Should(Say("   --protocol             Protocol to connect apps with \\(Default: tcp\\)"))
    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, "add-network-policy", "some-app", "--destination-app", "some-other-app")
    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  			It("creates a policy", func() {
    73  				session := helpers.CF("add-network-policy", appName, "--destination-app", appName, "--port", "8080-8090", "--protocol", "udp")
    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+udp\\s+8080-8090", appName, appName))
    85  				Eventually(session).Should(Exit(0))
    86  			})
    87  
    88  			When("port and protocol are not specified", func() {
    89  				It("creates a policy with the default values", func() {
    90  					session := helpers.CF("add-network-policy", appName, "--destination-app", appName)
    91  
    92  					username, _ := helpers.GetCredentials()
    93  					Eventually(session).Should(Say(`Adding network policy to app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
    94  					Eventually(session).Should(Say("OK"))
    95  					Eventually(session).Should(Exit(0))
    96  
    97  					session = helpers.CF("network-policies")
    98  					Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
    99  					Consistently(session).ShouldNot(Say("OK"))
   100  					Eventually(session).Should(Say("source\\s+destination\\s+protocol\\s+ports"))
   101  					Eventually(session).Should(Say("%s\\s+%s\\s+tcp\\s+8080[^-]", appName, appName))
   102  					Eventually(session).Should(Exit(0))
   103  				})
   104  			})
   105  		})
   106  
   107  		When("the source app does not exist", func() {
   108  			It("returns an error", func() {
   109  				session := helpers.CF("add-network-policy", "pineapple", "--destination-app", appName)
   110  
   111  				username, _ := helpers.GetCredentials()
   112  				Eventually(session).Should(Say(`Adding network policy to app pineapple in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
   113  				Eventually(session.Err).Should(Say("App pineapple not found"))
   114  				Eventually(session).Should(Say("FAILED"))
   115  				Eventually(session).Should(Exit(1))
   116  			})
   117  		})
   118  
   119  		When("the dest app does not exist", func() {
   120  			It("returns an error", func() {
   121  				session := helpers.CF("add-network-policy", appName, "--destination-app", "pineapple")
   122  
   123  				username, _ := helpers.GetCredentials()
   124  				Eventually(session).Should(Say(`Adding network policy to app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
   125  				Eventually(session.Err).Should(Say("App pineapple not found"))
   126  				Eventually(session).Should(Say("FAILED"))
   127  				Eventually(session).Should(Exit(1))
   128  			})
   129  		})
   130  
   131  		When("port is specified but protocol is not", func() {
   132  			It("returns an error", func() {
   133  				session := helpers.CF("add-network-policy", appName, "--destination-app", appName, "--port", "8080")
   134  
   135  				Eventually(session.Err).Should(Say("Incorrect Usage: --protocol and --port flags must be specified together"))
   136  				Eventually(session).Should(Say("FAILED"))
   137  				Eventually(session).Should(Say("NAME:"))
   138  				Eventually(session).Should(Exit(1))
   139  			})
   140  		})
   141  
   142  		When("protocol is specified but port is not", func() {
   143  			It("returns an error", func() {
   144  				session := helpers.CF("add-network-policy", appName, "--destination-app", appName, "--protocol", "tcp")
   145  
   146  				Eventually(session.Err).Should(Say("Incorrect Usage: --protocol and --port flags must be specified together"))
   147  				Eventually(session).Should(Say("FAILED"))
   148  				Eventually(session).Should(Say("NAME:"))
   149  				Eventually(session).Should(Exit(1))
   150  			})
   151  		})
   152  	})
   153  })