Skip to content

feat: implement runtime configuration package with multi-org support #14624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
increase test coverage:
  • Loading branch information
Emyrk committed Sep 6, 2024
commit 7d082553724c218b7ef3bb02b83684d2ede0ceea
66 changes: 66 additions & 0 deletions coderd/runtimeconfig/deploymententry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (

"github.com/google/uuid"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"golang.org/x/xerrors"

"github.com/coder/coder/v2/coderd/database/dbmem"
"github.com/coder/coder/v2/coderd/database/dbmock"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/runtimeconfig"
"github.com/coder/coder/v2/coderd/util/ptr"
Expand Down Expand Up @@ -45,6 +48,50 @@ func ExampleDeploymentValues() {
// Output: hello world
}

// TestResolveDBError ensures a db error that is not a sql.ErrNoRows
// will bubble up using Coalesce. The error should not be ignored and replaced
// with the startup value.
func TestResolveDBError(t *testing.T) {
t.Parallel()

dbErr := xerrors.Errorf("some db error")
ctrl := gomock.NewController(t)
mDB := dbmock.NewMockStore(ctrl)
// Error on fetch
mDB.EXPECT().
GetRuntimeConfig(gomock.Any(), gomock.Any()).
Times(1).
Return("", dbErr)

// Error on upsert
mDB.EXPECT().
UpsertRuntimeConfig(gomock.Any(), gomock.Any()).
Times(1).
Return(dbErr)

// Error on delete
mDB.EXPECT().
DeleteRuntimeConfig(gomock.Any(), gomock.Any()).
Times(1).
Return(dbErr)

st := runtimeconfig.NewStoreManager()
var stringField runtimeconfig.DeploymentEntry[*serpent.String]
stringField.Initialize("string-field")
stringField.SetStartupValue("default")

ctx := testutil.Context(t, testutil.WaitMedium)
// Resolve
_, err := stringField.Coalesce(ctx, st.Resolver(mDB))
require.ErrorIs(t, err, dbErr)
// Set
err = stringField.SetRuntimeValue(ctx, st.Resolver(mDB), serpent.StringOf(ptr.Ref("hello world")))
require.ErrorIs(t, err, dbErr)
// Unset
err = stringField.UnsetRuntimeValue(ctx, st.Resolver(mDB))
require.ErrorIs(t, err, dbErr)
}

// TestSerpentDeploymentEntry uses the package as the serpent options will use it.
// Some of the usage might feel awkward, since the serpent package values come from
// the serpent parsing (strings), not manual assignment.
Expand All @@ -69,6 +116,11 @@ func TestSerpentDeploymentEntry(t *testing.T) {
entries.Bool.Initialize("bool-field")
entries.Struct.Initialize("struct-field")

// Check the Type() methods are unchanged
require.Equal(t, entries.String.Type(), (serpent.String("")).Type())
require.Equal(t, entries.Bool.Type(), (serpent.Bool(false)).Type())
require.Equal(t, entries.Struct.Type(), (&serpent.Struct[codersdk.Feature]{}).Type())

// When using Coalesce, the default value is the empty value
stringVal, err := entries.String.Coalesce(ctx, st.Resolver(db))
require.NoError(t, err)
Expand Down Expand Up @@ -115,6 +167,13 @@ func TestSerpentDeploymentEntry(t *testing.T) {
require.NoError(t, err)
require.Equal(t, structVal.Value.Entitlement, codersdk.EntitlementGracePeriod)

// Test unset
err = entries.String.UnsetRuntimeValue(ctx, st.Resolver(db))
require.NoError(t, err)
stringVal, err = entries.String.Coalesce(ctx, st.Resolver(db))
require.NoError(t, err)
require.Equal(t, "default", stringVal.String())

// Test using org scoped resolver
orgID := uuid.New()
orgResolver := st.OrganizationResolver(db, orgID)
Expand All @@ -129,4 +188,11 @@ func TestSerpentDeploymentEntry(t *testing.T) {
stringVal, err = entries.String.Coalesce(ctx, orgResolver)
require.NoError(t, err)
require.Equal(t, "hello organizations", stringVal.String())
// Unset org runtime
err = entries.String.UnsetRuntimeValue(ctx, orgResolver)
require.NoError(t, err)
// Verify org runtime is back to default
stringVal, err = entries.String.Coalesce(ctx, orgResolver)
require.NoError(t, err)
require.Equal(t, "default", stringVal.String())
}
9 changes: 7 additions & 2 deletions coderd/runtimeconfig/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ func TestEntry(t *testing.T) {
// Setting a value will not produce an error.
require.NoError(t, field.SetStartupValue("true"))

// But attempting to resolve will produce an error.
// Attempting to resolve will produce an error.
_, err := field.Resolve(context.Background(), rlv)
require.ErrorIs(t, err, runtimeconfig.ErrNameNotSet)
// But attempting to set the runtime value will produce an error.

// Attempting to unset
err = field.UnsetRuntimeValue(context.Background(), rlv)
require.ErrorIs(t, err, runtimeconfig.ErrNameNotSet)

// Attempting to set
val := serpent.BoolOf(ptr.Ref(true))
require.ErrorIs(t, field.SetRuntimeValue(context.Background(), rlv, val), runtimeconfig.ErrNameNotSet)
})
Expand Down