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
10 changes: 10 additions & 0 deletions pkg/pipelines/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/windsorcli/cli/pkg/env"
"github.com/windsorcli/cli/pkg/kubernetes"
"github.com/windsorcli/cli/pkg/network"
"github.com/windsorcli/cli/pkg/shell"
"github.com/windsorcli/cli/pkg/stack"
"github.com/windsorcli/cli/pkg/virt"
)
Expand Down Expand Up @@ -91,6 +92,15 @@ func (p *DownPipeline) Initialize(injector di.Injector, ctx context.Context) err
return fmt.Errorf("failed to initialize container runtime: %w", err)
}
}

if secureShell := p.injector.Resolve("secureShell"); secureShell != nil {
if secureShellInterface, ok := secureShell.(shell.Shell); ok {
if err := secureShellInterface.Initialize(); err != nil {
return fmt.Errorf("failed to initialize secure shell: %w", err)
}
}
}

if p.networkManager != nil {
if err := p.networkManager.Initialize(); err != nil {
return fmt.Errorf("failed to initialize network manager: %w", err)
Expand Down
84 changes: 84 additions & 0 deletions pkg/pipelines/down_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/windsorcli/cli/pkg/env"
"github.com/windsorcli/cli/pkg/kubernetes"
"github.com/windsorcli/cli/pkg/network"
"github.com/windsorcli/cli/pkg/shell"
"github.com/windsorcli/cli/pkg/stack"
"github.com/windsorcli/cli/pkg/virt"
)
Expand Down Expand Up @@ -173,6 +174,89 @@ func TestDownPipeline_Initialize(t *testing.T) {
}
})

t.Run("InitializesSecureShellWhenRegistered", func(t *testing.T) {
// Given a down pipeline with secure shell registered
pipeline := NewDownPipeline()
mocks := setupDownMocks(t)

// Create mock secure shell
mockSecureShell := shell.NewMockShell()
secureShellInitialized := false
mockSecureShell.InitializeFunc = func() error {
secureShellInitialized = true
return nil
}
mocks.Injector.Register("secureShell", mockSecureShell)

// When initializing the pipeline
err := pipeline.Initialize(mocks.Injector, context.Background())

// Then no error should be returned
if err != nil {
t.Errorf("Expected no error, got %v", err)
}

// And secure shell should be initialized
if !secureShellInitialized {
t.Error("Expected secure shell to be initialized")
}
})

t.Run("ReturnsErrorWhenSecureShellInitializeFails", func(t *testing.T) {
// Given a down pipeline with failing secure shell
pipeline := NewDownPipeline()
mocks := setupDownMocks(t)

// Create mock secure shell that fails to initialize
mockSecureShell := shell.NewMockShell()
mockSecureShell.InitializeFunc = func() error {
return fmt.Errorf("secure shell failed")
}
mocks.Injector.Register("secureShell", mockSecureShell)

// When initializing the pipeline
err := pipeline.Initialize(mocks.Injector, context.Background())

// Then an error should be returned
if err == nil {
t.Fatal("Expected error, got nil")
}
if err.Error() != "failed to initialize secure shell: secure shell failed" {
t.Errorf("Expected secure shell error, got %q", err.Error())
}
})

t.Run("SkipsSecureShellWhenNotRegistered", func(t *testing.T) {
// Given a down pipeline without secure shell registered
pipeline := NewDownPipeline()
mocks := setupDownMocks(t)

// When initializing the pipeline
err := pipeline.Initialize(mocks.Injector, context.Background())

// Then no error should be returned
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
})

t.Run("SkipsSecureShellWhenRegisteredTypeIsIncorrect", func(t *testing.T) {
// Given a down pipeline with incorrectly typed secure shell
pipeline := NewDownPipeline()
mocks := setupDownMocks(t)

// Register something that's not a shell.Shell
mocks.Injector.Register("secureShell", "not-a-shell")

// When initializing the pipeline
err := pipeline.Initialize(mocks.Injector, context.Background())

// Then no error should be returned
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
})

t.Run("ErrorInitializingBasePipeline", func(t *testing.T) {
// Given a down pipeline with failing base pipeline
pipeline := NewDownPipeline()
Expand Down
9 changes: 9 additions & 0 deletions pkg/pipelines/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/windsorcli/cli/pkg/generators"
"github.com/windsorcli/cli/pkg/network"
"github.com/windsorcli/cli/pkg/services"
"github.com/windsorcli/cli/pkg/shell"
"github.com/windsorcli/cli/pkg/stack"
"github.com/windsorcli/cli/pkg/template"
"github.com/windsorcli/cli/pkg/terraform"
Expand Down Expand Up @@ -213,6 +214,14 @@ func (p *InitPipeline) Initialize(injector di.Injector, ctx context.Context) err
}
}

if secureShell := p.injector.Resolve("secureShell"); secureShell != nil {
if secureShellInterface, ok := secureShell.(shell.Shell); ok {
if err := secureShellInterface.Initialize(); err != nil {
return fmt.Errorf("failed to initialize secure shell: %w", err)
}
}
}

if p.networkManager != nil {
if err := p.networkManager.Initialize(); err != nil {
return fmt.Errorf("failed to initialize network manager: %w", err)
Expand Down
79 changes: 79 additions & 0 deletions pkg/pipelines/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,85 @@ func TestInitPipeline_Initialize(t *testing.T) {
}
})
}

t.Run("InitializesSecureShellWhenRegistered", func(t *testing.T) {
// Given an init pipeline with secure shell registered
pipeline, mocks := setup(t)

// Create mock secure shell
mockSecureShell := shell.NewMockShell()
secureShellInitialized := false
mockSecureShell.InitializeFunc = func() error {
secureShellInitialized = true
return nil
}
mocks.Injector.Register("secureShell", mockSecureShell)

// When initializing the pipeline
err := pipeline.Initialize(mocks.Injector, context.Background())

// Then no error should be returned
if err != nil {
t.Errorf("Expected no error, got %v", err)
}

// And secure shell should be initialized
if !secureShellInitialized {
t.Error("Expected secure shell to be initialized")
}
})

t.Run("ReturnsErrorWhenSecureShellInitializeFails", func(t *testing.T) {
// Given an init pipeline with failing secure shell
pipeline, mocks := setup(t)

// Create mock secure shell that fails to initialize
mockSecureShell := shell.NewMockShell()
mockSecureShell.InitializeFunc = func() error {
return fmt.Errorf("secure shell failed")
}
mocks.Injector.Register("secureShell", mockSecureShell)

// When initializing the pipeline
err := pipeline.Initialize(mocks.Injector, context.Background())

// Then an error should be returned
if err == nil {
t.Fatal("Expected error, got nil")
}
if err.Error() != "failed to initialize secure shell: secure shell failed" {
t.Errorf("Expected secure shell error, got %q", err.Error())
}
})

t.Run("SkipsSecureShellWhenNotRegistered", func(t *testing.T) {
// Given an init pipeline without secure shell registered
pipeline, mocks := setup(t)

// When initializing the pipeline
err := pipeline.Initialize(mocks.Injector, context.Background())

// Then no error should be returned
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
})

t.Run("SkipsSecureShellWhenRegisteredTypeIsIncorrect", func(t *testing.T) {
// Given an init pipeline with incorrectly typed secure shell
pipeline, mocks := setup(t)

// Register something that's not a shell.Shell
mocks.Injector.Register("secureShell", "not-a-shell")

// When initializing the pipeline
err := pipeline.Initialize(mocks.Injector, context.Background())

// Then no error should be returned
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
})
}

func TestInitPipeline_Execute(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions pkg/pipelines/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/windsorcli/cli/pkg/di"
"github.com/windsorcli/cli/pkg/env"
"github.com/windsorcli/cli/pkg/network"
"github.com/windsorcli/cli/pkg/shell"
"github.com/windsorcli/cli/pkg/stack"
"github.com/windsorcli/cli/pkg/tools"
"github.com/windsorcli/cli/pkg/virt"
Expand Down Expand Up @@ -94,6 +95,15 @@ func (p *UpPipeline) Initialize(injector di.Injector, ctx context.Context) error
return fmt.Errorf("failed to initialize container runtime: %w", err)
}
}

if secureShell := p.injector.Resolve("secureShell"); secureShell != nil {
if secureShellInterface, ok := secureShell.(shell.Shell); ok {
if err := secureShellInterface.Initialize(); err != nil {
return fmt.Errorf("failed to initialize secure shell: %w", err)
}
}
}

if p.networkManager != nil {
if err := p.networkManager.Initialize(); err != nil {
return fmt.Errorf("failed to initialize network manager: %w", err)
Expand Down
79 changes: 79 additions & 0 deletions pkg/pipelines/up_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,85 @@ func TestUpPipeline_Initialize(t *testing.T) {
}
})
}

t.Run("InitializesSecureShellWhenRegistered", func(t *testing.T) {
// Given an up pipeline with secure shell registered
pipeline, mocks := setup(t)

// Create mock secure shell
mockSecureShell := shell.NewMockShell()
secureShellInitialized := false
mockSecureShell.InitializeFunc = func() error {
secureShellInitialized = true
return nil
}
mocks.Injector.Register("secureShell", mockSecureShell)

// When initializing the pipeline
err := pipeline.Initialize(mocks.Injector, context.Background())

// Then no error should be returned
if err != nil {
t.Errorf("Expected no error, got %v", err)
}

// And secure shell should be initialized
if !secureShellInitialized {
t.Error("Expected secure shell to be initialized")
}
})

t.Run("ReturnsErrorWhenSecureShellInitializeFails", func(t *testing.T) {
// Given an up pipeline with failing secure shell
pipeline, mocks := setup(t)

// Create mock secure shell that fails to initialize
mockSecureShell := shell.NewMockShell()
mockSecureShell.InitializeFunc = func() error {
return fmt.Errorf("secure shell failed")
}
mocks.Injector.Register("secureShell", mockSecureShell)

// When initializing the pipeline
err := pipeline.Initialize(mocks.Injector, context.Background())

// Then an error should be returned
if err == nil {
t.Fatal("Expected error, got nil")
}
if err.Error() != "failed to initialize secure shell: secure shell failed" {
t.Errorf("Expected secure shell error, got %q", err.Error())
}
})

t.Run("SkipsSecureShellWhenNotRegistered", func(t *testing.T) {
// Given an up pipeline without secure shell registered
pipeline, mocks := setup(t)

// When initializing the pipeline
err := pipeline.Initialize(mocks.Injector, context.Background())

// Then no error should be returned
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
})

t.Run("SkipsSecureShellWhenRegisteredTypeIsIncorrect", func(t *testing.T) {
// Given an up pipeline with incorrectly typed secure shell
pipeline, mocks := setup(t)

// Register something that's not a shell.Shell
mocks.Injector.Register("secureShell", "not-a-shell")

// When initializing the pipeline
err := pipeline.Initialize(mocks.Injector, context.Background())

// Then no error should be returned
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
})
}

// =============================================================================
Expand Down
Loading