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) {
|
2015-07-28 21:10:17 +00:00
|
|
|
supervisorIP := os.Getenv("SUPERVISOR_IP")
|
|
|
|
if supervisorIP == "" {
|
2015-07-31 17:17:57 +00:00
|
|
|
log.Fatal("Supervisor IP not set - is it running?")
|
2015-07-28 21:10:17 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 17:17:57 +00:00
|
|
|
supervisorAddress = "http://" + supervisorIP + ":48484"
|
|
|
|
|
|
|
|
gopath := os.Getenv("GOPATH")
|
|
|
|
|
|
|
|
var err error
|
|
|
|
config, err = gosuper.ReadConfig(gopath + "/src/resin-supervisor/gosuper/config.json")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-07-28 21:10:17 +00:00
|
|
|
|
2015-07-31 17:17:57 +00:00
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
2015-07-28 21:10:17 +00:00
|
|
|
|
2015-07-31 17:17:57 +00:00
|
|
|
func TestPing(t *testing.T) {
|
|
|
|
request, err := http.NewRequest("GET", supervisorAddress+"/ping?apikey=bananas", nil)
|
2015-07-28 21:10:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := http.DefaultClient.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
t.Fatalf("Expected 200, got %d", res.StatusCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPurge(t *testing.T) {
|
|
|
|
appId := config.ApplicationId
|
|
|
|
dataPath := "/resin-data/" + appId
|
2015-07-31 17:17:57 +00:00
|
|
|
err := ioutil.WriteFile(dataPath+"/test", []byte("test"), 777)
|
2015-07-28 21:10:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Could not create test file for purge")
|
|
|
|
}
|
|
|
|
|
2015-07-31 17:17:57 +00:00
|
|
|
request, err := http.NewRequest("POST", supervisorAddress+"/v1/purge?apikey=bananas", strings.NewReader(`{"appId": "`+appId+`"}`))
|
2015-07-28 21:10:17 +00:00
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-31 17:17:57 +00:00
|
|
|
res, err := http.DefaultClient.Do(request)
|
2015-07-28 21:10:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
t.Fatalf("Expected 200, got %d", res.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
contents, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !strings.EqualFold(string(contents), `{"Status":"OK","Error":""}`) {
|
|
|
|
t.Errorf("Purge response didn't match the expected JSON, got: %s", contents)
|
|
|
|
}
|
|
|
|
|
|
|
|
dirContents, err := ioutil.ReadDir(dataPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not read the data path after purge: %s", err)
|
|
|
|
}
|
|
|
|
if len(dirContents) > 0 {
|
|
|
|
t.Error("Data directory not empty after purge")
|
|
|
|
}
|
|
|
|
}
|