chore: Use contexts with timeout in coderd tests (#3381)

This commit is contained in:
Mathias Fredriksson
2022-08-09 20:17:00 +03:00
committed by GitHub
parent 690ba661a7
commit ccf6f4e7ed
18 changed files with 1082 additions and 357 deletions

View File

@ -9,13 +9,18 @@ import (
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/testutil"
)
func TestOrganizationsByUser(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
orgs, err := client.OrganizationsByUser(context.Background(), codersdk.Me)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
orgs, err := client.OrganizationsByUser(ctx, codersdk.Me)
require.NoError(t, err)
require.NotNil(t, orgs)
require.Len(t, orgs, 1)
@ -27,7 +32,11 @@ func TestOrganizationByUserAndName(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
coderdtest.CreateFirstUser(t, client)
_, err := client.OrganizationByName(context.Background(), codersdk.Me, "nothing")
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
_, err := client.OrganizationByName(ctx, codersdk.Me, "nothing")
var apiErr *codersdk.Error
require.ErrorAs(t, err, &apiErr)
require.Equal(t, http.StatusNotFound, apiErr.StatusCode())
@ -38,11 +47,15 @@ func TestOrganizationByUserAndName(t *testing.T) {
client := coderdtest.New(t, nil)
first := coderdtest.CreateFirstUser(t, client)
other := coderdtest.CreateAnotherUser(t, client, first.OrganizationID)
org, err := client.CreateOrganization(context.Background(), codersdk.CreateOrganizationRequest{
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
org, err := client.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{
Name: "another",
})
require.NoError(t, err)
_, err = other.OrganizationByName(context.Background(), codersdk.Me, org.Name)
_, err = other.OrganizationByName(ctx, codersdk.Me, org.Name)
var apiErr *codersdk.Error
require.ErrorAs(t, err, &apiErr)
require.Equal(t, http.StatusNotFound, apiErr.StatusCode())
@ -52,9 +65,13 @@ func TestOrganizationByUserAndName(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
org, err := client.Organization(context.Background(), user.OrganizationID)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
org, err := client.Organization(ctx, user.OrganizationID)
require.NoError(t, err)
_, err = client.OrganizationByName(context.Background(), codersdk.Me, org.Name)
_, err = client.OrganizationByName(ctx, codersdk.Me, org.Name)
require.NoError(t, err)
})
}
@ -65,9 +82,13 @@ func TestPostOrganizationsByUser(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
org, err := client.Organization(context.Background(), user.OrganizationID)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
org, err := client.Organization(ctx, user.OrganizationID)
require.NoError(t, err)
_, err = client.CreateOrganization(context.Background(), codersdk.CreateOrganizationRequest{
_, err = client.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{
Name: org.Name,
})
var apiErr *codersdk.Error
@ -79,7 +100,11 @@ func TestPostOrganizationsByUser(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
_, err := client.CreateOrganization(context.Background(), codersdk.CreateOrganizationRequest{
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
_, err := client.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{
Name: "new",
})
require.NoError(t, err)