github.com/cloudfoundry/cli@v7.1.0+incompatible/actor/v3action/ssh_test.go (about)

     1  package v3action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	. "code.cloudfoundry.org/cli/actor/v3action"
     8  	"code.cloudfoundry.org/cli/actor/v3action/v3actionfakes"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    11  	"code.cloudfoundry.org/cli/resources"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("SSH Actions", func() {
    17  	var (
    18  		actor                     *Actor
    19  		fakeCloudControllerClient *v3actionfakes.FakeCloudControllerClient
    20  		fakeConfig                *v3actionfakes.FakeConfig
    21  		fakeSharedActor           *v3actionfakes.FakeSharedActor
    22  		fakeUAAClient             *v3actionfakes.FakeUAAClient
    23  		executeErr                error
    24  		warnings                  Warnings
    25  	)
    26  
    27  	BeforeEach(func() {
    28  		fakeCloudControllerClient = new(v3actionfakes.FakeCloudControllerClient)
    29  		fakeConfig = new(v3actionfakes.FakeConfig)
    30  		fakeSharedActor = new(v3actionfakes.FakeSharedActor)
    31  		fakeUAAClient = new(v3actionfakes.FakeUAAClient)
    32  		actor = NewActor(fakeCloudControllerClient, fakeConfig, fakeSharedActor, fakeUAAClient)
    33  	})
    34  
    35  	Describe("GetSecureShellConfigurationByApplicationNameSpaceProcessTypeAndIndex", func() {
    36  		var sshAuth SSHAuthentication
    37  
    38  		BeforeEach(func() {
    39  			fakeConfig.AccessTokenReturns("some-access-token")
    40  			fakeConfig.SSHOAuthClientReturns("some-access-oauth-client")
    41  		})
    42  
    43  		JustBeforeEach(func() {
    44  			sshAuth, warnings, executeErr = actor.GetSecureShellConfigurationByApplicationNameSpaceProcessTypeAndIndex("some-app", "some-space-guid", "some-process-type", 0)
    45  		})
    46  
    47  		When("the app ssh endpoint is empty", func() {
    48  			BeforeEach(func() {
    49  				fakeCloudControllerClient.AppSSHEndpointReturns("")
    50  			})
    51  			It("creates an ssh-endpoint-not-set error", func() {
    52  				Expect(executeErr).To(MatchError("SSH endpoint not set"))
    53  			})
    54  		})
    55  
    56  		When("the app ssh hostkey fingerprint is empty", func() {
    57  			BeforeEach(func() {
    58  				fakeCloudControllerClient.AppSSHEndpointReturns("some-app-ssh-endpoint")
    59  				fakeCloudControllerClient.AppSSHHostKeyFingerprintReturns("")
    60  			})
    61  			It("creates an ssh-hostkey-fingerprint-not-set error", func() {
    62  				Expect(executeErr).To(MatchError("SSH hostkey fingerprint not set"))
    63  			})
    64  		})
    65  
    66  		When("ssh endpoint and fingerprint are set", func() {
    67  			BeforeEach(func() {
    68  				fakeCloudControllerClient.AppSSHEndpointReturns("some-app-ssh-endpoint")
    69  				fakeCloudControllerClient.AppSSHHostKeyFingerprintReturns("some-app-ssh-fingerprint")
    70  			})
    71  
    72  			It("looks up the passcode with the config credentials", func() {
    73  				Expect(fakeUAAClient.GetSSHPasscodeCallCount()).To(Equal(1))
    74  				accessTokenArg, oathClientArg := fakeUAAClient.GetSSHPasscodeArgsForCall(0)
    75  				Expect(accessTokenArg).To(Equal("some-access-token"))
    76  				Expect(oathClientArg).To(Equal("some-access-oauth-client"))
    77  			})
    78  
    79  			When("getting the ssh passcode errors", func() {
    80  				BeforeEach(func() {
    81  					fakeUAAClient.GetSSHPasscodeReturns("", errors.New("some-ssh-passcode-error"))
    82  				})
    83  
    84  				It("returns the error", func() {
    85  					Expect(executeErr).To(MatchError("some-ssh-passcode-error"))
    86  				})
    87  			})
    88  
    89  			When("getting the ssh passcode succeeds", func() {
    90  				BeforeEach(func() {
    91  					fakeUAAClient.GetSSHPasscodeReturns("some-ssh-passcode", nil)
    92  				})
    93  
    94  				When("getting the application summary errors", func() {
    95  					BeforeEach(func() {
    96  						fakeCloudControllerClient.GetApplicationsReturns(nil, ccv3.Warnings{"some-app-warnings"}, errors.New("some-application-summary-error"))
    97  					})
    98  
    99  					It("returns all warnings and the error", func() {
   100  						Expect(executeErr).To(MatchError("some-application-summary-error"))
   101  						Expect(warnings).To(ConsistOf("some-app-warnings"))
   102  					})
   103  				})
   104  
   105  				When("getting the application summary succeeds", func() {
   106  					BeforeEach(func() {
   107  						fakeCloudControllerClient.GetApplicationsReturns([]resources.Application{{Name: "some-app"}}, ccv3.Warnings{"some-app-warnings"}, nil)
   108  					})
   109  
   110  					When("the process does not exist", func() {
   111  						It("returns all warnings and the error", func() {
   112  							Expect(executeErr).To(MatchError(actionerror.ProcessNotFoundError{ProcessType: "some-process-type"}))
   113  							Expect(warnings).To(ConsistOf("some-app-warnings"))
   114  						})
   115  					})
   116  
   117  					When("the application is not in the STARTED state", func() {
   118  						BeforeEach(func() {
   119  							fakeCloudControllerClient.GetApplicationProcessesReturns([]ccv3.Process{{Type: "some-process-type", GUID: "some-process-guid"}}, ccv3.Warnings{"some-process-warnings"}, nil)
   120  						})
   121  
   122  						It("returns a ApplicationNotStartedError", func() {
   123  							Expect(executeErr).To(MatchError(actionerror.ApplicationNotStartedError{Name: "some-app"}))
   124  							Expect(warnings).To(ConsistOf("some-app-warnings", "some-process-warnings"))
   125  						})
   126  					})
   127  
   128  					When("the process doesn't have the specified instance index", func() {
   129  						BeforeEach(func() {
   130  							fakeCloudControllerClient.GetApplicationsReturns([]resources.Application{{Name: "some-app", State: constant.ApplicationStarted}}, ccv3.Warnings{"some-app-warnings"}, nil)
   131  							fakeCloudControllerClient.GetApplicationProcessesReturns([]ccv3.Process{{Type: "some-process-type", GUID: "some-process-guid"}}, ccv3.Warnings{"some-process-warnings"}, nil)
   132  						})
   133  
   134  						It("returns a ProcessIndexNotFoundError", func() {
   135  							Expect(executeErr).To(MatchError(actionerror.ProcessInstanceNotFoundError{ProcessType: "some-process-type", InstanceIndex: 0}))
   136  						})
   137  					})
   138  
   139  					When("the process instance is not RUNNING", func() {
   140  						BeforeEach(func() {
   141  							fakeCloudControllerClient.GetApplicationsReturns([]resources.Application{{Name: "some-app", State: constant.ApplicationStarted}}, ccv3.Warnings{"some-app-warnings"}, nil)
   142  							fakeCloudControllerClient.GetApplicationProcessesReturns([]ccv3.Process{{Type: "some-process-type", GUID: "some-process-guid"}}, ccv3.Warnings{"some-process-warnings"}, nil)
   143  							fakeCloudControllerClient.GetProcessInstancesReturns([]ccv3.ProcessInstance{{State: constant.ProcessInstanceDown, Index: 0}}, ccv3.Warnings{"some-instance-warnings"}, nil)
   144  						})
   145  						It("returns a ProcessInstanceNotRunningError", func() {
   146  							Expect(executeErr).To(MatchError(actionerror.ProcessInstanceNotRunningError{ProcessType: "some-process-type", InstanceIndex: 0}))
   147  						})
   148  					})
   149  
   150  					When("the specified process and index exist, app is STARTED and the instance is RUNNING", func() {
   151  						BeforeEach(func() {
   152  							fakeCloudControllerClient.GetApplicationsReturns([]resources.Application{{Name: "some-app", State: constant.ApplicationStarted}}, ccv3.Warnings{"some-app-warnings"}, nil)
   153  							fakeCloudControllerClient.GetApplicationProcessesReturns([]ccv3.Process{{Type: "some-process-type", GUID: "some-process-guid"}}, ccv3.Warnings{"some-process-warnings"}, nil)
   154  							fakeCloudControllerClient.GetProcessInstancesReturns([]ccv3.ProcessInstance{{State: constant.ProcessInstanceRunning, Index: 0}}, ccv3.Warnings{"some-instance-warnings"}, nil)
   155  						})
   156  
   157  						When("starting the secure session succeeds", func() {
   158  							It("returns all warnings", func() {
   159  								Expect(executeErr).ToNot(HaveOccurred())
   160  								Expect(warnings).To(ConsistOf("some-app-warnings", "some-process-warnings", "some-instance-warnings"))
   161  
   162  								Expect(sshAuth).To(Equal(SSHAuthentication{
   163  									Endpoint:           "some-app-ssh-endpoint",
   164  									HostKeyFingerprint: "some-app-ssh-fingerprint",
   165  									Passcode:           "some-ssh-passcode",
   166  									Username:           "cf:some-process-guid/0",
   167  								}))
   168  
   169  								Expect(fakeCloudControllerClient.GetApplicationsCallCount()).To(Equal(1))
   170  								Expect(fakeCloudControllerClient.GetApplicationsArgsForCall(0)).To(ConsistOf(
   171  									ccv3.Query{Key: ccv3.NameFilter, Values: []string{"some-app"}},
   172  									ccv3.Query{Key: ccv3.SpaceGUIDFilter, Values: []string{"some-space-guid"}},
   173  								))
   174  							})
   175  						})
   176  					})
   177  				})
   178  			})
   179  		})
   180  	})
   181  })