github.com/pingcap/chaos@v0.0.0-20190710112158-c86faf4b3719/pkg/util/util_test.go (about) 1 package util 2 3 import ( 4 "context" 5 "fmt" 6 "path" 7 "strconv" 8 "strings" 9 "testing" 10 "time" 11 12 "github.com/pingcap/chaos/pkg/util/ssh" 13 ) 14 15 func TestWget(t *testing.T) { 16 var ( 17 name string 18 err error 19 ) 20 for i := 0; i < 2; i++ { 21 name, err = Wget(context.Background(), "n1", "https://raw.githubusercontent.com/pingcap/tikv/master/Cargo.toml", ".") 22 if err != nil { 23 t.Fatalf("download failed %v", err) 24 } 25 26 if !IsFileExist(context.Background(), "n1", name) { 27 t.Fatalf("stat %s failed %v", name, err) 28 } 29 } 30 31 RemoveDir(context.Background(), "n1", name) 32 } 33 34 func TestInstallArchive(t *testing.T) { 35 tmpDir := fmt.Sprintf("/tmp/chaos/test_%d", time.Now().UnixNano()) 36 t.Logf("install on %s", tmpDir) 37 38 Mkdir(context.Background(), "n1", tmpDir) 39 defer RemoveDir(context.Background(), "n1", tmpDir) 40 41 err := InstallArchive(context.Background(), "n1", "https://github.com/pingcap/chaos/archive/master.zip", path.Join(tmpDir, "1")) 42 if err != nil { 43 t.Fatalf("install archive failed %v", err) 44 } 45 46 err = InstallArchive(context.Background(), "n1", "https://github.com/pingcap/chaos/archive/master.tar.gz", path.Join(tmpDir, "2")) 47 if err != nil { 48 t.Fatalf("install archive failed %v", err) 49 } 50 51 archFile := path.Join(tmpDir, "a.tar.gz") 52 testCreateArchive(context.Background(), t, path.Join(tmpDir, "test"), archFile) 53 err = InstallArchive(context.Background(), "n1", "file://"+archFile, path.Join(tmpDir, "3")) 54 if err != nil { 55 t.Fatalf("install archive failed %v", err) 56 } 57 } 58 59 func testCreateArchive(ctx context.Context, t *testing.T, srcDir string, name string) { 60 t.Logf("crate archieve %s from %s", name, srcDir) 61 Mkdir(ctx, "n1", srcDir) 62 WriteFile(ctx, "n1", path.Join(srcDir, "a.log"), "\"hello world\"") 63 64 if err := ssh.Exec(ctx, "n1", "tar", "-cf", name, "-C", srcDir, "."); err != nil { 65 t.Fatalf("tar %s to %s failed %v", srcDir, name, err) 66 } 67 } 68 69 func TestDaemon(t *testing.T) { 70 t.Log("test may only be run in the chaos docker") 71 72 tmpDir := fmt.Sprintf("/tmp/chaos/var_%d", time.Now().UnixNano()) 73 Mkdir(context.Background(), "n1", tmpDir) 74 defer RemoveDir(context.Background(), "n1", tmpDir) 75 76 cmd := "/bin/sleep" 77 pidFile := path.Join(tmpDir, "sleep.pid") 78 opts := NewDaemonOptions(tmpDir, pidFile) 79 err := StartDaemon(context.Background(), "n1", opts, cmd, "100") 80 if err != nil { 81 t.Fatalf("start daemon failed %v", err) 82 } 83 84 pidStr := parsePID(context.Background(), "n1", pidFile) 85 if pidStr == "" { 86 t.Fatal("must have a pid file") 87 } 88 89 pid, _ := strconv.Atoi(pidStr) 90 if !IsProcessExist(context.Background(), "n1", pid) { 91 t.Fatalf("pid %d must exist", pid) 92 } 93 94 if !IsDaemonRunning(context.Background(), "n1", cmd, pidFile) { 95 t.Fatal("daemon must be running") 96 } 97 98 err = StopDaemon(context.Background(), "n1", cmd, pidFile) 99 if err != nil { 100 t.Fatalf("stop daemon failed %v", err) 101 } 102 103 time.Sleep(time.Second) 104 105 if IsProcessExist(context.Background(), "n1", pid) { 106 t.Fatalf("pid %d must not exist", pid) 107 } 108 109 if IsFileExist(context.Background(), "n1", pidFile) { 110 t.Fatalf("pid file must not exist") 111 } 112 113 if IsDaemonRunning(context.Background(), "n1", cmd, pidFile) { 114 t.Fatal("daemon must be not running") 115 } 116 } 117 118 func TestWriteFile(t *testing.T) { 119 name := "/tmp/chaos/test.log" 120 if err := WriteFile(context.Background(), "n1", name, "\"[section]\na=b\""); err != nil { 121 t.Fatalf("write file failed %v", err) 122 } 123 124 data, err := ssh.CombinedOutput(context.Background(), "n1", "cat", name) 125 if err != nil { 126 t.Fatalf("read file failed %v", err) 127 } 128 129 seps := strings.Split(strings.TrimSpace(string(data)), "\n") 130 if len(seps) != 2 { 131 t.Fatalf("invalid read data %s", data) 132 } 133 }