github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/tools/vnc_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 "strings" 22 "testing" 23 ) 24 25 func TestVNCCommand(t *testing.T) { 26 for _, tc := range []struct { 27 args string 28 expectedCommands map[string]string 29 expectedPortForwards []string 30 expectedOutput string 31 errSubstring string 32 }{ 33 { 34 args: "cirros", 35 expectedCommands: map[string]string{ 36 "virtlet-foo42/libvirt/kube-system: virsh domdisplay virtlet-cc349e91-dcf7-foocontainer": "vnc://127.0.0.1:0", 37 }, 38 expectedPortForwards: []string{ 39 "virtlet-foo42/kube-system: :5900", 40 }, 41 expectedOutput: "Press ctrl-c", 42 }, 43 { 44 args: "ubuntu", 45 errSubstring: "can't get VM pod info", 46 }, 47 } { 48 t.Run(tc.args, func(t *testing.T) { 49 c := &fakeKubeClient{ 50 t: t, 51 virtletPods: map[string]string{ 52 "kube-node-1": "virtlet-foo42", 53 }, 54 vmPods: map[string]VMPodInfo{ 55 "cirros": { 56 NodeName: "kube-node-1", 57 VirtletPodName: "virtlet-foo42", 58 ContainerID: "cc349e91-dcf7-4f11-a077-36c3673c3fc4", 59 ContainerName: "foocontainer", 60 }, 61 }, 62 expectedCommands: tc.expectedCommands, 63 expectedPortForwards: tc.expectedPortForwards, 64 } 65 var out bytes.Buffer 66 cmd := NewVNCCmd(c, &out, false) 67 cmd.SetArgs(strings.Split(tc.args, " ")) 68 cmd.SilenceUsage = true 69 cmd.SilenceErrors = true 70 switch err := cmd.Execute(); { 71 case err != nil && tc.errSubstring == "": 72 t.Errorf("vnc command returned an unexpected error: %v", err) 73 case err == nil && tc.errSubstring != "": 74 t.Errorf("Didn't get expected error (substring %q), output: %q", tc.errSubstring, out.String()) 75 case err != nil && !strings.Contains(err.Error(), tc.errSubstring): 76 t.Errorf("Didn't get expected substring %q in the error: %v", tc.errSubstring, err) 77 case err == nil && !strings.Contains(out.String(), tc.expectedOutput): 78 t.Errorf("Unexpected output from the command: %q instead of %q", out.String(), tc.expectedOutput) 79 } 80 for c := range tc.expectedCommands { 81 t.Errorf("command not executed: %q", c) 82 } 83 }) 84 } 85 }