github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/integration/isolated/network_policies_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"regexp"
     5  
     6  	"code.cloudfoundry.org/cli/integration/helpers"
     7  
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/gbytes"
    11  	. "github.com/onsi/gomega/gexec"
    12  	. "github.com/onsi/gomega/ghttp"
    13  )
    14  
    15  var _ = Describe("network-policies command", func() {
    16  	Describe("help", func() {
    17  		Context("when --help flag is set", func() {
    18  			It("Displays command usage to output", func() {
    19  				session := helpers.CF("network-policies", "--help")
    20  				Eventually(session).Should(Say("NAME:"))
    21  				Eventually(session).Should(Say("network-policies - List direct network traffic policies"))
    22  				Eventually(session).Should(Say("USAGE:"))
    23  				Eventually(session).Should(Say(regexp.QuoteMeta("cf network-policies [--source SOURCE_APP]")))
    24  				Eventually(session).Should(Say("OPTIONS:"))
    25  				Eventually(session).Should(Say("   --source      Source app to filter results by"))
    26  				Eventually(session).Should(Say("SEE ALSO:"))
    27  				Eventually(session).Should(Say("   add-network-policy, apps, remove-network-policy"))
    28  				Eventually(session).Should(Exit(0))
    29  			})
    30  		})
    31  	})
    32  
    33  	Context("when the environment is not setup correctly", func() {
    34  		Context("when no API endpoint is set", func() {
    35  			BeforeEach(func() {
    36  				helpers.UnsetAPI()
    37  			})
    38  
    39  			It("fails with no API endpoint set message", func() {
    40  				session := helpers.CF("network-policies")
    41  				Eventually(session).Should(Say("FAILED"))
    42  				Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint."))
    43  				Eventually(session).Should(Exit(1))
    44  			})
    45  		})
    46  
    47  		Context("when the v3 api does not exist", func() {
    48  			var server *Server
    49  
    50  			BeforeEach(func() {
    51  				server = helpers.StartAndTargetServerWithoutV3API()
    52  			})
    53  
    54  			AfterEach(func() {
    55  				server.Close()
    56  			})
    57  
    58  			It("fails with no networking api error message", func() {
    59  				session := helpers.CF("network-policies")
    60  				Eventually(session).Should(Say("FAILED"))
    61  				Eventually(session.Err).Should(Say("This command requires Network Policy API V1. Your targeted endpoint does not expose it."))
    62  				Eventually(session).Should(Exit(1))
    63  			})
    64  		})
    65  
    66  		Context("when not logged in", func() {
    67  			BeforeEach(func() {
    68  				helpers.LogoutCF()
    69  			})
    70  
    71  			It("fails with not logged in message", func() {
    72  				session := helpers.CF("network-policies")
    73  				Eventually(session).Should(Say("FAILED"))
    74  				Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in."))
    75  				Eventually(session).Should(Exit(1))
    76  			})
    77  		})
    78  
    79  		Context("when there is no org and space set", func() {
    80  			BeforeEach(func() {
    81  				helpers.LogoutCF()
    82  				helpers.LoginCF()
    83  			})
    84  
    85  			It("fails with no targeted org error message", func() {
    86  				session := helpers.CF("network-policies")
    87  				Eventually(session).Should(Say("FAILED"))
    88  				Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org."))
    89  				Eventually(session).Should(Exit(1))
    90  			})
    91  		})
    92  
    93  		Context("when there is no space set", func() {
    94  			BeforeEach(func() {
    95  				helpers.LogoutCF()
    96  				helpers.LoginCF()
    97  				helpers.TargetOrg(ReadOnlyOrg)
    98  			})
    99  
   100  			It("fails with no targeted space error message", func() {
   101  				session := helpers.CF("network-policies")
   102  				Eventually(session).Should(Say("FAILED"))
   103  				Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space."))
   104  				Eventually(session).Should(Exit(1))
   105  			})
   106  		})
   107  	})
   108  
   109  	Context("when the org and space are properly targetted", func() {
   110  		var (
   111  			orgName   string
   112  			spaceName string
   113  			appName   string
   114  		)
   115  
   116  		BeforeEach(func() {
   117  			orgName = helpers.NewOrgName()
   118  			spaceName = helpers.NewSpaceName()
   119  			appName = helpers.PrefixedRandomName("app")
   120  
   121  			setupCF(orgName, spaceName)
   122  
   123  			helpers.WithHelloWorldApp(func(appDir string) {
   124  				Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack", "--no-start")).Should(Exit(0))
   125  			})
   126  
   127  			session := helpers.CF("add-network-policy", appName, "--destination-app", appName)
   128  			Eventually(session).Should(Exit(0))
   129  		})
   130  
   131  		AfterEach(func() {
   132  			helpers.QuickDeleteOrg(orgName)
   133  		})
   134  
   135  		Context("when policies exists", func() {
   136  			It("lists all the policies", func() {
   137  				session := helpers.CF("network-policies")
   138  
   139  				username, _ := helpers.GetCredentials()
   140  				Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
   141  				Consistently(session).ShouldNot(Say("OK"))
   142  				Eventually(session).Should(Say("source\\s+destination\\s+protocol\\s+ports"))
   143  				Eventually(session).Should(Say("%s\\s+%s\\s+tcp\\s+8080[^-]", appName, appName))
   144  				Eventually(session).Should(Exit(0))
   145  			})
   146  		})
   147  
   148  		Context("when policies are filtered by a source app", func() {
   149  			var srcAppName string
   150  			BeforeEach(func() {
   151  				srcAppName = helpers.PrefixedRandomName("app")
   152  				helpers.WithHelloWorldApp(func(appDir string) {
   153  					Eventually(helpers.CF("push", srcAppName, "-p", appDir, "-b", "staticfile_buildpack", "--no-start")).Should(Exit(0))
   154  				})
   155  
   156  				session := helpers.CF("add-network-policy", srcAppName, "--destination-app", appName)
   157  				Eventually(session).Should(Exit(0))
   158  			})
   159  
   160  			It("lists only policies for which the app is a source", func() {
   161  				session := helpers.CF("network-policies", "--source", srcAppName)
   162  
   163  				username, _ := helpers.GetCredentials()
   164  				Eventually(session).Should(Say(`Listing network policies of app %s in org %s / space %s as %s\.\.\.`, srcAppName, orgName, spaceName, username))
   165  				Eventually(session).Should(Say("source\\s+destination\\s+protocol\\s+ports"))
   166  				Eventually(session).ShouldNot(Say("%s\\s+%s\\s+tcp\\s+8080[^-]", appName, appName))
   167  				Eventually(session).Should(Say("%s\\s+%s\\s+tcp\\s+8080[^-]", srcAppName, appName))
   168  				Eventually(session).Should(Exit(0))
   169  			})
   170  		})
   171  
   172  		Context("when policies are filtered by a non-existent source app", func() {
   173  			It("returns an error", func() {
   174  				session := helpers.CF("network-policies", "--source", "pineapple")
   175  
   176  				username, _ := helpers.GetCredentials()
   177  				Eventually(session).Should(Say(`Listing network policies of app pineapple in org %s / space %s as %s\.\.\.`, orgName, spaceName, username))
   178  				Eventually(session.Err).Should(Say("App pineapple not found"))
   179  				Eventually(session).Should(Say("FAILED"))
   180  				Eventually(session).Should(Exit(1))
   181  			})
   182  		})
   183  	})
   184  })