github.com/sleungcy/cli@v7.1.0+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.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  				fakeConfig.CurrentUserReturns(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  				fakeConfig.CurrentUserReturns(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"}}},
    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("}"))
   114  					Expect(testUI.Out).To(Say(`application-name: "application-value"`))
   115  
   116  					Expect(testUI.Out).To(Say("User-Provided:"))
   117  					Expect(testUI.Out).To(Say(`user-name: user-value`))
   118  
   119  					Expect(testUI.Out).To(Say("Running Environment Variable Groups:"))
   120  					Expect(testUI.Out).To(Say(`running-name: running-value`))
   121  
   122  					Expect(testUI.Out).To(Say("Staging Environment Variable Groups:"))
   123  					Expect(testUI.Out).To(Say(`staging-name: staging-value`))
   124  
   125  					Expect(testUI.Err).To(Say("get-warning-1"))
   126  					Expect(testUI.Err).To(Say("get-warning-2"))
   127  
   128  					Expect(fakeActor.GetEnvironmentVariablesByApplicationNameAndSpaceCallCount()).To(Equal(1))
   129  					appName, spaceGUID := fakeActor.GetEnvironmentVariablesByApplicationNameAndSpaceArgsForCall(0)
   130  					Expect(appName).To(Equal("some-app"))
   131  					Expect(spaceGUID).To(Equal("some-space-guid"))
   132  				})
   133  
   134  				Describe("sorting of non-json environment variables", func() {
   135  					BeforeEach(func() {
   136  						envGroups := v7action.EnvironmentVariableGroups{
   137  							System:      map[string]interface{}{},
   138  							Application: map[string]interface{}{},
   139  							EnvironmentVariables: map[string]interface{}{
   140  								"alpha":   "1",
   141  								"charlie": "1",
   142  								"bravo":   "1",
   143  							},
   144  							Running: map[string]interface{}{
   145  								"foxtrot": "1",
   146  								"delta":   "1",
   147  								"echo":    "1",
   148  							},
   149  							Staging: map[string]interface{}{
   150  								"hotel": "1",
   151  								"india": "1",
   152  								"golf":  "1",
   153  							},
   154  						}
   155  						fakeActor.GetEnvironmentVariablesByApplicationNameAndSpaceReturns(envGroups, v7action.Warnings{"get-warning-1", "get-warning-2"}, nil)
   156  					})
   157  
   158  					It("sorts the EnvironmentVariables alphabetically", func() {
   159  						Expect(executeErr).ToNot(HaveOccurred())
   160  
   161  						Expect(testUI.Out).To(Say(`alpha: 1`))
   162  						Expect(testUI.Out).To(Say(`bravo: 1`))
   163  						Expect(testUI.Out).To(Say(`charlie: 1`))
   164  					})
   165  
   166  					It("sorts the Running alphabetically", func() {
   167  						Expect(executeErr).ToNot(HaveOccurred())
   168  
   169  						Expect(testUI.Out).To(Say(`delta: 1`))
   170  						Expect(testUI.Out).To(Say(`echo: 1`))
   171  						Expect(testUI.Out).To(Say(`foxtrot: 1`))
   172  					})
   173  
   174  					It("sorts the Staging alphabetically", func() {
   175  						Expect(executeErr).ToNot(HaveOccurred())
   176  
   177  						Expect(testUI.Out).To(Say(`golf: 1`))
   178  						Expect(testUI.Out).To(Say(`hotel: 1`))
   179  						Expect(testUI.Out).To(Say(`india: 1`))
   180  					})
   181  				})
   182  			})
   183  
   184  			When("getting the environment returns empty env vars for all groups", func() {
   185  				BeforeEach(func() {
   186  					envGroups := v7action.EnvironmentVariableGroups{
   187  						System:               map[string]interface{}{},
   188  						Application:          map[string]interface{}{},
   189  						EnvironmentVariables: map[string]interface{}{},
   190  						Running:              map[string]interface{}{},
   191  						Staging:              map[string]interface{}{},
   192  					}
   193  					fakeActor.GetEnvironmentVariablesByApplicationNameAndSpaceReturns(envGroups, v7action.Warnings{"get-warning-1", "get-warning-2"}, nil)
   194  				})
   195  
   196  				It("displays helpful messages", func() {
   197  					Expect(executeErr).ToNot(HaveOccurred())
   198  
   199  					Expect(testUI.Out).To(Say(`Getting env variables for app some-app in org some-org / space some-space as banana\.\.\.`))
   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  
   230  					Expect(testUI.Err).To(Say("get-warning-1"))
   231  					Expect(testUI.Err).To(Say("get-warning-2"))
   232  				})
   233  			})
   234  		})
   235  	})
   236  })