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