Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7d08255

Browse files
committed
increase test coverage:
1 parent13ea99a commit7d08255

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

‎coderd/runtimeconfig/deploymententry_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ import (
77

88
"github.com/google/uuid"
99
"github.com/stretchr/testify/require"
10+
"go.uber.org/mock/gomock"
11+
"golang.org/x/xerrors"
1012

1113
"github.com/coder/coder/v2/coderd/database/dbmem"
14+
"github.com/coder/coder/v2/coderd/database/dbmock"
1215
"github.com/coder/coder/v2/coderd/database/dbtestutil"
1316
"github.com/coder/coder/v2/coderd/runtimeconfig"
1417
"github.com/coder/coder/v2/coderd/util/ptr"
@@ -45,6 +48,50 @@ func ExampleDeploymentValues() {
4548
// Output: hello world
4649
}
4750

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+
funcTestResolveDBError(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+
varstringField 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+
4895
// TestSerpentDeploymentEntry uses the package as the serpent options will use it.
4996
// Some of the usage might feel awkward, since the serpent package values come from
5097
// the serpent parsing (strings), not manual assignment.
@@ -69,6 +116,11 @@ func TestSerpentDeploymentEntry(t *testing.T) {
69116
entries.Bool.Initialize("bool-field")
70117
entries.Struct.Initialize("struct-field")
71118

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+
72124
// When using Coalesce, the default value is the empty value
73125
stringVal,err:=entries.String.Coalesce(ctx,st.Resolver(db))
74126
require.NoError(t,err)
@@ -115,6 +167,13 @@ func TestSerpentDeploymentEntry(t *testing.T) {
115167
require.NoError(t,err)
116168
require.Equal(t,structVal.Value.Entitlement,codersdk.EntitlementGracePeriod)
117169

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+
118177
// Test using org scoped resolver
119178
orgID:=uuid.New()
120179
orgResolver:=st.OrganizationResolver(db,orgID)
@@ -129,4 +188,11 @@ func TestSerpentDeploymentEntry(t *testing.T) {
129188
stringVal,err=entries.String.Coalesce(ctx,orgResolver)
130189
require.NoError(t,err)
131190
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())
132198
}

‎coderd/runtimeconfig/entry_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,15 @@ func TestEntry(t *testing.T) {
5151
// Setting a value will not produce an error.
5252
require.NoError(t,field.SetStartupValue("true"))
5353

54-
//But attempting to resolve will produce an error.
54+
//Attempting to resolve will produce an error.
5555
_,err:=field.Resolve(context.Background(),rlv)
5656
require.ErrorIs(t,err,runtimeconfig.ErrNameNotSet)
57-
// But attempting to set the runtime value will produce an error.
57+
58+
// Attempting to unset
59+
err=field.UnsetRuntimeValue(context.Background(),rlv)
60+
require.ErrorIs(t,err,runtimeconfig.ErrNameNotSet)
61+
62+
// Attempting to set
5863
val:=serpent.BoolOf(ptr.Ref(true))
5964
require.ErrorIs(t,field.SetRuntimeValue(context.Background(),rlv,val),runtimeconfig.ErrNameNotSet)
6065
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp