diff --git a/load_test.go b/load_test.go deleted file mode 100644 index 381a630..0000000 --- a/load_test.go +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright (c) 2023 Cloudnatively Services Pvt Ltd -// -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package main - -import ( - "fmt" - "os/exec" - "testing" -) - -const ( - vus = "10" - duration = "5m" - schema_count = "20" -) - -func TestStreamBatchLoadWithK6(t *testing.T) { - if NewGlob.Mode == "load" { - CreateStream(t, NewGlob.Client, NewGlob.Stream) - cmd := exec.Command("k6", - "run", - "-e", fmt.Sprintf("P_URL=%s", NewGlob.Url.String()), - "-e", fmt.Sprintf("P_USERNAME=%s", NewGlob.Username), - "-e", fmt.Sprintf("P_PASSWORD=%s", NewGlob.Password), - "-e", fmt.Sprintf("P_STREAM=%s", NewGlob.Stream), - "-e", fmt.Sprintf("P_SCHEMA_COUNT=%s", schema_count), - "./scripts/load_batch_events.js", - "--vus=", vus, - "--duration=", duration) - - cmd.Run() - op, err := cmd.Output() - if err != nil { - t.Log(err) - } - t.Log(string(op)) - } -} - -func TestStreamLoadWithK6(t *testing.T) { - if NewGlob.Mode == "load" { - cmd := exec.Command("k6", - "run", - "-e", fmt.Sprintf("P_URL=%s", NewGlob.Url.String()), - "-e", fmt.Sprintf("P_USERNAME=%s", NewGlob.Username), - "-e", fmt.Sprintf("P_PASSWORD=%s", NewGlob.Password), - "-e", fmt.Sprintf("P_STREAM=%s", NewGlob.Stream), - "-e", fmt.Sprintf("P_SCHEMA_COUNT=%s", schema_count), - "./scripts/load_single_events.js", - "--vus=", vus, - "--duration=", duration) - - cmd.Run() - op, err := cmd.Output() - if err != nil { - t.Log(err) - } - t.Log(string(op)) - } -} diff --git a/quest.test b/quest.test index bd9c732..424d2b2 100755 Binary files a/quest.test and b/quest.test differ diff --git a/smoke_test.go b/quest_test.go similarity index 74% rename from smoke_test.go rename to quest_test.go index e7dc16b..a244677 100644 --- a/smoke_test.go +++ b/quest_test.go @@ -26,7 +26,13 @@ import ( "github.com/stretchr/testify/require" ) -func TestListLogStream(t *testing.T) { +const ( + vus = "10" + duration = "5m" + schema_count = "20" +) + +func TestSmokeListLogStream(t *testing.T) { req, err := NewGlob.Client.NewRequest("GET", "logstream", nil) require.NoErrorf(t, err, "Request failed: %s", err) @@ -36,19 +42,20 @@ func TestListLogStream(t *testing.T) { body := readAsString(response.Body) require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status) res, err := readJsonBody[[]string](bytes.NewBufferString(body)) - require.NoErrorf(t, err, "Unmarshal failed: %s", err) - for _, stream := range res { - if stream == NewGlob.Stream { - DeleteStream(t, NewGlob.Client, NewGlob.Stream) + if err != nil { + for _, stream := range res { + if stream == NewGlob.Stream { + DeleteStream(t, NewGlob.Client, NewGlob.Stream) + } } } } -func TestCreateStream(t *testing.T) { +func TestSmokeCreateStream(t *testing.T) { CreateStream(t, NewGlob.Client, NewGlob.Stream) } -func TestIngestEventsToStream(t *testing.T) { +func TestSmokeIngestEventsToStream(t *testing.T) { cmd := exec.Command("flog", "-f", "json", "-n", "50") var out strings.Builder cmd.Stdout = &out @@ -74,7 +81,7 @@ func TestIngestEventsToStream(t *testing.T) { Sleep() } -func TestLoadWithK6Stream(t *testing.T) { +func TestSmokeLoadWithK6Stream(t *testing.T) { CreateStream(t, NewGlob.Client, NewGlob.Stream) cmd := exec.Command("k6", "run", @@ -90,14 +97,14 @@ func TestLoadWithK6Stream(t *testing.T) { AssertStreamSchema(t, NewGlob.Client, NewGlob.Stream, SchemaBody) } -func TestSetAlert(t *testing.T) { +func TestSmokeSetAlert(t *testing.T) { req, _ := NewGlob.Client.NewRequest("PUT", "logstream/"+NewGlob.Stream+"/alert", strings.NewReader(AlertBody)) response, err := NewGlob.Client.Do(req) require.NoErrorf(t, err, "Request failed: %s", err) require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body)) } -func TestGetAlert(t *testing.T) { +func TestSmokeGetAlert(t *testing.T) { req, _ := NewGlob.Client.NewRequest("GET", "logstream/"+NewGlob.Stream+"/alert", nil) response, err := NewGlob.Client.Do(req) require.NoErrorf(t, err, "Request failed: %s", err) @@ -106,14 +113,14 @@ func TestGetAlert(t *testing.T) { require.JSONEq(t, AlertBody, body, "Get alert response doesn't match with Alert config returned") } -func TestSetRetention(t *testing.T) { +func TestSmokeSetRetention(t *testing.T) { req, _ := NewGlob.Client.NewRequest("PUT", "logstream/"+NewGlob.Stream+"/retention", strings.NewReader(RetentionBody)) response, err := NewGlob.Client.Do(req) require.NoErrorf(t, err, "Request failed: %s", err) require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body)) } -func TestGetRetention(t *testing.T) { +func TestSmokeGetRetention(t *testing.T) { req, _ := NewGlob.Client.NewRequest("GET", "logstream/"+NewGlob.Stream+"/retention", nil) response, err := NewGlob.Client.Do(req) require.NoErrorf(t, err, "Request failed: %s", err) @@ -122,19 +129,19 @@ func TestGetRetention(t *testing.T) { require.JSONEq(t, RetentionBody, body, "Get retention response doesn't match with retention config returned") } -func TestRbacBasic(t *testing.T) { +func TestSmokeRbacBasic(t *testing.T) { SetRole(t, NewGlob.Client, "dummy", dummyRole) AssertRole(t, NewGlob.Client, "dummy", dummyRole) CreateUserWithRole(t, NewGlob.Client, "dummy", []string{"dummy"}) userClient := NewGlob.Client userClient.Username = "dummy" userClient.Password = RegenPassword(t, NewGlob.Client, "dummy") - checkAPIAccess(t, userClient, NewGlob.Stream) + checkAPIAccess(t, userClient, NewGlob.Stream, "editor") DeleteUser(t, NewGlob.Client, "dummy") DeleteRole(t, NewGlob.Client, "dummy") } -func TestRoles(t *testing.T) { +func TestSmokeRoles(t *testing.T) { cases := []struct { roleName string body string @@ -152,7 +159,7 @@ func TestRoles(t *testing.T) { body: RoleWriter(NewGlob.Stream), }, { - roleName: "ingestor", + roleName: "ingest", body: RoleIngestor(NewGlob.Stream), }, } @@ -167,13 +174,57 @@ func TestRoles(t *testing.T) { userClient := NewGlob.Client userClient.Username = username userClient.Password = password - checkAPIAccess(t, userClient, NewGlob.Stream) + checkAPIAccess(t, userClient, NewGlob.Stream, tc.roleName) DeleteUser(t, NewGlob.Client, username) DeleteRole(t, NewGlob.Client, tc.roleName) }) } } +func TestLoadStreamBatchWithK6(t *testing.T) { + if NewGlob.Mode == "load" { + cmd := exec.Command("k6", + "run", + "-e", fmt.Sprintf("P_URL=%s", NewGlob.Url.String()), + "-e", fmt.Sprintf("P_USERNAME=%s", NewGlob.Username), + "-e", fmt.Sprintf("P_PASSWORD=%s", NewGlob.Password), + "-e", fmt.Sprintf("P_STREAM=%s", NewGlob.Stream), + "-e", fmt.Sprintf("P_SCHEMA_COUNT=%s", schema_count), + "./scripts/load_batch_events.js", + "--vus=", vus, + "--duration=", duration) + + cmd.Run() + op, err := cmd.Output() + if err != nil { + t.Log(err) + } + t.Log(string(op)) + } +} + +func TestLoadStreamNoBatchWithK6(t *testing.T) { + if NewGlob.Mode == "load" { + cmd := exec.Command("k6", + "run", + "-e", fmt.Sprintf("P_URL=%s", NewGlob.Url.String()), + "-e", fmt.Sprintf("P_USERNAME=%s", NewGlob.Username), + "-e", fmt.Sprintf("P_PASSWORD=%s", NewGlob.Password), + "-e", fmt.Sprintf("P_STREAM=%s", NewGlob.Stream), + "-e", fmt.Sprintf("P_SCHEMA_COUNT=%s", schema_count), + "./scripts/load_single_events.js", + "--vus=", vus, + "--duration=", duration) + + cmd.Run() + op, err := cmd.Output() + if err != nil { + t.Log(err) + } + t.Log(string(op)) + } +} + func TestDeleteStream(t *testing.T) { DeleteStream(t, NewGlob.Client, NewGlob.Stream) } diff --git a/test_utils.go b/test_utils.go index 4184aff..24d5ffd 100644 --- a/test_utils.go +++ b/test_utils.go @@ -147,22 +147,101 @@ func DeleteRole(t *testing.T, client HTTPClient, roleName string) { require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body)) } -func checkAPIAccess(t *testing.T, client HTTPClient, stream string) { - // Check access to non-protected API - req, _ := client.NewRequest("GET", "liveness", nil) +func PutSingleEvent(t *testing.T, client HTTPClient, stream string) { + payload := `{ + "id": "id;objectId", + "maxRunDistance": "float;1;20;1", + "cpf": "cpf", + "cnpj": "cnpj", + "pretendSalary": "money", + "age": "int;20;80", + "gender": "gender", + "firstName": "firstName", + "lastName": "lastName", + "phone": "maskInt;+55 (83) 9####-####", + "address": "address", + "hairColor": "color" + }` + req, _ := client.NewRequest("POST", "logstream/"+stream, bytes.NewBufferString(payload)) response, err := client.Do(req) - require.NoErrorf(t, err, "Request failed: %s", err) - require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body)) - // Check access to protected API with access - req, _ = client.NewRequest("GET", "logstream", nil) - response, err = client.Do(req) require.NoErrorf(t, err, "Request failed: %s", err) require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body)) +} - // Attempt to call protected API without access - req, _ = client.NewRequest("DELETE", "logstream/"+stream, nil) - response, err = client.Do(req) - require.NoErrorf(t, err, "Request failed: %s", err) - require.Equalf(t, 403, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body)) +func checkAPIAccess(t *testing.T, client HTTPClient, stream string, role string) { + switch role { + case "editor": + // Check access to non-protected API + req, _ := client.NewRequest("GET", "liveness", nil) + response, err := client.Do(req) + require.NoErrorf(t, err, "Request failed: %s", err) + require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body)) + + // Check access to protected API with access + req, _ = client.NewRequest("GET", "logstream", nil) + response, err = client.Do(req) + require.NoErrorf(t, err, "Request failed: %s", err) + require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body)) + + // Attempt to call protected API without access + req, _ = client.NewRequest("DELETE", "logstream/"+stream, nil) + response, err = client.Do(req) + require.NoErrorf(t, err, "Request failed: %s", err) + require.Equalf(t, 403, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body)) + + case "writer": + // Check access to non-protected API + req, _ := client.NewRequest("GET", "liveness", nil) + response, err := client.Do(req) + require.NoErrorf(t, err, "Request failed: %s", err) + require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body)) + + // Check access to protected API with access + req, _ = client.NewRequest("GET", "logstream", nil) + response, err = client.Do(req) + require.NoErrorf(t, err, "Request failed: %s", err) + require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body)) + + // Attempt to call protected API without access + req, _ = client.NewRequest("DELETE", "logstream/"+stream, nil) + response, err = client.Do(req) + require.NoErrorf(t, err, "Request failed: %s", err) + require.Equalf(t, 403, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body)) + + case "reader": + // Check access to non-protected API + req, _ := client.NewRequest("GET", "liveness", nil) + response, err := client.Do(req) + require.NoErrorf(t, err, "Request failed: %s", err) + require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body)) + + // Check access to protected API with access + req, _ = client.NewRequest("GET", "logstream", nil) + response, err = client.Do(req) + require.NoErrorf(t, err, "Request failed: %s", err) + require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body)) + + // Attempt to call protected API without access + req, _ = client.NewRequest("DELETE", "logstream/"+stream, nil) + response, err = client.Do(req) + require.NoErrorf(t, err, "Request failed: %s", err) + require.Equalf(t, 403, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body)) + + case "ingest": + // Check access to non-protected API + req, _ := client.NewRequest("GET", "liveness", nil) + response, err := client.Do(req) + require.NoErrorf(t, err, "Request failed: %s", err) + require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body)) + + // Check access to protected API with access + PutSingleEvent(t, client, stream) + + // Attempt to call protected API without access + req, _ = client.NewRequest("DELETE", "logstream/"+stream, nil) + response, err = client.Do(req) + require.NoErrorf(t, err, "Request failed: %s", err) + require.Equalf(t, 403, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body)) + } }