-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Update service dependencies to use interfaces
- Loading branch information
Showing
16 changed files
with
674 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
package stop | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"testing" | ||
|
||
"github.com/ethpandaops/contributoor-installer/cmd/cli/options" | ||
"github.com/ethpandaops/contributoor-installer/internal/service" | ||
"github.com/ethpandaops/contributoor-installer/internal/service/mock" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/urfave/cli" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
func TestStopContributoor(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
tests := []struct { | ||
name string | ||
runMethod string | ||
setupMocks func(*mock.MockConfigManager, *mock.MockDockerService, *mock.MockBinaryService) | ||
expectedError string | ||
}{ | ||
{ | ||
name: "docker - stops running service successfully", | ||
runMethod: service.RunMethodDocker, | ||
setupMocks: func(cfg *mock.MockConfigManager, d *mock.MockDockerService, b *mock.MockBinaryService) { | ||
cfg.EXPECT().Get().Return(&service.ContributoorConfig{ | ||
RunMethod: service.RunMethodDocker, | ||
Version: "latest", | ||
}).Times(1) | ||
d.EXPECT().IsRunning().Return(true, nil) | ||
d.EXPECT().Stop().Return(nil) | ||
}, | ||
}, | ||
{ | ||
name: "docker - service not running", | ||
runMethod: service.RunMethodDocker, | ||
setupMocks: func(cfg *mock.MockConfigManager, d *mock.MockDockerService, b *mock.MockBinaryService) { | ||
cfg.EXPECT().Get().Return(&service.ContributoorConfig{ | ||
RunMethod: service.RunMethodDocker, | ||
}).Times(1) | ||
d.EXPECT().IsRunning().Return(false, nil) | ||
}, | ||
expectedError: "Contributoor is not running", | ||
}, | ||
{ | ||
name: "docker - stop fails", | ||
runMethod: service.RunMethodDocker, | ||
setupMocks: func(cfg *mock.MockConfigManager, d *mock.MockDockerService, b *mock.MockBinaryService) { | ||
cfg.EXPECT().Get().Return(&service.ContributoorConfig{ | ||
RunMethod: service.RunMethodDocker, | ||
}).Times(1) | ||
d.EXPECT().IsRunning().Return(true, nil) | ||
d.EXPECT().Stop().Return(errors.New("stop failed")) | ||
}, | ||
expectedError: "stop failed", | ||
}, | ||
{ | ||
name: "binary - stops running service successfully", | ||
runMethod: service.RunMethodBinary, | ||
setupMocks: func(cfg *mock.MockConfigManager, d *mock.MockDockerService, b *mock.MockBinaryService) { | ||
cfg.EXPECT().Get().Return(&service.ContributoorConfig{ | ||
RunMethod: service.RunMethodBinary, | ||
}).Times(1) | ||
b.EXPECT().IsRunning().Return(true, nil) | ||
b.EXPECT().Stop().Return(nil) | ||
}, | ||
}, | ||
{ | ||
name: "binary - service not running", | ||
runMethod: service.RunMethodBinary, | ||
setupMocks: func(cfg *mock.MockConfigManager, d *mock.MockDockerService, b *mock.MockBinaryService) { | ||
cfg.EXPECT().Get().Return(&service.ContributoorConfig{ | ||
RunMethod: service.RunMethodBinary, | ||
}).Times(1) | ||
b.EXPECT().IsRunning().Return(false, nil) | ||
}, | ||
expectedError: "Contributoor is not running", | ||
}, | ||
{ | ||
name: "invalid run method", | ||
runMethod: "invalid", | ||
setupMocks: func(cfg *mock.MockConfigManager, d *mock.MockDockerService, b *mock.MockBinaryService) { | ||
cfg.EXPECT().Get().Return(&service.ContributoorConfig{ | ||
RunMethod: "invalid", | ||
}).Times(1) | ||
}, | ||
expectedError: "invalid run method", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
mockConfig := mock.NewMockConfigManager(ctrl) | ||
mockDocker := mock.NewMockDockerService(ctrl) | ||
mockBinary := mock.NewMockBinaryService(ctrl) | ||
|
||
tt.setupMocks(mockConfig, mockDocker, mockBinary) | ||
|
||
app := cli.NewApp() | ||
ctx := cli.NewContext(app, nil, nil) | ||
|
||
err := stopContributoor(ctx, logrus.New(), mockConfig, mockDocker, mockBinary) | ||
|
||
if tt.expectedError != "" { | ||
assert.ErrorContains(t, err, tt.expectedError) | ||
|
||
return | ||
} | ||
|
||
assert.NoError(t, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestRegisterCommands(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
tests := []struct { | ||
name string | ||
configPath string | ||
expectedError string | ||
}{ | ||
{ | ||
name: "successfully registers command", | ||
configPath: "testdata/valid", // "testdata" is an ancillary dir provided by go-test. | ||
}, | ||
{ | ||
name: "fails when config service fails", | ||
configPath: "/invalid/path/that/doesnt/exist", | ||
expectedError: "error loading config", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// Create CLI app, with the config flag. | ||
app := cli.NewApp() | ||
app.Flags = []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "config-path", | ||
}, | ||
} | ||
|
||
// Ensure we set the config path flag. | ||
globalSet := flag.NewFlagSet("test", flag.ContinueOnError) | ||
globalSet.String("config-path", "", "") | ||
err := globalSet.Set("config-path", tt.configPath) | ||
require.NoError(t, err) | ||
|
||
// Create the cmd context. | ||
globalCtx := cli.NewContext(app, globalSet, nil) | ||
app.Metadata = map[string]interface{}{ | ||
"flagContext": globalCtx, | ||
} | ||
|
||
// Now test! | ||
err = RegisterCommands( | ||
app, | ||
options.NewCommandOpts( | ||
options.WithName("stop"), | ||
options.WithLogger(logrus.New()), | ||
options.WithAliases([]string{"s"}), | ||
), | ||
) | ||
|
||
if tt.expectedError != "" { | ||
// Ensure the command registration succeeded. | ||
assert.NoError(t, err) | ||
|
||
// Assert that the action execution fails as expected. | ||
cmd := app.Commands[0] | ||
ctx := cli.NewContext(app, nil, globalCtx) | ||
|
||
// Assert that the action is the func we expect, mainly because the linter is having a fit otherwise. | ||
action, ok := cmd.Action.(func(*cli.Context) error) | ||
require.True(t, ok, "expected action to be func(*cli.Context) error") | ||
|
||
// Execute the action and assert the error. | ||
actionErr := action(ctx) | ||
assert.Error(t, actionErr) | ||
assert.ErrorContains(t, actionErr, tt.expectedError) | ||
} else { | ||
// Ensure the command registration succeeded. | ||
assert.NoError(t, err) | ||
assert.Len(t, app.Commands, 1) | ||
|
||
// Ensure the command is registered as expected by dumping the command. | ||
cmd := app.Commands[0] | ||
assert.Equal(t, "stop", cmd.Name) | ||
assert.Equal(t, []string{"s"}, cmd.Aliases) | ||
assert.Equal(t, "Stop Contributoor", cmd.Usage) | ||
assert.Equal(t, "contributoor stop [options]", cmd.UsageText) | ||
assert.NotNil(t, cmd.Action) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.