github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/actor/v7action/ssh_test.go (about)

     1  package v7action_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/actor/v7action/v7actionfakes"
     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 *v7actionfakes.FakeCloudControllerClient
    19  		fakeConfig                *v7actionfakes.FakeConfig
    20  		fakeSharedActor           *v7actionfakes.FakeSharedActor
    21  		fakeUAAClient             *v7actionfakes.FakeUAAClient
    22  		executeErr                error
    23  		warnings                  Warnings
    24  	)
    25  
    26  	BeforeEach(func() {
    27  		fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
    28  		fakeConfig = new(v7actionfakes.FakeConfig)
    29  		fakeSharedActor = new(v7actionfakes.FakeSharedActor)
    30  		fakeUAAClient = new(v7actionfakes.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 errors", func() {
    94  					BeforeEach(func() {
    95  						fakeCloudControllerClient.GetApplicationsReturns(nil, ccv3.Warnings{"some-app-warnings"}, errors.New("some-application-error"))
    96  					})
    97  
    98  					It("returns all warnings and the error", func() {
    99  						Expect(executeErr).To(MatchError("some-application-error"))
   100  						Expect(warnings).To(ConsistOf("some-app-warnings"))
   101  					})
   102  				})
   103  
   104  				When("getting the application succeeds with a started application", func() {
   105  					BeforeEach(func() {
   106  						fakeCloudControllerClient.GetApplicationsReturns(
   107  							[]ccv3.Application{
   108  								{Name: "some-app", State: constant.ApplicationStarted},
   109  							},
   110  							ccv3.Warnings{"some-app-warnings"},
   111  							nil)
   112  					})
   113  
   114  					When("getting the process summaries fails", func() {
   115  						BeforeEach(func() {
   116  							fakeCloudControllerClient.GetApplicationProcessesReturns(nil, ccv3.Warnings{"some-process-warnings"}, errors.New("some-process-error"))
   117  						})
   118  
   119  						It("returns all warnings and the error", func() {
   120  							Expect(executeErr).To(MatchError("some-process-error"))
   121  							Expect(warnings).To(ConsistOf("some-app-warnings", "some-process-warnings"))
   122  						})
   123  					})
   124  
   125  					When("getting the process summaries succeeds", func() {
   126  						When("the process does not exist", func() {
   127  							BeforeEach(func() {
   128  								fakeCloudControllerClient.GetApplicationProcessesReturns([]ccv3.Process{}, ccv3.Warnings{"some-process-warnings"}, nil)
   129  							})
   130  
   131  							It("returns all warnings and the error", func() {
   132  								Expect(executeErr).To(MatchError(actionerror.ProcessNotFoundError{ProcessType: "some-process-type"}))
   133  								Expect(warnings).To(ConsistOf("some-app-warnings", "some-process-warnings"))
   134  							})
   135  						})
   136  
   137  						When("the process doesn't have the specified instance index", func() {
   138  							BeforeEach(func() {
   139  								fakeCloudControllerClient.GetApplicationsReturns([]ccv3.Application{{Name: "some-app", State: constant.ApplicationStarted}}, ccv3.Warnings{"some-app-warnings"}, nil)
   140  								fakeCloudControllerClient.GetApplicationProcessesReturns([]ccv3.Process{{Type: "some-process-type", GUID: "some-process-guid"}}, ccv3.Warnings{"some-process-warnings"}, nil)
   141  							})
   142  
   143  							It("returns a ProcessIndexNotFoundError", func() {
   144  								Expect(executeErr).To(MatchError(actionerror.ProcessInstanceNotFoundError{ProcessType: "some-process-type", InstanceIndex: 0}))
   145  							})
   146  						})
   147  
   148  						When("the process instance is not RUNNING", func() {
   149  							BeforeEach(func() {
   150  								fakeCloudControllerClient.GetApplicationsReturns([]ccv3.Application{{Name: "some-app", State: constant.ApplicationStarted}}, ccv3.Warnings{"some-app-warnings"}, nil)
   151  								fakeCloudControllerClient.GetApplicationProcessesReturns([]ccv3.Process{{Type: "some-process-type", GUID: "some-process-guid"}}, ccv3.Warnings{"some-process-warnings"}, nil)
   152  								fakeCloudControllerClient.GetProcessInstancesReturns([]ccv3.ProcessInstance{{State: constant.ProcessInstanceDown, Index: 0}}, ccv3.Warnings{"some-instance-warnings"}, nil)
   153  							})
   154  
   155  							It("returns a ProcessInstanceNotRunningError", func() {
   156  								Expect(executeErr).To(MatchError(actionerror.ProcessInstanceNotRunningError{ProcessType: "some-process-type", InstanceIndex: 0}))
   157  							})
   158  						})
   159  
   160  						When("the specified process and index exist and the instance is RUNNING", func() {
   161  							BeforeEach(func() {
   162  								fakeCloudControllerClient.GetApplicationsReturns([]ccv3.Application{{Name: "some-app", State: constant.ApplicationStarted}}, ccv3.Warnings{"some-app-warnings"}, nil)
   163  								fakeCloudControllerClient.GetApplicationProcessesReturns([]ccv3.Process{{Type: "some-process-type", GUID: "some-process-guid"}}, ccv3.Warnings{"some-process-warnings"}, nil)
   164  								fakeCloudControllerClient.GetProcessInstancesReturns([]ccv3.ProcessInstance{{State: constant.ProcessInstanceRunning, Index: 0}}, ccv3.Warnings{"some-instance-warnings"}, nil)
   165  							})
   166  
   167  							When("starting the secure session succeeds", func() {
   168  								It("returns all warnings", func() {
   169  									Expect(executeErr).ToNot(HaveOccurred())
   170  									Expect(warnings).To(ConsistOf("some-app-warnings", "some-process-warnings", "some-instance-warnings"))
   171  
   172  									Expect(sshAuth).To(Equal(SSHAuthentication{
   173  										Endpoint:           "some-app-ssh-endpoint",
   174  										HostKeyFingerprint: "some-app-ssh-fingerprint",
   175  										Passcode:           "some-ssh-passcode",
   176  										Username:           "cf:some-process-guid/0",
   177  									}))
   178  
   179  									Expect(fakeCloudControllerClient.GetApplicationsCallCount()).To(Equal(1))
   180  									Expect(fakeCloudControllerClient.GetApplicationsArgsForCall(0)).To(ConsistOf(
   181  										ccv3.Query{Key: ccv3.NameFilter, Values: []string{"some-app"}},
   182  										ccv3.Query{Key: ccv3.SpaceGUIDFilter, Values: []string{"some-space-guid"}},
   183  									))
   184  								})
   185  							})
   186  						})
   187  					})
   188  				})
   189  
   190  				When("getting the application succeeds with a stopped application", func() {
   191  					BeforeEach(func() {
   192  						fakeCloudControllerClient.GetApplicationsReturns(
   193  							[]ccv3.Application{
   194  								{Name: "some-app", State: constant.ApplicationStopped},
   195  							},
   196  							ccv3.Warnings{"some-app-warnings"},
   197  							nil)
   198  					})
   199  
   200  					It("returns a ApplicationNotStartedError", func() {
   201  						Expect(executeErr).To(MatchError(actionerror.ApplicationNotStartedError{Name: "some-app"}))
   202  						Expect(warnings).To(ConsistOf("some-app-warnings"))
   203  					})
   204  				})
   205  			})
   206  		})
   207  	})
   208  })