Skip to content

chore: Use contexts with timeout in coderd tests #3381

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 15 commits into from
Aug 9, 2022
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
22 changes: 13 additions & 9 deletions coderd/coderd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ func TestMain(m *testing.M) {
func TestBuildInfo(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
buildInfo, err := client.BuildInfo(context.Background())

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

buildInfo, err := client.BuildInfo(ctx)
require.NoError(t, err)
require.Equal(t, buildinfo.ExternalURL(), buildInfo.ExternalURL, "external URL")
require.Equal(t, buildinfo.Version(), buildInfo.Version, "version")
Expand All @@ -59,10 +63,10 @@ func TestBuildInfo(t *testing.T) {
// TestAuthorizeAllEndpoints will check `authorize` is called on every endpoint registered.
func TestAuthorizeAllEndpoints(t *testing.T) {
t.Parallel()
var (
ctx = context.Background()
authorizer = &fakeAuthorizer{}
)
authorizer := &fakeAuthorizer{}

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

// This function was taken from coderdtest.newWithAPI. It is intentionally
// copied to avoid exposing the API to other tests in coderd. Tests should
Expand All @@ -84,7 +88,7 @@ func TestAuthorizeAllEndpoints(t *testing.T) {
require.NoError(t, err)
db = database.New(sqlDB)

pubsub, err = database.NewPubsub(context.Background(), sqlDB, connectionURL)
pubsub, err = database.NewPubsub(ctx, sqlDB, connectionURL)
require.NoError(t, err)
t.Cleanup(func() {
_ = pubsub.Close()
Expand All @@ -94,8 +98,8 @@ func TestAuthorizeAllEndpoints(t *testing.T) {
tickerCh := make(chan time.Time)
t.Cleanup(func() { close(tickerCh) })

ctx, cancelFunc := context.WithCancel(context.Background())
defer t.Cleanup(cancelFunc) // Defer to ensure cancelFunc is executed first.
ctx, cancel := context.WithCancel(ctx) // Shadowed to avoid mixing contexts.
defer t.Cleanup(cancel) // Defer to ensure cancelFunc is executed first.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
defer t.Cleanup(cancel) // Defer to ensure cancelFunc is executed first.
defer t.Cleanup(cancel) // Defer to ensure cancel is executed first.


lifecycleExecutor := executor.New(
ctx,
Expand Down Expand Up @@ -513,7 +517,7 @@ func TestAuthorizeAllEndpoints(t *testing.T) {
route = strings.ReplaceAll(route, "{scope}", string(templateParam.Scope))
route = strings.ReplaceAll(route, "{id}", templateParam.ScopeID.String())

resp, err := client.Request(context.Background(), method, route, nil)
resp, err := client.Request(ctx, method, route, nil)
require.NoError(t, err, "do req")
body, _ := io.ReadAll(resp.Body)
t.Logf("Response Body: %q", string(body))
Expand Down
35 changes: 28 additions & 7 deletions coderd/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/testutil"
)

func TestPostFiles(t *testing.T) {
Expand All @@ -17,26 +18,38 @@ func TestPostFiles(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
_, err := client.Upload(context.Background(), "bad", []byte{'a'})

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
Copy link
Member Author

@mafredri mafredri Aug 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As much as possible, I tried to ensure contexts are initialized after test environment has been set up (e.g. post- coderd client, user creation etc). This ensures the timeout is applied only to the part being tested. The coderdtest methods have their own timeouts.

defer cancel()

_, err := client.Upload(ctx, "bad", []byte{'a'})
require.Error(t, err)
})

t.Run("Insert", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
_, err := client.Upload(context.Background(), codersdk.ContentTypeTar, make([]byte, 1024))

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

_, err := client.Upload(ctx, codersdk.ContentTypeTar, make([]byte, 1024))
require.NoError(t, err)
})

t.Run("InsertAlreadyExists", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

data := make([]byte, 1024)
_, err := client.Upload(context.Background(), codersdk.ContentTypeTar, data)
_, err := client.Upload(ctx, codersdk.ContentTypeTar, data)
require.NoError(t, err)
_, err = client.Upload(context.Background(), codersdk.ContentTypeTar, data)
_, err = client.Upload(ctx, codersdk.ContentTypeTar, data)
require.NoError(t, err)
})
}
Expand All @@ -47,7 +60,11 @@ func TestDownload(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
_, _, err := client.Download(context.Background(), "something")

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

_, _, err := client.Download(ctx, "something")
var apiErr *codersdk.Error
require.ErrorAs(t, err, &apiErr)
require.Equal(t, http.StatusNotFound, apiErr.StatusCode())
Expand All @@ -57,9 +74,13 @@ func TestDownload(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
resp, err := client.Upload(context.Background(), codersdk.ContentTypeTar, make([]byte, 1024))

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

resp, err := client.Upload(ctx, codersdk.ContentTypeTar, make([]byte, 1024))
require.NoError(t, err)
data, contentType, err := client.Download(context.Background(), resp.Hash)
data, contentType, err := client.Download(ctx, resp.Hash)
require.NoError(t, err)
require.Len(t, data, 1024)
require.Equal(t, codersdk.ContentTypeTar, contentType)
Expand Down
31 changes: 25 additions & 6 deletions coderd/gitsshkey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,75 @@ import (
"github.com/coder/coder/codersdk"
"github.com/coder/coder/provisioner/echo"
"github.com/coder/coder/provisionersdk/proto"
"github.com/coder/coder/testutil"
)

func TestGitSSHKey(t *testing.T) {
t.Parallel()
t.Run("None", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
client := coderdtest.New(t, nil)
res := coderdtest.CreateFirstUser(t, client)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

key, err := client.GitSSHKey(ctx, res.UserID.String())
require.NoError(t, err)
require.NotEmpty(t, key.PublicKey)
})
t.Run("Ed25519", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
client := coderdtest.New(t, &coderdtest.Options{
SSHKeygenAlgorithm: gitsshkey.AlgorithmEd25519,
})
res := coderdtest.CreateFirstUser(t, client)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

key, err := client.GitSSHKey(ctx, res.UserID.String())
require.NoError(t, err)
require.NotEmpty(t, key.PublicKey)
})
t.Run("ECDSA", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
client := coderdtest.New(t, &coderdtest.Options{
SSHKeygenAlgorithm: gitsshkey.AlgorithmECDSA,
})
res := coderdtest.CreateFirstUser(t, client)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

key, err := client.GitSSHKey(ctx, res.UserID.String())
require.NoError(t, err)
require.NotEmpty(t, key.PublicKey)
})
t.Run("RSA4096", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
client := coderdtest.New(t, &coderdtest.Options{
SSHKeygenAlgorithm: gitsshkey.AlgorithmRSA4096,
})
res := coderdtest.CreateFirstUser(t, client)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

key, err := client.GitSSHKey(ctx, res.UserID.String())
require.NoError(t, err)
require.NotEmpty(t, key.PublicKey)
})
t.Run("Regenerate", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
client := coderdtest.New(t, &coderdtest.Options{
SSHKeygenAlgorithm: gitsshkey.AlgorithmEd25519,
})
res := coderdtest.CreateFirstUser(t, client)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

key1, err := client.GitSSHKey(ctx, res.UserID.String())
require.NoError(t, err)
require.NotEmpty(t, key1.PublicKey)
Expand Down Expand Up @@ -112,7 +128,10 @@ func TestAgentGitSSHKey(t *testing.T) {
agentClient := codersdk.New(client.URL)
agentClient.SessionToken = authToken

agentKey, err := agentClient.AgentGitSSHKey(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

agentKey, err := agentClient.AgentGitSSHKey(ctx)
require.NoError(t, err)
require.NotEmpty(t, agentKey.PrivateKey)
}
43 changes: 34 additions & 9 deletions coderd/organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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)
})
}
Expand All @@ -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
Expand All @@ -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)
Expand Down
Loading