github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/integration/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  	. "github.com/onsi/gomega/ghttp"
    14  )
    15  
    16  var _ = Describe("add-network-policy command", func() {
    17  	BeforeEach(func() {
    18  		helpers.SkipIfVersionLessThan(ccversion.MinVersionNetworkingV3)
    19  	})
    20  
    21  	Describe("help", func() {
    22  		Context("when --help flag is set", func() {
    23  			It("Displays command usage to output", func() {
    24  				session := helpers.CF("add-network-policy", "--help")
    25  				Eventually(session).Should(Say("NAME:"))
    26  				Eventually(session).Should(Say("add-network-policy - Create policy to allow direct network traffic from one app to another"))
    27  				Eventually(session).Should(Say("USAGE:"))
    28  				Eventually(session).Should(Say(regexp.QuoteMeta("cf add-network-policy SOURCE_APP --destination-app DESTINATION_APP [(--protocol (tcp | udp) --port RANGE)]")))
    29  				Eventually(session).Should(Say("EXAMPLES:"))
    30  				Eventually(session).Should(Say("   cf add-network-policy frontend --destination-app backend --protocol tcp --port 8081"))
    31  				Eventually(session).Should(Say("   cf add-network-policy frontend --destination-app backend --protocol tcp --port 8080-8090"))
    32  				Eventually(session).Should(Say("OPTIONS:"))
    33  				Eventually(session).Should(Say("   --destination-app      Name of app to connect to"))
    34  				Eventually(session).Should(Say("   --port                 Port or range of ports for connection to destination app \\(Default: 8080\\)"))
    35  				Eventually(session).Should(Say("   --protocol             Protocol to connect apps with \\(Default: tcp\\)"))
    36  				Eventually(session).Should(Say("SEE ALSO:"))
    37  				Eventually(session).Should(Say("   apps, network-policies"))
    38  				Eventually(session).Should(Exit(0))
    39  			})
    40  		})
    41  	})
    42  
    43  	Context("when the environment is not setup correctly", func() {
    44  		It("fails with the appropriate errors", func() {
    45  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "add-network-policy", "some-app", "--destination-app", "some-other-app")
    46  		})
    47  
    48  		Context("when the v3 api does not exist", func() {
    49  			var server *Server
    50  
    51  			BeforeEach(func() {
    52  				server = helpers.StartAndTargetServerWithoutV3API()
    53  			})
    54  
    55  			AfterEach(func() {
    56  				server.Close()
    57  			})
    58  
    59  			It("fails with no networking api error message", func() {
    60  				session := helpers.CF("add-network-policy", "some-app", "--destination-app", "some-other-app")
    61  				Eventually(session).Should(Say("FAILED"))
    62  				Eventually(session.Err).Should(Say("This command requires Network Policy API V1. Your targeted endpoint does not expose it."))
    63  				Eventually(session).Should(Exit(1))
    64  			})
    65  		})
    66  	})
    67  
    68  	Context("when the org and space are properly targetted", func() {
    69  		var (
    70  			orgName   string
    71  			spaceName string
    72  			appName   string
    73  		)
    74  
    75  		BeforeEach(func() {
    76  			orgName = helpers.NewOrgName()
    77  			spaceName = helpers.NewSpaceName()
    78  			appName = helpers.PrefixedRandomName("app")
    79  
    80  			helpers.SetupCF(orgName, spaceName)
    81  
    82  			helpers.WithHelloWorldApp(func(appDir string) {
    83  				Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack", "--no-start")).Should(Exit(0))
    84  			})
    85  		})
    86  
    87  		AfterEach(func() {
    88  			helpers.QuickDeleteOrg(orgName)
    89  		})
    90  
    91  		Context("when an app exists", func() {
    92  			It("creates a policy", func() {
    93  				session := helpers.CF("add-network-policy", appName, "--destination-app", appName, "--port", "8080-8090", "--protocol", "udp")
    94  
    95  				username, _ := helpers.GetCredentials()
    96  				Eventually(session).Should(Say(`Adding network policy to app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
    97  				Eventually(session).Should(Say("OK"))
    98  				Eventually(session).Should(Exit(0))
    99  
   100  				session = helpers.CF("network-policies")
   101  				Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
   102  				Consistently(session).ShouldNot(Say("OK"))
   103  				Eventually(session).Should(Say("source\\s+destination\\s+protocol\\s+ports"))
   104  				Eventually(session).Should(Say("%s\\s+%s\\s+udp\\s+8080-8090", appName, appName))
   105  				Eventually(session).Should(Exit(0))
   106  			})
   107  
   108  			Context("when port and protocol are not specified", func() {
   109  				It("creates a policy with the default values", func() {
   110  					session := helpers.CF("add-network-policy", appName, "--destination-app", appName)
   111  
   112  					username, _ := helpers.GetCredentials()
   113  					Eventually(session).Should(Say(`Adding network policy to app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
   114  					Eventually(session).Should(Say("OK"))
   115  					Eventually(session).Should(Exit(0))
   116  
   117  					session = helpers.CF("network-policies")
   118  					Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
   119  					Consistently(session).ShouldNot(Say("OK"))
   120  					Eventually(session).Should(Say("source\\s+destination\\s+protocol\\s+ports"))
   121  					Eventually(session).Should(Say("%s\\s+%s\\s+tcp\\s+8080[^-]", appName, appName))
   122  					Eventually(session).Should(Exit(0))
   123  				})
   124  			})
   125  		})
   126  
   127  		Context("when the source app does not exist", func() {
   128  			It("returns an error", func() {
   129  				session := helpers.CF("add-network-policy", "pineapple", "--destination-app", appName)
   130  
   131  				username, _ := helpers.GetCredentials()
   132  				Eventually(session).Should(Say(`Adding network policy to app pineapple in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
   133  				Eventually(session.Err).Should(Say("App pineapple not found"))
   134  				Eventually(session).Should(Say("FAILED"))
   135  				Eventually(session).Should(Exit(1))
   136  			})
   137  		})
   138  
   139  		Context("when the dest app does not exist", func() {
   140  			It("returns an error", func() {
   141  				session := helpers.CF("add-network-policy", appName, "--destination-app", "pineapple")
   142  
   143  				username, _ := helpers.GetCredentials()
   144  				Eventually(session).Should(Say(`Adding network policy to app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username))
   145  				Eventually(session.Err).Should(Say("App pineapple not found"))
   146  				Eventually(session).Should(Say("FAILED"))
   147  				Eventually(session).Should(Exit(1))
   148  			})
   149  		})
   150  
   151  		Context("when port is specified but protocol is not", func() {
   152  			It("returns an error", func() {
   153  				session := helpers.CF("add-network-policy", appName, "--destination-app", appName, "--port", "8080")
   154  
   155  				Eventually(session.Err).Should(Say("Incorrect Usage: --protocol and --port flags must be specified together"))
   156  				Eventually(session).Should(Say("FAILED"))
   157  				Eventually(session).Should(Say("NAME:"))
   158  				Eventually(session).Should(Exit(1))
   159  			})
   160  		})
   161  
   162  		Context("when protocol is specified but port is not", func() {
   163  			It("returns an error", func() {
   164  				session := helpers.CF("add-network-policy", appName, "--destination-app", appName, "--protocol", "tcp")
   165  
   166  				Eventually(session.Err).Should(Say("Incorrect Usage: --protocol and --port flags must be specified together"))
   167  				Eventually(session).Should(Say("FAILED"))
   168  				Eventually(session).Should(Say("NAME:"))
   169  				Eventually(session).Should(Exit(1))
   170  			})
   171  		})
   172  	})
   173  })