|
| 1 | +package databasefake |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"testing" |
| 6 | +"time" |
| 7 | + |
| 8 | +"github.com/coder/coder/coderd/database" |
| 9 | +"github.com/google/uuid" |
| 10 | +"github.com/moby/moby/pkg/namesgenerator" |
| 11 | +"github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +constprimaryOrgName="primary-org" |
| 15 | + |
| 16 | +typeGeneratorstruct { |
| 17 | +// names is a map of names to uuids. |
| 18 | +namesmap[string]uuid.UUID |
| 19 | +primaryOrg*database.Organization |
| 20 | +testT*testing.T |
| 21 | + |
| 22 | +db database.Store |
| 23 | +} |
| 24 | + |
| 25 | +funcNewGenerator(t*testing.T,db database.Store)*Generator { |
| 26 | +if_,ok:=db.(FakeDatabase);!ok { |
| 27 | +panic("Generator db must be a FakeDatabase") |
| 28 | +} |
| 29 | +return&Generator{ |
| 30 | +names:make(map[string]uuid.UUID), |
| 31 | +testT:t, |
| 32 | +db:db, |
| 33 | +} |
| 34 | +} |
| 35 | + |
| 36 | +// PrimaryOrg is to keep all resources in the same default org if not |
| 37 | +// specified. |
| 38 | +func (g*Generator)PrimaryOrg(ctx context.Context) database.Organization { |
| 39 | +ifg.primaryOrg==nil { |
| 40 | +org:=g.Organization(ctx,"primary-org", database.Organization{ |
| 41 | +ID:g.Lookup(primaryOrgName), |
| 42 | +Name:primaryOrgName, |
| 43 | +Description:"This is the default primary organization for all tests", |
| 44 | +CreatedAt:time.Now(), |
| 45 | +UpdatedAt:time.Now(), |
| 46 | +}) |
| 47 | +g.primaryOrg=&org |
| 48 | +} |
| 49 | + |
| 50 | +return*g.primaryOrg |
| 51 | +} |
| 52 | + |
| 53 | +funcpopulate[DBTypeany](ctx context.Context,g*Generator,namestring,seedDBType)DBType { |
| 54 | +out:=g.Populate(ctx,map[string]interface{}{ |
| 55 | +name:seed, |
| 56 | +}) |
| 57 | +returnout[name].(DBType) |
| 58 | +} |
| 59 | + |
| 60 | +func (g*Generator)Group(ctx context.Context,namestring,seed database.Group) database.Group { |
| 61 | +returnpopulate(ctx,g,name,seed) |
| 62 | +} |
| 63 | + |
| 64 | +func (g*Generator)Organization(ctx context.Context,namestring,seed database.Organization) database.Organization { |
| 65 | +returnpopulate(ctx,g,name,seed) |
| 66 | +} |
| 67 | + |
| 68 | +func (g*Generator)Workspace(ctx context.Context,namestring,seed database.Workspace) database.Workspace { |
| 69 | +returnpopulate(ctx,g,name,seed) |
| 70 | +} |
| 71 | + |
| 72 | +func (g*Generator)Template(ctx context.Context,namestring,seed database.Template) database.Template { |
| 73 | +returnpopulate(ctx,g,name,seed) |
| 74 | +} |
| 75 | + |
| 76 | +func (g*Generator)TemplateVersion(ctx context.Context,namestring,seed database.TemplateVersion) database.TemplateVersion { |
| 77 | +returnpopulate(ctx,g,name,seed) |
| 78 | +} |
| 79 | + |
| 80 | +func (g*Generator)WorkspaceBuild(ctx context.Context,namestring,seed database.WorkspaceBuild) database.WorkspaceBuild { |
| 81 | +returnpopulate(ctx,g,name,seed) |
| 82 | +} |
| 83 | + |
| 84 | +func (g*Generator)User(ctx context.Context,namestring,seed database.User) database.User { |
| 85 | +returnpopulate(ctx,g,name,seed) |
| 86 | +} |
| 87 | + |
| 88 | +func (g*Generator)Populate(ctx context.Context,seedmap[string]interface{})map[string]interface{} { |
| 89 | +db:=g.db |
| 90 | +t:=g.testT |
| 91 | + |
| 92 | +forname,v:=rangeseed { |
| 93 | +switchorig:=v.(type) { |
| 94 | +case database.Template: |
| 95 | +template,err:=db.InsertTemplate(ctx, database.InsertTemplateParams{ |
| 96 | +ID:g.Lookup(name), |
| 97 | +CreatedAt:time.Now(), |
| 98 | +UpdatedAt:time.Now(), |
| 99 | +OrganizationID:takeFirst(orig.OrganizationID,g.PrimaryOrg(ctx).ID), |
| 100 | +Name:takeFirst(orig.Name,namesgenerator.GetRandomName(1)), |
| 101 | +Provisioner:takeFirst(orig.Provisioner,database.ProvisionerTypeEcho), |
| 102 | +ActiveVersionID:takeFirst(orig.ActiveVersionID,uuid.New()), |
| 103 | +Description:takeFirst(orig.Description,namesgenerator.GetRandomName(1)), |
| 104 | +DefaultTTL:takeFirst(orig.DefaultTTL,3600), |
| 105 | +CreatedBy:takeFirst(orig.CreatedBy,uuid.New()), |
| 106 | +Icon:takeFirst(orig.Icon,namesgenerator.GetRandomName(1)), |
| 107 | +UserACL:orig.UserACL, |
| 108 | +GroupACL:orig.GroupACL, |
| 109 | +DisplayName:takeFirst(orig.DisplayName,namesgenerator.GetRandomName(1)), |
| 110 | +AllowUserCancelWorkspaceJobs:takeFirst(orig.AllowUserCancelWorkspaceJobs,true), |
| 111 | +}) |
| 112 | +require.NoError(t,err,"insert template") |
| 113 | + |
| 114 | +seed[name]=template |
| 115 | +case database.Workspace: |
| 116 | +workspace,err:=db.InsertWorkspace(ctx, database.InsertWorkspaceParams{ |
| 117 | +ID:g.Lookup(name), |
| 118 | +CreatedAt:time.Now(), |
| 119 | +UpdatedAt:time.Now(), |
| 120 | +OrganizationID:takeFirst(orig.OrganizationID,g.PrimaryOrg(ctx).ID), |
| 121 | +TemplateID:takeFirst(orig.TemplateID,uuid.New()), |
| 122 | +Name:takeFirst(orig.Name,namesgenerator.GetRandomName(1)), |
| 123 | +AutostartSchedule:orig.AutostartSchedule, |
| 124 | +Ttl:orig.Ttl, |
| 125 | +}) |
| 126 | +require.NoError(t,err,"insert workspace") |
| 127 | + |
| 128 | +seed[name]=workspace |
| 129 | +case database.WorkspaceBuild: |
| 130 | +build,err:=db.InsertWorkspaceBuild(ctx, database.InsertWorkspaceBuildParams{ |
| 131 | +ID:g.Lookup(name), |
| 132 | +CreatedAt:time.Now(), |
| 133 | +UpdatedAt:time.Now(), |
| 134 | +WorkspaceID:takeFirst(orig.WorkspaceID,uuid.New()), |
| 135 | +TemplateVersionID:takeFirst(orig.TemplateVersionID,uuid.New()), |
| 136 | +BuildNumber:takeFirst(orig.BuildNumber,0), |
| 137 | +Transition:takeFirst(orig.Transition,database.WorkspaceTransitionStart), |
| 138 | +InitiatorID:takeFirst(orig.InitiatorID,uuid.New()), |
| 139 | +JobID:takeFirst(orig.InitiatorID,uuid.New()), |
| 140 | +ProvisionerState: []byte{}, |
| 141 | +Deadline:time.Now(), |
| 142 | +Reason:takeFirst(orig.Reason,database.BuildReasonInitiator), |
| 143 | +}) |
| 144 | +require.NoError(t,err,"insert workspace build") |
| 145 | + |
| 146 | +seed[name]=build |
| 147 | +case database.User: |
| 148 | +user,err:=db.InsertUser(ctx, database.InsertUserParams{ |
| 149 | +ID:g.Lookup(name), |
| 150 | +Email:takeFirst(orig.Email,namesgenerator.GetRandomName(1)), |
| 151 | +Username:takeFirst(orig.Username,namesgenerator.GetRandomName(1)), |
| 152 | +HashedPassword: []byte{}, |
| 153 | +CreatedAt:time.Now(), |
| 154 | +UpdatedAt:time.Now(), |
| 155 | +RBACRoles: []string{}, |
| 156 | +LoginType:takeFirst(orig.LoginType,database.LoginTypePassword), |
| 157 | +}) |
| 158 | +require.NoError(t,err,"insert user") |
| 159 | + |
| 160 | +seed[name]=user |
| 161 | + |
| 162 | +case database.Organization: |
| 163 | +org,err:=db.InsertOrganization(ctx, database.InsertOrganizationParams{ |
| 164 | +ID:g.Lookup(name), |
| 165 | +Name:takeFirst(orig.Name,namesgenerator.GetRandomName(1)), |
| 166 | +Description:takeFirst(orig.Name,namesgenerator.GetRandomName(1)), |
| 167 | +CreatedAt:time.Now(), |
| 168 | +UpdatedAt:time.Now(), |
| 169 | +}) |
| 170 | +require.NoError(t,err,"insert organization") |
| 171 | + |
| 172 | +seed[name]=org |
| 173 | + |
| 174 | +case database.Group: |
| 175 | +org,err:=db.InsertGroup(ctx, database.InsertGroupParams{ |
| 176 | +ID:g.Lookup(name), |
| 177 | +Name:takeFirst(orig.Name,namesgenerator.GetRandomName(1)), |
| 178 | +OrganizationID:takeFirst(orig.OrganizationID,g.PrimaryOrg(ctx).ID), |
| 179 | +AvatarURL:takeFirst(orig.Name,"https://logo.example.com"), |
| 180 | +QuotaAllowance:takeFirst(orig.QuotaAllowance,0), |
| 181 | +}) |
| 182 | +require.NoError(t,err,"insert organization") |
| 183 | + |
| 184 | +seed[name]=org |
| 185 | +} |
| 186 | +} |
| 187 | +returnseed |
| 188 | +} |
| 189 | + |
| 190 | +func (tc*Generator)Lookup(namestring) uuid.UUID { |
| 191 | +iftc.names==nil { |
| 192 | +tc.names=make(map[string]uuid.UUID) |
| 193 | +} |
| 194 | +ifid,ok:=tc.names[name];ok { |
| 195 | +returnid |
| 196 | +} |
| 197 | +id:=uuid.New() |
| 198 | +tc.names[name]=id |
| 199 | +returnid |
| 200 | +} |
| 201 | + |
| 202 | +// takeFirst will take the first non-empty value. |
| 203 | +functakeFirst[Valuecomparable](values...Value)Value { |
| 204 | +varemptyValue |
| 205 | +for_,v:=rangevalues { |
| 206 | +ifv!=empty { |
| 207 | +returnv |
| 208 | +} |
| 209 | +} |
| 210 | +// If all empty, return empty |
| 211 | +returnempty |
| 212 | +} |