github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/tools/virsh_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 TestVirshCommand(t *testing.T) { 26 for _, tc := range []struct { 27 args string 28 expectedCommands map[string]string 29 expectedOutput string 30 errSubstring string 31 }{ 32 { 33 args: "list --node=kube-node-1", 34 expectedCommands: map[string]string{ 35 "virtlet-foo42/libvirt/kube-system: virsh list": "foobar", 36 }, 37 expectedOutput: "foobar", 38 }, 39 { 40 args: "list", 41 expectedCommands: map[string]string{ 42 "virtlet-foo42/libvirt/kube-system: virsh list": "foobar", 43 "virtlet-bar42/libvirt/kube-system: virsh list": "baz", 44 }, 45 expectedOutput: "*** node: kube-node-1 pod: virtlet-foo42 ***\n" + 46 "foobar\n" + 47 "*** node: kube-node-2 pod: virtlet-bar42 ***\n" + 48 "baz\n", 49 }, 50 { 51 args: "dumpxml @cirros", 52 expectedCommands: map[string]string{ 53 "virtlet-foo42/libvirt/kube-system: virsh dumpxml virtlet-cc349e91-dcf7-foocontainer": "foobar", 54 }, 55 expectedOutput: "foobar", 56 }, 57 { 58 args: "dumpxml @ubuntu", 59 expectedCommands: map[string]string{ 60 "virtlet-bar42/libvirt/kube-system: virsh dumpxml virtlet-4707196f-1d93-vm": "foobar", 61 }, 62 expectedOutput: "foobar", 63 }, 64 { 65 args: "dumpxml @cirros --node=kube-node-1", 66 expectedCommands: map[string]string{ 67 "virtlet-foo42/libvirt/kube-system: virsh dumpxml virtlet-cc349e91-dcf7-foocontainer": "foobar", 68 }, 69 expectedOutput: "foobar", 70 }, 71 { 72 args: "dumpxml @cirros --node=kube-node-2", 73 errSubstring: "--node specifies a node other than one that runs the VM pod", 74 }, 75 { 76 args: "whatever @cirros @cirros1", 77 expectedCommands: map[string]string{ 78 "virtlet-foo42/libvirt/kube-system: virsh whatever virtlet-cc349e91-dcf7-foocontainer virtlet-68e6fede-aab2-qq": "foobar", 79 }, 80 expectedOutput: "foobar", 81 }, 82 { 83 args: "whatever @cirros @ubuntu", 84 errSubstring: "can't reference VM pods that run on different nodes", 85 }, 86 } { 87 t.Run(tc.args, func(t *testing.T) { 88 c := &fakeKubeClient{ 89 t: t, 90 virtletPods: map[string]string{ 91 "kube-node-1": "virtlet-foo42", 92 "kube-node-2": "virtlet-bar42", 93 }, 94 vmPods: map[string]VMPodInfo{ 95 "cirros": { 96 NodeName: "kube-node-1", 97 VirtletPodName: "virtlet-foo42", 98 ContainerID: "cc349e91-dcf7-4f11-a077-36c3673c3fc4", 99 ContainerName: "foocontainer", 100 }, 101 "cirros1": { 102 NodeName: "kube-node-1", 103 VirtletPodName: "virtlet-foo42", 104 ContainerID: "68e6fede-aab2-4abe-b339-466386734ddb", 105 ContainerName: "qq", 106 }, 107 "ubuntu": { 108 NodeName: "kube-node-2", 109 VirtletPodName: "virtlet-bar42", 110 ContainerID: "4707196f-1d93-46ba-a138-b6201e13db6d", 111 ContainerName: "vm", 112 }, 113 }, 114 expectedCommands: tc.expectedCommands, 115 } 116 var out bytes.Buffer 117 cmd := NewVirshCmd(c, &out) 118 cmd.SetArgs(strings.Split(tc.args, " ")) 119 cmd.SilenceUsage = true 120 cmd.SilenceErrors = true 121 switch err := cmd.Execute(); { 122 case err != nil && tc.errSubstring == "": 123 t.Errorf("virsh command returned an unexpected error: %v", err) 124 case err == nil && tc.errSubstring != "": 125 t.Errorf("Didn't get expected error (substring %q), output: %q", tc.errSubstring, out.String()) 126 case err != nil && !strings.Contains(err.Error(), tc.errSubstring): 127 t.Errorf("Didn't get expected substring %q in the error: %v", tc.errSubstring, err) 128 case err == nil && out.String() != tc.expectedOutput: 129 t.Errorf("Unexpected output from the command: %q instead of %q", out.String(), tc.expectedOutput) 130 } 131 for c := range tc.expectedCommands { 132 t.Errorf("command not executed: %q", c) 133 } 134 }) 135 } 136 }