github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/enable_ssh_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/actionerror"
     7  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action"
     8  	"github.com/LukasHeimann/cloudfoundrycli/v8/api/cloudcontroller/ccv3"
     9  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/commandfakes"
    10  	. "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7"
    11  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/v7fakes"
    12  	"github.com/LukasHeimann/cloudfoundrycli/v8/resources"
    13  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/configv3"
    14  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/ui"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  var _ = Describe("enable-ssh Command", func() {
    21  	var (
    22  		cmd                EnableSSHCommand
    23  		testUI             *ui.UI
    24  		fakeConfig         *commandfakes.FakeConfig
    25  		fakeSharedActor    *commandfakes.FakeSharedActor
    26  		fakeEnableSSHActor *v7fakes.FakeActor
    27  
    28  		binaryName      string
    29  		currentUserName string
    30  		executeErr      error
    31  	)
    32  
    33  	BeforeEach(func() {
    34  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    35  		fakeConfig = new(commandfakes.FakeConfig)
    36  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    37  		fakeEnableSSHActor = new(v7fakes.FakeActor)
    38  
    39  		cmd = EnableSSHCommand{
    40  			BaseCommand: BaseCommand{
    41  				UI:          testUI,
    42  				Config:      fakeConfig,
    43  				SharedActor: fakeSharedActor,
    44  				Actor:       fakeEnableSSHActor,
    45  			},
    46  		}
    47  
    48  		cmd.RequiredArgs.AppName = "some-app"
    49  
    50  		binaryName = "faceman"
    51  		fakeConfig.BinaryNameReturns(binaryName)
    52  		currentUserName = "some-user"
    53  		fakeEnableSSHActor.GetCurrentUserReturns(configv3.User{Name: currentUserName}, nil)
    54  	})
    55  
    56  	JustBeforeEach(func() {
    57  		executeErr = cmd.Execute(nil)
    58  	})
    59  
    60  	When("checking target fails", func() {
    61  		BeforeEach(func() {
    62  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    63  		})
    64  
    65  		It("returns an error", func() {
    66  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: "faceman"}))
    67  
    68  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    69  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    70  			Expect(checkTargetedOrg).To(BeTrue())
    71  			Expect(checkTargetedSpace).To(BeTrue())
    72  		})
    73  	})
    74  
    75  	When("the user is logged in", func() {
    76  		When("no errors occur", func() {
    77  			BeforeEach(func() {
    78  				fakeEnableSSHActor.GetApplicationByNameAndSpaceReturns(
    79  					resources.Application{Name: "some-app", GUID: "some-app-guid"},
    80  					v7action.Warnings{"some-get-app-warnings"},
    81  					nil,
    82  				)
    83  				fakeEnableSSHActor.GetAppFeatureReturns(
    84  					resources.ApplicationFeature{Enabled: false, Name: "ssh"},
    85  					v7action.Warnings{"some-feature-warnings"},
    86  					nil,
    87  				)
    88  				fakeEnableSSHActor.UpdateAppFeatureReturns(
    89  					v7action.Warnings{"some-update-ssh-warnings"},
    90  					nil,
    91  				)
    92  				fakeEnableSSHActor.GetSSHEnabledReturns(
    93  					ccv3.SSHEnabled{Enabled: true, Reason: ""},
    94  					v7action.Warnings{"some-get-ssh-enabled-warnings"},
    95  					nil,
    96  				)
    97  			})
    98  
    99  			It("enables ssh on the app", func() {
   100  				Expect(executeErr).ToNot(HaveOccurred())
   101  
   102  				Expect(fakeEnableSSHActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   103  
   104  				appName, spaceGUID := fakeEnableSSHActor.GetApplicationByNameAndSpaceArgsForCall(0)
   105  				Expect(appName).To(Equal(cmd.RequiredArgs.AppName))
   106  				Expect(spaceGUID).To(Equal(cmd.Config.TargetedSpace().GUID))
   107  
   108  				Expect(fakeEnableSSHActor.GetAppFeatureCallCount()).To(Equal(1))
   109  
   110  				appGUID, featureName := fakeEnableSSHActor.GetAppFeatureArgsForCall(0)
   111  				Expect(appGUID).To(Equal("some-app-guid"))
   112  				Expect(featureName).To(Equal("ssh"))
   113  
   114  				Expect(fakeEnableSSHActor.UpdateAppFeatureCallCount()).To(Equal(1))
   115  				app, enabled, featureName := fakeEnableSSHActor.UpdateAppFeatureArgsForCall(0)
   116  				Expect(app.Name).To(Equal("some-app"))
   117  				Expect(enabled).To(Equal(true))
   118  				Expect(featureName).To(Equal("ssh"))
   119  
   120  				Expect(fakeEnableSSHActor.GetSSHEnabledCallCount()).To(Equal(1))
   121  
   122  				Expect(testUI.Err).To(Say("some-get-app-warnings"))
   123  				Expect(testUI.Err).To(Say("some-feature-warnings"))
   124  				Expect(testUI.Err).To(Say("some-update-ssh-warnings"))
   125  				Expect(testUI.Err).To(Say("some-get-ssh-enabled-warnings"))
   126  				Expect(testUI.Out).To(Say(`Enabling ssh support for app %s as %s\.\.\.`, appName, currentUserName))
   127  				Expect(testUI.Out).To(Say("OK"))
   128  			})
   129  
   130  			When("SSH is disabled at a level above the app level", func() {
   131  				BeforeEach(func() {
   132  					fakeEnableSSHActor.GetSSHEnabledReturns(
   133  						ccv3.SSHEnabled{Enabled: false, Reason: "get-ssh-reason"},
   134  						v7action.Warnings{"some-get-ssh-enabled-warnings"},
   135  						nil,
   136  					)
   137  				})
   138  
   139  				It("indicates that the feature was not enabled", func() {
   140  					Expect(fakeEnableSSHActor.GetSSHEnabledCallCount()).To(Equal(1))
   141  					Expect(testUI.Out).To(Say("TIP: Ensure ssh is also enabled on the space and global level."))
   142  					Expect(executeErr).ToNot(HaveOccurred())
   143  				})
   144  			})
   145  		})
   146  
   147  		When("app ssh is already enabled", func() {
   148  			BeforeEach(func() {
   149  				fakeEnableSSHActor.UpdateAppFeatureReturns(
   150  					v7action.Warnings{"ssh support for app 'some-app' is already enabled.", "some-other-warnings"},
   151  					nil,
   152  				)
   153  				fakeEnableSSHActor.GetAppFeatureReturns(
   154  					resources.ApplicationFeature{Enabled: true, Name: "ssh"},
   155  					v7action.Warnings{},
   156  					nil,
   157  				)
   158  			})
   159  
   160  			It("shows the app ssh is already enabled", func() {
   161  				Expect(testUI.Out).To(Say("ssh support for app 'some-app' is already enabled."))
   162  				Expect(testUI.Out).To(Say("OK"))
   163  			})
   164  		})
   165  
   166  		When("an error occurs", func() {
   167  			When("GetApp action errors", func() {
   168  				When("no user is found", func() {
   169  					var returnedErr error
   170  
   171  					BeforeEach(func() {
   172  						returnedErr = actionerror.ApplicationNotFoundError{Name: "some-app"}
   173  						fakeEnableSSHActor.GetApplicationByNameAndSpaceReturns(
   174  							resources.Application{},
   175  							nil,
   176  							returnedErr)
   177  					})
   178  
   179  					It("returns the same error", func() {
   180  						Expect(executeErr).To(HaveOccurred())
   181  						Expect(executeErr).To(MatchError(returnedErr))
   182  					})
   183  				})
   184  			})
   185  
   186  			When("GetAppFeature action errors", func() {
   187  				returnedErr := errors.New("some-error")
   188  				BeforeEach(func() {
   189  					fakeEnableSSHActor.GetAppFeatureReturns(
   190  						resources.ApplicationFeature{},
   191  						nil,
   192  						returnedErr,
   193  					)
   194  				})
   195  
   196  				It("returns the same error", func() {
   197  					Expect(executeErr).To(HaveOccurred())
   198  					Expect(executeErr).To(MatchError(returnedErr))
   199  				})
   200  			})
   201  
   202  			When("Enable ssh action errors", func() {
   203  				var returnedErr error
   204  
   205  				BeforeEach(func() {
   206  					returnedErr = errors.New("some-error")
   207  					fakeEnableSSHActor.GetApplicationByNameAndSpaceReturns(
   208  						resources.Application{Name: "some-app"},
   209  						v7action.Warnings{"some-warning"},
   210  						nil,
   211  					)
   212  					fakeEnableSSHActor.UpdateAppFeatureReturns(nil, returnedErr)
   213  				})
   214  
   215  				It("returns the same error", func() {
   216  					Expect(executeErr).To(MatchError(returnedErr))
   217  				})
   218  			})
   219  		})
   220  	})
   221  })