github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/utils/ssh/sshcmd_test.go (about) 1 // Copyright © 2021 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package ssh 16 17 import ( 18 "net" 19 "testing" 20 ) 21 22 func TestSSH_Cmd(t *testing.T) { 23 type args struct { 24 ssh SSH 25 host, cmd string 26 } 27 tests := []struct { 28 name string 29 args args 30 want string 31 wantErr bool 32 }{ 33 { 34 name: "touch test.txt", 35 args: args{ 36 ssh: SSH{false, 37 false, 38 "root", 39 "huaijiahui.com", 40 "", 41 "", 42 "", 43 nil, 44 []net.Addr{}, 45 }, 46 host: "192.168.56.103", 47 cmd: "bash /opt/touchTxt.sh", 48 }, 49 want: "success touch test.txt\r\n", //命令返回值后缀为/r/n 50 wantErr: false, 51 }, 52 { 53 name: "ls /opt/test", 54 args: args{ 55 ssh: SSH{false, 56 false, 57 "root", 58 "huaijiahui.com", 59 "", 60 "", 61 "", 62 nil, 63 []net.Addr{}, 64 }, 65 host: "192.168.56.103", 66 cmd: "ls /opt/test", 67 }, 68 want: "test.txt\r\n", 69 wantErr: false, 70 }, 71 { 72 name: "remove test.txt", 73 args: args{ 74 ssh: SSH{false, 75 false, 76 "root", 77 "huaijiahui.com", 78 "", 79 "", 80 "", 81 nil, 82 []net.Addr{}, 83 }, 84 host: "192.168.56.103", 85 cmd: "bash /opt/removeTxt.sh", 86 }, 87 want: "test remove success\r\n", 88 wantErr: false, 89 }, 90 { 91 name: "exist 1", 92 args: args{ 93 ssh: SSH{false, 94 false, 95 "root", 96 "huaijiahui.com", 97 "", 98 "", 99 "", 100 nil, 101 []net.Addr{}, 102 }, 103 host: "192.168.56.103", 104 cmd: "bash /opt/exit1.sh", 105 }, 106 want: "", 107 wantErr: true, 108 }, 109 } 110 111 for _, tt := range tests { 112 t.Run(tt.name, func(t *testing.T) { 113 got, err := tt.args.ssh.Cmd(tt.args.host, tt.args.cmd) 114 if (err != nil) != tt.wantErr { 115 t.Errorf("Cmd err : %v, wangErr is %v", err, tt.wantErr) 116 } 117 118 if string(got) != tt.want { 119 t.Errorf("got={%s},want={%s}", string(got), tt.want) 120 } 121 }) 122 } 123 } 124 125 func TestSSH_CmdAsync(t *testing.T) { 126 type args struct { 127 ssh SSH 128 host, cmd string 129 } 130 tests := []struct { 131 name string 132 args args 133 want string 134 wantErr bool 135 }{ 136 { 137 name: "touch test.txt", 138 args: args{ 139 ssh: SSH{false, 140 false, 141 "root", 142 "huaijiahui.com", 143 "", 144 "", 145 "", 146 nil, 147 []net.Addr{}, 148 }, 149 host: "192.168.56.103", 150 cmd: "bash /opt/touchTxt.sh", 151 }, 152 wantErr: false, 153 }, 154 { 155 name: "ls /opt/test", 156 args: args{ 157 ssh: SSH{false, 158 false, 159 "root", 160 "huaijiahui.com", 161 "", 162 "", 163 "", 164 nil, 165 []net.Addr{}, 166 }, 167 host: "192.168.56.103", 168 cmd: "ls /opt/test", 169 }, 170 wantErr: false, 171 }, 172 { 173 name: "remove test.txt", 174 args: args{ 175 ssh: SSH{false, 176 false, 177 "root", 178 "huaijiahui.com", 179 "", 180 "", 181 "", 182 nil, 183 []net.Addr{}, 184 }, 185 host: "192.168.56.103", 186 cmd: "bash /opt/removeTxt.sh", 187 }, 188 wantErr: false, 189 }, 190 { 191 name: "exist 1", 192 args: args{ 193 ssh: SSH{false, 194 false, 195 "root", 196 "huaijiahui.com", 197 "", 198 "", 199 "", 200 nil, 201 []net.Addr{}, 202 }, 203 host: "192.168.56.103", 204 cmd: "bash /opt/exit1.sh", 205 }, 206 wantErr: true, //Process exited with status 1 207 }, 208 } 209 210 for _, tt := range tests { 211 t.Run(tt.name, func(t *testing.T) { 212 if err := tt.args.ssh.CmdAsync(tt.args.host, tt.args.cmd); (err != nil) != tt.wantErr { 213 t.Errorf("Cmd err : %v, wangErr is %v", err, tt.wantErr) 214 } 215 }) 216 } 217 }