Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 1 addition & 7 deletions cmd/build_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/spf13/cobra"
"github.com/windsorcli/cli/pkg/di"
"github.com/windsorcli/cli/pkg/runtime"
)

Expand All @@ -26,13 +25,8 @@ Examples:
BUILD_ID=$(windsor build-id --new) && docker build -t myapp:$BUILD_ID .`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
injector := cmd.Context().Value(injectorKey).(di.Injector)

rt := &runtime.Runtime{
Injector: injector,
}

rt, err := runtime.NewRuntime(rt)
rt, err := runtime.NewRuntime()
if err != nil {
return fmt.Errorf("failed to initialize context: %w", err)
}
Expand Down
26 changes: 8 additions & 18 deletions cmd/build_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"testing"

"github.com/windsorcli/cli/pkg/di"
"github.com/windsorcli/cli/pkg/runtime/shell"
)

Expand All @@ -21,10 +20,9 @@ func TestBuildIDCmd(t *testing.T) {
t.Run("Success", func(t *testing.T) {
// Given proper output capture and mock setup
_, stderr := setup(t)
mocks := setupMocks(t)

// Set up command context with injector
ctx := context.WithValue(context.Background(), injectorKey, mocks.Injector)
ctx := context.Background()
rootCmd.SetContext(ctx)

rootCmd.SetArgs([]string{"build-id"})
Expand All @@ -46,10 +44,9 @@ func TestBuildIDCmd(t *testing.T) {
t.Run("SuccessWithNewFlag", func(t *testing.T) {
// Given proper output capture and mock setup
_, stderr := setup(t)
mocks := setupMocks(t)

// Set up command context with injector
ctx := context.WithValue(context.Background(), injectorKey, mocks.Injector)
ctx := context.Background()
rootCmd.SetContext(ctx)

rootCmd.SetArgs([]string{"build-id", "--new"})
Expand Down Expand Up @@ -125,10 +122,9 @@ func TestBuildIDCmd(t *testing.T) {
t.Run("PipelineSetupError", func(t *testing.T) {
// Given proper output capture and mock setup
_, stderr := setup(t)
mocks := setupMocks(t)

// Set up command context with injector
ctx := context.WithValue(context.Background(), injectorKey, mocks.Injector)
ctx := context.Background()
rootCmd.SetContext(ctx)

rootCmd.SetArgs([]string{"build-id"})
Expand All @@ -150,10 +146,9 @@ func TestBuildIDCmd(t *testing.T) {
t.Run("PipelineExecuteError", func(t *testing.T) {
// Given proper output capture and mock setup
_, stderr := setup(t)
mocks := setupMocks(t)

// Set up command context with injector
ctx := context.WithValue(context.Background(), injectorKey, mocks.Injector)
ctx := context.Background()
rootCmd.SetContext(ctx)

rootCmd.SetArgs([]string{"build-id"})
Expand Down Expand Up @@ -198,10 +193,9 @@ func TestBuildIDCmd(t *testing.T) {
t.Run("ContextWithNewFlag", func(t *testing.T) {
// Given proper output capture and mock setup
_, stderr := setup(t)
mocks := setupMocks(t)

// Set up command context with injector
ctx := context.WithValue(context.Background(), injectorKey, mocks.Injector)
ctx := context.Background()
rootCmd.SetContext(ctx)

rootCmd.SetArgs([]string{"build-id", "--new"})
Expand All @@ -223,10 +217,9 @@ func TestBuildIDCmd(t *testing.T) {
t.Run("ContextWithoutNewFlag", func(t *testing.T) {
// Given proper output capture and mock setup
_, stderr := setup(t)
mocks := setupMocks(t)

// Set up command context with injector
ctx := context.WithValue(context.Background(), injectorKey, mocks.Injector)
ctx := context.Background()
rootCmd.SetContext(ctx)

rootCmd.SetArgs([]string{"build-id"})
Expand All @@ -250,8 +243,6 @@ func TestBuildIDCmd(t *testing.T) {
setup(t)

// Set up mocks with pipeline that fails to initialize
mockInjector := di.NewInjector()

// Register a mock shell to prevent nil pointer dereference
mockShell := shell.NewMockShell()
mockShell.GetProjectRootFunc = func() (string, error) {
Expand All @@ -260,10 +251,9 @@ func TestBuildIDCmd(t *testing.T) {
mockShell.CheckTrustedDirectoryFunc = func() error {
return nil
}
mockInjector.Register("shell", mockShell)

// Set up command context with injector
ctx := context.WithValue(context.Background(), injectorKey, mockInjector)
ctx := context.Background()
rootCmd.SetContext(ctx)

rootCmd.SetArgs([]string{"build-id"})
Expand All @@ -286,7 +276,7 @@ func TestBuildIDCmd(t *testing.T) {
setup(t)

// Set up command context with invalid injector type
ctx := context.WithValue(context.Background(), injectorKey, "invalid")
ctx := context.Background()
rootCmd.SetContext(ctx)

rootCmd.SetArgs([]string{"build-id"})
Expand Down
10 changes: 4 additions & 6 deletions cmd/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/spf13/cobra"
"github.com/windsorcli/cli/pkg/composer"
"github.com/windsorcli/cli/pkg/di"
"github.com/windsorcli/cli/pkg/runtime"
)

Expand All @@ -32,13 +31,12 @@ Examples:
windsor bundle`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
injector := cmd.Context().Value(injectorKey).(di.Injector)

rt := &runtime.Runtime{
Injector: injector,
var rtOpts []*runtime.Runtime
if overridesVal := cmd.Context().Value(runtimeOverridesKey); overridesVal != nil {
rtOpts = []*runtime.Runtime{overridesVal.(*runtime.Runtime)}
}

rt, err := runtime.NewRuntime(rt)
rt, err := runtime.NewRuntime(rtOpts...)
if err != nil {
return fmt.Errorf("failed to initialize context: %w", err)
}
Expand Down
87 changes: 35 additions & 52 deletions cmd/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import (
"github.com/windsorcli/cli/pkg/composer"
"github.com/windsorcli/cli/pkg/composer/artifact"
"github.com/windsorcli/cli/pkg/composer/blueprint"
"github.com/windsorcli/cli/pkg/di"
"github.com/windsorcli/cli/pkg/provisioner/kubernetes"
"github.com/windsorcli/cli/pkg/runtime"
"github.com/windsorcli/cli/pkg/runtime/config"
"github.com/windsorcli/cli/pkg/runtime/shell"
)
Expand All @@ -38,37 +35,41 @@ func TestBundleCmdWithRuntime(t *testing.T) {
templateDir := filepath.Join(contextsDir, "_template")
os.MkdirAll(templateDir, 0755)

// Create injector with required mocks
injector := di.NewInjector()

// Mock shell
mockShell := shell.NewMockShell()
mockShell.GetProjectRootFunc = func() (string, error) {
return tmpDir, nil
}
injector.Register("shell", mockShell)

// Mock config handler
mockConfigHandler := config.NewMockConfigHandler()
mockConfigHandler.GetContextValuesFunc = func() (map[string]any, error) {
return map[string]any{}, nil
}
injector.Register("configHandler", mockConfigHandler)
mockConfigHandler.GetContextFunc = func() string {
return "test-context"
}
mockShell.CheckTrustedDirectoryFunc = func() error {
return nil
}

// Mock kubernetes manager
mockK8sManager := kubernetes.NewMockKubernetesManager(injector)
injector.Register("kubernetesManager", mockK8sManager)
// Get base mocks (includes ToolsManager)
baseMocks := setupMocks(t)

// Override Shell, ConfigHandler, and ProjectRoot in runtime
baseMocks.Runtime.Shell = mockShell
baseMocks.Runtime.ConfigHandler = mockConfigHandler
baseMocks.Runtime.ProjectRoot = tmpDir

// Mock blueprint handler
mockBlueprintHandler := blueprint.NewMockBlueprintHandler(injector)
mockBlueprintHandler := blueprint.NewMockBlueprintHandler()
mockBlueprintHandler.GetLocalTemplateDataFunc = func() (map[string][]byte, error) {
return map[string][]byte{}, nil
}
injector.Register("blueprintHandler", mockBlueprintHandler)

// Mock artifact builder
mockArtifactBuilder := artifact.NewMockArtifact()
injector.Register("artifactBuilder", mockArtifactBuilder)
// Create composer with mocked blueprint handler
comp := composer.NewComposer(baseMocks.Runtime)
comp.BlueprintHandler = mockBlueprintHandler

// Create test command
cmd := &cobra.Command{
Expand All @@ -79,8 +80,10 @@ func TestBundleCmdWithRuntime(t *testing.T) {
cmd.Flags().StringP("output", "o", ".", "Output path for bundle archive")
cmd.Flags().StringP("tag", "t", "", "Tag in 'name:version' format")

// Set up context
ctx := context.WithValue(context.Background(), injectorKey, injector)
// Set up context with runtime and composer overrides
ctx := context.Background()
ctx = context.WithValue(ctx, runtimeOverridesKey, baseMocks.Runtime)
ctx = context.WithValue(ctx, composerOverridesKey, comp)
cmd.SetContext(ctx)

// Set arguments
Expand All @@ -106,7 +109,6 @@ func TestBundleCmdWithRuntime(t *testing.T) {

// Create injector without required dependencies
// The runtime is now resilient and will create default dependencies
injector := di.NewInjector()

// Create test command
cmd := &cobra.Command{
Expand All @@ -118,7 +120,7 @@ func TestBundleCmdWithRuntime(t *testing.T) {
cmd.Flags().StringP("tag", "t", "", "Tag in 'name:version' format")

// Set up context
ctx := context.WithValue(context.Background(), injectorKey, injector)
ctx := context.Background()
cmd.SetContext(ctx)

// Set arguments
Expand Down Expand Up @@ -147,15 +149,11 @@ func TestBundleCmdWithRuntime(t *testing.T) {
templateDir := filepath.Join(contextsDir, "_template")
os.MkdirAll(templateDir, 0755)

// Create injector with mocks that will cause execution to fail
injector := di.NewInjector()

// Mock shell
mockShell := shell.NewMockShell()
mockShell.GetProjectRootFunc = func() (string, error) {
return tmpDir, nil
}
injector.Register("shell", mockShell)

// Mock config handler
mockConfigHandler := config.NewMockConfigHandler()
Expand All @@ -165,36 +163,20 @@ func TestBundleCmdWithRuntime(t *testing.T) {
mockConfigHandler.GetContextFunc = func() string {
return "test-context"
}
injector.Register("configHandler", mockConfigHandler)

// Mock kubernetes manager
mockK8sManager := kubernetes.NewMockKubernetesManager(injector)
injector.Register("kubernetesManager", mockK8sManager)
baseMocks := setupMocks(t)

// Mock blueprint handler
mockBlueprintHandler := blueprint.NewMockBlueprintHandler(injector)
mockBlueprintHandler.GetLocalTemplateDataFunc = func() (map[string][]byte, error) {
return map[string][]byte{}, nil
}
injector.Register("blueprintHandler", mockBlueprintHandler)
// Override Shell, ConfigHandler, and ProjectRoot in runtime
baseMocks.Runtime.Shell = mockShell
baseMocks.Runtime.ConfigHandler = mockConfigHandler
baseMocks.Runtime.ProjectRoot = tmpDir

// Mock artifact builder that fails during bundle
comp := composer.NewComposer(baseMocks.Runtime)
mockArtifactBuilder := artifact.NewMockArtifact()
mockArtifactBuilder.WriteFunc = func(outputPath string, tag string) (string, error) {
return "", fmt.Errorf("artifact bundle failed")
return "", fmt.Errorf("failed to write artifact")
}

// Create runtime and composer with mock artifact builder override
rt := &runtime.Runtime{
Injector: injector,
}
rt, err := runtime.NewRuntime(rt)
if err != nil {
t.Fatalf("Failed to create runtime: %v", err)
}
mockComposer := composer.NewComposer(rt, &composer.Composer{
ArtifactBuilder: mockArtifactBuilder,
})
comp.ArtifactBuilder = mockArtifactBuilder

// Create test command
cmd := &cobra.Command{
Expand All @@ -205,16 +187,17 @@ func TestBundleCmdWithRuntime(t *testing.T) {
cmd.Flags().StringP("output", "o", ".", "Output path for bundle archive")
cmd.Flags().StringP("tag", "t", "", "Tag in 'name:version' format")

// Set up context with composer overrides
ctx := context.WithValue(context.Background(), injectorKey, injector)
ctx = context.WithValue(ctx, composerOverridesKey, mockComposer)
// Set up context with runtime and composer overrides
ctx := context.Background()
ctx = context.WithValue(ctx, runtimeOverridesKey, baseMocks.Runtime)
ctx = context.WithValue(ctx, composerOverridesKey, comp)
cmd.SetContext(ctx)

// Set arguments
cmd.SetArgs([]string{"--tag", "test:v1.0.0"})

// Execute command
err = cmd.Execute()
err := cmd.Execute()

// Verify error
if err == nil {
Expand Down
18 changes: 8 additions & 10 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/spf13/cobra"
"github.com/windsorcli/cli/pkg/composer"
"github.com/windsorcli/cli/pkg/constants"
"github.com/windsorcli/cli/pkg/di"
"github.com/windsorcli/cli/pkg/provisioner"
"github.com/windsorcli/cli/pkg/runtime"
)
Expand All @@ -26,13 +25,12 @@ var checkCmd = &cobra.Command{
Long: "Check the tool versions required by the project",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
injector := cmd.Context().Value(injectorKey).(di.Injector)

rt := &runtime.Runtime{
Injector: injector,
var rtOpts []*runtime.Runtime
if overridesVal := cmd.Context().Value(runtimeOverridesKey); overridesVal != nil {
rtOpts = []*runtime.Runtime{overridesVal.(*runtime.Runtime)}
}

rt, err := runtime.NewRuntime(rt)
rt, err := runtime.NewRuntime(rtOpts...)
if err != nil {
return fmt.Errorf("failed to initialize context: %w", err)
}
Expand Down Expand Up @@ -63,7 +61,6 @@ var checkNodeHealthCmd = &cobra.Command{
Long: "Check the health status of specified cluster nodes",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
injector := cmd.Context().Value(injectorKey).(di.Injector)

if len(nodeHealthNodes) == 0 && k8sEndpoint == "" {
return fmt.Errorf("No health checks specified. Use --nodes and/or --k8s-endpoint flags to specify health checks to perform")
Expand All @@ -73,11 +70,12 @@ var checkNodeHealthCmd = &cobra.Command{
nodeHealthTimeout = constants.DefaultNodeHealthCheckTimeout
}

rt := &runtime.Runtime{
Injector: injector,
var rtOpts []*runtime.Runtime
if overridesVal := cmd.Context().Value(runtimeOverridesKey); overridesVal != nil {
rtOpts = []*runtime.Runtime{overridesVal.(*runtime.Runtime)}
}

rt, err := runtime.NewRuntime(rt)
rt, err := runtime.NewRuntime(rtOpts...)
if err != nil {
return fmt.Errorf("failed to initialize context: %w", err)
}
Expand Down
Loading
Loading