@@ -16,6 +16,7 @@ import (
16
16
"github.com/coder/coder/v2/coderd/database/dbgen"
17
17
"github.com/coder/coder/v2/coderd/database/dbtestutil"
18
18
agplschedule "github.com/coder/coder/v2/coderd/schedule"
19
+ "github.com/coder/coder/v2/coderd/util/ptr"
19
20
"github.com/coder/coder/v2/cryptorand"
20
21
"github.com/coder/coder/v2/enterprise/coderd/schedule"
21
22
"github.com/coder/coder/v2/testutil"
@@ -67,11 +68,12 @@ func TestTemplateUpdateBuildDeadlines(t *testing.T) {
67
68
68
69
// Workspace old max_deadline too soon
69
70
cases := []struct {
70
- name string
71
- now time.Time
72
- deadline time.Time
73
- maxDeadline time.Time
74
- newDeadline time.Time // 0 for no change
71
+ name string
72
+ now time.Time
73
+ deadline time.Time
74
+ maxDeadline time.Time
75
+ // Set to nil for no change.
76
+ newDeadline * time.Time
75
77
newMaxDeadline time.Time
76
78
noQuietHours bool
77
79
autostopReq * agplschedule.TemplateAutostopRequirement
@@ -82,7 +84,7 @@ func TestTemplateUpdateBuildDeadlines(t *testing.T) {
82
84
deadline : buildTime ,
83
85
maxDeadline : buildTime .Add (1 * time .Hour ),
84
86
// Unchanged since the max deadline is too soon.
85
- newDeadline : time. Time {} ,
87
+ newDeadline : nil ,
86
88
newMaxDeadline : buildTime .Add (1 * time .Hour ),
87
89
},
88
90
{
@@ -92,7 +94,7 @@ func TestTemplateUpdateBuildDeadlines(t *testing.T) {
92
94
deadline : buildTime ,
93
95
// Far into the future...
94
96
maxDeadline : nextQuietHours .Add (24 * time .Hour ),
95
- newDeadline : time. Time {} ,
97
+ newDeadline : nil ,
96
98
// We will use now() + 2 hours if the newly calculated max deadline
97
99
// from the workspace build time is before now.
98
100
newMaxDeadline : nextQuietHours .Add (8 * time .Hour ),
@@ -104,7 +106,7 @@ func TestTemplateUpdateBuildDeadlines(t *testing.T) {
104
106
deadline : buildTime ,
105
107
// Far into the future...
106
108
maxDeadline : nextQuietHours .Add (24 * time .Hour ),
107
- newDeadline : time. Time {} ,
109
+ newDeadline : nil ,
108
110
// We will use now() + 2 hours if the newly calculated max deadline
109
111
// from the workspace build time is within the next 2 hours.
110
112
newMaxDeadline : nextQuietHours .Add (1 * time .Hour ),
@@ -116,7 +118,7 @@ func TestTemplateUpdateBuildDeadlines(t *testing.T) {
116
118
deadline : buildTime ,
117
119
// Far into the future...
118
120
maxDeadline : nextQuietHours .Add (24 * time .Hour ),
119
- newDeadline : time. Time {} ,
121
+ newDeadline : nil ,
120
122
newMaxDeadline : nextQuietHours ,
121
123
},
122
124
{
@@ -127,7 +129,7 @@ func TestTemplateUpdateBuildDeadlines(t *testing.T) {
127
129
deadline : nextQuietHours .Add (24 * time .Hour ),
128
130
maxDeadline : nextQuietHours .Add (24 * time .Hour ),
129
131
// The deadline should match since it is after the new max deadline.
130
- newDeadline : nextQuietHours ,
132
+ newDeadline : ptr . Ref ( nextQuietHours ) ,
131
133
newMaxDeadline : nextQuietHours ,
132
134
},
133
135
{
@@ -142,14 +144,43 @@ func TestTemplateUpdateBuildDeadlines(t *testing.T) {
142
144
deadline : buildTime .Add (time .Hour * 8 ),
143
145
maxDeadline : time.Time {}, // No max set
144
146
// Should be unchanged
145
- newDeadline : buildTime .Add (time .Hour * 8 ),
147
+ newDeadline : ptr . Ref ( buildTime .Add (time .Hour * 8 ) ),
146
148
newMaxDeadline : time.Time {},
147
149
noQuietHours : true ,
148
150
autostopReq : & agplschedule.TemplateAutostopRequirement {
149
151
DaysOfWeek : 0 ,
150
152
Weeks : 0 ,
151
153
},
152
154
},
155
+ {
156
+ // A bug existed where MaxDeadline could be set, but deadline was
157
+ // `time.Time{}`. This is a logical inconsistency because the "max"
158
+ // deadline was ignored.
159
+ name : "NoDeadline" ,
160
+ now : buildTime ,
161
+ deadline : time.Time {},
162
+ maxDeadline : time.Time {}, // No max set
163
+ // Should be unchanged
164
+ newDeadline : ptr .Ref (time.Time {}),
165
+ newMaxDeadline : time.Time {},
166
+ noQuietHours : true ,
167
+ autostopReq : & agplschedule.TemplateAutostopRequirement {
168
+ DaysOfWeek : 0 ,
169
+ Weeks : 0 ,
170
+ },
171
+ },
172
+
173
+ {
174
+ // Similar to 'NoDeadline' test. This has a MaxDeadline set, so
175
+ // the deadline of the workspace should now be set.
176
+ name : "WorkspaceDeadlineNowSet" ,
177
+ now : nextQuietHours .Add (- 6 * time .Hour ),
178
+ // Start with unset times
179
+ deadline : time.Time {},
180
+ maxDeadline : time.Time {},
181
+ newDeadline : ptr .Ref (nextQuietHours ),
182
+ newMaxDeadline : nextQuietHours ,
183
+ },
153
184
}
154
185
155
186
for _ , c := range cases {
@@ -275,10 +306,10 @@ func TestTemplateUpdateBuildDeadlines(t *testing.T) {
275
306
newBuild , err := db .GetWorkspaceBuildByID (ctx , wsBuild .ID )
276
307
require .NoError (t , err )
277
308
278
- if c .newDeadline . IsZero () {
279
- c .newDeadline = wsBuild .Deadline
309
+ if c .newDeadline == nil {
310
+ c .newDeadline = & wsBuild .Deadline
280
311
}
281
- require .WithinDuration (t , c .newDeadline , newBuild .Deadline , time .Second )
312
+ require .WithinDuration (t , * c .newDeadline , newBuild .Deadline , time .Second )
282
313
require .WithinDuration (t , c .newMaxDeadline , newBuild .MaxDeadline , time .Second )
283
314
284
315
// Check that the new build has the same state as before.
0 commit comments