github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/v6/v3_ssh_command_test.go (about)

     1  package v6_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/v3action"
     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/v6"
    13  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    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("v3-ssh Command", func() {
    23  	var (
    24  		cmd             V3SSHCommand
    25  		testUI          *ui.UI
    26  		fakeConfig      *commandfakes.FakeConfig
    27  		fakeSharedActor *commandfakes.FakeSharedActor
    28  		fakeActor       *v6fakes.FakeV3SSHActor
    29  		fakeSSHActor    *v6fakes.FakeSSHActor
    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(v6fakes.FakeV3SSHActor)
    39  		fakeSSHActor = new(v6fakes.FakeSSHActor)
    40  
    41  		appName = "some-app"
    42  		cmd = V3SSHCommand{
    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  		It("displays the experimental warning", func() {
    65  			Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    66  		})
    67  
    68  		When("checking target fails", func() {
    69  			BeforeEach(func() {
    70  				fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: "steve"})
    71  			})
    72  
    73  			It("returns an error", func() {
    74  				Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: "steve"}))
    75  
    76  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    77  				checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    78  				Expect(checkTargetedOrg).To(BeTrue())
    79  				Expect(checkTargetedSpace).To(BeTrue())
    80  			})
    81  		})
    82  
    83  		When("the user is targeted to an organization and space", func() {
    84  			BeforeEach(func() {
    85  				fakeConfig.TargetedSpaceReturns(configv3.Space{GUID: "some-space-guid"})
    86  			})
    87  
    88  			When("getting the secure shell authentication information succeeds", func() {
    89  				var sshAuth v3action.SSHAuthentication
    90  
    91  				BeforeEach(func() {
    92  					sshAuth = v3action.SSHAuthentication{
    93  						Endpoint:           "some-endpoint",
    94  						HostKeyFingerprint: "some-fingerprint",
    95  						Passcode:           "some-passcode",
    96  						Username:           "some-username",
    97  					}
    98  
    99  					fakeActor.GetSecureShellConfigurationByApplicationNameSpaceProcessTypeAndIndexReturns(sshAuth, v3action.Warnings{"some-warnings"}, nil)
   100  				})
   101  
   102  				When("executing the secure shell succeeds", func() {
   103  					BeforeEach(func() {
   104  						cmd.DisablePseudoTTY = true
   105  					})
   106  
   107  					It("displays all warnings", func() {
   108  						Expect(executeErr).ToNot(HaveOccurred())
   109  						Expect(testUI.Err).To(Say("some-warnings"))
   110  
   111  						Expect(fakeActor.GetSecureShellConfigurationByApplicationNameSpaceProcessTypeAndIndexCallCount()).To(Equal(1))
   112  						appNameArg, spaceGUIDArg, processTypeArg, processIndexArg := fakeActor.GetSecureShellConfigurationByApplicationNameSpaceProcessTypeAndIndexArgsForCall(0)
   113  						Expect(appNameArg).To(Equal(appName))
   114  						Expect(spaceGUIDArg).To(Equal("some-space-guid"))
   115  						Expect(processTypeArg).To(Equal("some-process-type"))
   116  						Expect(processIndexArg).To(Equal(uint(1)))
   117  
   118  						Expect(fakeSSHActor.ExecuteSecureShellCallCount()).To(Equal(1))
   119  						_, sshOptionsArg := fakeSSHActor.ExecuteSecureShellArgsForCall(0)
   120  						Expect(sshOptionsArg).To(Equal(sharedaction.SSHOptions{
   121  							Commands:            []string{"some", "commands"},
   122  							Endpoint:            "some-endpoint",
   123  							HostKeyFingerprint:  "some-fingerprint",
   124  							Passcode:            "some-passcode",
   125  							SkipHostValidation:  true,
   126  							SkipRemoteExecution: true,
   127  							TTYOption:           sharedaction.RequestTTYNo,
   128  							Username:            "some-username",
   129  						}))
   130  					})
   131  
   132  					When("working with local port forwarding", func() {
   133  						BeforeEach(func() {
   134  							cmd.LocalPortForwardSpecs = []flag.SSHPortForwarding{
   135  								{LocalAddress: "localhost:8888", RemoteAddress: "remote:4444"},
   136  								{LocalAddress: "localhost:7777", RemoteAddress: "remote:3333"},
   137  							}
   138  						})
   139  
   140  						It("passes along port forwarding information", func() {
   141  							Expect(executeErr).ToNot(HaveOccurred())
   142  							Expect(testUI.Err).To(Say("some-warnings"))
   143  
   144  							Expect(fakeSSHActor.ExecuteSecureShellCallCount()).To(Equal(1))
   145  							_, sshOptionsArg := fakeSSHActor.ExecuteSecureShellArgsForCall(0)
   146  							Expect(sshOptionsArg).To(Equal(sharedaction.SSHOptions{
   147  								Commands:            []string{"some", "commands"},
   148  								Endpoint:            "some-endpoint",
   149  								HostKeyFingerprint:  "some-fingerprint",
   150  								Passcode:            "some-passcode",
   151  								TTYOption:           sharedaction.RequestTTYNo,
   152  								SkipHostValidation:  true,
   153  								SkipRemoteExecution: true,
   154  								LocalPortForwardSpecs: []sharedaction.LocalPortForward{
   155  									{LocalAddress: "localhost:8888", RemoteAddress: "remote:4444"},
   156  									{LocalAddress: "localhost:7777", RemoteAddress: "remote:3333"},
   157  								},
   158  								Username: "some-username",
   159  							}))
   160  						})
   161  					})
   162  				})
   163  
   164  				When("executing the secure shell fails", func() {
   165  					BeforeEach(func() {
   166  						cmd.DisablePseudoTTY = true
   167  
   168  						fakeSSHActor.ExecuteSecureShellReturns(errors.New("banananannananana"))
   169  					})
   170  
   171  					It("displays all warnings", func() {
   172  						Expect(executeErr).To(MatchError("banananannananana"))
   173  						Expect(testUI.Err).To(Say("some-warnings"))
   174  					})
   175  				})
   176  			})
   177  
   178  			When("getting the secure shell authentication fails", func() {
   179  				BeforeEach(func() {
   180  					fakeActor.GetSecureShellConfigurationByApplicationNameSpaceProcessTypeAndIndexReturns(v3action.SSHAuthentication{}, v3action.Warnings{"some-warnings"}, errors.New("some-error"))
   181  				})
   182  
   183  				It("returns the error and displays all warnings", func() {
   184  					Expect(executeErr).To(MatchError("some-error"))
   185  					Expect(testUI.Err).To(Say("some-warnings"))
   186  				})
   187  			})
   188  		})
   189  	})
   190  
   191  	DescribeTable("EvaluateTTYOption",
   192  		func(disablePseudoTTY bool, forcePseudoTTY bool, requestPseudoTTY bool, expectedErr error, ttyOption sharedaction.TTYOption) {
   193  			cmd.DisablePseudoTTY = disablePseudoTTY
   194  			cmd.ForcePseudoTTY = forcePseudoTTY
   195  			cmd.RequestPseudoTTY = requestPseudoTTY
   196  			returnedTTYOption, executeErr := cmd.EvaluateTTYOption()
   197  
   198  			if expectedErr == nil {
   199  				Expect(executeErr).To(BeNil())
   200  				Expect(returnedTTYOption).To(Equal(ttyOption))
   201  			} else {
   202  				Expect(executeErr).To(MatchError(expectedErr))
   203  			}
   204  		},
   205  		Entry("default - auto TTY", false, false, false, nil, sharedaction.RequestTTYAuto),
   206  		Entry("disable tty - no TTY", true, false, false, nil, sharedaction.RequestTTYNo),
   207  		Entry("force tty - forced TTY", false, true, false, nil, sharedaction.RequestTTYForce),
   208  		Entry("psudo tty - yes TTY", false, false, true, nil, sharedaction.RequestTTYYes),
   209  		Entry("disable and force tty", true, true, false,
   210  			translatableerror.ArgumentCombinationError{Args: []string{
   211  				"--disable-pseudo-tty", "-T", "--force-pseudo-tty", "--request-pseudo-tty", "-t",
   212  			}},
   213  			sharedaction.TTYOption(0),
   214  		),
   215  		Entry("disable and requst tty", true, false, true,
   216  			translatableerror.ArgumentCombinationError{Args: []string{
   217  				"--disable-pseudo-tty", "-T", "--force-pseudo-tty", "--request-pseudo-tty", "-t",
   218  			}},
   219  			sharedaction.TTYOption(0),
   220  		),
   221  		Entry("force and request tty", false, true, true,
   222  			translatableerror.ArgumentCombinationError{Args: []string{
   223  				"--disable-pseudo-tty", "-T", "--force-pseudo-tty", "--request-pseudo-tty", "-t",
   224  			}},
   225  			sharedaction.TTYOption(0),
   226  		),
   227  		Entry("disable, force, and request tty", true, true, true,
   228  			translatableerror.ArgumentCombinationError{Args: []string{
   229  				"--disable-pseudo-tty", "-T", "--force-pseudo-tty", "--request-pseudo-tty", "-t",
   230  			}},
   231  			sharedaction.TTYOption(0),
   232  		),
   233  	)
   234  })