-
Notifications
You must be signed in to change notification settings - Fork 894
feat: add dbfake for workspace builds and resources #10426
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
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b1c5a08
feat: add dbfakedata for workspace builds and resources
kylecarbs e360e33
Merge branch 'main' into fakebuild
kylecarbs b622bdd
Rename dbfakedata to dbfake
kylecarbs c4d9108
Migrate workspaceagents_test.go to use the new dbfake
kylecarbs 003f752
Migrate agent_test.go to use the new fakes
kylecarbs 7e0fefd
Fix comments
kylecarbs 0422e41
Try to fix tests
kylecarbs e74024d
Fix tests
kylecarbs 3849577
Actually fix tests
kylecarbs cff7aca
Fix listening ports
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package dbfake | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/v2/coderd/database" | ||
"github.com/coder/coder/v2/coderd/database/dbauthz" | ||
"github.com/coder/coder/v2/coderd/database/dbgen" | ||
"github.com/coder/coder/v2/coderd/database/dbtime" | ||
"github.com/coder/coder/v2/coderd/provisionerdserver" | ||
"github.com/coder/coder/v2/coderd/telemetry" | ||
"github.com/coder/coder/v2/provisionersdk/proto" | ||
sdkproto "github.com/coder/coder/v2/provisionersdk/proto" | ||
) | ||
|
||
// Workspace inserts a workspace into the database. | ||
func Workspace(t testing.TB, db database.Store, seed database.Workspace) database.Workspace { | ||
t.Helper() | ||
|
||
// This intentionally fulfills the minimum requirements of the schema. | ||
// Tests can provide a custom template ID if necessary. | ||
if seed.TemplateID == uuid.Nil { | ||
template := dbgen.Template(t, db, database.Template{ | ||
OrganizationID: seed.OrganizationID, | ||
CreatedBy: seed.OwnerID, | ||
}) | ||
seed.TemplateID = template.ID | ||
seed.OwnerID = template.CreatedBy | ||
seed.OrganizationID = template.OrganizationID | ||
} | ||
return dbgen.Workspace(t, db, seed) | ||
} | ||
|
||
// WorkspaceWithAgent is a helper that generates a workspace with a single resource | ||
// that has an agent attached to it. The agent token is returned. | ||
func WorkspaceWithAgent(t testing.TB, db database.Store, seed database.Workspace) (database.Workspace, string) { | ||
t.Helper() | ||
authToken := uuid.NewString() | ||
ws := Workspace(t, db, seed) | ||
WorkspaceBuild(t, db, ws, database.WorkspaceBuild{}, &proto.Resource{ | ||
Name: "example", | ||
Type: "aws_instance", | ||
Agents: []*proto.Agent{{ | ||
Id: uuid.NewString(), | ||
Auth: &proto.Agent_Token{ | ||
Token: authToken, | ||
}, | ||
}}, | ||
}) | ||
return ws, authToken | ||
} | ||
|
||
// WorkspaceBuild inserts a build and a successful job into the database. | ||
func WorkspaceBuild(t testing.TB, db database.Store, ws database.Workspace, seed database.WorkspaceBuild, resources ...*sdkproto.Resource) database.WorkspaceBuild { | ||
t.Helper() | ||
jobID := uuid.New() | ||
seed.JobID = jobID | ||
seed.WorkspaceID = ws.ID | ||
// This intentionally fulfills the minimum requirements of the schema. | ||
// Tests can provide a custom version ID if necessary. | ||
if seed.TemplateVersionID == uuid.Nil { | ||
jobID := uuid.New() | ||
templateVersion := dbgen.TemplateVersion(t, db, database.TemplateVersion{ | ||
JobID: jobID, | ||
OrganizationID: ws.OrganizationID, | ||
CreatedBy: ws.OwnerID, | ||
TemplateID: uuid.NullUUID{ | ||
UUID: ws.TemplateID, | ||
Valid: true, | ||
}, | ||
}) | ||
payload, _ := json.Marshal(provisionerdserver.TemplateVersionImportJob{ | ||
TemplateVersionID: templateVersion.ID, | ||
}) | ||
dbgen.ProvisionerJob(t, db, nil, database.ProvisionerJob{ | ||
ID: jobID, | ||
OrganizationID: ws.OrganizationID, | ||
Input: payload, | ||
Type: database.ProvisionerJobTypeTemplateVersionImport, | ||
CompletedAt: sql.NullTime{ | ||
Time: dbtime.Now(), | ||
Valid: true, | ||
}, | ||
}) | ||
seed.TemplateVersionID = templateVersion.ID | ||
} | ||
build := dbgen.WorkspaceBuild(t, db, seed) | ||
|
||
payload, err := json.Marshal(provisionerdserver.WorkspaceProvisionJob{ | ||
WorkspaceBuildID: build.ID, | ||
}) | ||
require.NoError(t, err) | ||
job := dbgen.ProvisionerJob(t, db, nil, database.ProvisionerJob{ | ||
ID: jobID, | ||
Input: payload, | ||
OrganizationID: ws.OrganizationID, | ||
CompletedAt: sql.NullTime{ | ||
Time: dbtime.Now(), | ||
Valid: true, | ||
}, | ||
}) | ||
ProvisionerJobResources(t, db, job.ID, seed.Transition, resources...) | ||
return build | ||
} | ||
|
||
// ProvisionerJobResources inserts a series of resources into a provisioner job. | ||
func ProvisionerJobResources(t testing.TB, db database.Store, job uuid.UUID, transition database.WorkspaceTransition, resources ...*sdkproto.Resource) { | ||
t.Helper() | ||
if transition == "" { | ||
// Default to start! | ||
transition = database.WorkspaceTransitionStart | ||
} | ||
for _, resource := range resources { | ||
//nolint:gocritic // This is only used by tests. | ||
err := provisionerdserver.InsertWorkspaceResource(dbauthz.AsSystemRestricted(context.Background()), db, job, transition, resource, &telemetry.Snapshot{}) | ||
require.NoError(t, err) | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.