mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-02-21 02:01:35 +00:00
First API implementation with purge and unit testing.
This commit is contained in:
parent
ed8adcdda9
commit
0587858f69
@ -9,4 +9,4 @@ WORKDIR /usr/src/app
|
||||
|
||||
# Run go install with -a (force rebuilding of all packages)
|
||||
# and -v (print package names as they are built)
|
||||
CMD go install -a -v ./...
|
||||
CMD go test ./... && go install -a -v ./...
|
||||
|
38
gosuper/config.go
Normal file
38
gosuper/config.go
Normal file
@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
)
|
||||
|
||||
type UserConfig struct {
|
||||
ApplicationId string
|
||||
ApiKey string
|
||||
UserId string
|
||||
Username string
|
||||
DeviceType string
|
||||
Uuid string
|
||||
RegisteredAt float64
|
||||
DeviceId float64
|
||||
}
|
||||
|
||||
func ReadConfig(path string) (UserConfig, error) {
|
||||
|
||||
var config UserConfig
|
||||
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return config, err
|
||||
}
|
||||
data := make([]byte, 1000)
|
||||
|
||||
count, err := f.Read(data)
|
||||
|
||||
if err != nil {
|
||||
return config, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data[:count], &config)
|
||||
|
||||
return config, err
|
||||
}
|
1
gosuper/config_for_test.json
Normal file
1
gosuper/config_for_test.json
Normal file
@ -0,0 +1 @@
|
||||
{"applicationId":"1939","apiKey":"SuperSecretAPIKey","userId":"141","username":"gh_pcarranzav","deviceType":"raspberry-pi2","files":{"network/settings":"[global]\nOfflineMode=false\n\n[WiFi]\nEnable=true\nTethering=false\n\n[Wired]\nEnable=true\nTethering=false\n\n[Bluetooth]\nEnable=true\nTethering=false","network/network.config":"[service_home_ethernet]\nType = ethernet\nNameservers = 8.8.8.8,8.8.4.4"},"uuid":"07cf830a18757a78e69293ef2daf315506074a59f694555c124f0151e67f8d","registered_at":1436987572906,"deviceId":24748}
|
@ -7,14 +7,31 @@ import (
|
||||
"resin-supervisor/Godeps/_workspace/src/github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
func homeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "Hello, resin user!")
|
||||
func pingHandler(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "OK")
|
||||
}
|
||||
|
||||
//var Config UserConfig // Disabled until we use Config
|
||||
var ResinDataPath string
|
||||
|
||||
func main() {
|
||||
fmt.Println("I'm in the main loop!!")
|
||||
fmt.Println("Resin Go Supervisor starting")
|
||||
/* Disabled until we use Config
|
||||
var err error
|
||||
Config, err = ReadConfig("/boot/config.json")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
*/
|
||||
|
||||
ResinDataPath = "/resin-data/"
|
||||
|
||||
r := mux.NewRouter()
|
||||
r.HandleFunc("/", homeHandler)
|
||||
r.HandleFunc("/ping", pingHandler)
|
||||
apiv1 := r.PathPrefix("/v1").Subrouter()
|
||||
|
||||
apiv1.HandleFunc("/purge", PurgeHandler).Methods("POST")
|
||||
|
||||
http.ListenAndServe(":8080", r)
|
||||
|
||||
}
|
||||
|
@ -1,11 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAdd(t *testing.T) {
|
||||
v := add(1,1)
|
||||
if v != 2 {
|
||||
t.Error("1+1 != 2")
|
||||
func TestPurge(t *testing.T) {
|
||||
appId := "1"
|
||||
req, _ := http.NewRequest("POST", "/api/v1/purge", strings.NewReader("applicationId="+appId))
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ResinDataPath = "test-data/"
|
||||
|
||||
dataPath := ResinDataPath + appId
|
||||
|
||||
err := os.MkdirAll(dataPath, 0755)
|
||||
if err != nil {
|
||||
t.Error("Could not create test directory for purge")
|
||||
}
|
||||
}
|
||||
|
||||
PurgeHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Purge didn't return %v", http.StatusOK)
|
||||
}
|
||||
if !strings.EqualFold(w.Body.String(), `{"Status":"OK","Error":""}`) {
|
||||
t.Errorf("Purge response didn't match the expected JSON, got: %s", w.Body.String())
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadConfig(t *testing.T) {
|
||||
config, err := ReadConfig("config_for_test.json")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if !strings.EqualFold(config.ApplicationId, "1939") || !strings.EqualFold(config.ApiKey, "SuperSecretAPIKey") {
|
||||
t.Error("Config not parsed correctly")
|
||||
}
|
||||
}
|
||||
|
56
gosuper/purge.go
Normal file
56
gosuper/purge.go
Normal file
@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type ApiResponse struct {
|
||||
Status string
|
||||
Error string
|
||||
}
|
||||
|
||||
func jsonResponse(w http.ResponseWriter, response interface{}, status int) {
|
||||
j, _ := json.Marshal(response)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
w.Write(j)
|
||||
}
|
||||
|
||||
func PurgeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("Purging /data")
|
||||
|
||||
appId := r.FormValue("applicationId")
|
||||
if appId == "" {
|
||||
jsonResponse(w, ApiResponse{"Error", "applicationId is required"}, 422)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate that the appId is an integer
|
||||
_, err := strconv.ParseInt(appId, 10, 0)
|
||||
if err != nil {
|
||||
jsonResponse(w, ApiResponse{"Error", "Invalid applicationId"}, 422)
|
||||
return
|
||||
}
|
||||
|
||||
directory := ResinDataPath + appId
|
||||
|
||||
err = os.RemoveAll(directory)
|
||||
|
||||
if err != nil {
|
||||
jsonResponse(w, ApiResponse{"Error", err.Error()}, 500)
|
||||
return
|
||||
}
|
||||
|
||||
err = os.Mkdir(directory, 0755)
|
||||
if err != nil {
|
||||
jsonResponse(w, ApiResponse{"Error", err.Error()}, 500)
|
||||
return
|
||||
}
|
||||
|
||||
jsonResponse(w, ApiResponse{"OK", ""}, 200)
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user