diff --git a/ciao-controller/compute_test.go b/ciao-controller/compute_test.go index b7e873859..8d3328dac 100644 --- a/ciao-controller/compute_test.go +++ b/ciao-controller/compute_test.go @@ -31,9 +31,12 @@ import ( "github.com/01org/ciao/testutil" ) -func testHTTPRequest(t *testing.T, method string, URL string, expectedResponse int, data []byte) []byte { +func testHTTPRequest(t *testing.T, method string, URL string, expectedResponse int, data []byte, validToken bool) []byte { req, err := http.NewRequest(method, URL, bytes.NewBuffer(data)) - req.Header.Set("X-Auth-Token", "imavalidtoken") + + if validToken { + req.Header.Set("X-Auth-Token", "imavalidtoken") + } if data != nil { req.Header.Set("Content-Type", "application/json") } @@ -91,7 +94,7 @@ func testCreateServer(t *testing.T, n int) payloads.ComputeServers { t.Fatal(err) } - body := testHTTPRequest(t, "POST", url, http.StatusAccepted, b) + body := testHTTPRequest(t, "POST", url, http.StatusAccepted, b, true) servers := payloads.NewComputeServers() @@ -110,7 +113,7 @@ func testCreateServer(t *testing.T, n int) payloads.ComputeServers { func testListServerDetailsTenant(t *testing.T, tenantID string) payloads.ComputeServers { url := testutil.ComputeURL + "/v2.1/" + tenantID + "/servers/detail" - body := testHTTPRequest(t, "GET", url, http.StatusOK, nil) + body := testHTTPRequest(t, "GET", url, http.StatusOK, nil, true) s := payloads.NewComputeServers() err := json.Unmarshal(body, &s) @@ -125,6 +128,36 @@ func TestCreateSingleServer(t *testing.T) { _ = testCreateServer(t, 1) } +func TestCreateSingleServerInvalidToken(t *testing.T) { + tenant, err := context.ds.GetTenant(testutil.ComputeUser) + if err != nil { + t.Fatal(err) + } + + // get a valid workload ID + wls, err := context.ds.GetWorkloads() + if err != nil { + t.Fatal(err) + } + + if len(wls) == 0 { + t.Fatal("No valid workloads") + } + + url := testutil.ComputeURL + "/v2.1/" + tenant.ID + "/servers" + + var server payloads.ComputeCreateServer + server.Server.MaxInstances = 1 + server.Server.Workload = wls[0].ID + + b, err := json.Marshal(server) + if err != nil { + t.Fatal(err) + } + + _ = testHTTPRequest(t, "POST", url, http.StatusUnauthorized, b, false) +} + func TestListServerDetailsTenant(t *testing.T) { tenant, err := context.ds.GetTenant(testutil.ComputeUser) if err != nil { @@ -143,7 +176,22 @@ func TestListServerDetailsTenant(t *testing.T) { } } -func TestListServerDetailsWorkload(t *testing.T) { +func TestListServerDetailsTenantInvalidToken(t *testing.T) { + tenant, err := context.ds.GetTenant(testutil.ComputeUser) + if err != nil { + t.Fatal(err) + } + + servers := testCreateServer(t, 1) + if servers.TotalServers != 1 { + t.Fatal(err) + } + + url := testutil.ComputeURL + "/v2.1/" + tenant.ID + "/servers/detail" + _ = testHTTPRequest(t, "GET", url, http.StatusUnauthorized, nil, false) +} + +func testListServerDetailsWorkload(t *testing.T, httpExpectedStatus int, validToken bool) { // get a valid workload ID wls, err := context.ds.GetWorkloads() if err != nil { @@ -161,7 +209,7 @@ func TestListServerDetailsWorkload(t *testing.T) { url := testutil.ComputeURL + "/v2.1/flavors/" + wls[0].ID + "/servers/detail" - body := testHTTPRequest(t, "GET", url, http.StatusOK, nil) + body := testHTTPRequest(t, "GET", url, httpExpectedStatus, nil, validToken) var s payloads.ComputeServers err = json.Unmarshal(body, &s) @@ -174,7 +222,11 @@ func TestListServerDetailsWorkload(t *testing.T) { } } -func TestShowServerDetails(t *testing.T) { +func TestListServerDetailsWorkload(t *testing.T) { + testListServerDetailsWorkload(t, http.StatusOK, true) +} + +func testShowServerDetails(t *testing.T, httpExpectedStatus int, validToken bool) { tenant, err := context.ds.GetTenant(testutil.ComputeUser) if err != nil { t.Fatal(err) @@ -196,7 +248,11 @@ func TestShowServerDetails(t *testing.T) { for _, s1 := range s.Servers { url := tURL + s1.ID - body := testHTTPRequest(t, "GET", url, http.StatusOK, nil) + body := testHTTPRequest(t, "GET", url, httpExpectedStatus, nil, validToken) + // stop evaluating in case the scenario is InvalidToken + if httpExpectedStatus == 401 { + return + } var s2 payloads.ComputeServer err = json.Unmarshal(body, &s2) @@ -211,7 +267,15 @@ func TestShowServerDetails(t *testing.T) { } } -func TestDeleteServer(t *testing.T) { +func TestShowServerDetails(t *testing.T) { + testShowServerDetails(t, http.StatusOK, true) +} + +func TestShowServerDetailsInvalidToken(t *testing.T) { + testShowServerDetails(t, http.StatusUnauthorized, false) +} + +func testDeleteServer(t *testing.T, httpExpectedStatus int, httpExpectedErrorStatus int, validToken bool) { tenant, err := context.ds.GetTenant(testutil.ComputeUser) if err != nil { t.Fatal(err) @@ -246,15 +310,22 @@ func TestDeleteServer(t *testing.T) { for _, s1 := range s.Servers { url := tURL + s1.ID if s1.HostID != "" { - _ = testHTTPRequest(t, "DELETE", url, http.StatusAccepted, nil) + _ = testHTTPRequest(t, "DELETE", url, httpExpectedStatus, nil, validToken) } else { - _ = testHTTPRequest(t, "DELETE", url, http.StatusInternalServerError, nil) + _ = testHTTPRequest(t, "DELETE", url, httpExpectedErrorStatus, nil, validToken) } - } } -func TestServersActionStart(t *testing.T) { +func TestDeleteServer(t *testing.T) { + testDeleteServer(t, http.StatusAccepted, http.StatusInternalServerError, true) +} + +func TestDeleteServerInvalidToken(t *testing.T) { + testDeleteServer(t, http.StatusUnauthorized, http.StatusUnauthorized, false) +} + +func testServersActionStart(t *testing.T, httpExpectedStatus int, validToken bool) { tenant, err := context.ds.GetTenant(testutil.ComputeUser) if err != nil { t.Fatal(err) @@ -303,10 +374,18 @@ func TestServersActionStart(t *testing.T) { t.Fatal(err) } - _ = testHTTPRequest(t, "POST", url, http.StatusAccepted, b) + _ = testHTTPRequest(t, "POST", url, httpExpectedStatus, b, validToken) } -func TestServersActionStop(t *testing.T) { +func TestServersActionStart(t *testing.T) { + testServersActionStart(t, http.StatusAccepted, true) +} + +func TestServersActionStartInvalidToken(t *testing.T) { + testServersActionStart(t, http.StatusUnauthorized, false) +} + +func testServersActionStop(t *testing.T, httpExpectedStatus int, action string) { tenant, err := context.ds.GetTenant(testutil.ComputeUser) if err != nil { t.Fatal(err) @@ -335,7 +414,7 @@ func TestServersActionStop(t *testing.T) { ids = append(ids, servers.Servers[0].ID) cmd := payloads.CiaoServersAction{ - Action: "os-stop", + Action: action, ServerIDs: ids, } @@ -344,10 +423,18 @@ func TestServersActionStop(t *testing.T) { t.Fatal(err) } - _ = testHTTPRequest(t, "POST", url, http.StatusAccepted, b) + _ = testHTTPRequest(t, "POST", url, httpExpectedStatus, b, true) } -func TestServerActionStop(t *testing.T) { +func TestServersActionStop(t *testing.T) { + testServersActionStop(t, http.StatusAccepted, "os-stop") +} + +func TestServersActionStopWrongAction(t *testing.T) { + testServersActionStop(t, http.StatusServiceUnavailable, "wrong-action") +} + +func testServerActionStop(t *testing.T, httpExpectedStatus int, validToken bool) { action := "os-stop" tenant, err := context.ds.GetTenant(testutil.ComputeUser) @@ -373,7 +460,15 @@ func TestServerActionStop(t *testing.T) { time.Sleep(1 * time.Second) url := testutil.ComputeURL + "/v2.1/" + tenant.ID + "/servers/" + servers.Servers[0].ID + "/action" - _ = testHTTPRequest(t, "POST", url, http.StatusAccepted, []byte(action)) + _ = testHTTPRequest(t, "POST", url, httpExpectedStatus, []byte(action), validToken) +} + +func TestServerActionStop(t *testing.T) { + testServerActionStop(t, http.StatusAccepted, true) +} + +func TestServerActionStopInvalidToken(t *testing.T) { + testServerActionStop(t, http.StatusUnauthorized, false) } func TestServerActionStart(t *testing.T) { @@ -420,10 +515,10 @@ func TestServerActionStart(t *testing.T) { time.Sleep(1 * time.Second) url := testutil.ComputeURL + "/v2.1/" + tenant.ID + "/servers/" + servers.Servers[0].ID + "/action" - _ = testHTTPRequest(t, "POST", url, http.StatusAccepted, []byte(action)) + _ = testHTTPRequest(t, "POST", url, http.StatusAccepted, []byte(action), true) } -func TestListFlavors(t *testing.T) { +func testListFlavors(t *testing.T, httpExpectedStatus int, data []byte, validToken bool) { tenant, err := context.ds.GetTenant(testutil.ComputeUser) if err != nil { t.Fatal(err) @@ -436,7 +531,11 @@ func TestListFlavors(t *testing.T) { t.Fatal(err) } - body := testHTTPRequest(t, "GET", url, http.StatusOK, nil) + body := testHTTPRequest(t, "GET", url, httpExpectedStatus, data, validToken) + // stop evaluating in case the scenario is InvalidToken + if httpExpectedStatus == 401 { + return + } var flavors payloads.ComputeFlavors err = json.Unmarshal(body, &flavors) @@ -463,7 +562,15 @@ func TestListFlavors(t *testing.T) { } } -func TestShowFlavorDetails(t *testing.T) { +func TestListFlavors(t *testing.T) { + testListFlavors(t, http.StatusOK, nil, true) +} + +func TestListFlavorsInvalidToken(t *testing.T) { + testListFlavors(t, http.StatusUnauthorized, nil, false) +} + +func testShowFlavorDetails(t *testing.T, httpExpectedStatus int, validToken bool) { tenant, err := context.ds.GetTenant(testutil.ComputeUser) if err != nil { t.Fatal(err) @@ -495,7 +602,11 @@ func TestShowFlavorDetails(t *testing.T) { } url := tURL + w.ID - body := testHTTPRequest(t, "GET", url, http.StatusOK, nil) + body := testHTTPRequest(t, "GET", url, httpExpectedStatus, nil, validToken) + // stop evaluating in case the scenario is InvalidToken + if httpExpectedStatus == 401 { + return + } var f payloads.ComputeFlavorDetails @@ -510,7 +621,33 @@ func TestShowFlavorDetails(t *testing.T) { } } -func TestListTenantResources(t *testing.T) { +func TestShowFlavorDetails(t *testing.T) { + testShowFlavorDetails(t, http.StatusOK, true) +} + +func TestShowFlavorDetailsInvalidToken(t *testing.T) { + testShowFlavorDetails(t, http.StatusUnauthorized, false) +} + +func testListFlavorsDetails(t *testing.T, httpExpectedStatus int, data []byte, validToken bool) { + tenant, err := context.ds.GetTenant(testutil.ComputeUser) + if err != nil { + t.Fatal(err) + } + + url := testutil.ComputeURL + "/v2.1/" + tenant.ID + "/flavors/detail" + _ = testHTTPRequest(t, "GET", url, httpExpectedStatus, data, validToken) +} + +func TestListFlavorsDetails(t *testing.T) { + testListFlavorsDetails(t, http.StatusOK, nil, true) +} + +func TestListFlavorsDetailsInvalidToken(t *testing.T) { + testListFlavorsDetails(t, http.StatusUnauthorized, nil, false) +} + +func testListTenantResources(t *testing.T, httpExpectedStatus int, validToken bool) { var usage payloads.CiaoUsageHistory endTime := time.Now() @@ -534,7 +671,11 @@ func TestListTenantResources(t *testing.T) { tURL += v.Encode() - body := testHTTPRequest(t, "GET", tURL, http.StatusOK, nil) + body := testHTTPRequest(t, "GET", tURL, httpExpectedStatus, nil, validToken) + // stop evaluating in case the scenario is InvalidToken + if httpExpectedStatus == 401 { + return + } var result payloads.CiaoUsageHistory @@ -548,7 +689,15 @@ func TestListTenantResources(t *testing.T) { } } -func TestListTenantQuotas(t *testing.T) { +func TestListTenantResources(t *testing.T) { + testListTenantResources(t, http.StatusOK, true) +} + +func TestListTenantResourcesInvalidToken(t *testing.T) { + testListTenantResources(t, http.StatusUnauthorized, false) +} + +func testListTenantQuotas(t *testing.T, httpExpectedStatus int, validToken bool) { tenant, err := context.ds.GetTenant(testutil.ComputeUser) if err != nil { t.Fatal(err) @@ -580,7 +729,11 @@ func TestListTenantQuotas(t *testing.T) { expected.ID = tenant.ID - body := testHTTPRequest(t, "GET", url, http.StatusOK, nil) + body := testHTTPRequest(t, "GET", url, httpExpectedStatus, nil, validToken) + // stop evaluating in case the scenario is InvalidToken + if httpExpectedStatus == 401 { + return + } var result payloads.CiaoTenantResources @@ -596,7 +749,15 @@ func TestListTenantQuotas(t *testing.T) { } } -func TestListEventsTenant(t *testing.T) { +func TestListTenantQuotas(t *testing.T) { + testListTenantQuotas(t, http.StatusOK, true) +} + +func TestListTenantQuotasInvalidToken(t *testing.T) { + testListTenantQuotas(t, http.StatusUnauthorized, false) +} + +func testListEventsTenant(t *testing.T, httpExpectedStatus int, validToken bool) { tenant, err := context.ds.GetTenant(testutil.ComputeUser) if err != nil { t.Fatal(err) @@ -625,7 +786,7 @@ func TestListEventsTenant(t *testing.T) { expected.Events = append(expected.Events, event) } - body := testHTTPRequest(t, "GET", url, http.StatusOK, nil) + body := testHTTPRequest(t, "GET", url, httpExpectedStatus, nil, validToken) var result payloads.CiaoEvents @@ -639,7 +800,11 @@ func TestListEventsTenant(t *testing.T) { } } -func TestListNodeServers(t *testing.T) { +func TestListEventsTenant(t *testing.T) { + testListEventsTenant(t, http.StatusOK, true) +} + +func testListNodeServers(t *testing.T, httpExpectedStatus int, validToken bool) { computeNodes := context.ds.GetNodeLastStats() for _, n := range computeNodes.Nodes { @@ -650,7 +815,11 @@ func TestListNodeServers(t *testing.T) { url := testutil.ComputeURL + "/v2.1/nodes/" + n.ID + "/servers/detail" - body := testHTTPRequest(t, "GET", url, http.StatusOK, nil) + body := testHTTPRequest(t, "GET", url, httpExpectedStatus, nil, validToken) + // stop evaluating in case the scenario is InvalidToken + if httpExpectedStatus == 401 { + return + } var result payloads.CiaoServersStats @@ -669,7 +838,15 @@ func TestListNodeServers(t *testing.T) { } } -func TestListTenants(t *testing.T) { +func TestListNodeServers(t *testing.T) { + testListNodeServers(t, http.StatusOK, true) +} + +func TestListNodeServersInvalidToken(t *testing.T) { + testListNodeServers(t, http.StatusUnauthorized, false) +} + +func testListTenants(t *testing.T, httpExpectedStatus int, validToken bool) { tenants, err := context.ds.GetAllTenants() if err != nil { t.Fatal(err) @@ -691,7 +868,11 @@ func TestListTenants(t *testing.T) { url := testutil.ComputeURL + "/v2.1/tenants" - body := testHTTPRequest(t, "GET", url, http.StatusOK, nil) + body := testHTTPRequest(t, "GET", url, httpExpectedStatus, nil, validToken) + // stop evaluating in case the scenario is InvalidToken + if httpExpectedStatus == 401 { + return + } var result payloads.CiaoComputeTenants @@ -705,7 +886,15 @@ func TestListTenants(t *testing.T) { } } -func TestListNodes(t *testing.T) { +func TestListTenants(t *testing.T) { + testListTenants(t, http.StatusOK, true) +} + +func TestListTenantsInvalidToken(t *testing.T) { + testListTenants(t, http.StatusUnauthorized, false) +} + +func testListNodes(t *testing.T, httpExpectedStatus int, validToken bool) { expected := context.ds.GetNodeLastStats() summary, err := context.ds.GetNodeSummary() @@ -731,7 +920,11 @@ func TestListNodes(t *testing.T) { url := testutil.ComputeURL + "/v2.1/nodes" - body := testHTTPRequest(t, "GET", url, http.StatusOK, nil) + body := testHTTPRequest(t, "GET", url, httpExpectedStatus, nil, validToken) + // stop evaluating in case the scenario is InvalidToken + if httpExpectedStatus == 401 { + return + } var result payloads.CiaoComputeNodes @@ -749,7 +942,15 @@ func TestListNodes(t *testing.T) { } } -func TestNodeSummary(t *testing.T) { +func TestListNodes(t *testing.T) { + testListNodes(t, http.StatusOK, true) +} + +func TestListNodesInvalidToken(t *testing.T) { + testListNodes(t, http.StatusUnauthorized, false) +} + +func testNodeSummary(t *testing.T, httpExpectedStatus int, validToken bool) { var expected payloads.CiaoClusterStatus computeNodes := context.ds.GetNodeLastStats() @@ -769,7 +970,11 @@ func TestNodeSummary(t *testing.T) { url := testutil.ComputeURL + "/v2.1/nodes/summary" - body := testHTTPRequest(t, "GET", url, http.StatusOK, nil) + body := testHTTPRequest(t, "GET", url, httpExpectedStatus, nil, validToken) + // stop evaluating in case the scenario is InvalidToken + if httpExpectedStatus == 401 { + return + } var result payloads.CiaoClusterStatus @@ -783,7 +988,15 @@ func TestNodeSummary(t *testing.T) { } } -func TestListCNCIs(t *testing.T) { +func TestNodeSummary(t *testing.T) { + testNodeSummary(t, http.StatusOK, true) +} + +func TestNodeSummaryInvalidToken(t *testing.T) { + testNodeSummary(t, http.StatusUnauthorized, false) +} + +func testListCNCIs(t *testing.T, httpExpectedStatus int, validToken bool) { var expected payloads.CiaoCNCIs cncis, err := context.ds.GetTenantCNCISummary("") @@ -818,7 +1031,11 @@ func TestListCNCIs(t *testing.T) { url := testutil.ComputeURL + "/v2.1/cncis" - body := testHTTPRequest(t, "GET", url, http.StatusOK, nil) + body := testHTTPRequest(t, "GET", url, httpExpectedStatus, nil, validToken) + // stop evaluating in case the scenario is InvalidToken + if httpExpectedStatus == 401 { + return + } var result payloads.CiaoCNCIs @@ -832,7 +1049,15 @@ func TestListCNCIs(t *testing.T) { } } -func TestListCNCIDetails(t *testing.T) { +func TestListCNCIs(t *testing.T) { + testListCNCIs(t, http.StatusOK, true) +} + +func TestListCNCIsInvalidToken(t *testing.T) { + testListCNCIs(t, http.StatusUnauthorized, false) +} + +func testListCNCIDetails(t *testing.T, httpExpectedStatus int, validToken bool) { cncis, err := context.ds.GetTenantCNCISummary("") if err != nil { t.Fatal(err) @@ -868,7 +1093,11 @@ func TestListCNCIDetails(t *testing.T) { url := testutil.ComputeURL + "/v2.1/cncis/" + cnci.InstanceID + "/detail" - body := testHTTPRequest(t, "GET", url, http.StatusOK, nil) + body := testHTTPRequest(t, "GET", url, httpExpectedStatus, nil, validToken) + // stop evaluating in case the scenario is InvalidToken + if httpExpectedStatus == 401 { + return + } var result payloads.CiaoCNCI @@ -883,7 +1112,15 @@ func TestListCNCIDetails(t *testing.T) { } } -func TestListTraces(t *testing.T) { +func TestListCNCIDetails(t *testing.T) { + testListCNCIDetails(t, http.StatusOK, true) +} + +func TestListCNCIDetailsInvalidToken(t *testing.T) { + testListCNCIDetails(t, http.StatusUnauthorized, false) +} + +func testListTraces(t *testing.T, httpExpectedStatus int, validToken bool) { var expected payloads.CiaoTracesSummary client := testStartTracedWorkload(t) @@ -908,7 +1145,11 @@ func TestListTraces(t *testing.T) { url := testutil.ComputeURL + "/v2.1/traces" - body := testHTTPRequest(t, "GET", url, http.StatusOK, nil) + body := testHTTPRequest(t, "GET", url, httpExpectedStatus, nil, validToken) + // stop evaluating in case the scenario is InvalidToken + if httpExpectedStatus == 401 { + return + } var result payloads.CiaoTracesSummary @@ -922,7 +1163,15 @@ func TestListTraces(t *testing.T) { } } -func TestListEvents(t *testing.T) { +func TestListTraces(t *testing.T) { + testListTraces(t, http.StatusOK, true) +} + +func TestListTracesInvalidToken(t *testing.T) { + testListTraces(t, http.StatusUnauthorized, false) +} + +func testListEvents(t *testing.T, httpExpectedStatus int, validToken bool) { url := testutil.ComputeURL + "/v2.1/events" expected := payloads.NewCiaoEvents() @@ -942,7 +1191,11 @@ func TestListEvents(t *testing.T) { expected.Events = append(expected.Events, event) } - body := testHTTPRequest(t, "GET", url, http.StatusOK, nil) + body := testHTTPRequest(t, "GET", url, httpExpectedStatus, nil, validToken) + // stop evaluating in case the scenario is InvalidToken + if httpExpectedStatus == 401 { + return + } var result payloads.CiaoEvents @@ -956,10 +1209,22 @@ func TestListEvents(t *testing.T) { } } -func TestClearEvents(t *testing.T) { +func TestListEvents(t *testing.T) { + testListEvents(t, http.StatusOK, true) +} + +func TestListEventsInvalidToken(t *testing.T) { + testListEvents(t, http.StatusUnauthorized, false) +} + +func testClearEvents(t *testing.T, httpExpectedStatus int, validToken bool) { url := testutil.ComputeURL + "/v2.1/events" - _ = testHTTPRequest(t, "DELETE", url, http.StatusAccepted, nil) + _ = testHTTPRequest(t, "DELETE", url, httpExpectedStatus, nil, validToken) + // stop evaluating in case the scenario is InvalidToken + if httpExpectedStatus == 401 { + return + } logs, err := context.ds.GetEventLog() if err != nil { @@ -971,7 +1236,15 @@ func TestClearEvents(t *testing.T) { } } -func TestTraceData(t *testing.T) { +func TestClearEvents(t *testing.T) { + testClearEvents(t, http.StatusAccepted, true) +} + +func TestClearEventsInvalidToken(t *testing.T) { + testClearEvents(t, http.StatusUnauthorized, false) +} + +func testTraceData(t *testing.T, httpExpectedStatus int, validToken bool) { client := testStartTracedWorkload(t) defer client.Ssntp.Close() @@ -1006,7 +1279,11 @@ func TestTraceData(t *testing.T) { url := testutil.ComputeURL + "/v2.1/traces/" + s.BatchID - body := testHTTPRequest(t, "GET", url, http.StatusOK, nil) + body := testHTTPRequest(t, "GET", url, httpExpectedStatus, nil, validToken) + // stop evaluating in case the scenario is InvalidToken + if httpExpectedStatus == 401 { + return + } var result payloads.CiaoTraceData @@ -1020,3 +1297,11 @@ func TestTraceData(t *testing.T) { } } } + +func TestTraceData(t *testing.T) { + testTraceData(t, http.StatusOK, true) +} + +func TestTraceDataInvalidToken(t *testing.T) { + testTraceData(t, http.StatusUnauthorized, false) +}