Skip to content

test: add integration test with concurrent request load #402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/rond-authz/rond

go 1.21
go 1.22

toolchain go1.23.2

require (
github.com/davidebianchi/gswagger v0.10.0
Expand Down Expand Up @@ -30,6 +32,7 @@ require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/davidebianchi/go-jsonclient v1.5.0 // indirect
github.com/fredmaggiowski/gowq v0.5.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-ini/ini v1.67.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ github.com/foxcpp/go-mockdns v1.1.0 h1:jI0rD8M0wuYAxL7r/ynTrCQQq0BVqfB99Vgk7Dlme
github.com/foxcpp/go-mockdns v1.1.0/go.mod h1:IhLeSFGed3mJIAXPH2aiRQB+kqz7oqu8ld2qVbOu7Wk=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fredmaggiowski/gowq v0.5.0 h1:cUVA/u9pNr6qrGh8GQPHunZyRZNiTFStOKLwofABVCY=
github.com/fredmaggiowski/gowq v0.5.0/go.mod h1:yutQQ5WPK33bokWzB6jlkRoGRo5zw2zX+aML1TvNnTg=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
Expand Down
187 changes: 174 additions & 13 deletions integration_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package main

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"

"github.com/caarlos0/env/v11"
"github.com/rond-authz/rond/core"
"github.com/rond-authz/rond/internal/config"
"github.com/rond-authz/rond/internal/testutils"
"github.com/rond-authz/rond/openapi"

"github.com/caarlos0/env/v11"
"github.com/fredmaggiowski/gowq"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/require"
"gopkg.in/h2non/gock.v1"
Expand Down Expand Up @@ -80,8 +84,174 @@ func BenchmarkStartup(b *testing.B) {
}
}

func TestStartupAndLoadWithConcurrentRequests(t *testing.T) {
log, _ := test.NewNullLogger()

tmpdir, err := os.MkdirTemp("", "rond-startup-test-")
require.NoError(t, err)

policies := []string{`package policies`}
policies = append(policies, `allow_get {
verb := input.request.method
verb == "GET"
}`)
policies = append(policies, `allow_post {
verb := input.request.method
verb == "POST"
}`)
policies = append(policies, generateFilterPolicy("something")) // filter_something
policies = append(policies, generateProjectionPolicy("data")) // proj_data

oas := &openapi.OpenAPISpec{
Paths: map[string]openapi.PathVerbs{
"/allow-get": {
http.MethodGet: {
PermissionV2: &core.RondConfig{
RequestFlow: core.RequestFlow{PolicyName: "allow_get"},
},
},
http.MethodPost: {
PermissionV2: &core.RondConfig{
RequestFlow: core.RequestFlow{PolicyName: "allow_get"},
},
},
},
"/filter-something": {
http.MethodGet: {
PermissionV2: &core.RondConfig{
RequestFlow: core.RequestFlow{PolicyName: "filter_something", GenerateQuery: true},
},
},
},
"/project-data": {
http.MethodPost: {
PermissionV2: &core.RondConfig{
RequestFlow: core.RequestFlow{PolicyName: "allow_post"},
ResponseFlow: core.ResponseFlow{PolicyName: "proj_data"},
},
},
},
},
}
oasFileName := writeOAS(t, tmpdir, oas)
policiesFileName := writePolicies(t, tmpdir, policies)

defer gock.Off()
defer gock.DisableNetworkingFilters()
defer gock.DisableNetworking()

gock.EnableNetworking()
gock.NetworkingFilter(func(r *http.Request) bool {
if r.URL.Host == "localhost:3050" {
return false
}
if r.URL.Path == "/documentation/json" && r.URL.Host == "localhost:3050" {
return false
}
return true
})

gock.New("http://localhost:3050").
Persist().
Get("/documentation/json").
Reply(200).
File(oasFileName)

gock.New("http://localhost:3050/").
Persist().
Get("/allow-get").
Reply(200)
gock.New("http://localhost:3050/").
Persist().
Get("/filter-something").
Reply(200)
gock.New("http://localhost:3050/").
Persist().
Post("/project-data").
Reply(200).
JSON([]string{})

mongoHost := os.Getenv("MONGO_HOST_CI")
if mongoHost == "" {
mongoHost = testutils.LocalhostMongoDB
t.Logf("Connection to localhost MongoDB, on CI env this is a problem!")
}
randomizedDBNamePart := testutils.GetRandomName(10)
mongoDBName := fmt.Sprintf("test-%s", randomizedDBNamePart)
envs, err := env.ParseAsWithOptions[config.EnvironmentVariables](env.Options{
Environment: map[string]string{
"TARGET_SERVICE_HOST": "localhost:3050",
"TARGET_SERVICE_OAS_PATH": "/documentation/json",
"OPA_MODULES_DIRECTORY": policiesFileName,
"LOG_LEVEL": "fatal",
"MONGODB_URL": fmt.Sprintf("mongodb://%s/%s", mongoHost, mongoDBName),
"BINDINGS_COLLECTION_NAME": "bindings",
"ROLES_COLLECTION_NAME": "roles",
},
})
require.NoError(t, err)

app, err := setupService(envs, log)
require.NoError(t, err)
require.True(t, <-app.sdkBootState.IsReadyChan())
defer app.close()

// everything is up and running, now start bombarding the webserver
type RequestConf struct {
Verb string
Path string
ExpectedStatus int
}
dictionary := []RequestConf{
{Verb: http.MethodGet, Path: "/allow-get", ExpectedStatus: http.StatusOK},
{Verb: http.MethodPost, Path: "/allow-get", ExpectedStatus: http.StatusForbidden},
{Verb: http.MethodGet, Path: "/filter-something", ExpectedStatus: http.StatusOK},
{Verb: http.MethodPost, Path: "/filter-something", ExpectedStatus: http.StatusNotFound},
{Verb: http.MethodPost, Path: "/project-data", ExpectedStatus: http.StatusOK},
{Verb: http.MethodGet, Path: "/project-data", ExpectedStatus: http.StatusNotFound},
}

queue := gowq.New[RequestConf](100)

i := 0
for i < 100_000 {
d := dictionary[i%len(dictionary)]
i++
queue.Push(func(ctx context.Context) (RequestConf, error) {
w := httptest.NewRecorder()

req := httptest.NewRequest(d.Verb, d.Path, nil)
app.router.ServeHTTP(w, req)
require.Equal(t, d.ExpectedStatus, w.Result().StatusCode)

return d, nil
})
}

_, errors := queue.RunAll(context.TODO())
require.Len(t, errors, 0)
}

func writeOAS(t require.TestingT, tmpdir string, oas *openapi.OpenAPISpec) string {
oasContent, err := json.Marshal(oas)
require.NoError(t, err)

oasFileName := fmt.Sprintf("%s/oas.json", tmpdir)
err = os.WriteFile(oasFileName, oasContent, 0644)
require.NoError(t, err)
return oasFileName
}

func writePolicies(t require.TestingT, tmpdir string, policies []string) string {
policyFileName := fmt.Sprintf("%s/policies.rego", tmpdir)
policiesContent := []byte(strings.Join(policies, "\n"))
err := os.WriteFile(policyFileName, policiesContent, 0644)
require.NoError(t, err)
return policyFileName
}

func generateAndSaveConfig(t require.TestingT, tmpdir string, numberOfPaths int) (string, string) {
oas := openapi.OpenAPISpec{
oas := &openapi.OpenAPISpec{
Paths: make(map[string]openapi.PathVerbs),
}
policies := []string{"package policies"}
Expand Down Expand Up @@ -135,17 +305,8 @@ func generateAndSaveConfig(t require.TestingT, tmpdir string, numberOfPaths int)
}
}

oasContent, err := json.Marshal(oas)
require.NoError(t, err)

oasFileName := fmt.Sprintf("%s/oas.json", tmpdir)
err = os.WriteFile(oasFileName, oasContent, 0644)
require.NoError(t, err)

policyFileName := fmt.Sprintf("%s/policies.rego", tmpdir)
policiesContent := []byte(strings.Join(policies, "\n"))
err = os.WriteFile(policyFileName, policiesContent, 0644)
require.NoError(t, err)
oasFileName := writeOAS(t, tmpdir, oas)
policyFileName := writePolicies(t, tmpdir, policies)

return oasFileName, policyFileName
}
Expand Down
Loading