@@ -18,56 +18,127 @@ import (
18
18
func TestTemplateTTL (t * testing.T ) {
19
19
t .Parallel ()
20
20
21
- t .Run ("ModifiesWorkspaceTTL" ,func (t * testing.T ) {
22
- t .Parallel ()
21
+ tests := []struct {
22
+ name string
23
+ fromTTL time.Duration
24
+ toTTL time.Duration
25
+ expected sql.NullInt64
26
+ }{
27
+ {
28
+ name :"ModifyTTLDurationDown" ,
29
+ fromTTL :24 * time .Hour ,
30
+ toTTL :1 * time .Hour ,
31
+ expected : sql.NullInt64 {Valid :true ,Int64 :int64 (1 * time .Hour )},
32
+ },
33
+ {
34
+ name :"ModifyTTLDurationUp" ,
35
+ fromTTL :24 * time .Hour ,
36
+ toTTL :36 * time .Hour ,
37
+ expected : sql.NullInt64 {Valid :true ,Int64 :int64 (36 * time .Hour )},
38
+ },
39
+ {
40
+ name :"DisableTTL" ,
41
+ fromTTL :24 * time .Hour ,
42
+ toTTL :0 ,
43
+ expected : sql.NullInt64 {},
44
+ },
45
+ }
23
46
24
- var (
25
- db ,_ = dbtestutil .NewDB (t )
26
- ctx = testutil .Context (t ,testutil .WaitLong )
27
- user = dbgen .User (t ,db , database.User {})
28
- file = dbgen .File (t ,db , database.File {CreatedBy :user .ID })
29
- templateJob = dbgen .ProvisionerJob (t ,db ,nil , database.ProvisionerJob {
30
- FileID :file .ID ,
31
- InitiatorID :user .ID ,
32
- Tags : database.StringMap {"foo" :"bar" },
33
- })
34
- defaultTTL = 24 * time .Hour
35
- templateVersion = dbgen .TemplateVersion (t ,db , database.TemplateVersion {
36
- CreatedBy :user .ID ,
37
- JobID :templateJob .ID ,
38
- OrganizationID :templateJob .OrganizationID ,
47
+ for _ ,tt := range tests {
48
+ tt := tt
49
+
50
+ t .Run (tt .name ,func (t * testing.T ) {
51
+ t .Parallel ()
52
+
53
+ var (
54
+ db ,_ = dbtestutil .NewDB (t )
55
+ ctx = testutil .Context (t ,testutil .WaitLong )
56
+ user = dbgen .User (t ,db , database.User {})
57
+ file = dbgen .File (t ,db , database.File {CreatedBy :user .ID })
58
+ // Create first template
59
+ templateJob = dbgen .ProvisionerJob (t ,db ,nil , database.ProvisionerJob {
60
+ FileID :file .ID ,
61
+ InitiatorID :user .ID ,
62
+ Tags : database.StringMap {"foo" :"bar" },
63
+ })
64
+ templateVersion = dbgen .TemplateVersion (t ,db , database.TemplateVersion {
65
+ CreatedBy :user .ID ,
66
+ JobID :templateJob .ID ,
67
+ OrganizationID :templateJob .OrganizationID ,
68
+ })
69
+ template = dbgen .Template (t ,db , database.Template {
70
+ ActiveVersionID :templateVersion .ID ,
71
+ CreatedBy :user .ID ,
72
+ OrganizationID :templateJob .OrganizationID ,
73
+ })
74
+ // Create second template
75
+ otherTTL = tt .fromTTL + 6 * time .Hour
76
+ otherTemplateJob = dbgen .ProvisionerJob (t ,db ,nil , database.ProvisionerJob {
77
+ FileID :file .ID ,
78
+ InitiatorID :user .ID ,
79
+ Tags : database.StringMap {"foo" :"bar" },
80
+ })
81
+ otherTemplateVersion = dbgen .TemplateVersion (t ,db , database.TemplateVersion {
82
+ CreatedBy :user .ID ,
83
+ JobID :otherTemplateJob .ID ,
84
+ OrganizationID :otherTemplateJob .OrganizationID ,
85
+ })
86
+ otherTemplate = dbgen .Template (t ,db , database.Template {
87
+ ActiveVersionID :otherTemplateVersion .ID ,
88
+ CreatedBy :user .ID ,
89
+ OrganizationID :otherTemplateJob .OrganizationID ,
90
+ })
91
+ )
92
+
93
+ templateScheduleStore := schedule .NewAGPLTemplateScheduleStore ()
94
+
95
+ // Set both template's default TTL
96
+ template ,err := templateScheduleStore .Set (ctx ,db ,template , schedule.TemplateScheduleOptions {
97
+ DefaultTTL :tt .fromTTL ,
39
98
})
40
- template = dbgen .Template (t ,db , database.Template {
41
- ActiveVersionID :templateVersion .ID ,
42
- CreatedBy :user .ID ,
43
- OrganizationID :templateJob .OrganizationID ,
44
- DefaultTTL :int64 (defaultTTL ),
99
+ require .NoError (t ,err )
100
+ otherTemplate ,err = templateScheduleStore .Set (ctx ,db ,otherTemplate , schedule.TemplateScheduleOptions {
101
+ DefaultTTL :otherTTL ,
45
102
})
46
- workspace = dbgen .Workspace (t ,db , database.WorkspaceTable {
103
+ require .NoError (t ,err )
104
+
105
+ // We create two workspaces here, one with the template we're modifying, the
106
+ // other with a different template. We want to ensure we only modify one
107
+ // of the workspaces.
108
+ workspace := dbgen .Workspace (t ,db , database.WorkspaceTable {
47
109
OwnerID :user .ID ,
48
110
TemplateID :template .ID ,
49
111
OrganizationID :templateJob .OrganizationID ,
50
112
LastUsedAt :dbtime .Now (),
51
- Ttl : sql.NullInt64 {Valid :true ,Int64 :int64 (defaultTTL )},
113
+ Ttl : sql.NullInt64 {Valid :true ,Int64 :int64 (tt .fromTTL )},
114
+ })
115
+ otherWorkspace := dbgen .Workspace (t ,db , database.WorkspaceTable {
116
+ OwnerID :user .ID ,
117
+ TemplateID :otherTemplate .ID ,
118
+ OrganizationID :otherTemplateJob .OrganizationID ,
119
+ LastUsedAt :dbtime .Now (),
120
+ Ttl : sql.NullInt64 {Valid :true ,Int64 :int64 (otherTTL )},
52
121
})
53
- )
54
122
55
- templateScheduleStore := schedule .NewAGPLTemplateScheduleStore ()
123
+ // Ensure the workspace's start with the correct TTLs
124
+ require .Equal (t , sql.NullInt64 {Valid :true ,Int64 :int64 (tt .fromTTL )},workspace .Ttl )
125
+ require .Equal (t , sql.NullInt64 {Valid :true ,Int64 :int64 (otherTTL )},otherWorkspace .Ttl )
56
126
57
- // We've created a template with a TTL of 24 hours, so we expect our
58
- // workspace to have a TTL of 24 hours.
59
- require .Equal (t , sql.NullInt64 {Valid :true ,Int64 :int64 (defaultTTL )},workspace .Ttl )
127
+ // Update _only_ the primary template's TTL
128
+ _ ,err = templateScheduleStore .Set (ctx ,db ,template , schedule.TemplateScheduleOptions {
129
+ DefaultTTL :tt .toTTL ,
130
+ })
131
+ require .NoError (t ,err )
60
132
61
- // We expect an AGPL template schedule store to always update
62
- // the TTL of existing workspaces.
63
- _ ,err := templateScheduleStore .Set (ctx ,db ,template , schedule.TemplateScheduleOptions {
64
- DefaultTTL :1 * time .Hour ,
65
- })
66
- require .NoError (t ,err )
133
+ // Verify the primary workspace's TTL has been updated.
134
+ ws ,err := db .GetWorkspaceByID (ctx ,workspace .ID )
135
+ require .NoError (t ,err )
136
+ require .Equal (t ,tt .expected ,ws .Ttl )
67
137
68
- // Verify that the workspace's TTL has been updated.
69
- ws ,err := db .GetWorkspaceByID (ctx ,workspace .ID )
70
- require .NoError (t ,err )
71
- require .Equal (t , sql.NullInt64 {Valid :true ,Int64 :int64 (1 * time .Hour )},ws .Ttl )
72
- })
138
+ // Verify that the other workspace's TTL has not been touched.
139
+ ws ,err = db .GetWorkspaceByID (ctx ,otherWorkspace .ID )
140
+ require .NoError (t ,err )
141
+ require .Equal (t , sql.NullInt64 {Valid :true ,Int64 :int64 (otherTTL )},ws .Ttl )
142
+ })
143
+ }
73
144
}