github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/command/v7/enable_ssh_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/api/cloudcontroller/ccv3"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	. "code.cloudfoundry.org/cli/command/v7"
    11  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    12  	"code.cloudfoundry.org/cli/resources"
    13  	"code.cloudfoundry.org/cli/util/configv3"
    14  	"code.cloudfoundry.org/cli/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  				Expect(testUI.Out).To(Say("TIP: An app restart is required for the change to take effect."))
   129  			})
   130  
   131  			When("SSH is disabled at a level above the app level", func() {
   132  				BeforeEach(func() {
   133  					fakeEnableSSHActor.GetSSHEnabledReturns(
   134  						ccv3.SSHEnabled{Enabled: false, Reason: "get-ssh-reason"},
   135  						v7action.Warnings{"some-get-ssh-enabled-warnings"},
   136  						nil,
   137  					)
   138  				})
   139  
   140  				It("indicates that the feature was not enabled", func() {
   141  					Expect(fakeEnableSSHActor.GetSSHEnabledCallCount()).To(Equal(1))
   142  					Expect(testUI.Out).To(Say("TIP: Ensure ssh is also enabled on the space and global level."))
   143  					Expect(executeErr).ToNot(HaveOccurred())
   144  				})
   145  			})
   146  		})
   147  
   148  		When("app ssh is already enabled", func() {
   149  			BeforeEach(func() {
   150  				fakeEnableSSHActor.UpdateAppFeatureReturns(
   151  					v7action.Warnings{"ssh support for app 'some-app' is already enabled.", "some-other-warnings"},
   152  					nil,
   153  				)
   154  				fakeEnableSSHActor.GetAppFeatureReturns(
   155  					resources.ApplicationFeature{Enabled: true, Name: "ssh"},
   156  					v7action.Warnings{},
   157  					nil,
   158  				)
   159  			})
   160  
   161  			It("shows the app ssh is already enabled", func() {
   162  				Expect(testUI.Out).To(Say("ssh support for app 'some-app' is already enabled."))
   163  				Expect(testUI.Out).To(Say("OK"))
   164  				Expect(testUI.Out).To(Say("TIP: An app restart may be required for the change to take effect."))
   165  			})
   166  		})
   167  
   168  		When("an error occurs", func() {
   169  			When("GetApp action errors", func() {
   170  				When("no user is found", func() {
   171  					var returnedErr error
   172  
   173  					BeforeEach(func() {
   174  						returnedErr = actionerror.ApplicationNotFoundError{Name: "some-app"}
   175  						fakeEnableSSHActor.GetApplicationByNameAndSpaceReturns(
   176  							resources.Application{},
   177  							nil,
   178  							returnedErr)
   179  					})
   180  
   181  					It("returns the same error", func() {
   182  						Expect(executeErr).To(HaveOccurred())
   183  						Expect(executeErr).To(MatchError(returnedErr))
   184  					})
   185  				})
   186  			})
   187  
   188  			When("GetAppFeature action errors", func() {
   189  				returnedErr := errors.New("some-error")
   190  				BeforeEach(func() {
   191  					fakeEnableSSHActor.GetAppFeatureReturns(
   192  						resources.ApplicationFeature{},
   193  						nil,
   194  						returnedErr,
   195  					)
   196  				})
   197  
   198  				It("returns the same error", func() {
   199  					Expect(executeErr).To(HaveOccurred())
   200  					Expect(executeErr).To(MatchError(returnedErr))
   201  				})
   202  			})
   203  
   204  			When("Enable ssh action errors", func() {
   205  				var returnedErr error
   206  
   207  				BeforeEach(func() {
   208  					returnedErr = errors.New("some-error")
   209  					fakeEnableSSHActor.GetApplicationByNameAndSpaceReturns(
   210  						resources.Application{Name: "some-app"},
   211  						v7action.Warnings{"some-warning"},
   212  						nil,
   213  					)
   214  					fakeEnableSSHActor.UpdateAppFeatureReturns(nil, returnedErr)
   215  				})
   216  
   217  				It("returns the same error", func() {
   218  					Expect(executeErr).To(MatchError(returnedErr))
   219  				})
   220  			})
   221  		})
   222  	})
   223  })