github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/tools/ssh_test.go (about) 1 /* 2 Copyright 2018 Mirantis 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package tools 18 19 import ( 20 "bytes" 21 "testing" 22 ) 23 24 func TestSSHCommand(t *testing.T) { 25 c := &fakeKubeClient{ 26 t: t, 27 virtletPods: map[string]string{ 28 "kube-node-1": "virtlet-foo42", 29 }, 30 vmPods: map[string]VMPodInfo{ 31 "cirros": { 32 NodeName: "kube-node-1", 33 VirtletPodName: "virtlet-foo42", 34 ContainerID: "cc349e91-dcf7-4f11-a077-36c3673c3fc4", 35 ContainerName: "foocontainer", 36 }, 37 }, 38 expectedPortForwards: []string{ 39 "cirros/default: :22", 40 }, 41 } 42 var out bytes.Buffer 43 cmd := NewSSHCmd(c, &out, "/bin/echo") 44 cmd.SetArgs([]string{"user@cirros", "--", "ls", "-l", "/"}) 45 cmd.SilenceUsage = true 46 cmd.SilenceErrors = true 47 if err := cmd.Execute(); err != nil { 48 t.Fatalf("ssh error: %v", err) 49 } 50 expectedCommand := "-q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p 4242 user@127.0.0.1 ls -l /\n" 51 if out.String() != expectedCommand { 52 t.Errorf("bad ssh command line: %q instead of %q", out.String(), expectedCommand) 53 } 54 // TODO: support alternative remote ports 55 } 56 57 func TestSSHErrorOnNonVMPod(t *testing.T) { 58 c := &fakeKubeClient{ 59 t: t, 60 virtletPods: map[string]string{ 61 "kube-node-1": "virtlet-foo42", 62 }, 63 } 64 var out bytes.Buffer 65 cmd := NewSSHCmd(c, &out, "/bin/echo") 66 cmd.SetArgs([]string{"user@cirros", "--", "ls", "-l", "/"}) 67 cmd.SilenceUsage = true 68 cmd.SilenceErrors = true 69 if err := cmd.Execute(); err == nil { 70 t.Errorf("didn't get an error for non-VM pod") 71 } 72 }