8000 chore: improve times of ratelimit tests by kylecarbs · Pull Request #6346 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

chore: improve times of ratelimit tests #6346

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 1 commit into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,9 @@ func createAnotherUserRetry(t *testing.T, client *codersdk.Client, organizationI

other := codersdk.New(client.URL)
other.SetSessionToken(login.SessionToken)
t.Cleanup(func() {
other.HTTPClient.CloseIdleConnections()
})

if len(roles) > 0 {
// Find the roles for the org vs the site wide roles
Expand Down
42 changes: 21 additions & 21 deletions coderd/httpmw/ratelimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/coder/coder/coderd/httpmw"
"github.com/coder/coder/coderd/rbac"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/testutil"
)

func randRemoteAddr() string {
Expand All @@ -34,38 +33,39 @@ func TestRateLimit(t *testing.T) {
t.Run("NoUserSucceeds", func(t *testing.T) {
t.Parallel()
rtr := chi.NewRouter()
rtr.Use(httpmw.RateLimit(5, time.Second))
rtr.Use(httpmw.RateLimit(1, time.Second))
rtr.Get("/", func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusOK)
})

require.Eventually(t, func() bool {
for i := 0; i < 5; i++ {
req := httptest.NewRequest("GET", "/", nil)
rec := httptest.NewRecorder()
rtr.ServeHTTP(rec, req)
resp := rec.Result()
defer resp.Body.Close()
return resp.StatusCode == http.StatusTooManyRequests
}, testutil.WaitShort, testutil.IntervalFast)
_ = resp.Body.Close()
require.Equal(t, i != 0, resp.StatusCode == http.StatusTooManyRequests)
}
})

t.Run("RandomIPs", func(t *testing.T) {
t.Parallel()
rtr := chi.NewRouter()
rtr.Use(httpmw.RateLimit(5, time.Second))
// Because these are random IPs, the limit should never be hit!
rtr.Use(httpmw.RateLimit(1, time.Second))
rtr.Get("/", func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusOK)
})

require.Never(t, func() bool {
for i := 0; i < 5; i++ {
req := httptest.NewRequest("GET", "/", nil)
rec := httptest.NewRecorder()
req.RemoteAddr = randRemoteAddr()
rtr.ServeHTTP(rec, req)
resp := rec.Result()
defer resp.Body.Close()
return resp.StatusCode == http.StatusTooManyRequests
}, testutil.WaitShort, testutil.IntervalFast)
_ = resp.Body.Close()
require.False(t, resp.StatusCode == http.StatusTooManyRequests)
}
})

t.Run("RegularUser", func(t *testing.T) {
Expand All @@ -81,7 +81,7 @@ func TestRateLimit(t *testing.T) {
Optional: false,
}))

rtr.Use(httpmw.RateLimit(5, time.Second))
rtr.Use(httpmw.RateLimit(1, time.Second))
rtr.Get("/", func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusOK)
})
Expand All @@ -98,17 +98,17 @@ func TestRateLimit(t *testing.T) {
defer resp.Body.Close()
require.Equal(t, http.StatusPreconditionRequired, resp.StatusCode)

require.Eventually(t, func() bool {
for i := 0; i < 5; i++ {
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set(codersdk.SessionTokenHeader, key)
rec := httptest.NewRecorder()
// Assert we're not using IP address.
req.RemoteAddr = randRemoteAddr()
rtr.ServeHTTP(rec, req)
resp := rec.Result()
defer resp.Body.Close()
return resp.StatusCode == http.StatusTooManyRequests
}, testutil.WaitShort, testutil.IntervalFast)
_ = resp.Body.Close()
require.Equal(t, i != 0, resp.StatusCode == http.StatusTooManyRequests)
}
})

8000 t.Run("OwnerBypass", func(t *testing.T) {
Expand All @@ -127,12 +127,12 @@ func TestRateLimit(t *testing.T) {
Optional: false,
}))

rtr.Use(httpmw.RateLimit(5, time.Second))
rtr.Use(httpmw.RateLimit(1, time.Second))
rtr.Get("/", func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusOK)
})

require.Never(t, func() bool {
for i := 0; i < 5; i++ {
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set(codersdk.SessionTokenHeader, key)
req.Header.Set(codersdk.BypassRatelimitHeader, "true")
Expand All @@ -141,8 +141,8 @@ func TestRateLimit(t *testing.T) {
req.RemoteAddr = randRemoteAddr()
rtr.ServeHTTP(rec, req)
resp := rec.Result()
defer resp.Body.Close()
return resp.StatusCode == http.StatusTooManyRequests
}, testutil.WaitShort, testutil.IntervalFast)
_ = resp.Body.Close()
require.False(t, resp.StatusCode == http.StatusTooManyRequests)
}
})
}
3 changes: 3 additions & 0 deletions provisioner/terraform/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (

func TestInstall(t *testing.T) {
t.Parallel()
if testing.Short() {
t.SkipNow()
}
ctx := context.Background()
dir := t.TempDir()
log := slogtest.Make(t, nil)
Expand Down
0