github.com/vishnupahwa/lakctl@v0.0.2-alpha/test/basic_test.go (about) 1 package test 2 3 import ( 4 "bytes" 5 . "github.com/vishnupahwa/lakctl/test/short" 6 "net/http" 7 "os" 8 "os/exec" 9 "strings" 10 "syscall" 11 "testing" 12 "time" 13 ) 14 15 const port = ":8008" 16 const commandPort = ":9999" 17 18 var lakctl *exec.Cmd 19 20 func init() { 21 _ = os.Chdir("..") 22 } 23 func setup(t *testing.T) { 24 lakctl = exec.Command("./lakctl", "start", "-r", "go run ./testdata/testapp/main.go World") 25 lakctl.Stdout = os.Stdout 26 lakctl.Stderr = os.Stderr 27 lakctl.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} 28 Must(t, lakctl.Start()) 29 time.Sleep(1 * time.Second) 30 } 31 32 func cleanup() { 33 pid := lakctl.Process.Pid 34 _ = syscall.Kill(-pid, syscall.SIGTERM) 35 _ = lakctl.Process.Signal(syscall.SIGTERM) 36 _ = lakctl.Wait() 37 } 38 39 func TestStoppingCommand(t *testing.T) { 40 setup(t) 41 t.Cleanup(cleanup) 42 _, err := http.Get("http://localhost" + port + "/stop") 43 Ok(t, err) 44 time.Sleep(1 * time.Second) 45 46 _, err = http.Get("http://localhost" + commandPort) 47 Assert(t, err != nil, "expected error from get request") 48 } 49 50 func TestStoppingThenStartingCommand(t *testing.T) { 51 setup(t) 52 t.Cleanup(cleanup) 53 _, err := http.Get("http://localhost" + port + "/stop") 54 Ok(t, err) 55 _, err = http.Get("http://localhost" + port + "/start") 56 Ok(t, err) 57 time.Sleep(1 * time.Second) 58 59 response, _ := http.Get("http://localhost" + commandPort) 60 body := toString(t, response) 61 Equals(t, "Hello World", body) 62 } 63 64 func TestStoppingThenStartingThenStoppingCommand(t *testing.T) { 65 setup(t) 66 t.Cleanup(cleanup) 67 _, err := http.Get("http://localhost" + port + "/stop") 68 Ok(t, err) 69 _, err = http.Get("http://localhost" + port + "/start") 70 Ok(t, err) 71 _, err = http.Get("http://localhost" + port + "/stop") 72 Ok(t, err) 73 time.Sleep(1 * time.Second) 74 75 _, err = http.Get("http://localhost" + commandPort) 76 Assert(t, err != nil, "expected error from get request") 77 } 78 79 func TestRestart(t *testing.T) { 80 setup(t) 81 t.Cleanup(cleanup) 82 replaceJson := strings.NewReader(`{ 83 "old": "World", 84 "new": "世界" 85 }`) 86 _, err := http.Post("http://localhost"+port+"/restart/", "application/json", replaceJson) 87 Ok(t, err) 88 time.Sleep(1 * time.Second) 89 90 response, _ := http.Get("http://localhost" + commandPort) 91 body := toString(t, response) 92 Equals(t, "Hello 世界", body) 93 } 94 95 func TestRestartWithNoSubstitution(t *testing.T) { 96 setup(t) 97 t.Cleanup(cleanup) 98 replaceJson := strings.NewReader(`{}`) 99 _, err := http.Post("http://localhost"+port+"/restart/", "application/json", replaceJson) 100 Ok(t, err) 101 time.Sleep(1 * time.Second) 102 103 response, _ := http.Get("http://localhost" + commandPort) 104 body := toString(t, response) 105 Equals(t, "Hello World", body) 106 } 107 108 func toString(t *testing.T, response *http.Response) string { 109 if response == nil { 110 t.Fatal("No response was returned") 111 } 112 buf := new(bytes.Buffer) 113 _, _ = buf.ReadFrom(response.Body) 114 r := buf.String() 115 return r 116 }