github.com/pingcap/chaos@v0.0.0-20190710112158-c86faf4b3719/pkg/util/ssh/ssh_test.go (about)

     1  package ssh
     2  
     3  import (
     4  	"context"
     5  	"io/ioutil"
     6  	"os"
     7  	"path"
     8  	"testing"
     9  )
    10  
    11  func TestExec(t *testing.T) {
    12  	ctx := context.Background()
    13  	if err := Exec(ctx, "n1", "sleep 1"); err != nil {
    14  		t.Fatalf("ssh failed %v", err)
    15  	}
    16  
    17  	if err := Exec(ctx, "n1", "cat non_exist_file"); err == nil {
    18  		t.Fatal("ssh must fail")
    19  	}
    20  
    21  	if err := Exec(ctx, "n6", "sleep 1"); err == nil {
    22  		t.Fatal("ssh must fail")
    23  	}
    24  }
    25  
    26  func TestScp(t *testing.T) {
    27  	dir, err := ioutil.TempDir(".", "var")
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	//defer os.RemoveAll(dir)
    32  
    33  	f, err := os.Create(path.Join(dir, "a.log"))
    34  	if err != nil {
    35  		t.Fatalf("create file failed %v", err)
    36  	}
    37  	f.WriteString("Hello world")
    38  	f.Close()
    39  
    40  	ctx := context.Background()
    41  	if err = Upload(ctx, path.Join(dir, "a.log"), "n1", "/tmp/b.log"); err != nil {
    42  		t.Fatalf("upload file failed %v", err)
    43  	}
    44  
    45  	if err = Download(ctx, path.Join(dir, "b.log"), "n1", "/tmp/b.log"); err != nil {
    46  		t.Fatalf("download file failed %v", err)
    47  	}
    48  
    49  	if err = Upload(ctx, dir, "n1", "/tmp/"); err != nil {
    50  		t.Fatalf("upload folder failed %v", err)
    51  	}
    52  
    53  	if err = Download(ctx, path.Join(dir, "sub"), "n1", path.Join("/tmp", path.Base(dir))); err != nil {
    54  		t.Fatalf("download foler failed %v", err)
    55  	}
    56  }