github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/utils/ssh/scp_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 "testing" 19 20 "github.com/alibaba/sealer/logger" 21 ) 22 23 func TestSSHCopyLocalToRemote(t *testing.T) { 24 type args struct { 25 host string 26 localPath string 27 remotePath string 28 } 29 var ( 30 host = "10.96.33.168" 31 ssh = SSH{ 32 User: "root", 33 Password: "123456", 34 PkFile: "", 35 PkPassword: "", 36 Timeout: nil, 37 } 38 ) 39 tests := []struct { 40 name string 41 fields SSH 42 args args 43 wantErr bool 44 }{ 45 { 46 name: "test for copy file to remote server", 47 fields: ssh, 48 args: args{ 49 host, 50 "../test/file/01", 51 "/home/temp/01", 52 }, 53 wantErr: false, 54 }, 55 { 56 name: "test copy for file when local file is not exist", 57 fields: ssh, 58 args: args{ 59 host, 60 // local file 001 is not exist. 61 "../test/file/123", 62 "/home/temp/01", 63 }, 64 wantErr: false, 65 }, 66 { 67 name: "test copy dir to remote server", 68 fields: ssh, 69 args: args{ 70 host, 71 "../test/file", 72 "/home/temp011", 73 }, 74 wantErr: false, 75 }, 76 } 77 for _, tt := range tests { 78 t.Run(tt.name, func(t *testing.T) { 79 ss := &SSH{ 80 User: tt.fields.User, 81 Password: tt.fields.Password, 82 PkFile: tt.fields.PkFile, 83 PkPassword: tt.fields.PkPassword, 84 Timeout: tt.fields.Timeout, 85 } 86 87 if !fileExist(tt.args.localPath) { 88 logger.Error("local filepath is not exit") 89 return 90 } 91 //if ss.IsFileExist(host, tt.args.remotePath) { 92 // log.Error("remote filepath is exit") 93 // return 94 //} 95 // test copy dir 96 err := ss.Copy(tt.args.host, tt.args.localPath, tt.args.remotePath) 97 if (err != nil) != tt.wantErr { 98 logger.Error(err) 99 t.Errorf("err: %v", err) 100 } 101 102 // test the copy result 103 //ss.Cmd(tt.args.host, "ls -lh "+tt.args.remotePath) 104 105 // rm remote file 106 //ss.Cmd(tt.args.host, "rm -rf "+tt.args.remotePath) 107 }) 108 } 109 } 110 111 func TestSSHFetchRemoteToLocal(t *testing.T) { 112 type args struct { 113 host string 114 localPath string 115 remotePath string 116 } 117 var ( 118 host = "" 119 ssh = SSH{ 120 User: "root", 121 Password: "", 122 PkFile: "", 123 PkPassword: "", 124 Timeout: nil, 125 } 126 ) 127 tests := []struct { 128 name string 129 fields SSH 130 args args 131 wantErr bool 132 }{ 133 { 134 name: "test for fetch remote file to local", 135 fields: ssh, 136 args: args{ 137 host, 138 "/root/.kube/config", 139 "/root/Clusterfile", 140 }, 141 wantErr: false, 142 }, 143 } 144 for _, tt := range tests { 145 t.Run(tt.name, func(t *testing.T) { 146 ss := &SSH{ 147 User: tt.fields.User, 148 Password: tt.fields.Password, 149 PkFile: tt.fields.PkFile, 150 PkPassword: tt.fields.PkPassword, 151 Timeout: tt.fields.Timeout, 152 } 153 154 if exist, err := ss.IsFileExist(host, tt.args.remotePath); err != nil { 155 logger.Error("err: ", err) 156 return 157 } else if !exist { 158 logger.Error("remote filepath is not exit") 159 return 160 } 161 err := ss.Fetch(tt.args.host, tt.args.localPath, tt.args.remotePath) 162 if (err != nil) != tt.wantErr { 163 logger.Error(err) 164 t.Errorf("err: %v", err) 165 } 166 }) 167 } 168 } 169 170 func TestSSH_Copy(t *testing.T) { 171 type fields struct { 172 User string 173 Password string 174 PkFile string 175 PkPassword string 176 } 177 type args struct { 178 host string 179 localPath string 180 remotePath string 181 } 182 tests := []struct { 183 name string 184 fields fields 185 args args 186 wantErr bool 187 }{ 188 { 189 "test copy dir", 190 fields{ 191 User: "root", 192 Password: "", 193 PkFile: "", 194 PkPassword: "", 195 }, 196 args{ 197 host: "", 198 localPath: "./pkg/cert/pki", 199 remotePath: "/root/kubernetes/pki", 200 }, 201 false, 202 }, 203 } 204 for _, tt := range tests { 205 t.Run(tt.name, func(t *testing.T) { 206 s := &SSH{ 207 User: tt.fields.User, 208 Password: tt.fields.Password, 209 PkFile: tt.fields.PkFile, 210 PkPassword: tt.fields.PkPassword, 211 } 212 if err := s.Copy(tt.args.host, tt.args.localPath, tt.args.remotePath); (err != nil) != tt.wantErr { 213 t.Errorf("Copy() error = %v, wantErr %v", err, tt.wantErr) 214 } 215 }) 216 } 217 }