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