Files
threshold/internal/cmd/commands/database/flags_test.go
T
Ryan Derr 25bb217a7a Database Init Flags Refactor (#5668)
* ICU-16920 Refactor Boundary Database Init Flags for Improved Dependency Handling

This commit refactors the database initialization flags to prevent early exits and ensure explicit handling of flag dependencies. If one flag relies on resources skipped by another, the dependent flag will also be skipped.

Key Changes:
- Introduced a `InitFlags` struct to organize existing flags and establish a clear pattern for adding future flags.
- Added receiver functions on the `InitFlags` struct to determine whether a flag should be skipped based on provided options or dependencies.
- Refactored the `Run()` method to avoid early returns when a flag skips resource creation. Instead, a log message is provided to indicate the skipped option, and the process continues with resource creation.

* Resolve Comments
2025-04-27 13:16:02 -05:00

226 lines
5.6 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package database
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestInitFlags_SkipInitialLoginRoleCreation(t *testing.T) {
t.Parallel()
tests := []struct {
name string
flags *initFlags
expectedSkip bool
expectedReason string
}{
{
name: "SkipInitialLoginRoleCreation is true",
flags: &initFlags{flagSkipInitialLoginRoleCreation: true},
expectedSkip: true,
expectedReason: "flag `-skip-initial-login-role-creation` was set",
},
{
name: "SkipInitialLoginRoleCreation is false",
flags: &initFlags{flagSkipInitialLoginRoleCreation: false},
expectedSkip: false,
expectedReason: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
skip, reason := tt.flags.SkipInitialLoginRoleCreation()
assert.Equal(t, tt.expectedSkip, skip)
assert.Equal(t, tt.expectedReason, reason)
})
}
}
func TestInitFlags_SkipInitialAuthenticatedUserRoleCreation(t *testing.T) {
t.Parallel()
tests := []struct {
name string
flags *initFlags
expectedSkip bool
expectedReason string
}{
{
name: "SkipInitialAuthenticatedUserRoleCreation is true",
flags: &initFlags{flagSkipInitialAuthenticatedUserRoleCreation: true},
expectedSkip: true,
expectedReason: "flag `-skip-initial-authenticated-user-role-creation` was set",
},
{
name: "SkipInitialAuthenticatedUserRoleCreation is false",
flags: &initFlags{flagSkipInitialAuthenticatedUserRoleCreation: false},
expectedSkip: false,
expectedReason: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
skip, reason := tt.flags.SkipInitialAuthenticatedUserRoleCreation()
assert.Equal(t, tt.expectedSkip, skip)
assert.Equal(t, tt.expectedReason, reason)
})
}
}
func TestInitFlags_SkipAuthMethodCreation(t *testing.T) {
t.Parallel()
tests := []struct {
name string
flags *initFlags
expectedSkip bool
expectedReason string
}{
{
name: "SkipAuthMethodCreation is true",
flags: &initFlags{flagSkipAuthMethodCreation: true},
expectedSkip: true,
expectedReason: "flag `-skip-auth-method-creation` was set",
},
{
name: "SkipAuthMethodCreation is false",
flags: &initFlags{flagSkipAuthMethodCreation: false},
expectedSkip: false,
expectedReason: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
skip, reason := tt.flags.SkipAuthMethodCreation()
assert.Equal(t, tt.expectedSkip, skip)
assert.Equal(t, tt.expectedReason, reason)
})
}
}
func TestInitFlags_SkipScopesCreation(t *testing.T) {
t.Parallel()
tests := []struct {
name string
flags *initFlags
expectedSkip bool
expectedReason string
}{
{
name: "SkipScopesCreation is true",
flags: &initFlags{flagSkipScopesCreation: true},
expectedSkip: true,
expectedReason: "flag `-skip-scopes-creation` was set",
},
{
name: "SkipScopesCreation is false",
flags: &initFlags{flagSkipScopesCreation: false},
expectedSkip: false,
expectedReason: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
skip, reason := tt.flags.SkipScopesCreation()
assert.Equal(t, tt.expectedSkip, skip)
assert.Equal(t, tt.expectedReason, reason)
})
}
}
func TestInitFlags_SkipHostResourcesCreation(t *testing.T) {
t.Parallel()
tests := []struct {
name string
flags *initFlags
expectedSkip bool
expectedReason string
}{
{
name: "SkipHostResourcesCreation is true",
flags: &initFlags{flagSkipHostResourcesCreation: true},
expectedSkip: true,
expectedReason: "flag `-skip-host-resources-creation` was set",
},
{
name: "SkipScopesCreation is true",
flags: &initFlags{flagSkipScopesCreation: true},
expectedSkip: true,
expectedReason: "flag `-skip-scopes-creation` was set",
},
{
name: "No flags set",
flags: &initFlags{},
expectedSkip: false,
expectedReason: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
skip, reason := tt.flags.SkipHostResourcesCreation()
assert.Equal(t, tt.expectedSkip, skip)
assert.Equal(t, tt.expectedReason, reason)
})
}
}
func TestInitFlags_SkipTargetCreation(t *testing.T) {
t.Parallel()
tests := []struct {
name string
flags *initFlags
expectedSkip bool
expectedReason string
}{
{
name: "SkipTargetCreation is true",
flags: &initFlags{flagSkipTargetCreation: true},
expectedSkip: true,
expectedReason: "flag `-skip-target-creation` was set",
},
{
name: "SkipHostResourcesCreation is true",
flags: &initFlags{flagSkipHostResourcesCreation: true},
expectedSkip: true,
expectedReason: "flag `-skip-host-resources-creation` was set",
},
{
name: "SkipScopesCreation is true",
flags: &initFlags{flagSkipScopesCreation: true},
expectedSkip: true,
expectedReason: "flag `-skip-scopes-creation` was set",
},
{
name: "No flags set",
flags: &initFlags{},
expectedSkip: false,
expectedReason: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
skip, reason := tt.flags.SkipTargetCreation()
assert.Equal(t, tt.expectedSkip, skip)
assert.Equal(t, tt.expectedReason, reason)
})
}
}