Skip to content

chore: Add organizationmember and parameter gen functions #6007

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 2 commits into from
Feb 3, 2023
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
85 changes: 74 additions & 11 deletions coderd/database/dbgen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func AuditLog(t *testing.T, db database.Store, seed database.AuditLog) database.
ResourceID: takeFirst(seed.ResourceID, uuid.New()),
ResourceTarget: takeFirst(seed.ResourceTarget, uuid.NewString()),
Action: takeFirst(seed.Action, database.AuditActionCreate),
Diff: takeFirstBytes(seed.Diff, []byte("{}")),
Diff: takeFirstSlice(seed.Diff, []byte("{}")),
StatusCode: takeFirst(seed.StatusCode, 200),
AdditionalFields: takeFirstBytes(seed.Diff, []byte("{}")),
AdditionalFields: takeFirstSlice(seed.Diff, []byte("{}")),
RequestID: takeFirst(seed.RequestID, uuid.New()),
ResourceIcon: takeFirst(seed.ResourceIcon, ""),
})
Expand Down Expand Up @@ -81,7 +81,7 @@ func APIKey(t *testing.T, db database.Store, seed database.APIKey) (key database
ID: takeFirst(seed.ID, id),
// 0 defaults to 86400 at the db layer
LifetimeSeconds: takeFirst(seed.LifetimeSeconds, 0),
HashedSecret: takeFirstBytes(seed.HashedSecret, hashed[:]),
HashedSecret: takeFirstSlice(seed.HashedSecret, hashed[:]),
IPAddress: pqtype.Inet{},
UserID: takeFirst(seed.UserID, uuid.New()),
LastUsed: takeFirst(seed.LastUsed, time.Now()),
Expand Down Expand Up @@ -109,7 +109,7 @@ func WorkspaceAgent(t *testing.T, db database.Store, orig database.WorkspaceAgen
},
Architecture: takeFirst(orig.Architecture, "amd64"),
EnvironmentVariables: pqtype.NullRawMessage{
RawMessage: takeFirstBytes(orig.EnvironmentVariables.RawMessage, []byte("{}")),
RawMessage: takeFirstSlice(orig.EnvironmentVariables.RawMessage, []byte("{}")),
Valid: takeFirst(orig.EnvironmentVariables.Valid, false),
},
OperatingSystem: takeFirst(orig.OperatingSystem, "linux"),
Expand All @@ -119,11 +119,11 @@ func WorkspaceAgent(t *testing.T, db database.Store, orig database.WorkspaceAgen
},
Directory: takeFirst(orig.Directory, ""),
InstanceMetadata: pqtype.NullRawMessage{
RawMessage: takeFirstBytes(orig.ResourceMetadata.RawMessage, []byte("{}")),
RawMessage: takeFirstSlice(orig.ResourceMetadata.RawMessage, []byte("{}")),
Valid: takeFirst(orig.ResourceMetadata.Valid, false),
},
ResourceMetadata: pqtype.NullRawMessage{
RawMessage: takeFirstBytes(orig.ResourceMetadata.RawMessage, []byte("{}")),
RawMessage: takeFirstSlice(orig.ResourceMetadata.RawMessage, []byte("{}")),
Valid: takeFirst(orig.ResourceMetadata.Valid, false),
},
ConnectionTimeoutSeconds: takeFirst(orig.ConnectionTimeoutSeconds, 3600),
Expand Down Expand Up @@ -163,7 +163,7 @@ func WorkspaceBuild(t *testing.T, db database.Store, orig database.WorkspaceBuil
Transition: takeFirst(orig.Transition, database.WorkspaceTransitionStart),
InitiatorID: takeFirst(orig.InitiatorID, uuid.New()),
JobID: takeFirst(orig.JobID, uuid.New()),
ProvisionerState: takeFirstBytes(orig.ProvisionerState, []byte{}),
ProvisionerState: takeFirstSlice(orig.ProvisionerState, []byte{}),
Deadline: takeFirst(orig.Deadline, time.Now().Add(time.Hour)),
Reason: takeFirst(orig.Reason, database.BuildReasonInitiator),
})
Expand All @@ -176,10 +176,10 @@ func User(t *testing.T, db database.Store, orig database.User) database.User {
ID: takeFirst(orig.ID, uuid.New()),
Email: takeFirst(orig.Email, namesgenerator.GetRandomName(1)),
Username: takeFirst(orig.Username, namesgenerator.GetRandomName(1)),
HashedPassword: takeFirstBytes(orig.HashedPassword, []byte{}),
HashedPassword: takeFirstSlice(orig.HashedPassword, []byte{}),
CreatedAt: takeFirst(orig.CreatedAt, time.Now()),
UpdatedAt: takeFirst(orig.UpdatedAt, time.Now()),
RBACRoles: []string{},
RBACRoles: takeFirstSlice(orig.RBACRoles, []string{}),
LoginType: takeFirst(orig.LoginType, database.LoginTypePassword),
})
require.NoError(t, err, "insert user")
Expand All @@ -198,6 +198,18 @@ func Organization(t *testing.T, db database.Store, orig database.Organization) d
return org
}

func OrganizationMember(t *testing.T, db database.Store, orig database.OrganizationMember) database.OrganizationMember {
mem, err := db.InsertOrganizationMember(context.Background(), database.InsertOrganizationMemberParams{
OrganizationID: takeFirst(orig.OrganizationID, uuid.New()),
UserID: takeFirst(orig.UserID, uuid.New()),
CreatedAt: takeFirst(orig.CreatedAt, time.Now()),
UpdatedAt: takeFirst(orig.UpdatedAt, time.Now()),
Roles: takeFirstSlice(orig.Roles, []string{}),
})
require.NoError(t, err, "insert organization")
return mem
}

func Group(t *testing.T, db database.Store, orig database.Group) database.Group {
group, err := db.InsertGroup(context.Background(), database.InsertGroupParams{
ID: takeFirst(orig.ID, uuid.New()),
Expand Down Expand Up @@ -235,7 +247,7 @@ func ProvisionerJob(t *testing.T, db database.Store, orig database.ProvisionerJo
StorageMethod: takeFirst(orig.StorageMethod, database.ProvisionerStorageMethodFile),
FileID: takeFirst(orig.FileID, uuid.New()),
Type: takeFirst(orig.Type, database.ProvisionerJobTypeWorkspaceBuild),
Input: takeFirstBytes(orig.Input, []byte("{}")),
Input: takeFirstSlice(orig.Input, []byte("{}")),
Tags: orig.Tags,
})
require.NoError(t, err, "insert job")
Expand All @@ -262,14 +274,25 @@ func WorkspaceResource(t *testing.T, db database.Store, orig database.WorkspaceR
return resource
}

func WorkspaceResourceMetadatums(t *testing.T, db database.Store, seed database.WorkspaceResourceMetadatum) []database.WorkspaceResourceMetadatum {
meta, err := db.InsertWorkspaceResourceMetadata(context.Background(), database.InsertWorkspaceResourceMetadataParams{
WorkspaceResourceID: takeFirst(seed.WorkspaceResourceID, uuid.New()),
Key: []string{takeFirst(seed.Key, namesgenerator.GetRandomName(1))},
Value: []string{takeFirst(seed.Value.String, namesgenerator.GetRandomName(1))},
Sensitive: []bool{takeFirst(seed.Sensitive, false)},
})
require.NoError(t, err, "insert meta data")
return meta
}

func File(t *testing.T, db database.Store, orig database.File) database.File {
file, err := db.InsertFile(context.Background(), database.InsertFileParams{
ID: takeFirst(orig.ID, uuid.New()),
Hash: takeFirst(orig.Hash, hex.EncodeToString(make([]byte, 32))),
CreatedAt: takeFirst(orig.CreatedAt, time.Now()),
CreatedBy: takeFirst(orig.CreatedBy, uuid.New()),
Mimetype: takeFirst(orig.Mimetype, "application/x-tar"),
Data: takeFirstBytes(orig.Data, []byte{}),
Data: takeFirstSlice(orig.Data, []byte{}),
})
require.NoError(t, err, "insert file")
return file
Expand Down Expand Up @@ -307,3 +330,43 @@ func TemplateVersion(t *testing.T, db database.Store, orig database.TemplateVers
require.NoError(t, err, "insert template version")
return version
}

func ParameterSchema(t *testing.T, db database.Store, seed database.ParameterSchema) database.ParameterSchema {
scheme, err := db.InsertParameterSchema(context.Background(), database.InsertParameterSchemaParams{
ID: takeFirst(seed.ID, uuid.New()),
JobID: takeFirst(seed.JobID, uuid.New()),
CreatedAt: takeFirst(seed.CreatedAt, time.Now()),
Name: takeFirst(seed.Name, namesgenerator.GetRandomName(1)),
Description: takeFirst(seed.Description, namesgenerator.GetRandomName(1)),
DefaultSourceScheme: takeFirst(seed.DefaultSourceScheme, database.ParameterSourceSchemeNone),
DefaultSourceValue: takeFirst(seed.DefaultSourceValue, ""),
AllowOverrideSource: takeFirst(seed.AllowOverrideSource, false),
DefaultDestinationScheme: takeFirst(seed.DefaultDestinationScheme, database.ParameterDestinationSchemeNone),
AllowOverrideDestination: takeFirst(seed.AllowOverrideDestination, false),
DefaultRefresh: takeFirst(seed.DefaultRefresh, ""),
RedisplayValue: takeFirst(seed.RedisplayValue, false),
ValidationError: takeFirst(seed.ValidationError, ""),
ValidationCondition: takeFirst(seed.ValidationCondition, ""),
ValidationTypeSystem: takeFirst(seed.ValidationTypeSystem, database.ParameterTypeSystemNone),
ValidationValueType: takeFirst(seed.ValidationValueType, ""),
Index: takeFirst(seed.Index, 1),
})
require.NoError(t, err, "insert parameter scheme")
return scheme
}

func ParameterValue(t *testing.T, db database.Store, seed database.ParameterValue) database.ParameterValue {
scheme, err := db.InsertParameterValue(context.Background(), database.InsertParameterValueParams{
ID: takeFirst(seed.ID, uuid.New()),
Name: takeFirst(seed.Name, namesgenerator.GetRandomName(1)),
CreatedAt: takeFirst(seed.CreatedAt, time.Now()),
UpdatedAt: takeFirst(seed.UpdatedAt, time.Now()),
Scope: takeFirst(seed.Scope, database.ParameterScopeWorkspace),
ScopeID: takeFirst(seed.ScopeID, uuid.New()),
SourceScheme: takeFirst(seed.SourceScheme, database.ParameterSourceSchemeNone),
SourceValue: takeFirst(seed.SourceValue, ""),
DestinationScheme: takeFirst(seed.DestinationScheme, database.ParameterDestinationSchemeNone),
})
require.NoError(t, err, "insert parameter value")
return scheme
}
36 changes: 36 additions & 0 deletions coderd/database/dbgen/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/require"

"github.com/coder/coder/coderd/database"
Expand Down Expand Up @@ -50,6 +51,13 @@ func TestGenerator(t *testing.T) {
require.Equal(t, exp, must(db.GetWorkspaceResourceByID(context.Background(), exp.ID)))
})

t.Run("WorkspaceResourceMetadatum", func(t *testing.T) {
t.Parallel()
db := databasefake.New()
exp := dbgen.WorkspaceResourceMetadatums(t, db, database.WorkspaceResourceMetadatum{})
require.Equal(t, exp, must(db.GetWorkspaceResourceMetadataByResourceIDs(context.Background(), []uuid.UUID{exp[0].WorkspaceResourceID})))
})

t.Run("Job", func(t *testing.T) {
t.Parallel()
db := databasefake.New()
Expand Down Expand Up @@ -82,6 +90,16 @@ func TestGenerator(t *testing.T) {
require.Equal(t, exp, must(db.GetOrganizationByID(context.Background(), exp.ID)))
})

t.Run("OrganizationMember", func(t *testing.T) {
t.Parallel()
db := databasefake.New()
exp := dbgen.OrganizationMember(t, db, database.OrganizationMember{})
require.Equal(t, exp, must(db.GetOrganizationMemberByUserID(context.Background(), database.GetOrganizationMemberByUserIDParams{
OrganizationID: exp.OrganizationID,
UserID: exp.UserID,
})))
})

t.Run("Workspace", func(t *testing.T) {
t.Parallel()
db := databasefake.New()
Expand Down Expand Up @@ -110,6 +128,24 @@ func TestGenerator(t *testing.T) {
require.Equal(t, exp, must(db.GetTemplateVersionByID(context.Background(), exp.ID)))
})

t.Run("ParameterSchema", func(t *testing.T) {
t.Parallel()
db := databasefake.New()
exp := dbgen.ParameterSchema(t, db, database.ParameterSchema{})
require.Equal(t, []database.ParameterSchema{exp}, must(db.GetParameterSchemasByJobID(context.Background(), exp.JobID)))
})

t.Run("ParameterValue", func(t *testing.T) {
t.Parallel()
db := databasefake.New()
exp := dbgen.ParameterValue(t, db, database.ParameterValue{})
require.Equal(t, exp, must(db.GetParameterValueByScopeAndName(context.Background(), database.GetParameterValueByScopeAndNameParams{
Scope: exp.Scope,
ScopeID: exp.ScopeID,
Name: exp.Name,
})))
})

t.Run("WorkspaceBuild", func(t *testing.T) {
t.Parallel()
db := databasefake.New()
Expand Down
10 changes: 6 additions & 4 deletions coderd/database/dbgen/take.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package dbgen
import "net"

func takeFirstIP(values ...net.IPNet) net.IPNet {
takeFirstSlice([]string{})

return takeFirstF(values, func(v net.IPNet) bool {
return len(v.IP) != 0 && len(v.Mask) != 0
})
}

// takeFirstBytes implements takeFirst for []byte.
// []byte is not a comparable type.
func takeFirstBytes(values ...[]byte) []byte {
return takeFirstF(values, func(v []byte) bool {
// takeFirstSlice implements takeFirst for []any.
// []any is not a comparable type.
func takeFirstSlice[T any](values ...[]T) []T {
return takeFirstF(values, func(v []T) bool {
return len(v) != 0
})
}
Expand Down