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