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