func TestGetPool(t *testing.T) { pool := testPool() poolData := testPoolData() if err := zk.SetPool(pool); err != nil { t.Fatalf("couldn't set pool for get") } if err := zk.AddHosts(pool.Name, pool.Hosts); err != nil { t.Fatalf("couldn't set pool for get") } defer func() { if err := zk.DeletePool(pool.Name); err != nil { t.Fatalf("couldn't clean up") } }() statusCode, data, err := client.BuildAndSendRequest("GET", "/pools/"+pool.Name, "") if err != nil { t.Fatalf("could not get pool: %s", err) } if statusCode != 200 { t.Fatalf("incorrect status code returned, should be 200") } if data != poolData { t.Fatalf("Value from get not as expected \n %s \b %s", data, poolData) } }
func TestGetHosts(t *testing.T) { pool := testPool() host := testHost() hostData := testHostData() if err := zk.SetPool(pool); err != nil { t.Fatalf("failed to put pool to test add host") } defer func() { if err := zk.DeletePool(pool.Name); err != nil { t.Fatalf("could not clean up") } }() if err := zk.AddHosts(pool.Name, host); err != nil { t.Fatalf("couldn't add hosts to attempt get") } statusCode, data, err := client.BuildAndSendRequest("GET", "/pools/"+pool.Name+"/hosts", "") if err != nil { t.Fatalf("couldn't get hosts") } if statusCode != 200 { t.Fatalf("incorrect status code") } if data != hostData { t.Fatalf("data from get doesn't match the put data") } }
func TestDeleteHosts(t *testing.T) { pool := testPool() hostData := testHostData() if err := zk.SetPool(pool); err != nil { t.Fatalf("could not add pool to add hosts") } defer func() { if err := zk.DeletePool(pool.Name); err != nil { t.Fatalf("could not clean up") } }() statusCode, _, err := client.BuildAndSendRequest("PUT", "/pools/"+pool.Name+"/hosts", hostData) if err != nil { t.Fatalf("could not add hosts") } if statusCode != 200 { t.Fatalf("incorrect response status from add host req") } hMap := make(map[string][]string, 1) hRay := []string{"myHost", "yourHost", "ourHost"} hMap["Hosts"] = hRay b, err := json.Marshal(hMap) if err != nil { t.Fatalf("could not marshal hmap") } delHData := string(b) statusCode, _, err = client.BuildAndSendRequest("DELETE", "/pools/"+pool.Name+"/hosts", delHData) if err != nil { t.Fatalf("could not delete hosts") } if statusCode != 200 { t.Fatalf("incorrect status code from delete") } hList, err := zk.GetHosts(pool.Name) if err != nil { t.Fatalf("couldnt get hosts") } if len(hList) > 0 { t.Fatalf("hosts not deleted properly") } }
func SetPool(w http.ResponseWriter, r *http.Request) { err := GetUserSecretAndAuth(r) if err != nil { WriteResponse(w, NotAuthorizedStatusCode, GetErrorStatusJson(NotAuthenticatedStatus, err)) return } if r.Header.Get("Content-Type") != "application/json" { WriteResponse(w, BadRequestStatusCode, GetStatusJson(IncorrectContentTypeStatus)) return } body, err := GetRequestBody(r) if err != nil { WriteResponse(w, BadRequestStatusCode, GetErrorStatusJson(CouldNotReadRequestDataStatus, err)) return } var pool cfg.Pool err = json.Unmarshal(body, &pool) if err != nil { WriteResponse(w, BadRequestStatusCode, GetErrorStatusJson(CouldNotReadRequestDataStatus, err)) return } err = zk.SetPool(pool) if err != nil { WriteResponse(w, ServerErrorCode, GetErrorStatusJson(CouldNotCompleteOperationStatus, err)) return } //If the pool has hosts when sent in, call AddHosts with them if len(pool.Hosts) > 0 { err = zk.AddHosts(pool.Name, pool.Hosts) if err != nil { WriteResponse(w, ServerErrorCode, GetErrorStatusJson(CouldNotCompleteOperationStatus, err)) return } } WriteResponse(w, OkStatusCode, GetStatusJson(RequestSuccesfulStatus)) }
func TestAddHosts(t *testing.T) { pool := testPool() hostData := testHostData() if err := zk.SetPool(pool); err != nil { t.Fatalf("could not add pool to add hosts") } defer func() { if err := zk.DeletePool(pool.Name); err != nil { t.Fatalf("could not clean up") } }() statusCode, _, err := client.BuildAndSendRequest("PUT", "/pools/"+pool.Name+"/hosts", hostData) if err != nil { t.Fatalf("could not add hosts") } if statusCode != 200 { t.Fatalf("incorrect response status from add host req") } status, data, err := client.BuildAndSendRequest("GET", "/pools/"+pool.Name+"/hosts", "") if err != nil { t.Fatalf("could not get hosts") } if status != 200 { t.Fatalf("incorrect status code from get hosts req") } if data != hostData { t.Fatalf("host data not added properly") } }