github.com/loafoe/cli@v7.1.0+incompatible/command/v7/create_route_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 "fmt" 6 7 "code.cloudfoundry.org/cli/actor/actionerror" 8 "code.cloudfoundry.org/cli/actor/v7action" 9 "code.cloudfoundry.org/cli/command/commandfakes" 10 "code.cloudfoundry.org/cli/command/flag" 11 . "code.cloudfoundry.org/cli/command/v7" 12 "code.cloudfoundry.org/cli/command/v7/v7fakes" 13 "code.cloudfoundry.org/cli/resources" 14 "code.cloudfoundry.org/cli/util/configv3" 15 "code.cloudfoundry.org/cli/util/ui" 16 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 . "github.com/onsi/gomega/gbytes" 20 ) 21 22 var _ = Describe("create-route Command", func() { 23 var ( 24 cmd CreateRouteCommand 25 testUI *ui.UI 26 fakeConfig *commandfakes.FakeConfig 27 fakeSharedActor *commandfakes.FakeSharedActor 28 fakeActor *v7fakes.FakeActor 29 30 executeErr error 31 32 binaryName string 33 domainName string 34 spaceName string 35 spaceGUID string 36 orgName string 37 hostname string 38 path string 39 port int 40 ) 41 42 BeforeEach(func() { 43 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 44 fakeConfig = new(commandfakes.FakeConfig) 45 fakeSharedActor = new(commandfakes.FakeSharedActor) 46 fakeActor = new(v7fakes.FakeActor) 47 48 domainName = "example.com" 49 spaceName = "space" 50 spaceGUID = "space-guid" 51 orgName = "org" 52 hostname = "" 53 path = "" 54 port = 0 55 56 binaryName = "faceman" 57 fakeConfig.BinaryNameReturns(binaryName) 58 }) 59 60 JustBeforeEach(func() { 61 cmd = CreateRouteCommand{ 62 RequiredArgs: flag.Domain{ 63 Domain: domainName, 64 }, 65 Hostname: hostname, 66 Path: flag.V7RoutePath{Path: path}, 67 Port: port, 68 BaseCommand: BaseCommand{ 69 UI: testUI, 70 Config: fakeConfig, 71 SharedActor: fakeSharedActor, 72 Actor: fakeActor, 73 }, 74 } 75 executeErr = cmd.Execute(nil) 76 }) 77 78 When("the environment is not set up correctly", func() { 79 BeforeEach(func() { 80 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 81 }) 82 83 It("returns an error", func() { 84 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 85 86 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 87 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 88 Expect(checkTargetedOrg).To(BeTrue()) 89 Expect(checkTargetedSpace).To(BeTrue()) 90 }) 91 }) 92 93 When("the environment is setup correctly", func() { 94 BeforeEach(func() { 95 fakeConfig.CurrentUserReturns(configv3.User{Name: "the-user"}, nil) 96 fakeConfig.TargetedSpaceReturns(configv3.Space{ 97 Name: spaceName, 98 GUID: spaceGUID, 99 }) 100 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 101 Name: orgName, 102 GUID: "some-org-guid", 103 }) 104 105 fakeActor.CreateRouteReturns(resources.Route{ 106 URL: domainName, 107 }, v7action.Warnings{"warnings-1", "warnings-2"}, nil) 108 }) 109 110 It("prints text indicating it is creating a route", func() { 111 Expect(executeErr).NotTo(HaveOccurred()) 112 Expect(testUI.Out).To(Say(`Creating route %s for org %s / space %s as the-user\.\.\.`, domainName, orgName, spaceName)) 113 }) 114 115 When("passing in a hostname", func() { 116 BeforeEach(func() { 117 hostname = "flan" 118 }) 119 120 It("prints text indicating it is creating a route", func() { 121 Expect(executeErr).NotTo(HaveOccurred()) 122 Expect(testUI.Out).To(Say(`Creating route %s\.%s for org %s / space %s as the-user\.\.\.`, hostname, domainName, orgName, spaceName)) 123 }) 124 }) 125 126 When("passing in a path", func() { 127 BeforeEach(func() { 128 path = "/lion" 129 130 fakeActor.CreateRouteReturns(resources.Route{ 131 URL: domainName + path, 132 }, v7action.Warnings{"warnings-1", "warnings-2"}, nil) 133 }) 134 135 It("prints information about the created route ", func() { 136 Expect(executeErr).NotTo(HaveOccurred()) 137 Expect(testUI.Out).To(Say(`Creating route %s%s for org %s / space %s as the-user\.\.\.`, domainName, path, orgName, spaceName)) 138 Expect(testUI.Out).To(Say("Route %s%s has been created.", domainName, path)) 139 Expect(testUI.Out).To(Say("OK")) 140 }) 141 }) 142 143 When("creating the route errors", func() { 144 BeforeEach(func() { 145 fakeActor.CreateRouteReturns(resources.Route{}, v7action.Warnings{"warnings-1", "warnings-2"}, errors.New("err-create-route")) 146 }) 147 148 It("returns an error and displays warnings", func() { 149 Expect(executeErr).To(MatchError("err-create-route")) 150 Expect(testUI.Err).To(Say("warnings-1")) 151 Expect(testUI.Err).To(Say("warnings-2")) 152 }) 153 }) 154 155 When("creating the route is successful", func() { 156 BeforeEach(func() { 157 fakeActor.CreateRouteReturns(resources.Route{ 158 URL: domainName, 159 }, v7action.Warnings{"warnings-1", "warnings-2"}, nil) 160 }) 161 162 It("prints all warnings, text indicating creation completion, ok and then a tip", func() { 163 Expect(executeErr).ToNot(HaveOccurred()) 164 Expect(testUI.Err).To(Say("warnings-1")) 165 Expect(testUI.Err).To(Say("warnings-2")) 166 Expect(testUI.Out).To(Say(`Route %s has been created.`, domainName)) 167 Expect(testUI.Out).To(Say("OK")) 168 }) 169 170 It("creates the route", func() { 171 Expect(fakeActor.CreateRouteCallCount()).To(Equal(1)) 172 expectedSpaceGUID, expectedDomainName, expectedHostname, _, _ := fakeActor.CreateRouteArgsForCall(0) 173 Expect(expectedSpaceGUID).To(Equal(spaceGUID)) 174 Expect(expectedDomainName).To(Equal(domainName)) 175 Expect(expectedHostname).To(Equal(hostname)) 176 }) 177 178 When("passing in a hostname", func() { 179 BeforeEach(func() { 180 hostname = "flan" 181 182 fakeActor.CreateRouteReturns(resources.Route{ 183 URL: hostname + "." + domainName, 184 }, v7action.Warnings{"warnings-1", "warnings-2"}, nil) 185 }) 186 187 It("prints all warnings, text indicating creation completion, ok and then a tip", func() { 188 Expect(executeErr).ToNot(HaveOccurred()) 189 Expect(testUI.Err).To(Say("warnings-1")) 190 Expect(testUI.Err).To(Say("warnings-2")) 191 Expect(testUI.Out).To(Say(`Route %s\.%s has been created.`, hostname, domainName)) 192 Expect(testUI.Out).To(Say("OK")) 193 }) 194 195 It("creates the route", func() { 196 Expect(fakeActor.CreateRouteCallCount()).To(Equal(1)) 197 expectedSpaceGUID, expectedDomainName, expectedHostname, _, _ := fakeActor.CreateRouteArgsForCall(0) 198 Expect(expectedSpaceGUID).To(Equal(spaceGUID)) 199 Expect(expectedDomainName).To(Equal(domainName)) 200 Expect(expectedHostname).To(Equal(hostname)) 201 }) 202 }) 203 204 When("passing in a port", func() { 205 BeforeEach(func() { 206 port = 1234 207 208 fakeActor.CreateRouteReturns(resources.Route{ 209 URL: domainName + ":" + fmt.Sprintf("%d", port), 210 }, v7action.Warnings{"warnings-1", "warnings-2"}, nil) 211 }) 212 213 It("prints all warnings, text indicating creation completion, ok and then a tip", func() { 214 Expect(executeErr).ToNot(HaveOccurred()) 215 Expect(testUI.Err).To(Say("warnings-1")) 216 Expect(testUI.Err).To(Say("warnings-2")) 217 Expect(testUI.Out).To(Say(`Route %s:%d has been created.`, domainName, port)) 218 Expect(testUI.Out).To(Say("OK")) 219 }) 220 221 It("calls the actor with the correct arguments", func() { 222 Expect(fakeActor.CreateRouteCallCount()).To(Equal(1)) 223 expectedSpaceGUID, expectedDomainName, expectedHostname, _, expectedPort := fakeActor.CreateRouteArgsForCall(0) 224 Expect(expectedSpaceGUID).To(Equal(spaceGUID)) 225 Expect(expectedDomainName).To(Equal(domainName)) 226 Expect(expectedHostname).To(Equal(hostname)) 227 Expect(expectedPort).To(Equal(port)) 228 }) 229 }) 230 }) 231 232 When("the route already exists", func() { 233 BeforeEach(func() { 234 fakeActor.CreateRouteReturns(resources.Route{}, v7action.Warnings{"some-warning"}, actionerror.RouteAlreadyExistsError{Err: errors.New("api error for a route that already exists")}) 235 }) 236 237 It("displays all warnings, that the route already exists, and does not error", func() { 238 Expect(executeErr).ToNot(HaveOccurred()) 239 240 Expect(testUI.Err).To(Say("some-warning")) 241 Expect(testUI.Err).To(Say("api error for a route that already exists")) 242 Expect(testUI.Out).To(Say(`Creating route %s for org %s / space %s as the-user\.\.\.`, domainName, orgName, spaceName)) 243 Expect(testUI.Out).To(Say("OK")) 244 }) 245 }) 246 }) 247 })