2015-07-28 21:10:17 +00:00
|
|
|
package supertest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2015-07-31 17:17:57 +00:00
|
|
|
"log"
|
2015-07-28 21:10:17 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2015-07-31 17:17:57 +00:00
|
|
|
gosuper "resin-supervisor/gosuper/gosuper"
|
2015-07-28 21:10:17 +00:00
|
|
|
)
|
|
|
|
|
2015-07-31 17:17:57 +00:00
|
|
|
var supervisorAddress string
|
|
|
|
var config gosuper.UserConfig
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
var err error
|
2015-08-03 19:06:03 +00:00
|
|
|
if gopath := os.Getenv("GOPATH"); gopath == "" {
|
|
|
|
log.Fatal("GOPATH is not set - where are you running this?")
|
|
|
|
} else if supervisorIP := os.Getenv("SUPERVISOR_IP"); supervisorIP == "" {
|
|
|
|
log.Fatal("Supervisor IP not set - is it running?")
|
|
|
|
} else if config, err = gosuper.ReadConfig(gopath + "/src/resin-supervisor/gosuper/config.json"); err != nil {
|
2015-07-31 17:17:57 +00:00
|
|
|
log.Fatal(err)
|
2015-08-03 19:06:03 +00:00
|
|
|
} else {
|
|
|
|
supervisorAddress = "http://" + supervisorIP + ":48484"
|
|
|
|
os.Exit(m.Run())
|
2015-07-31 17:17:57 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-28 21:10:17 +00:00
|
|
|
|
2015-07-31 17:17:57 +00:00
|
|
|
func TestPing(t *testing.T) {
|
2015-08-03 19:06:03 +00:00
|
|
|
if request, err := http.NewRequest("GET", supervisorAddress+"/ping?apikey=bananas", nil); err != nil {
|
2015-07-28 21:10:17 +00:00
|
|
|
t.Fatal(err)
|
2015-08-03 19:06:03 +00:00
|
|
|
} else if response, err := http.DefaultClient.Do(request); err != nil {
|
2015-07-28 21:10:17 +00:00
|
|
|
t.Fatal(err)
|
2015-08-03 19:06:03 +00:00
|
|
|
} else if response.StatusCode != http.StatusOK {
|
|
|
|
t.Fatalf("Expected 200, got %d", response.StatusCode)
|
2015-07-28 21:10:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPurge(t *testing.T) {
|
|
|
|
appId := config.ApplicationId
|
|
|
|
dataPath := "/resin-data/" + appId
|
|
|
|
|
2015-08-10 17:46:30 +00:00
|
|
|
if err := ioutil.WriteFile(dataPath+"/test", []byte("test"), 0777); err != nil {
|
2015-08-03 19:06:03 +00:00
|
|
|
t.Fatal("Could not create test file for purge")
|
|
|
|
} else if request, err := http.NewRequest("POST", supervisorAddress+"/v1/purge?apikey=bananas", strings.NewReader(`{"appId": "`+appId+`"}`)); err != nil {
|
2015-07-28 21:10:17 +00:00
|
|
|
t.Fatal(err)
|
2015-08-03 19:06:03 +00:00
|
|
|
} else {
|
|
|
|
request.Header.Set("Content-Type", "application/json")
|
2015-08-10 17:46:30 +00:00
|
|
|
response, err := http.DefaultClient.Do(request)
|
|
|
|
defer response.Body.Close()
|
|
|
|
if err != nil {
|
2015-08-03 19:06:03 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
} else if response.StatusCode != http.StatusOK {
|
|
|
|
t.Errorf("Expected 200, got %d", response.StatusCode)
|
|
|
|
if contents, err := ioutil.ReadAll(response.Body); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
|
|
|
t.Fatalf("Response: %s", contents)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if contents, err := ioutil.ReadAll(response.Body); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-09-18 13:48:39 +00:00
|
|
|
} else if !strings.EqualFold(string(contents), `{"Data":"OK","Error":""}`) {
|
2015-08-03 19:06:03 +00:00
|
|
|
t.Errorf("Purge response didn't match the expected JSON, got: %s", contents)
|
|
|
|
}
|
|
|
|
if dirContents, err := ioutil.ReadDir(dataPath); err != nil {
|
|
|
|
t.Errorf("Could not read the data path after purge: %s", err)
|
|
|
|
} else if len(dirContents) > 0 {
|
|
|
|
t.Error("Data directory not empty after purge")
|
|
|
|
}
|
|
|
|
}
|
2015-07-28 21:10:17 +00:00
|
|
|
}
|
|
|
|
}
|