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

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