github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v7/set_running_environment_variable_group_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     8  
     9  	"code.cloudfoundry.org/cli/actor/actionerror"
    10  	"code.cloudfoundry.org/cli/actor/v7action"
    11  	"code.cloudfoundry.org/cli/command/commandfakes"
    12  	. "code.cloudfoundry.org/cli/command/v7"
    13  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    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("set-running-environment-variable-group Command", func() {
    23  	var (
    24  		cmd             SetRunningEnvironmentVariableGroupCommand
    25  		testUI          *ui.UI
    26  		fakeConfig      *commandfakes.FakeConfig
    27  		fakeSharedActor *commandfakes.FakeSharedActor
    28  		fakeActor       *v7fakes.FakeSetRunningEnvironmentVariableGroupActor
    29  		executeErr      error
    30  		binaryName      string
    31  	)
    32  
    33  	BeforeEach(func() {
    34  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    35  		fakeConfig = new(commandfakes.FakeConfig)
    36  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    37  		fakeActor = new(v7fakes.FakeSetRunningEnvironmentVariableGroupActor)
    38  
    39  		cmd = SetRunningEnvironmentVariableGroupCommand{
    40  			UI:          testUI,
    41  			Config:      fakeConfig,
    42  			SharedActor: fakeSharedActor,
    43  			Actor:       fakeActor,
    44  		}
    45  
    46  		cmd.RequiredArgs.EnvVarGroupJson = `{"key1":"val1", "key2":"val2"}`
    47  
    48  		binaryName = "faceman"
    49  		fakeConfig.BinaryNameReturns(binaryName)
    50  	})
    51  
    52  	JustBeforeEach(func() {
    53  		executeErr = cmd.Execute(nil)
    54  	})
    55  
    56  	When("the environment is not set up correctly", 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(BeFalse())
    67  			Expect(checkTargetedSpace).To(BeFalse())
    68  		})
    69  	})
    70  
    71  	When("the environment is setup correctly", func() {
    72  		BeforeEach(func() {
    73  			fakeConfig.CurrentUserReturns(configv3.User{Name: "apple"}, nil)
    74  		})
    75  
    76  		It("should print text indicating its running", func() {
    77  			Expect(executeErr).NotTo(HaveOccurred())
    78  			Expect(testUI.Out).To(Say(`Setting the contents of the running environment variable group as apple\.\.\.`))
    79  		})
    80  
    81  		When("jsonUnmarshalling fails", func() {
    82  			BeforeEach(func() {
    83  				cmd.RequiredArgs.EnvVarGroupJson = "bad json"
    84  			})
    85  
    86  			It("should err", func() {
    87  				Expect(executeErr).To(MatchError("Invalid environment variable group provided. Please provide a valid JSON object."))
    88  			})
    89  		})
    90  
    91  		When("setting the environment variables fails", func() {
    92  			BeforeEach(func() {
    93  				fakeActor.SetEnvironmentVariableGroupReturns(
    94  					v7action.Warnings{"some-warning-1", "some-warning-2"},
    95  					errors.New("some-error"),
    96  				)
    97  			})
    98  
    99  			It("prints warnings and returns error", func() {
   100  				Expect(executeErr).To(MatchError("some-error"))
   101  
   102  				Expect(testUI.Err).To(Say("some-warning-1"))
   103  				Expect(testUI.Err).To(Say("some-warning-2"))
   104  			})
   105  		})
   106  
   107  		When("setting the environment variables succeeds", func() {
   108  			BeforeEach(func() {
   109  				fakeActor.SetEnvironmentVariableGroupReturns(
   110  					v7action.Warnings{"some-warning-1", "some-warning-2"},
   111  					nil,
   112  				)
   113  			})
   114  
   115  			It("should print text indicating its set", func() {
   116  				Expect(fakeActor.SetEnvironmentVariableGroupCallCount()).To(Equal(1))
   117  				group, envVars := fakeActor.SetEnvironmentVariableGroupArgsForCall(0)
   118  				Expect(group).To(Equal(constant.RunningEnvironmentVariableGroup))
   119  				Expect(envVars).To(Equal(ccv3.EnvironmentVariables{
   120  					"key1": {Value: "val1", IsSet: true},
   121  					"key2": {Value: "val2", IsSet: true},
   122  				}))
   123  				Expect(executeErr).NotTo(HaveOccurred())
   124  				Expect(testUI.Out).To(Say("OK"))
   125  			})
   126  		})
   127  	})
   128  })