github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/integration/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 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("add-network-policy 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("add-network-policy", "--help") 20 Eventually(session).Should(Say("NAME:")) 21 Eventually(session).Should(Say("add-network-policy - Create policy to allow direct network traffic from one app to another")) 22 Eventually(session).Should(Say("USAGE:")) 23 Eventually(session).Should(Say(regexp.QuoteMeta("cf add-network-policy SOURCE_APP --destination-app DESTINATION_APP [(--protocol (tcp | udp) --port RANGE)]"))) 24 Eventually(session).Should(Say("EXAMPLES:")) 25 Eventually(session).Should(Say(" cf add-network-policy frontend --destination-app backend --protocol tcp --port 8081")) 26 Eventually(session).Should(Say(" cf add-network-policy frontend --destination-app backend --protocol tcp --port 8080-8090")) 27 Eventually(session).Should(Say("OPTIONS:")) 28 Eventually(session).Should(Say(" --destination-app Name of app to connect to")) 29 Eventually(session).Should(Say(" --port Port or range of ports for connection to destination app \\(Default: 8080\\)")) 30 Eventually(session).Should(Say(" --protocol Protocol to connect apps with \\(Default: tcp\\)")) 31 Eventually(session).Should(Say("SEE ALSO:")) 32 Eventually(session).Should(Say(" apps, network-policies")) 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, "add-network-policy", "some-app", "--destination-app", "some-other-app") 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("add-network-policy", "some-app", "--destination-app", "some-other-app") 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 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 82 AfterEach(func() { 83 helpers.QuickDeleteOrg(orgName) 84 }) 85 86 Context("when an app exists", func() { 87 It("creates a policy", func() { 88 session := helpers.CF("add-network-policy", appName, "--destination-app", appName, "--port", "8080-8090", "--protocol", "udp") 89 90 username, _ := helpers.GetCredentials() 91 Eventually(session).Should(Say(`Adding network policy to app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username)) 92 Eventually(session).Should(Say("OK")) 93 Eventually(session).Should(Exit(0)) 94 95 session = helpers.CF("network-policies") 96 Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, orgName, spaceName, username)) 97 Consistently(session).ShouldNot(Say("OK")) 98 Eventually(session).Should(Say("source\\s+destination\\s+protocol\\s+ports")) 99 Eventually(session).Should(Say("%s\\s+%s\\s+udp\\s+8080-8090", appName, appName)) 100 Eventually(session).Should(Exit(0)) 101 }) 102 103 Context("when port and protocol are not specified", func() { 104 It("creates a policy with the default values", func() { 105 session := helpers.CF("add-network-policy", appName, "--destination-app", appName) 106 107 username, _ := helpers.GetCredentials() 108 Eventually(session).Should(Say(`Adding network policy to app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username)) 109 Eventually(session).Should(Say("OK")) 110 Eventually(session).Should(Exit(0)) 111 112 session = helpers.CF("network-policies") 113 Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, orgName, spaceName, username)) 114 Consistently(session).ShouldNot(Say("OK")) 115 Eventually(session).Should(Say("source\\s+destination\\s+protocol\\s+ports")) 116 Eventually(session).Should(Say("%s\\s+%s\\s+tcp\\s+8080[^-]", appName, appName)) 117 Eventually(session).Should(Exit(0)) 118 }) 119 }) 120 }) 121 122 Context("when the source app does not exist", func() { 123 It("returns an error", func() { 124 session := helpers.CF("add-network-policy", "pineapple", "--destination-app", appName) 125 126 username, _ := helpers.GetCredentials() 127 Eventually(session).Should(Say(`Adding network policy to app pineapple in org %s / space %s as %s\.\.\.`, orgName, spaceName, username)) 128 Eventually(session.Err).Should(Say("App pineapple not found")) 129 Eventually(session).Should(Say("FAILED")) 130 Eventually(session).Should(Exit(1)) 131 }) 132 }) 133 134 Context("when the dest app does not exist", func() { 135 It("returns an error", func() { 136 session := helpers.CF("add-network-policy", appName, "--destination-app", "pineapple") 137 138 username, _ := helpers.GetCredentials() 139 Eventually(session).Should(Say(`Adding network policy to app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username)) 140 Eventually(session.Err).Should(Say("App pineapple not found")) 141 Eventually(session).Should(Say("FAILED")) 142 Eventually(session).Should(Exit(1)) 143 }) 144 }) 145 146 Context("when port is specified but protocol is not", func() { 147 It("returns an error", func() { 148 session := helpers.CF("add-network-policy", appName, "--destination-app", appName, "--port", "8080") 149 150 Eventually(session.Err).Should(Say("Incorrect Usage: --protocol and --port flags must be specified together")) 151 Eventually(session).Should(Say("FAILED")) 152 Eventually(session).Should(Say("NAME:")) 153 Eventually(session).Should(Exit(1)) 154 }) 155 }) 156 157 Context("when protocol is specified but port is not", func() { 158 It("returns an error", func() { 159 session := helpers.CF("add-network-policy", appName, "--destination-app", appName, "--protocol", "tcp") 160 161 Eventually(session.Err).Should(Say("Incorrect Usage: --protocol and --port flags must be specified together")) 162 Eventually(session).Should(Say("FAILED")) 163 Eventually(session).Should(Say("NAME:")) 164 Eventually(session).Should(Exit(1)) 165 }) 166 }) 167 }) 168 })