@@ -7,8 +7,11 @@ import (
7
7
8
8
"github.com/google/uuid"
9
9
"github.com/stretchr/testify/require"
10
+ "go.uber.org/mock/gomock"
11
+ "golang.org/x/xerrors"
10
12
11
13
"github.com/coder/coder/v2/coderd/database/dbmem"
14
+ "github.com/coder/coder/v2/coderd/database/dbmock"
12
15
"github.com/coder/coder/v2/coderd/database/dbtestutil"
13
16
"github.com/coder/coder/v2/coderd/runtimeconfig"
14
17
"github.com/coder/coder/v2/coderd/util/ptr"
@@ -45,6 +48,50 @@ func ExampleDeploymentValues() {
45
48
// Output: hello world
46
49
}
47
50
51
+ // TestResolveDBError ensures a db error that is not a sql.ErrNoRows
52
+ // will bubble up using Coalesce. The error should not be ignored and replaced
53
+ // with the startup value.
54
+ func TestResolveDBError (t * testing.T ) {
55
+ t .Parallel ()
56
+
57
+ dbErr := xerrors .Errorf ("some db error" )
58
+ ctrl := gomock .NewController (t )
59
+ mDB := dbmock .NewMockStore (ctrl )
60
+ // Error on fetch
61
+ mDB .EXPECT ().
62
+ GetRuntimeConfig (gomock .Any (), gomock .Any ()).
63
+ Times (1 ).
64
+ Return ("" , dbErr )
65
+
66
+ // Error on upsert
67
+ mDB .EXPECT ().
68
+ UpsertRuntimeConfig (gomock .Any (), gomock .Any ()).
69
+ Times (1 ).
70
+ Return (dbErr )
71
+
72
+ // Error on delete
73
+ mDB .EXPECT ().
74
+ DeleteRuntimeConfig (gomock .Any (), gomock .Any ()).
75
+ Times (1 ).
76
+ Return (dbErr )
77
+
78
+ st := runtimeconfig .NewStoreManager ()
79
+ var stringField runtimeconfig.DeploymentEntry [* serpent.String ]
80
+ stringField .Initialize ("string-field" )
81
+ stringField .SetStartupValue ("default" )
82
+
83
+ ctx := testutil .Context (t , testutil .WaitMedium )
84
+ // Resolve
85
+ _ , err := stringField .Coalesce (ctx , st .Resolver (mDB ))
86
+ require .ErrorIs (t , err , dbErr )
87
+ // Set
88
+ err = stringField .SetRuntimeValue (ctx , st .Resolver (mDB ), serpent .StringOf (ptr .Ref ("hello world" )))
89
+ require .ErrorIs (t , err , dbErr )
90
+ // Unset
91
+ err = stringField .UnsetRuntimeValue (ctx , st .Resolver (mDB ))
92
+ require .ErrorIs (t , err , dbErr )
93
+ }
94
+
48
95
// TestSerpentDeploymentEntry uses the package as the serpent options will use it.
49
96
// Some of the usage might feel awkward, since the serpent package values come from
50
97
// the serpent parsing (strings), not manual assignment.
@@ -69,6 +116,11 @@ func TestSerpentDeploymentEntry(t *testing.T) {
69
116
entries .Bool .Initialize ("bool-field" )
70
117
entries .Struct .Initialize ("struct-field" )
71
118
119
+ // Check the Type() methods are unchanged
120
+ require .Equal (t , entries .String .Type (), (serpent .String ("" )).Type ())
121
+ require .Equal (t , entries .Bool .Type (), (serpent .Bool (false )).Type ())
122
+ require .Equal (t , entries .Struct .Type (), (& serpent.Struct [codersdk.Feature ]{}).Type ())
123
+
72
124
// When using Coalesce, the default value is the empty value
73
125
stringVal , err := entries .String .Coalesce (ctx , st .Resolver (db ))
74
126
require .NoError (t , err )
@@ -115,6 +167,13 @@ func TestSerpentDeploymentEntry(t *testing.T) {
115
167
require .NoError (t , err )
116
168
require .Equal (t , structVal .Value .Entitlement , codersdk .EntitlementGracePeriod )
117
169
170
+ // Test unset
171
+ err = entries .String .UnsetRuntimeValue (ctx , st .Resolver (db ))
172
+ require .NoError (t , err )
173
+ stringVal , err = entries .String .Coalesce (ctx , st .Resolver (db ))
174
+ require .NoError (t , err )
175
+ require .Equal (t , "default" , stringVal .String ())
176
+
118
177
// Test using org scoped resolver
119
178
orgID := uuid .New ()
120
179
orgResolver := st .OrganizationResolver (db , orgID )
@@ -129,4 +188,11 @@ func TestSerpentDeploymentEntry(t *testing.T) {
129
188
stringVal , err = entries .String .Coalesce (ctx , orgResolver )
130
189
require .NoError (t , err )
131
190
require .Equal (t , "hello organizations" , stringVal .String ())
191
+ // Unset org runtime
192
+ err = entries .String .UnsetRuntimeValue (ctx , orgResolver )
193
+ require .NoError (t , err )
194
+ // Verify org runtime is back to default
195
+ stringVal , err = entries .String .Coalesce (ctx , orgResolver )
196
+ require .NoError (t , err )
197
+ require .Equal (t , "default" , stringVal .String ())
132
198
}
0 commit comments