github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/command/v7/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/sharedaction"
     8  	"code.cloudfoundry.org/cli/actor/v7action"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	"code.cloudfoundry.org/cli/command/flag"
    11  	"code.cloudfoundry.org/cli/command/translatableerror"
    12  	. "code.cloudfoundry.org/cli/command/v7"
    13  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    14  	"code.cloudfoundry.org/cli/util/configv3"
    15  	"code.cloudfoundry.org/cli/util/ui"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/ginkgo/extensions/table"
    18  	. "github.com/onsi/gomega"
    19  	. "github.com/onsi/gomega/gbytes"
    20  )
    21  
    22  var _ = Describe("ssh Command", func() {
    23  	var (
    24  		cmd             SSHCommand
    25  		testUI          *ui.UI
    26  		fakeConfig      *commandfakes.FakeConfig
    27  		fakeSharedActor *commandfakes.FakeSharedActor
    28  		fakeActor       *v7fakes.FakeSSHActor
    29  		fakeSSHActor    *v7fakes.FakeSharedSSHActor
    30  		executeErr      error
    31  		appName         string
    32  	)
    33  
    34  	BeforeEach(func() {
    35  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    36  		fakeConfig = new(commandfakes.FakeConfig)
    37  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    38  		fakeActor = new(v7fakes.FakeSSHActor)
    39  		fakeSSHActor = new(v7fakes.FakeSharedSSHActor)
    40  
    41  		appName = "some-app"
    42  		cmd = SSHCommand{
    43  			RequiredArgs: flag.AppName{AppName: appName},
    44  
    45  			ProcessType:         "some-process-type",
    46  			ProcessIndex:        1,
    47  			Commands:            []string{"some", "commands"},
    48  			SkipHostValidation:  true,
    49  			SkipRemoteExecution: true,
    50  
    51  			UI:          testUI,
    52  			Config:      fakeConfig,
    53  			SharedActor: fakeSharedActor,
    54  			Actor:       fakeActor,
    55  			SSHActor:    fakeSSHActor,
    56  		}
    57  	})
    58  
    59  	Describe("Execute", func() {
    60  		JustBeforeEach(func() {
    61  			executeErr = cmd.Execute(nil)
    62  		})
    63  
    64  		When("checking target fails", func() {
    65  			BeforeEach(func() {
    66  				fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: "steve"})
    67  			})
    68  
    69  			It("returns an error", func() {
    70  				Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: "steve"}))
    71  
    72  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    73  				checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    74  				Expect(checkTargetedOrg).To(BeTrue())
    75  				Expect(checkTargetedSpace).To(BeTrue())
    76  			})
    77  		})
    78  
    79  		When("the user is targeted to an organization and space", func() {
    80  			BeforeEach(func() {
    81  				fakeConfig.TargetedSpaceReturns(configv3.Space{GUID: "some-space-guid"})
    82  			})
    83  
    84  			When("getting the secure shell authentication information succeeds", func() {
    85  				var sshAuth v7action.SSHAuthentication
    86  
    87  				BeforeEach(func() {
    88  					sshAuth = v7action.SSHAuthentication{
    89  						Endpoint:           "some-endpoint",
    90  						HostKeyFingerprint: "some-fingerprint",
    91  						Passcode:           "some-passcode",
    92  						Username:           "some-username",
    93  					}
    94  
    95  					fakeActor.GetSecureShellConfigurationByApplicationNameSpaceProcessTypeAndIndexReturns(sshAuth, v7action.Warnings{"some-warnings"}, nil)
    96  				})
    97  
    98  				When("executing the secure shell succeeds", func() {
    99  					BeforeEach(func() {
   100  						cmd.DisablePseudoTTY = true
   101  					})
   102  
   103  					It("displays all warnings", func() {
   104  						Expect(executeErr).ToNot(HaveOccurred())
   105  						Expect(testUI.Err).To(Say("some-warnings"))
   106  
   107  						Expect(fakeActor.GetSecureShellConfigurationByApplicationNameSpaceProcessTypeAndIndexCallCount()).To(Equal(1))
   108  						appNameArg, spaceGUIDArg, processTypeArg, processIndexArg := fakeActor.GetSecureShellConfigurationByApplicationNameSpaceProcessTypeAndIndexArgsForCall(0)
   109  						Expect(appNameArg).To(Equal(appName))
   110  						Expect(spaceGUIDArg).To(Equal("some-space-guid"))
   111  						Expect(processTypeArg).To(Equal("some-process-type"))
   112  						Expect(processIndexArg).To(Equal(uint(1)))
   113  
   114  						Expect(fakeSSHActor.ExecuteSecureShellCallCount()).To(Equal(1))
   115  						_, sshOptionsArg := fakeSSHActor.ExecuteSecureShellArgsForCall(0)
   116  						Expect(sshOptionsArg).To(Equal(sharedaction.SSHOptions{
   117  							Commands:            []string{"some", "commands"},
   118  							Endpoint:            "some-endpoint",
   119  							HostKeyFingerprint:  "some-fingerprint",
   120  							Passcode:            "some-passcode",
   121  							SkipHostValidation:  true,
   122  							SkipRemoteExecution: true,
   123  							TTYOption:           sharedaction.RequestTTYNo,
   124  							Username:            "some-username",
   125  						}))
   126  					})
   127  
   128  					When("working with local port forwarding", func() {
   129  						BeforeEach(func() {
   130  							cmd.LocalPortForwardSpecs = []flag.SSHPortForwarding{
   131  								{LocalAddress: "localhost:8888", RemoteAddress: "remote:4444"},
   132  								{LocalAddress: "localhost:7777", RemoteAddress: "remote:3333"},
   133  							}
   134  						})
   135  
   136  						It("passes along port forwarding information", func() {
   137  							Expect(executeErr).ToNot(HaveOccurred())
   138  							Expect(testUI.Err).To(Say("some-warnings"))
   139  
   140  							Expect(fakeSSHActor.ExecuteSecureShellCallCount()).To(Equal(1))
   141  							_, sshOptionsArg := fakeSSHActor.ExecuteSecureShellArgsForCall(0)
   142  							Expect(sshOptionsArg).To(Equal(sharedaction.SSHOptions{
   143  								Commands:            []string{"some", "commands"},
   144  								Endpoint:            "some-endpoint",
   145  								HostKeyFingerprint:  "some-fingerprint",
   146  								Passcode:            "some-passcode",
   147  								TTYOption:           sharedaction.RequestTTYNo,
   148  								SkipHostValidation:  true,
   149  								SkipRemoteExecution: true,
   150  								LocalPortForwardSpecs: []sharedaction.LocalPortForward{
   151  									{LocalAddress: "localhost:8888", RemoteAddress: "remote:4444"},
   152  									{LocalAddress: "localhost:7777", RemoteAddress: "remote:3333"},
   153  								},
   154  								Username: "some-username",
   155  							}))
   156  						})
   157  					})
   158  				})
   159  
   160  				When("executing the secure shell fails", func() {
   161  					BeforeEach(func() {
   162  						cmd.DisablePseudoTTY = true
   163  
   164  						fakeSSHActor.ExecuteSecureShellReturns(errors.New("banananannananana"))
   165  					})
   166  
   167  					It("displays all warnings", func() {
   168  						Expect(executeErr).To(MatchError("banananannananana"))
   169  						Expect(testUI.Err).To(Say("some-warnings"))
   170  					})
   171  				})
   172  			})
   173  
   174  			When("getting the secure shell authentication fails", func() {
   175  				BeforeEach(func() {
   176  					fakeActor.GetSecureShellConfigurationByApplicationNameSpaceProcessTypeAndIndexReturns(v7action.SSHAuthentication{}, v7action.Warnings{"some-warnings"}, errors.New("some-error"))
   177  				})
   178  
   179  				It("returns the error and displays all warnings", func() {
   180  					Expect(executeErr).To(MatchError("some-error"))
   181  					Expect(testUI.Err).To(Say("some-warnings"))
   182  				})
   183  			})
   184  		})
   185  	})
   186  
   187  	DescribeTable("EvaluateTTYOption",
   188  		func(disablePseudoTTY bool, forcePseudoTTY bool, requestPseudoTTY bool, expectedErr error, ttyOption sharedaction.TTYOption) {
   189  			cmd.DisablePseudoTTY = disablePseudoTTY
   190  			cmd.ForcePseudoTTY = forcePseudoTTY
   191  			cmd.RequestPseudoTTY = requestPseudoTTY
   192  			returnedTTYOption, executeErr := cmd.EvaluateTTYOption()
   193  
   194  			if expectedErr == nil {
   195  				Expect(executeErr).To(BeNil())
   196  				Expect(returnedTTYOption).To(Equal(ttyOption))
   197  			} else {
   198  				Expect(executeErr).To(MatchError(expectedErr))
   199  			}
   200  		},
   201  		Entry("default - auto TTY", false, false, false, nil, sharedaction.RequestTTYAuto),
   202  		Entry("disable tty - no TTY", true, false, false, nil, sharedaction.RequestTTYNo),
   203  		Entry("force tty - forced TTY", false, true, false, nil, sharedaction.RequestTTYForce),
   204  		Entry("psudo tty - yes TTY", false, false, true, nil, sharedaction.RequestTTYYes),
   205  		Entry("disable and force tty", true, true, false,
   206  			translatableerror.ArgumentCombinationError{Args: []string{
   207  				"--disable-pseudo-tty", "-T", "--force-pseudo-tty", "--request-pseudo-tty", "-t",
   208  			}},
   209  			sharedaction.TTYOption(0),
   210  		),
   211  		Entry("disable and requst tty", true, false, true,
   212  			translatableerror.ArgumentCombinationError{Args: []string{
   213  				"--disable-pseudo-tty", "-T", "--force-pseudo-tty", "--request-pseudo-tty", "-t",
   214  			}},
   215  			sharedaction.TTYOption(0),
   216  		),
   217  		Entry("force and request tty", false, true, true,
   218  			translatableerror.ArgumentCombinationError{Args: []string{
   219  				"--disable-pseudo-tty", "-T", "--force-pseudo-tty", "--request-pseudo-tty", "-t",
   220  			}},
   221  			sharedaction.TTYOption(0),
   222  		),
   223  		Entry("disable, force, and request tty", true, true, true,
   224  			translatableerror.ArgumentCombinationError{Args: []string{
   225  				"--disable-pseudo-tty", "-T", "--force-pseudo-tty", "--request-pseudo-tty", "-t",
   226  			}},
   227  			sharedaction.TTYOption(0),
   228  		),
   229  	)
   230  })