1
1
package runtimeconfig_test
2
2
3
3
import (
4
+ "context"
4
5
"testing"
5
6
6
7
"github.com/coder/serpent"
7
8
"github.com/stretchr/testify/require"
8
9
9
10
"github.com/coder/coder/v2/coderd/coderdtest"
10
11
"github.com/coder/coder/v2/coderd/runtimeconfig"
12
+ "github.com/coder/coder/v2/coderd/util/ptr"
11
13
"github.com/coder/coder/v2/codersdk"
12
14
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
13
15
"github.com/coder/coder/v2/enterprise/coderd/license"
@@ -30,20 +32,39 @@ func TestConfig(t *testing.T) {
30
32
})
31
33
altOrg := coderdenttest .CreateOrganization (t , adminClient , coderdenttest.CreateOrganizationOptions {})
32
34
33
- t .Run ("panics unless initialized " , func (t * testing.T ) {
35
+ t .Run ("new " , func (t * testing.T ) {
34
36
t .Parallel ()
35
37
36
- field := runtimeconfig.Entry [* serpent.String ]{}
37
38
require .Panics (t , func () {
38
- field .StartupValue ().String ()
39
+ // "hello" cannot be set on a *serpent.Float64 field.
40
+ runtimeconfig .MustNew [* serpent.Float64 ]("key" , "hello" )
39
41
})
40
42
41
- field .Init ("my-field" )
42
43
require .NotPanics (t , func () {
43
- field . StartupValue (). String ( )
44
+ runtimeconfig . MustNew [ * serpent. Float64 ]( "key" , "91.1234" )
44
45
})
45
46
})
46
47
48
+ t .Run ("zero" , func (t * testing.T ) {
49
+ t .Parallel ()
50
+
51
+ // A zero-value declaration of a runtimeconfig.Entry should behave as a zero value of the generic type.
52
+ // NB! A key has not been set for this entry.
53
+ var field runtimeconfig.Entry [* serpent.Bool ]
54
+ var zero serpent.Bool
55
+ require .Equal (t , field .StartupValue ().Value (), zero .Value ())
56
+
57
+ // Setting a value will not produce an error.
58
+ require .NoError (t , field .SetStartupValue ("true" ))
59
+
60
+ // But attempting to resolve will produce an error.
61
+ _ , err := field .Resolve (context .Background (), runtimeconfig .NewNoopResolver ())
62
+ require .ErrorIs (t , err , runtimeconfig .ErrKeyNotSet )
63
+ // But attempting to set the runtime value will produce an error.
64
+ val := serpent .BoolOf (ptr .Ref (true ))
65
+ require .ErrorIs (t , field .SetRuntimeValue (context .Background (), runtimeconfig .NewNoopMutator (), val ), runtimeconfig .ErrKeyNotSet )
66
+ })
67
+
47
68
t .Run ("simple" , func (t * testing.T ) {
48
69
t .Parallel ()
49
70
@@ -57,12 +78,9 @@ func TestConfig(t *testing.T) {
57
78
override = serpent .String ("dogfood@dev.coder.com" )
58
79
)
59
80
60
- field := runtimeconfig.Entry [* serpent.String ]{}
61
- field .Init ("my-field" )
62
- // Check that no default has been set.
63
- require .Empty (t , field .StartupValue ().String ())
64
- // Initialize the value.
65
- require .NoError (t , field .Set (base .String ()))
81
+ field := runtimeconfig .MustNew [* serpent.String ]("my-field" , base .String ())
82
+ // Check that default has been set.
83
+ require .Equal (t , base .String (), field .StartupValue ().String ())
66
84
// Validate that it returns that value.
67
85
require .Equal (t , base .String (), field .String ())
68
86
// Validate that there is no org-level override right now.
@@ -73,7 +91,7 @@ func TestConfig(t *testing.T) {
73
91
require .NoError (t , err )
74
92
require .Equal (t , base .String (), val .String ())
75
93
// Set an org-level override.
76
- require .NoError (t , field .Save (ctx , mutator , & override ))
94
+ require .NoError (t , field .SetRuntimeValue (ctx , mutator , & override ))
77
95
// Coalesce now returns the org-level value.
78
96
val , err = field .Coalesce (ctx , resolver )
79
97
require .NoError (t , err )
@@ -88,8 +106,6 @@ func TestConfig(t *testing.T) {
88
106
resolver := runtimeconfig .NewOrgResolver (altOrg .ID , runtimeconfig .NewStoreResolver (store ))
89
107
mutator := runtimeconfig .NewOrgMutator (altOrg .ID , runtimeconfig .NewStoreMutator (store ))
90
108
91
- field := runtimeconfig.Entry [* serpent.Struct [map [string ]string ]]{}
92
- field .Init ("my-field" )
93
109
var (
94
110
base = serpent.Struct [map [string ]string ]{
95
111
Value : map [string ]string {"access_type" : "offline" },
@@ -102,10 +118,10 @@ func TestConfig(t *testing.T) {
102
118
}
103
119
)
104
120
105
- // Check that no default has been set.
106
- require . Empty ( t , field . StartupValue (). Value )
107
- // Initialize the value .
108
- require .NoError (t , field .Set ( base .String () ))
121
+ field := runtimeconfig . MustNew [ * serpent. Struct [ map [ string ] string ]]( "my-field" , base . String ())
122
+
123
+ // Check that default has been set .
124
+ require .Equal (t , base . String (), field .StartupValue () .String ())
109
125
// Validate that there is no org-level override right now.
110
126
_ , err := field .Resolve (ctx , resolver )
111
127
require .ErrorIs (t , err , runtimeconfig .EntryNotFound )
@@ -114,7 +130,7 @@ func TestConfig(t *testing.T) {
114
130
require .NoError (t , err )
115
131
require .Equal (t , base .Value , val .Value )
116
132
// Set an org-level override.
117
- require .NoError (t , field .Save (ctx , mutator , & override ))
133
+ require .NoError (t , field .SetRuntimeValue (ctx , mutator , & override ))
118
134
// Coalesce now returns the org-level value.
119
135
structVal , err := field .Resolve (ctx , resolver )
120
136
require .NoError (t , err )
0 commit comments