github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/integration/isolated/network_policies_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("network-policies 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("network-policies", "--help") 25 Eventually(session).Should(Say("NAME:")) 26 Eventually(session).Should(Say("network-policies - List direct network traffic policies")) 27 Eventually(session).Should(Say("USAGE:")) 28 Eventually(session).Should(Say(regexp.QuoteMeta("cf network-policies [--source SOURCE_APP]"))) 29 Eventually(session).Should(Say("OPTIONS:")) 30 Eventually(session).Should(Say(" --source Source app to filter results by")) 31 Eventually(session).Should(Say("SEE ALSO:")) 32 Eventually(session).Should(Say(" add-network-policy, apps, remove-network-policy")) 33 Eventually(session).Should(Exit(0)) 34 }) 35 }) 36 }) 37 38 Context("when the environment is not setup correctly", func() { 39 It("fails with the appropriate errors", func() { 40 helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "network-policies") 41 }) 42 43 Context("when the v3 api does not exist", func() { 44 var server *Server 45 46 BeforeEach(func() { 47 server = helpers.StartAndTargetServerWithoutV3API() 48 }) 49 50 AfterEach(func() { 51 server.Close() 52 }) 53 54 It("fails with no networking api error message", func() { 55 session := helpers.CF("network-policies") 56 Eventually(session).Should(Say("FAILED")) 57 Eventually(session.Err).Should(Say("This command requires Network Policy API V1. Your targeted endpoint does not expose it.")) 58 Eventually(session).Should(Exit(1)) 59 }) 60 }) 61 }) 62 63 Context("when the org and space are properly targetted", func() { 64 var ( 65 orgName string 66 spaceName string 67 appName string 68 ) 69 70 BeforeEach(func() { 71 orgName = helpers.NewOrgName() 72 spaceName = helpers.NewSpaceName() 73 appName = helpers.PrefixedRandomName("app") 74 75 helpers.SetupCF(orgName, spaceName) 76 77 helpers.WithHelloWorldApp(func(appDir string) { 78 Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack", "--no-start")).Should(Exit(0)) 79 }) 80 81 session := helpers.CF("add-network-policy", appName, "--destination-app", appName) 82 Eventually(session).Should(Exit(0)) 83 }) 84 85 AfterEach(func() { 86 helpers.QuickDeleteOrg(orgName) 87 }) 88 89 Context("when policies exists", func() { 90 It("lists all the policies", func() { 91 session := helpers.CF("network-policies") 92 93 username, _ := helpers.GetCredentials() 94 Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, orgName, spaceName, username)) 95 Consistently(session).ShouldNot(Say("OK")) 96 Eventually(session).Should(Say("source\\s+destination\\s+protocol\\s+ports")) 97 Eventually(session).Should(Say("%s\\s+%s\\s+tcp\\s+8080[^-]", appName, appName)) 98 Eventually(session).Should(Exit(0)) 99 }) 100 }) 101 102 Context("when policies are filtered by a source app", func() { 103 var srcAppName string 104 BeforeEach(func() { 105 srcAppName = helpers.PrefixedRandomName("app") 106 helpers.WithHelloWorldApp(func(appDir string) { 107 Eventually(helpers.CF("push", srcAppName, "-p", appDir, "-b", "staticfile_buildpack", "--no-start")).Should(Exit(0)) 108 }) 109 110 session := helpers.CF("add-network-policy", srcAppName, "--destination-app", appName) 111 Eventually(session).Should(Exit(0)) 112 }) 113 114 It("lists only policies for which the app is a source", func() { 115 session := helpers.CF("network-policies", "--source", srcAppName) 116 117 username, _ := helpers.GetCredentials() 118 Eventually(session).Should(Say(`Listing network policies of app %s in org %s / space %s as %s\.\.\.`, srcAppName, orgName, spaceName, username)) 119 Eventually(session).Should(Say("source\\s+destination\\s+protocol\\s+ports")) 120 Eventually(session).ShouldNot(Say("%s\\s+%s\\s+tcp\\s+8080[^-]", appName, appName)) 121 Eventually(session).Should(Say("%s\\s+%s\\s+tcp\\s+8080[^-]", srcAppName, appName)) 122 Eventually(session).Should(Exit(0)) 123 }) 124 }) 125 126 Context("when policies are filtered by a non-existent source app", func() { 127 It("returns an error", func() { 128 session := helpers.CF("network-policies", "--source", "pineapple") 129 130 username, _ := helpers.GetCredentials() 131 Eventually(session).Should(Say(`Listing network policies of app pineapple in org %s / space %s as %s\.\.\.`, orgName, spaceName, username)) 132 Eventually(session.Err).Should(Say("App pineapple not found")) 133 Eventually(session).Should(Say("FAILED")) 134 Eventually(session).Should(Exit(1)) 135 }) 136 }) 137 }) 138 })