@@ -17,21 +17,77 @@ import (
17
17
"github.com/coder/coder/v2/testutil"
18
18
)
19
19
20
+ func TestUsage (t * testing.T ) {
21
+ t .Run ("deployment value without runtimeconfig" , func (t * testing.T ) {
22
+ t .Parallel ()
23
+
24
+ var field serpent.StringArray
25
+ opt := serpent.Option {
26
+ Name : "my deployment value" ,
27
+ Description : "this mimicks an option we'd define in codersdk/deployment.go" ,
28
+ Env : "MY_DEPLOYMENT_VALUE" ,
29
+ Default : "pestle,mortar" ,
30
+ Value : & field ,
31
+ }
32
+
33
+ set := serpent.OptionSet {opt }
34
+ require .NoError (t , set .SetDefaults ())
35
+ require .Equal (t , []string {"pestle" , "mortar" }, field .Value ())
36
+ })
37
+
38
+ t .Run ("deployment value with runtimeconfig" , func (t * testing.T ) {
39
+ t .Parallel ()
40
+
41
+ _ , altOrg := setup (t )
42
+
43
+ ctx := testutil .Context (t , testutil .WaitShort )
44
+ store := dbmem .New ()
45
+ resolver := runtimeconfig .NewOrgResolver (altOrg .ID , runtimeconfig .NewStoreResolver (store ))
46
+ mutator := runtimeconfig .NewOrgMutator (altOrg .ID , runtimeconfig .NewStoreMutator (store ))
47
+
48
+ // NOTE: this field is now wrapped
49
+ var field runtimeconfig.Entry [* serpent.HostPort ]
50
+ opt := serpent.Option {
51
+ Name : "my deployment value" ,
52
+ Description : "this mimicks an option we'd define in codersdk/deployment.go" ,
53
+ Env : "MY_DEPLOYMENT_VALUE" ,
54
+ Default : "localhost:1234" ,
55
+ Value : & field ,
56
+ }
57
+
58
+ set := serpent.OptionSet {opt }
59
+ require .NoError (t , set .SetDefaults ())
60
+
61
+ // The value has to now be retrieved from a StartupValue() call.
62
+ require .Equal (t , "localhost:1234" , field .StartupValue ().String ())
63
+
64
+ // One new constraint is that we have to set the key on the runtimeconfig.Entry.
65
+ // Attempting to perform any operation which accesses the store will enforce the need for a key.
66
+ _ , err := field .Resolve (ctx , resolver )
67
+ require .ErrorIs (t , err , runtimeconfig .ErrKeyNotSet )
68
+
69
+ // Let's see that key. The environment var name is likely to be the most stable.
70
+ field .SetKey (opt .Env )
71
+
72
+ newVal := serpent.HostPort {Host : "12.34.56.78" , Port : "1234" }
73
+ // Now that we've set it, we can update the runtime value of this field, which modifies given store.
74
+ require .NoError (t , field .SetRuntimeValue (ctx , mutator , & newVal ))
75
+
76
+ // ...and we can retrieve the value, as well.
77
+ resolved , err := field .Resolve (ctx , resolver )
78
+ require .NoError (t , err )
79
+ require .Equal (t , newVal .String (), resolved .String ())
80
+
81
+ // We can also remove the runtime config.
82
+ require .NoError (t , field .UnsetRuntimeValue (ctx , mutator ))
83
+ })
84
+ }
85
+
20
86
// TestConfig demonstrates creating org-level overrides for deployment-level settings.
21
87
func TestConfig (t * testing.T ) {
22
88
t .Parallel ()
23
89
24
- vals := coderdtest .DeploymentValues (t )
25
- vals .Experiments = []string {string (codersdk .ExperimentMultiOrganization )}
26
- adminClient , _ , _ , _ := coderdenttest .NewWithAPI (t , & coderdenttest.Options {
27
- Options : & coderdtest.Options {DeploymentValues : vals },
28
- LicenseOptions : & coderdenttest.LicenseOptions {
29
- Features : license.Features {
30
- codersdk .FeatureMultipleOrganizations : 1 ,
31
- },
32
- },
33
- })
34
- altOrg := coderdenttest .CreateOrganization (t , adminClient , coderdenttest.CreateOrganizationOptions {})
90
+ _ , altOrg := setup (t )
35
91
36
92
t .Run ("new" , func (t * testing.T ) {
37
93
t .Parallel ()
@@ -119,7 +175,7 @@ func TestConfig(t *testing.T) {
119
175
}
120
176
)
121
177
122
- field := runtimeconfig .MustNew [* serpent.Struct [map [string ]string ]]("my-field" , base .String ())
178
+ field := runtimeconfig .MustNew [* serpent.Struct [map [string ]string ]]("my-field" , base .String ())
123
179
124
180
// Check that default has been set.
125
181
require .Equal (t , base .String (), field .StartupValue ().String ())
@@ -138,3 +194,20 @@ func TestConfig(t *testing.T) {
138
194
require .Equal (t , override .Value , structVal .Value )
139
195
})
140
196
}
197
+
198
+ // setup creates a new API, enabled notifications + multi-org experiments, and returns the API client and a new org.
199
+ func setup (t * testing.T ) (* codersdk.Client , codersdk.Organization ) {
200
+ t .Helper ()
201
+
202
+ vals := coderdtest .DeploymentValues (t )
203
+ vals .Experiments = []string {string (codersdk .ExperimentMultiOrganization )}
204
+ adminClient , _ , _ , _ := coderdenttest .NewWithAPI (t , & coderdenttest.Options {
205
+ Options : & coderdtest.Options {DeploymentValues : vals },
206
+ LicenseOptions : & coderdenttest.LicenseOptions {
207
+ Features : license.Features {
208
+ codersdk .FeatureMultipleOrganizations : 1 ,
209
+ },
210
+ },
211
+ })
212
+ return adminClient , coderdenttest .CreateOrganization (t , adminClient , coderdenttest.CreateOrganizationOptions {})
213
+ }
0 commit comments