Skip to content

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 18 commits into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Improve test coverage for workspace history
  • Loading branch information
kylecarbs committed Feb 5, 2022
commit be3e6ac696d5438a00d58f9f132c7dc008bb8289
81 changes: 43 additions & 38 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package coderdtest
import (
"context"
"database/sql"
"fmt"
"io"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -67,37 +68,6 @@ func New(t *testing.T) *codersdk.Client {
return codersdk.New(serverURL)
}

// 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
}

// NewInitialUser creates a user with preset credentials and authenticates
// with the passed in codersdk client.
func NewInitialUser(t *testing.T, client *codersdk.Client) coderd.CreateInitialUserRequest {
req := coderd.CreateInitialUserRequest{
Email: "testuser@coder.com",
Username: "testuser",
Password: "testpass",
Organization: "testorg",
}
_, err := client.CreateInitialUser(context.Background(), req)
require.NoError(t, err)

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
}

// NewProvisionerDaemon launches a provisionerd instance configured to work
// well with coderd testing. It registers the "echo" provisioner for
// quick testing.
Expand Down Expand Up @@ -131,9 +101,31 @@ func NewProvisionerDaemon(t *testing.T, client *codersdk.Client) io.Closer {
return closer
}

// NewProject creates a project with the "echo" provisioner for
// 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: "testuser@coder.com",
Username: "testuser",
Password: "testpass",
Organization: "testorg",
}
_, err := client.CreateInitialUser(context.Background(), req)
require.NoError(t, err)

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 NewProject(t *testing.T, client *codersdk.Client, organization string) coderd.Project {
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,
Expand All @@ -142,9 +134,9 @@ func NewProject(t *testing.T, client *codersdk.Client, organization string) code
return project
}

// NewProjectVersion creates a project version for the "echo" provisioner
// CreateProjectVersion creates a project version for the "echo" provisioner
// for compatibility with testing.
func NewProjectVersion(t *testing.T, client *codersdk.Client, organization, project string, responses *echo.Responses) coderd.ProjectVersion {
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{
Expand All @@ -162,14 +154,15 @@ func AwaitProjectVersionImported(t *testing.T, client *codersdk.Client, organiza
var err error
projectVersion, err = client.ProjectVersion(context.Background(), organization, project, version)
require.NoError(t, err)
fmt.Printf("GOT: %s\n", projectVersion.Import.Status)
return projectVersion.Import.Status.Completed()
}, 3*time.Second, 50*time.Millisecond)
}, 3*time.Second, 25*time.Millisecond)
return projectVersion
}

// NewWorkspace creates a workspace for the user and project provided.
// CreateWorkspace creates a workspace for the user and project provided.
// A random name is generated for it.
func NewWorkspace(t *testing.T, client *codersdk.Client, user string, projectID uuid.UUID) coderd.Workspace {
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(),
Expand All @@ -178,6 +171,18 @@ func NewWorkspace(t *testing.T, client *codersdk.Client, user string, projectID
return workspace
}

// 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), "_", "-")
}
2 changes: 1 addition & 1 deletion coderd/coderdtest/coderdtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ func TestMain(m *testing.M) {
func TestNew(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
_ = coderdtest.NewInitialUser(t, client)
_ = coderdtest.CreateInitialUser(t, client)
_ = coderdtest.NewProvisionerDaemon(t, client)
}
32 changes: 16 additions & 16 deletions coderd/projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestProjects(t *testing.T) {
t.Run("ListEmpty", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
_ = coderdtest.NewInitialUser(t, client)
_ = coderdtest.CreateInitialUser(t, client)
projects, err := client.Projects(context.Background(), "")
Copy link
Contributor

Choose a reason for hiding this comment

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

I really like how much simpler this is 🎉

require.NoError(t, err)
require.NotNil(t, projects)
Expand All @@ -29,8 +29,8 @@ func TestProjects(t *testing.T) {
t.Run("List", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.NewInitialUser(t, client)
_ = coderdtest.NewProject(t, client, user.Organization)
user := coderdtest.CreateInitialUser(t, client)
_ = coderdtest.CreateProject(t, client, user.Organization)
projects, err := client.Projects(context.Background(), "")
require.NoError(t, err)
require.Len(t, projects, 1)
Expand All @@ -42,7 +42,7 @@ func TestProjectsByOrganization(t *testing.T) {
t.Run("ListEmpty", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.NewInitialUser(t, client)
user := coderdtest.CreateInitialUser(t, client)
projects, err := client.Projects(context.Background(), user.Organization)
require.NoError(t, err)
require.NotNil(t, projects)
Expand All @@ -52,8 +52,8 @@ func TestProjectsByOrganization(t *testing.T) {
t.Run("List", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.NewInitialUser(t, client)
_ = coderdtest.NewProject(t, client, user.Organization)
user := coderdtest.CreateInitialUser(t, client)
_ = coderdtest.CreateProject(t, client, user.Organization)
projects, err := client.Projects(context.Background(), "")
require.NoError(t, err)
require.Len(t, projects, 1)
Expand All @@ -65,15 +65,15 @@ func TestPostProjectsByOrganization(t *testing.T) {
t.Run("Create", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.NewInitialUser(t, client)
_ = coderdtest.NewProject(t, client, user.Organization)
user := coderdtest.CreateInitialUser(t, client)
_ = coderdtest.CreateProject(t, client, user.Organization)
})

t.Run("AlreadyExists", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.NewInitialUser(t, client)
project := coderdtest.NewProject(t, client, user.Organization)
user := coderdtest.CreateInitialUser(t, client)
project := coderdtest.CreateProject(t, client, user.Organization)
_, err := client.CreateProject(context.Background(), user.Organization, coderd.CreateProjectRequest{
Name: project.Name,
Provisioner: database.ProvisionerTypeEcho,
Expand All @@ -89,8 +89,8 @@ func TestProjectByOrganization(t *testing.T) {
t.Run("Get", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.NewInitialUser(t, client)
project := coderdtest.NewProject(t, client, user.Organization)
user := coderdtest.CreateInitialUser(t, client)
project := coderdtest.CreateProject(t, client, user.Organization)
_, err := client.Project(context.Background(), user.Organization, project.Name)
require.NoError(t, err)
})
Expand All @@ -101,8 +101,8 @@ func TestPostParametersByProject(t *testing.T) {
t.Run("Create", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.NewInitialUser(t, client)
project := coderdtest.NewProject(t, client, user.Organization)
user := coderdtest.CreateInitialUser(t, client)
project := coderdtest.CreateProject(t, client, user.Organization)
_, err := client.CreateProjectParameter(context.Background(), user.Organization, project.Name, coderd.CreateParameterValueRequest{
Name: "somename",
SourceValue: "tomato",
Expand All @@ -119,8 +119,8 @@ func TestParametersByProject(t *testing.T) {
t.Run("List", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.NewInitialUser(t, client)
project := coderdtest.NewProject(t, client, user.Organization)
user := coderdtest.CreateInitialUser(t, client)
project := coderdtest.CreateProject(t, client, user.Organization)
params, err := client.ProjectParameters(context.Background(), user.Organization, project.Name)
require.NoError(t, err)
require.NotNil(t, params)
Expand Down
44 changes: 22 additions & 22 deletions coderd/projectversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func TestProjectVersionsByOrganization(t *testing.T) {
t.Run("ListEmpty", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.NewInitialUser(t, client)
project := coderdtest.NewProject(t, client, user.Organization)
user := coderdtest.CreateInitialUser(t, client)
project := coderdtest.CreateProject(t, client, user.Organization)
versions, err := client.ProjectVersions(context.Background(), user.Organization, project.Name)
require.NoError(t, err)
require.NotNil(t, versions)
Expand All @@ -31,9 +31,9 @@ func TestProjectVersionsByOrganization(t *testing.T) {
t.Run("List", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.NewInitialUser(t, client)
project := coderdtest.NewProject(t, client, user.Organization)
_ = coderdtest.NewProjectVersion(t, client, user.Organization, project.Name, nil)
user := coderdtest.CreateInitialUser(t, client)
project := coderdtest.CreateProject(t, client, user.Organization)
_ = coderdtest.CreateProjectVersion(t, client, user.Organization, project.Name, nil)
versions, err := client.ProjectVersions(context.Background(), user.Organization, project.Name)
require.NoError(t, err)
require.Len(t, versions, 1)
Expand All @@ -45,9 +45,9 @@ func TestProjectVersionByOrganizationAndName(t *testing.T) {
t.Run("Get", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.NewInitialUser(t, client)
project := coderdtest.NewProject(t, client, user.Organization)
version := coderdtest.NewProjectVersion(t, client, user.Organization, project.Name, nil)
user := coderdtest.CreateInitialUser(t, client)
project := coderdtest.CreateProject(t, client, user.Organization)
version := coderdtest.CreateProjectVersion(t, client, user.Organization, project.Name, nil)
require.Equal(t, version.Import.Status, coderd.ProvisionerJobStatusPending)
})
}
Expand All @@ -57,16 +57,16 @@ func TestPostProjectVersionByOrganization(t *testing.T) {
t.Run("Create", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.NewInitialUser(t, client)
project := coderdtest.NewProject(t, client, user.Organization)
_ = coderdtest.NewProjectVersion(t, client, user.Organization, project.Name, nil)
user := coderdtest.CreateInitialUser(t, client)
project := coderdtest.CreateProject(t, client, user.Organization)
_ = coderdtest.CreateProjectVersion(t, client, user.Organization, project.Name, nil)
})

t.Run("InvalidStorage", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.NewInitialUser(t, client)
project := coderdtest.NewProject(t, client, user.Organization)
user := coderdtest.CreateInitialUser(t, client)
project := coderdtest.CreateProject(t, client, user.Organization)
_, err := client.CreateProjectVersion(context.Background(), user.Organization, project.Name, coderd.CreateProjectVersionRequest{
StorageMethod: database.ProjectStorageMethod("invalid"),
StorageSource: []byte{},
Expand All @@ -80,9 +80,9 @@ func TestProjectVersionParametersByOrganizationAndName(t *testing.T) {
t.Run("NotImported", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.NewInitialUser(t, client)
project := coderdtest.NewProject(t, client, user.Organization)
version := coderdtest.NewProjectVersion(t, client, user.Organization, project.Name, nil)
user := coderdtest.CreateInitialUser(t, client)
project := coderdtest.CreateProject(t, client, user.Organization)
version := coderdtest.CreateProjectVersion(t, client, user.Organization, project.Name, nil)
_, err := client.ProjectVersionParameters(context.Background(), user.Organization, project.Name, version.Name)
var apiErr *codersdk.Error
require.ErrorAs(t, err, &apiErr)
Expand All @@ -92,10 +92,10 @@ func TestProjectVersionParametersByOrganizationAndName(t *testing.T) {
t.Run("FailedImport", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.NewInitialUser(t, client)
user := coderdtest.CreateInitialUser(t, client)
_ = coderdtest.NewProvisionerDaemon(t, client)
project := coderdtest.NewProject(t, client, user.Organization)
version := coderdtest.NewProjectVersion(t, client, user.Organization, project.Name, &echo.Responses{
project := coderdtest.CreateProject(t, client, user.Organization)
version := coderdtest.CreateProjectVersion(t, client, user.Organization, project.Name, &echo.Responses{
Provision: []*proto.Provision_Response{{}},
})
coderdtest.AwaitProjectVersionImported(t, client, user.Organization, project.Name, version.Name)
Expand All @@ -107,10 +107,10 @@ func TestProjectVersionParametersByOrganizationAndName(t *testing.T) {
t.Run("List", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.NewInitialUser(t, client)
user := coderdtest.CreateInitialUser(t, client)
_ = coderdtest.NewProvisionerDaemon(t, client)
project := coderdtest.NewProject(t, client, user.Organization)
version := coderdtest.NewProjectVersion(t, client, user.Organization, project.Name, &echo.Responses{
project := coderdtest.CreateProject(t, client, user.Organization)
version := coderdtest.CreateProjectVersion(t, client, user.Organization, project.Name, &echo.Responses{
Parse: []*proto.Parse_Response{{
Type: &proto.Parse_Response_Complete{
Complete: &proto.Parse_Complete{
Expand Down
2 changes: 1 addition & 1 deletion coderd/provisioners.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func convertProvisionerJob(provisionerJob database.ProvisionerJob) ProvisionerJo
job.Status = ProvisionerJobStatusRunning
}

if job.Error != "" {
if !provisionerJob.CancelledAt.Valid && job.Error != "" {
job.Status = ProvisionerJobStatusFailed
}

Expand Down
4 changes: 4 additions & 0 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ func (api *api) organizationsByUser(rw http.ResponseWriter, r *http.Request) {
user := httpmw.UserParam(r)

organizations, err := api.Database.GetOrganizationsByUserID(r.Context(), user.ID)
if errors.Is(err, sql.ErrNoRows) {
err = nil
organizations = []database.Organization{}
}
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: fmt.Sprintf("get organizations: %s", err.Error()),
Expand Down
Loading