github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v7/env_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 "code.cloudfoundry.org/cli/actor/v7action" 8 "code.cloudfoundry.org/cli/command/commandfakes" 9 . "code.cloudfoundry.org/cli/command/v7" 10 "code.cloudfoundry.org/cli/command/v7/v7fakes" 11 "code.cloudfoundry.org/cli/util/configv3" 12 "code.cloudfoundry.org/cli/util/ui" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 . "github.com/onsi/gomega/gbytes" 16 ) 17 18 var _ = Describe("env Command", func() { 19 var ( 20 cmd EnvCommand 21 testUI *ui.UI 22 fakeConfig *commandfakes.FakeConfig 23 fakeSharedActor *commandfakes.FakeSharedActor 24 fakeActor *v7fakes.FakeEnvActor 25 binaryName string 26 executeErr error 27 appName string 28 ) 29 30 BeforeEach(func() { 31 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 32 fakeConfig = new(commandfakes.FakeConfig) 33 fakeSharedActor = new(commandfakes.FakeSharedActor) 34 fakeActor = new(v7fakes.FakeEnvActor) 35 36 cmd = EnvCommand{ 37 UI: testUI, 38 Config: fakeConfig, 39 SharedActor: fakeSharedActor, 40 Actor: fakeActor, 41 } 42 43 binaryName = "faceman" 44 fakeConfig.BinaryNameReturns(binaryName) 45 appName = "some-app" 46 47 cmd.RequiredArgs.AppName = appName 48 }) 49 50 JustBeforeEach(func() { 51 executeErr = cmd.Execute(nil) 52 }) 53 54 When("checking target fails", func() { 55 BeforeEach(func() { 56 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 57 }) 58 59 It("returns an error", func() { 60 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 61 62 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 63 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 64 Expect(checkTargetedOrg).To(BeTrue()) 65 Expect(checkTargetedSpace).To(BeTrue()) 66 }) 67 }) 68 69 When("the user is logged in, an org is targeted and a space is targeted", func() { 70 BeforeEach(func() { 71 fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"}) 72 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"}) 73 }) 74 75 When("getting the current user returns an error", func() { 76 BeforeEach(func() { 77 fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("some-error")) 78 }) 79 80 It("returns the error", func() { 81 Expect(executeErr).To(MatchError("some-error")) 82 }) 83 }) 84 85 When("getting the current user succeeds", func() { 86 BeforeEach(func() { 87 fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil) 88 }) 89 90 When("getting the environment returns env vars for all groups", func() { 91 BeforeEach(func() { 92 envGroups := v7action.EnvironmentVariableGroups{ 93 System: map[string]interface{}{"system-name": map[string]interface{}{"mysql": []string{"system-value"}}}, 94 Application: map[string]interface{}{"application-name": "application-value"}, 95 EnvironmentVariables: map[string]interface{}{"user-name": "user-value"}, 96 Running: map[string]interface{}{"running-name": "running-value"}, 97 Staging: map[string]interface{}{"staging-name": "staging-value"}, 98 } 99 fakeActor.GetEnvironmentVariablesByApplicationNameAndSpaceReturns(envGroups, v7action.Warnings{"get-warning-1", "get-warning-2"}, nil) 100 }) 101 102 It("displays the environment variable and value pair", func() { 103 Expect(executeErr).ToNot(HaveOccurred()) 104 105 Expect(testUI.Out).To(Say(`Getting env variables for app some-app in org some-org / space some-space as banana\.\.\.`)) 106 Expect(testUI.Out).To(Say("OK")) 107 Expect(testUI.Out).To(Say("System-Provided:")) 108 Expect(testUI.Out).To(Say("system-name: {")) 109 Expect(testUI.Out).To(Say(`"mysql": \[`)) 110 Expect(testUI.Out).To(Say(`"system-value"`)) 111 Expect(testUI.Out).To(Say(`\]`)) 112 Expect(testUI.Out).To(Say("}")) 113 Expect(testUI.Out).To(Say(`application-name: "application-value"`)) 114 115 Expect(testUI.Out).To(Say("User-Provided:")) 116 Expect(testUI.Out).To(Say(`user-name: user-value`)) 117 118 Expect(testUI.Out).To(Say("Running Environment Variable Groups:")) 119 Expect(testUI.Out).To(Say(`running-name: running-value`)) 120 121 Expect(testUI.Out).To(Say("Staging Environment Variable Groups:")) 122 Expect(testUI.Out).To(Say(`staging-name: staging-value`)) 123 124 Expect(testUI.Err).To(Say("get-warning-1")) 125 Expect(testUI.Err).To(Say("get-warning-2")) 126 127 Expect(fakeActor.GetEnvironmentVariablesByApplicationNameAndSpaceCallCount()).To(Equal(1)) 128 appName, spaceGUID := fakeActor.GetEnvironmentVariablesByApplicationNameAndSpaceArgsForCall(0) 129 Expect(appName).To(Equal("some-app")) 130 Expect(spaceGUID).To(Equal("some-space-guid")) 131 }) 132 133 Describe("sorting of non-json environment variables", func() { 134 BeforeEach(func() { 135 envGroups := v7action.EnvironmentVariableGroups{ 136 System: map[string]interface{}{}, 137 Application: map[string]interface{}{}, 138 EnvironmentVariables: map[string]interface{}{ 139 "alpha": "1", 140 "charlie": "1", 141 "bravo": "1", 142 }, 143 Running: map[string]interface{}{ 144 "foxtrot": "1", 145 "delta": "1", 146 "echo": "1", 147 }, 148 Staging: map[string]interface{}{ 149 "hotel": "1", 150 "india": "1", 151 "golf": "1", 152 }, 153 } 154 fakeActor.GetEnvironmentVariablesByApplicationNameAndSpaceReturns(envGroups, v7action.Warnings{"get-warning-1", "get-warning-2"}, nil) 155 }) 156 157 It("sorts the EnvironmentVariables alphabetically", func() { 158 Expect(executeErr).ToNot(HaveOccurred()) 159 160 Expect(testUI.Out).To(Say(`alpha: 1`)) 161 Expect(testUI.Out).To(Say(`bravo: 1`)) 162 Expect(testUI.Out).To(Say(`charlie: 1`)) 163 }) 164 165 It("sorts the Running alphabetically", func() { 166 Expect(executeErr).ToNot(HaveOccurred()) 167 168 Expect(testUI.Out).To(Say(`delta: 1`)) 169 Expect(testUI.Out).To(Say(`echo: 1`)) 170 Expect(testUI.Out).To(Say(`foxtrot: 1`)) 171 }) 172 173 It("sorts the Staging alphabetically", func() { 174 Expect(executeErr).ToNot(HaveOccurred()) 175 176 Expect(testUI.Out).To(Say(`golf: 1`)) 177 Expect(testUI.Out).To(Say(`hotel: 1`)) 178 Expect(testUI.Out).To(Say(`india: 1`)) 179 }) 180 }) 181 }) 182 183 When("getting the environment returns empty env vars for all groups", func() { 184 BeforeEach(func() { 185 envGroups := v7action.EnvironmentVariableGroups{ 186 System: map[string]interface{}{}, 187 Application: map[string]interface{}{}, 188 EnvironmentVariables: map[string]interface{}{}, 189 Running: map[string]interface{}{}, 190 Staging: map[string]interface{}{}, 191 } 192 fakeActor.GetEnvironmentVariablesByApplicationNameAndSpaceReturns(envGroups, v7action.Warnings{"get-warning-1", "get-warning-2"}, nil) 193 }) 194 195 It("displays helpful messages", func() { 196 Expect(executeErr).ToNot(HaveOccurred()) 197 198 Expect(testUI.Out).To(Say(`Getting env variables for app some-app in org some-org / space some-space as banana\.\.\.`)) 199 Expect(testUI.Out).To(Say("OK")) 200 201 Expect(testUI.Out).To(Say("No system-provided env variables have been set")) 202 203 Expect(testUI.Out).To(Say("No user-provided env variables have been set")) 204 205 Expect(testUI.Out).To(Say("No running env variables have been set")) 206 207 Expect(testUI.Out).To(Say("No staging env variables have been set")) 208 209 Expect(testUI.Err).To(Say("get-warning-1")) 210 Expect(testUI.Err).To(Say("get-warning-2")) 211 212 Expect(fakeActor.GetEnvironmentVariablesByApplicationNameAndSpaceCallCount()).To(Equal(1)) 213 appName, spaceGUID := fakeActor.GetEnvironmentVariablesByApplicationNameAndSpaceArgsForCall(0) 214 Expect(appName).To(Equal("some-app")) 215 Expect(spaceGUID).To(Equal("some-space-guid")) 216 }) 217 }) 218 219 When("the get environment variables returns an unknown error", func() { 220 var expectedErr error 221 BeforeEach(func() { 222 expectedErr = errors.New("some-error") 223 fakeActor.GetEnvironmentVariablesByApplicationNameAndSpaceReturns(v7action.EnvironmentVariableGroups{}, v7action.Warnings{"get-warning-1", "get-warning-2"}, expectedErr) 224 }) 225 226 It("returns the error", func() { 227 Expect(executeErr).To(Equal(expectedErr)) 228 Expect(testUI.Out).To(Say(`Getting env variables for app some-app in org some-org / space some-space as banana\.\.\.`)) 229 Expect(testUI.Out).To(Say("OK")) 230 231 Expect(testUI.Err).To(Say("get-warning-1")) 232 Expect(testUI.Err).To(Say("get-warning-2")) 233 }) 234 }) 235 }) 236 }) 237 })