github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/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 /* 18 func TestSSH_Cmd(t *testing.T) { 19 type args struct { 20 ssh SSH 21 host, cmd string 22 } 23 tests := []struct { 24 name string 25 args args 26 want string 27 wantErr bool 28 }{ 29 { 30 name: "touch test.txt", 31 args: args{ 32 ssh: SSH{false, 33 false, 34 "root", 35 "huaijiahui.com", 36 "", 37 "", 38 "", 39 nil, 40 []net.Addr{}, nil, 41 }, 42 host: "192.168.56.103", 43 cmd: "bash /opt/touchTxt.sh", 44 }, 45 want: "success touch test.txt\r\n", //命令返回值后缀为/r/n 46 wantErr: false, 47 }, 48 { 49 name: "ls /opt/test", 50 args: args{ 51 ssh: SSH{false, 52 false, 53 "root", 54 "huaijiahui.com", 55 "", 56 "", 57 "", 58 nil, 59 []net.Addr{}, nil, 60 }, 61 host: "192.168.56.103", 62 cmd: "ls /opt/test", 63 }, 64 want: "test.txt\r\n", 65 wantErr: false, 66 }, 67 { 68 name: "remove test.txt", 69 args: args{ 70 ssh: SSH{false, 71 false, 72 "root", 73 "huaijiahui.com", 74 "", 75 "", 76 "", 77 nil, 78 []net.Addr{}, nil, 79 }, 80 host: "192.168.56.103", 81 cmd: "bash /opt/removeTxt.sh", 82 }, 83 want: "test remove success\r\n", 84 wantErr: false, 85 }, 86 { 87 name: "exist 1", 88 args: args{ 89 ssh: SSH{false, 90 false, 91 "root", 92 "huaijiahui.com", 93 "", 94 "", 95 "", 96 nil, 97 []net.Addr{}, nil, 98 }, 99 host: "192.168.56.103", 100 cmd: "bash /opt/exit1.sh", 101 }, 102 want: "", 103 wantErr: true, 104 }, 105 } 106 107 for _, tt := range tests { 108 t.Run(tt.name, func(t *testing.T) { 109 got, err := tt.args.ssh.Cmd(tt.args.host, tt.args.cmd) 110 if (err != nil) != tt.wantErr { 111 t.Errorf("Cmd err : %v, wangErr is %v", err, tt.wantErr) 112 } 113 114 if string(got) != tt.want { 115 t.Errorf("got={%s},want={%s}", string(got), tt.want) 116 } 117 }) 118 } 119 } 120 121 func TestSSH_CmdAsync(t *testing.T) { 122 type args struct { 123 ssh SSH 124 host, cmd string 125 } 126 tests := []struct { 127 name string 128 args args 129 want string 130 wantErr bool 131 }{ 132 { 133 name: "touch test.txt", 134 args: args{ 135 ssh: SSH{false, 136 false, 137 "root", 138 "huaijiahui.com", 139 "", 140 "", 141 "", 142 nil, 143 []net.Addr{}, nil, 144 }, 145 host: "192.168.56.103", 146 cmd: "bash /opt/touchTxt.sh", 147 }, 148 wantErr: false, 149 }, 150 { 151 name: "ls /opt/test", 152 args: args{ 153 ssh: SSH{false, 154 false, 155 "root", 156 "huaijiahui.com", 157 "", 158 "", 159 "", 160 nil, 161 []net.Addr{}, nil, 162 }, 163 host: "192.168.56.103", 164 cmd: "ls /opt/test", 165 }, 166 wantErr: false, 167 }, 168 { 169 name: "remove test.txt", 170 args: args{ 171 ssh: SSH{false, 172 false, 173 "root", 174 "huaijiahui.com", 175 "", 176 "", 177 "", 178 nil, 179 []net.Addr{}, nil, 180 }, 181 host: "192.168.56.103", 182 cmd: "bash /opt/removeTxt.sh", 183 }, 184 wantErr: false, 185 }, 186 { 187 name: "exist 1", 188 args: args{ 189 ssh: SSH{false, 190 false, 191 "root", 192 "huaijiahui.com", 193 "", 194 "", 195 "", 196 nil, 197 []net.Addr{}, nil, 198 }, 199 host: "192.168.56.103", 200 cmd: "bash /opt/exit1.sh", 201 }, 202 wantErr: true, //Process exited with status 1 203 }, 204 } 205 206 for _, tt := range tests { 207 t.Run(tt.name, func(t *testing.T) { 208 if err := tt.args.ssh.CmdAsync(tt.args.host, tt.args.cmd); (err != nil) != tt.wantErr { 209 t.Errorf("Cmd err : %v, wangErr is %v", err, tt.wantErr) 210 } 211 }) 212 } 213 } 214 */