-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd006e3
commit 3ac17d3
Showing
12 changed files
with
306 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ on: | |
push: | ||
branches: | ||
- 'main' | ||
- 'issue-193-load-testing' | ||
tags: | ||
- 'v[0-9]+.[0-9]+.[0-9]+' | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/eurofurence/reg-attendee-service/internal/web/app" | ||
"os" | ||
) | ||
|
||
func main() { | ||
os.Exit(app.New().Loadtest()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Package selfclient provides a very simple self API client. | ||
package selfclient | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
aurestclientapi "github.com/StephanHCB/go-autumn-restclient/api" | ||
auresthttpclient "github.com/StephanHCB/go-autumn-restclient/implementation/httpclient" | ||
aurestlogging "github.com/StephanHCB/go-autumn-restclient/implementation/requestlogging" | ||
"github.com/eurofurence/reg-attendee-service/internal/api/v1/attendee" | ||
"github.com/eurofurence/reg-attendee-service/internal/repository/config" | ||
"github.com/go-http-utils/headers" | ||
"net/http" | ||
"strings" | ||
"time" | ||
) | ||
|
||
var ( | ||
client aurestclientapi.Client | ||
baseUrl string | ||
idToken string | ||
accessToken string | ||
cookieDomain string | ||
) | ||
|
||
var ( | ||
UnauthorizedError = errors.New("got unauthorized from userinfo endpoint") | ||
DownstreamError = errors.New("downstream unavailable - see log for details") | ||
) | ||
|
||
func requestManipulator(ctx context.Context, r *http.Request) { | ||
r.AddCookie(&http.Cookie{ | ||
Name: config.OidcIdTokenCookieName(), | ||
Value: idToken, | ||
Domain: cookieDomain, | ||
Expires: time.Now().Add(10 * time.Minute), | ||
Path: "/", | ||
Secure: true, | ||
HttpOnly: true, | ||
SameSite: http.SameSiteStrictMode, | ||
}) | ||
r.AddCookie(&http.Cookie{ | ||
Name: config.OidcAccessTokenCookieName(), | ||
Value: accessToken, | ||
Domain: cookieDomain, | ||
Expires: time.Now().Add(10 * time.Minute), | ||
Path: "/", | ||
Secure: true, | ||
HttpOnly: true, | ||
SameSite: http.SameSiteStrictMode, | ||
}) | ||
} | ||
|
||
func Setup() error { | ||
httpClient, err := auresthttpclient.New(0, nil, requestManipulator) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
requestLoggingClient := aurestlogging.New(httpClient) | ||
|
||
client = requestLoggingClient | ||
|
||
baseUrl = config.GeneratorBaseUrl() | ||
idToken = config.GeneratorIdToken() | ||
accessToken = config.GeneratorAccessToken() | ||
cookieDomain = config.GeneratorCookieDomain() | ||
|
||
if baseUrl == "" || idToken == "" || accessToken == "" || cookieDomain == "" { | ||
return errors.New("missing parameters for setting up selfclient - cannot run generator") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func errByStatus(err error, status int) error { | ||
if err != nil { | ||
return err | ||
} | ||
if status == http.StatusUnauthorized { | ||
return UnauthorizedError | ||
} | ||
if status >= 300 { | ||
return DownstreamError | ||
} | ||
return nil | ||
} | ||
|
||
func SendRegistration(ctx context.Context, dto *attendee.AttendeeDto) (string, error) { | ||
url := fmt.Sprintf("%s/api/rest/v1/attendees", baseUrl) | ||
response := aurestclientapi.ParsedResponse{} | ||
err := client.Perform(ctx, http.MethodPost, url, dto, &response) | ||
|
||
// parse location header | ||
loc := "" | ||
if val, ok := response.Header[headers.Location]; ok { | ||
loc = val[0] | ||
} | ||
id := strings.TrimPrefix(loc, url+"/") | ||
|
||
return id, errByStatus(err, response.Status) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,11 @@ import ( | |
"context" | ||
"fmt" | ||
aulogging "github.com/StephanHCB/go-autumn-logging" | ||
"github.com/eurofurence/reg-attendee-service/internal/api/v1/attendee" | ||
"github.com/eurofurence/reg-attendee-service/internal/entity" | ||
"github.com/eurofurence/reg-attendee-service/internal/repository/config" | ||
"github.com/eurofurence/reg-attendee-service/internal/repository/database" | ||
"github.com/eurofurence/reg-attendee-service/internal/repository/selfclient" | ||
"math/rand" | ||
"strings" | ||
"time" | ||
|
@@ -26,6 +28,17 @@ func (s *AttendeeServiceImplData) GenerateFakeRegistrations(ctx context.Context, | |
return nil | ||
} | ||
|
||
func (s *AttendeeServiceImplData) SendFakeRegistrationToAPI(ctx context.Context) (string, error) { | ||
a := fakeRegistration() | ||
dto := &attendee.AttendeeDto{} | ||
mapAttendeeToDto(a, dto) | ||
|
||
ctxTimeout, cancel := context.WithTimeout(ctx, 20*time.Second) | ||
defer cancel() | ||
|
||
return selfclient.SendRegistration(ctxTimeout, dto) | ||
} | ||
|
||
func fakeRegistration() *entity.Attendee { | ||
return &entity.Attendee{ | ||
Nickname: randomString(3, 40, 2), | ||
|
@@ -34,7 +47,7 @@ func fakeRegistration() *entity.Attendee { | |
Street: randomString(3, 120, 0), | ||
Zip: randomString(3, 15, 0), | ||
City: randomString(3, 80, 0), | ||
Country: "", | ||
Country: oneOf(config.AllowedCountries()), | ||
Email: "[email protected]", | ||
Phone: "12345", | ||
Birthday: randomValidBirthday(), | ||
|
@@ -43,9 +56,9 @@ func fakeRegistration() *entity.Attendee { | |
SpokenLanguages: "," + randomSelection(config.AllowedSpokenLanguages(), 1, 5) + ",", | ||
RegistrationLanguage: oneOf(config.AllowedRegistrationLanguages()), | ||
Flags: ",terms-accepted," + randomSelection([]string{"hc", "anon", "digi-book"}, 1, 3) + ",", | ||
Packages: ",room-none,attendance," + oneOf([]string{"sponsor", "sponsor2", "tshirt"}) + ",", | ||
Packages: ",room-none,attendance,stage," + oneOf([]string{"sponsor", "sponsor2", "tshirt"}) + ",", | ||
Options: "," + randomSelection(config.AllowedOptions(), 1, 4) + ",", | ||
UserComments: "", | ||
UserComments: "generated by load test", | ||
Identity: randomString(10, 12, 0) + "_gen", | ||
} | ||
} | ||
|
@@ -132,3 +145,30 @@ func safeRandIntn(len int) int { | |
return 0 | ||
} | ||
} | ||
|
||
func mapAttendeeToDto(a *entity.Attendee, dto *attendee.AttendeeDto) { | ||
// this cannot fail | ||
dto.Id = a.ID | ||
dto.Nickname = a.Nickname | ||
dto.FirstName = a.FirstName | ||
dto.LastName = a.LastName | ||
dto.Street = a.Street | ||
dto.Zip = a.Zip | ||
dto.City = a.City | ||
dto.Country = a.Country | ||
dto.State = a.State | ||
dto.Email = a.Email | ||
dto.Phone = a.Phone | ||
dto.Telegram = a.Telegram | ||
dto.Partner = a.Partner | ||
dto.Birthday = a.Birthday | ||
dto.Gender = a.Gender | ||
dto.Pronouns = a.Pronouns | ||
dto.TshirtSize = a.TshirtSize | ||
dto.SpokenLanguages = removeWrappingCommas(a.SpokenLanguages) | ||
dto.RegistrationLanguage = removeWrappingCommas(a.RegistrationLanguage) | ||
dto.Flags = removeWrappingCommas(a.Flags) | ||
dto.Packages = removeWrappingCommas(a.Packages) | ||
dto.Options = removeWrappingCommas(a.Options) | ||
dto.UserComments = a.UserComments | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.