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