-
Notifications
You must be signed in to change notification settings - Fork 881
chore: Add test helpers to improve coverage #166
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
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7f2a99f
chore: Rename ProjectHistory to ProjectVersion
kylecarbs 7c46342
Rename files
kylecarbs 9e04a0e
Standardize tests a bit more
kylecarbs 9cd8b43
Remove Server struct from coderdtest
kylecarbs be3e6ac
Improve test coverage for workspace history
kylecarbs 6e4ca5a
Merge branch 'main' into testcleanup
kylecarbs ab4dcb1
Fix linting errors
kylecarbs 57efae2
Fix coderd test leak
kylecarbs 9fa815c
Fix coderd test leak
kylecarbs 0fb2a5a
Merge branch 'testcleanup' of github.com:coder/coder into testcleanup
kylecarbs c50cee0
Improve workspace history logs
kylecarbs e202b2d
Standardize test structure for codersdk
kylecarbs ca31d90
Fix linting errors
kylecarbs 673c59d
Fix WebSocket compression
kylecarbs aad577f
Update coderd/workspaces.go
kylecarbs 6c39920
Add test for listing project parameters
kylecarbs a4d17d0
Cache npm dependencies with setup node
kylecarbs a8a0531
Remove windows npm cache key
kylecarbs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,11 @@ | ||
package coderd_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"go.uber.org/goleak" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
goleak.VerifyTestMain(m) | ||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -7,16 +7,18 @@ import ( | |
"net/http/httptest" | ||
"net/url" | ||
"os" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/moby/moby/pkg/namesgenerator" | ||
"github.com/stretchr/testify/require" | ||
|
||
"cdr.dev/slog" | ||
"cdr.dev/slog/sloggers/slogtest" | ||
"github.com/coder/coder/coderd" | ||
"github.com/coder/coder/codersdk" | ||
"github.com/coder/coder/cryptorand" | ||
"github.com/coder/coder/database" | ||
"github.com/coder/coder/database/databasefake" | ||
"github.com/coder/coder/database/postgres" | ||
|
@@ -26,47 +28,49 @@ import ( | |
"github.com/coder/coder/provisionersdk/proto" | ||
) | ||
|
||
// Server represents a test instance of coderd. | ||
// The database is intentionally omitted from | ||
// this struct to promote data being exposed via | ||
// the API. | ||
type Server struct { | ||
Client *codersdk.Client | ||
URL *url.URL | ||
} | ||
|
||
// RandomInitialUser generates a random initial user and authenticates | ||
// it with the client on the Server struct. | ||
func (s *Server) RandomInitialUser(t *testing.T) coderd.CreateInitialUserRequest { | ||
username, err := cryptorand.String(12) | ||
require.NoError(t, err) | ||
password, err := cryptorand.String(12) | ||
require.NoError(t, err) | ||
organization, err := cryptorand.String(12) | ||
require.NoError(t, err) | ||
// New constructs a new coderd test instance. This returned Server | ||
// should contain no side-effects. | ||
func New(t *testing.T) *codersdk.Client { | ||
// This can be hotswapped for a live database instance. | ||
db := databasefake.New() | ||
pubsub := database.NewPubsubInMemory() | ||
if os.Getenv("DB") != "" { | ||
connectionURL, close, err := postgres.Open() | ||
require.NoError(t, err) | ||
t.Cleanup(close) | ||
sqlDB, err := sql.Open("postgres", connectionURL) | ||
require.NoError(t, err) | ||
t.Cleanup(func() { | ||
_ = sqlDB.Close() | ||
}) | ||
err = database.Migrate(sqlDB) | ||
require.NoError(t, err) | ||
db = database.New(sqlDB) | ||
|
||
req := coderd.CreateInitialUserRequest{ | ||
Email: "[email protected]", | ||
Username: username, | ||
Password: password, | ||
Organization: organization, | ||
pubsub, err = database.NewPubsub(context.Background(), sqlDB, connectionURL) | ||
require.NoError(t, err) | ||
t.Cleanup(func() { | ||
_ = pubsub.Close() | ||
}) | ||
} | ||
_, err = s.Client.CreateInitialUser(context.Background(), req) | ||
require.NoError(t, err) | ||
|
||
login, err := s.Client.LoginWithPassword(context.Background(), coderd.LoginWithPasswordRequest{ | ||
Email: "[email protected]", | ||
Password: password, | ||
handler := coderd.New(&coderd.Options{ | ||
Logger: slogtest.Make(t, nil).Leveled(slog.LevelDebug), | ||
Database: db, | ||
Pubsub: pubsub, | ||
}) | ||
srv := httptest.NewServer(handler) | ||
serverURL, err := url.Parse(srv.URL) | ||
require.NoError(t, err) | ||
err = s.Client.SetSessionToken(login.SessionToken) | ||
require.NoError(t, err) | ||
return req | ||
t.Cleanup(srv.Close) | ||
|
||
return codersdk.New(serverURL) | ||
} | ||
|
||
// AddProvisionerd launches a new provisionerd instance with the | ||
// test provisioner registered. | ||
func (s *Server) AddProvisionerd(t *testing.T) io.Closer { | ||
// NewProvisionerDaemon launches a provisionerd instance configured to work | ||
// well with coderd testing. It registers the "echo" provisioner for | ||
// quick testing. | ||
func NewProvisionerDaemon(t *testing.T, client *codersdk.Client) io.Closer { | ||
echoClient, echoServer := provisionersdk.TransportPipe() | ||
ctx, cancelFunc := context.WithCancel(context.Background()) | ||
t.Cleanup(func() { | ||
|
@@ -81,7 +85,7 @@ func (s *Server) AddProvisionerd(t *testing.T) io.Closer { | |
require.NoError(t, err) | ||
}() | ||
|
||
closer := provisionerd.New(s.Client.ProvisionerDaemonClient, &provisionerd.Options{ | ||
closer := provisionerd.New(client.ProvisionerDaemonClient, &provisionerd.Options{ | ||
Logger: slogtest.Make(t, nil).Named("provisionerd").Leveled(slog.LevelDebug), | ||
PollInterval: 50 * time.Millisecond, | ||
UpdateInterval: 50 * time.Millisecond, | ||
|
@@ -96,44 +100,87 @@ func (s *Server) AddProvisionerd(t *testing.T) io.Closer { | |
return closer | ||
} | ||
|
||
// New constructs a new coderd test instance. This returned Server | ||
// should contain no side-effects. | ||
func New(t *testing.T) Server { | ||
// This can be hotswapped for a live database instance. | ||
db := databasefake.New() | ||
pubsub := database.NewPubsubInMemory() | ||
if os.Getenv("DB") != "" { | ||
connectionURL, close, err := postgres.Open() | ||
require.NoError(t, err) | ||
t.Cleanup(close) | ||
sqlDB, err := sql.Open("postgres", connectionURL) | ||
require.NoError(t, err) | ||
t.Cleanup(func() { | ||
_ = sqlDB.Close() | ||
}) | ||
err = database.Migrate(sqlDB) | ||
require.NoError(t, err) | ||
db = database.New(sqlDB) | ||
// CreateInitialUser creates a user with preset credentials and authenticates | ||
// with the passed in codersdk client. | ||
func CreateInitialUser(t *testing.T, client *codersdk.Client) coderd.CreateInitialUserRequest { | ||
req := coderd.CreateInitialUserRequest{ | ||
Email: "[email protected]", | ||
Username: "testuser", | ||
Password: "testpass", | ||
Organization: "testorg", | ||
} | ||
_, err := client.CreateInitialUser(context.Background(), req) | ||
require.NoError(t, err) | ||
|
||
pubsub, err = database.NewPubsub(context.Background(), sqlDB, connectionURL) | ||
login, err := client.LoginWithPassword(context.Background(), coderd.LoginWithPasswordRequest{ | ||
Email: req.Email, | ||
Password: req.Password, | ||
}) | ||
require.NoError(t, err) | ||
err = client.SetSessionToken(login.SessionToken) | ||
require.NoError(t, err) | ||
return req | ||
} | ||
|
||
// CreateProject creates a project with the "echo" provisioner for | ||
// compatibility with testing. The name assigned is randomly generated. | ||
func CreateProject(t *testing.T, client *codersdk.Client, organization string) coderd.Project { | ||
project, err := client.CreateProject(context.Background(), organization, coderd.CreateProjectRequest{ | ||
Name: randomUsername(), | ||
Provisioner: database.ProvisionerTypeEcho, | ||
}) | ||
require.NoError(t, err) | ||
return project | ||
} | ||
|
||
// CreateProjectVersion creates a project version for the "echo" provisioner | ||
// for compatibility with testing. | ||
func CreateProjectVersion(t *testing.T, client *codersdk.Client, organization, project string, responses *echo.Responses) coderd.ProjectVersion { | ||
data, err := echo.Tar(responses) | ||
require.NoError(t, err) | ||
version, err := client.CreateProjectVersion(context.Background(), organization, project, coderd.CreateProjectVersionRequest{ | ||
StorageMethod: database.ProjectStorageMethodInlineArchive, | ||
StorageSource: data, | ||
}) | ||
require.NoError(t, err) | ||
return version | ||
} | ||
|
||
// AwaitProjectVersionImported awaits for the project import job to reach completed status. | ||
func AwaitProjectVersionImported(t *testing.T, client *codersdk.Client, organization, project, version string) coderd.ProjectVersion { | ||
var projectVersion coderd.ProjectVersion | ||
require.Eventually(t, func() bool { | ||
var err error | ||
projectVersion, err = client.ProjectVersion(context.Background(), organization, project, version) | ||
require.NoError(t, err) | ||
t.Cleanup(func() { | ||
_ = pubsub.Close() | ||
}) | ||
} | ||
return projectVersion.Import.Status.Completed() | ||
}, 3*time.Second, 25*time.Millisecond) | ||
return projectVersion | ||
} | ||
|
||
handler := coderd.New(&coderd.Options{ | ||
Logger: slogtest.Make(t, nil).Leveled(slog.LevelDebug), | ||
Database: db, | ||
Pubsub: pubsub, | ||
// CreateWorkspace creates a workspace for the user and project provided. | ||
// A random name is generated for it. | ||
func CreateWorkspace(t *testing.T, client *codersdk.Client, user string, projectID uuid.UUID) coderd.Workspace { | ||
workspace, err := client.CreateWorkspace(context.Background(), user, coderd.CreateWorkspaceRequest{ | ||
ProjectID: projectID, | ||
Name: randomUsername(), | ||
}) | ||
srv := httptest.NewServer(handler) | ||
serverURL, err := url.Parse(srv.URL) | ||
require.NoError(t, err) | ||
t.Cleanup(srv.Close) | ||
return workspace | ||
} | ||
|
||
return Server{ | ||
Client: codersdk.New(serverURL), | ||
URL: serverURL, | ||
} | ||
// AwaitWorkspaceHistoryProvisioned awaits for the workspace provision job to reach completed status. | ||
func AwaitWorkspaceHistoryProvisioned(t *testing.T, client *codersdk.Client, user, workspace, history string) coderd.WorkspaceHistory { | ||
var workspaceHistory coderd.WorkspaceHistory | ||
require.Eventually(t, func() bool { | ||
var err error | ||
workspaceHistory, err = client.WorkspaceHistory(context.Background(), user, workspace, history) | ||
require.NoError(t, err) | ||
return workspaceHistory.Provision.Status.Completed() | ||
}, 3*time.Second, 25*time.Millisecond) | ||
return workspaceHistory | ||
} | ||
|
||
func randomUsername() string { | ||
return strings.ReplaceAll(namesgenerator.GetRandomName(0), "_", "-") | ||
} |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggest using separate files for ruleguard like we do in the monorepo (and using a wildcard here), it helps keep each rule organized, and gives us a way to group related rules
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer for us to wait until this becomes more unruly.