github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/integration/shared/isolated/add_network_policy_command_test.go (about) 1 package isolated 2 3 import ( 4 "fmt" 5 "regexp" 6 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccversion" 8 "code.cloudfoundry.org/cli/integration/helpers" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 . "github.com/onsi/gomega/gbytes" 12 . "github.com/onsi/gomega/gexec" 13 ) 14 15 var _ = Describe("add-network-policy command", func() { 16 BeforeEach(func() { 17 helpers.SkipIfVersionLessThan(ccversion.MinVersionNetworkingV3) 18 }) 19 20 Describe("help", func() { 21 When("--help flag is set", func() { 22 It("Displays command usage to output", func() { 23 session := helpers.CF("add-network-policy", "--help") 24 Eventually(session).Should(Say("NAME:")) 25 Eventually(session).Should(Say("add-network-policy - Create policy to allow direct network traffic from one app to another")) 26 Eventually(session).Should(Say("USAGE:")) 27 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]"))) 28 Eventually(session).Should(Say("EXAMPLES:")) 29 Eventually(session).Should(Say(" cf add-network-policy frontend --destination-app backend --protocol tcp --port 8081")) 30 Eventually(session).Should(Say(" cf add-network-policy frontend --destination-app backend -s backend-space -o backend-org --protocol tcp --port 8080-8090")) 31 Eventually(session).Should(Say("OPTIONS:")) 32 Eventually(session).Should(Say(" --destination-app Name of app to connect to")) 33 Eventually(session).Should(Say(` --port Port or range of ports for connection to destination app \(Default: 8080\)`)) 34 Eventually(session).Should(Say(` --protocol Protocol to connect apps with \(Default: tcp\)`)) 35 Eventually(session).Should(Say(` -o The org of the destination app \(Default: targeted org\)`)) 36 Eventually(session).Should(Say(` -s The space of the destination app \(Default: targeted space\)`)) 37 Eventually(session).Should(Say("SEE ALSO:")) 38 Eventually(session).Should(Say(" apps, network-policies, remove-network-policy")) 39 Eventually(session).Should(Exit(0)) 40 }) 41 }) 42 }) 43 44 When("the environment is not setup correctly", func() { 45 It("fails with the appropriate errors", func() { 46 helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "add-network-policy", "some-app", "--destination-app", "some-other-app") 47 }) 48 }) 49 50 When("the org and space are properly targetted", func() { 51 var ( 52 orgName string 53 spaceName string 54 appName string 55 appGUID string 56 ) 57 58 BeforeEach(func() { 59 orgName = helpers.NewOrgName() 60 spaceName = helpers.NewSpaceName() 61 appName = helpers.PrefixedRandomName("app") 62 63 helpers.SetupCF(orgName, spaceName) 64 65 helpers.WithHelloWorldApp(func(appDir string) { 66 Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack", "--no-start")).Should(Exit(0)) 67 }) 68 69 appGUID = helpers.AppGUID(appName) 70 }) 71 72 AfterEach(func() { 73 helpers.QuickDeleteOrg(orgName) 74 }) 75 76 When("an app exists", func() { 77 It("creates a policy", func() { 78 session := helpers.CF("add-network-policy", appName, "--destination-app", appName, "--port", "8080-8090", "--protocol", "udp") 79 80 username, _ := helpers.GetCredentials() 81 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)) 82 Eventually(session).Should(Say("OK")) 83 Eventually(session).Should(Exit(0)) 84 85 session = helpers.CF("network-policies") 86 Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, orgName, spaceName, username)) 87 Consistently(session).ShouldNot(Say("OK")) 88 Eventually(session).Should(Say(`source\s+destination\s+protocol\s+ports`)) 89 Eventually(session).Should(Say(`%s\s+%s\s+udp\s+8080-8090`, appName, appName)) 90 Eventually(session).Should(Exit(0)) 91 }) 92 93 When("destination space and org are specified", func() { 94 var ( 95 sourceOrg string 96 sourceSpace string 97 sourceApp string 98 sourceAppGUID string 99 ) 100 101 BeforeEach(func() { 102 sourceOrg = helpers.NewOrgName() 103 sourceSpace = helpers.NewSpaceName() 104 sourceApp = helpers.PrefixedRandomName("sourceapp") 105 106 helpers.SetupCF(sourceOrg, sourceSpace) 107 108 helpers.WithHelloWorldApp(func(appDir string) { 109 Eventually(helpers.CF("push", sourceApp, "-p", appDir, "-b", "staticfile_buildpack", "--no-start")).Should(Exit(0)) 110 }) 111 112 sourceAppGUID = helpers.AppGUID(sourceApp) 113 }) 114 115 It("creates a policy", func() { 116 session := helpers.CF("add-network-policy", sourceApp, "--destination-app", appName, "-o", orgName, "-s", spaceName) 117 username, _ := helpers.GetCredentials() 118 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)) 119 Eventually(session).Should(Say("OK")) 120 Eventually(session).Should(Exit(0)) 121 122 session = helpers.CF("curl", fmt.Sprintf("/networking/v1/external/policies?id=%s", sourceAppGUID)) 123 Eventually(session).Should(Exit(0)) 124 Expect(string(session.Out.Contents())).To(MatchJSON(fmt.Sprintf(`{ 125 "total_policies": 1, 126 "policies": [{ 127 "source": { "id": %q }, 128 "destination": { "id": %q, "protocol": "tcp", "ports": { "start": 8080, "end": 8080 } } 129 }] 130 }`, sourceAppGUID, appGUID))) 131 }) 132 }) 133 134 When("port and protocol are not specified", func() { 135 It("creates a policy with the default values", func() { 136 session := helpers.CF("add-network-policy", appName, "--destination-app", appName) 137 138 username, _ := helpers.GetCredentials() 139 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)) 140 Eventually(session).Should(Say("OK")) 141 Eventually(session).Should(Exit(0)) 142 143 session = helpers.CF("network-policies") 144 Eventually(session).Should(Say(`Listing network policies in org %s / space %s as %s\.\.\.`, orgName, spaceName, username)) 145 Consistently(session).ShouldNot(Say("OK")) 146 Eventually(session).Should(Say(`source\s+destination\s+protocol\s+ports`)) 147 Eventually(session).Should(Say(`%s\s+%s\s+tcp\s+8080[^-]`, appName, appName)) 148 Eventually(session).Should(Exit(0)) 149 }) 150 }) 151 }) 152 153 When("the source app does not exist", func() { 154 It("returns an error", func() { 155 session := helpers.CF("add-network-policy", "pineapple", "--destination-app", appName) 156 157 username, _ := helpers.GetCredentials() 158 Eventually(session).Should(Say(`Adding network policy from app pineapple to app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username)) 159 Eventually(session.Err).Should(Say("App pineapple not found")) 160 Eventually(session).Should(Say("FAILED")) 161 Eventually(session).Should(Exit(1)) 162 }) 163 }) 164 165 When("the dest app does not exist", func() { 166 It("returns an error", func() { 167 session := helpers.CF("add-network-policy", appName, "--destination-app", "pineapple") 168 169 username, _ := helpers.GetCredentials() 170 Eventually(session).Should(Say(`Adding network policy from app %s to app pineapple in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, username)) 171 Eventually(session.Err).Should(Say("App pineapple not found")) 172 Eventually(session).Should(Say("FAILED")) 173 Eventually(session).Should(Exit(1)) 174 }) 175 }) 176 177 When("port is specified but protocol is not", func() { 178 It("returns an error", func() { 179 session := helpers.CF("add-network-policy", appName, "--destination-app", appName, "--port", "8080") 180 181 Eventually(session.Err).Should(Say("Incorrect Usage: --protocol and --port flags must be specified together")) 182 Eventually(session).Should(Say("FAILED")) 183 Eventually(session).Should(Say("USAGE:")) 184 Eventually(session).Should(Exit(1)) 185 }) 186 }) 187 188 When("protocol is specified but port is not", func() { 189 It("returns an error", func() { 190 session := helpers.CF("add-network-policy", appName, "--destination-app", appName, "--protocol", "tcp") 191 192 Eventually(session.Err).Should(Say("Incorrect Usage: --protocol and --port flags must be specified together")) 193 Eventually(session).Should(Say("FAILED")) 194 Eventually(session).Should(Say("USAGE:")) 195 Eventually(session).Should(Exit(1)) 196 }) 197 }) 198 199 When("policy to another space is specified", func() { 200 When("org is specified but space is not", func() { 201 It("returns an error", func() { 202 session := helpers.CF("add-network-policy", appName, "--destination-app", appName, "-o", "myorg") 203 204 Eventually(session.Err).Should(Say("Incorrect Usage: A space must be provided when an org is provided, but the '-s' flag was not specified.")) 205 Eventually(session).Should(Say("FAILED")) 206 Eventually(session).Should(Say("USAGE:")) 207 Eventually(session).Should(Exit(1)) 208 }) 209 }) 210 }) 211 }) 212 })