Skip to content

Commit 5660760

Browse files
Add unit tests with dependency injection
- Refactor CLI commands to support dependency injection via functional options pattern - Add DrasiClient interface to enable mocking of Management API - Create testutil package with mock implementations for DrasiClient, PlatformClient, and TaskOutput - Add comprehensive unit tests covering success/failure paths for all resource management commands - Fix error handling bugs: commands now properly return errors instead of printing - Fix output routing: use cmd.OutOrStdout() for testability - Individual command coverage: list 78.3%, describe 78.6%, apply 84.4%, delete 84.4% Signed-off-by: Aman Singh <[email protected]>
1 parent e71624d commit 5660760

18 files changed

+1551
-47
lines changed

.github/workflows/build-test.yml

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ jobs:
265265
- name: Set up Go
266266
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
267267
with:
268-
go-version: '1.21'
268+
go-version: '1.22'
269269

270270
- name: Cache Go modules
271271
uses: actions/cache@v4
@@ -287,6 +287,39 @@ jobs:
287287
name: cli
288288
path: cli/bin
289289

290+
test-cli:
291+
runs-on: ubuntu-latest
292+
steps:
293+
- name: Checkout repository
294+
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
295+
296+
- name: Set up Go
297+
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
298+
with:
299+
go-version: '1.22'
300+
301+
- name: Cache Go modules
302+
uses: actions/cache@v4
303+
with:
304+
path: |
305+
~/go/pkg/mod
306+
~/.cache/go-build
307+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
308+
restore-keys: |
309+
${{ runner.os }}-go-
310+
311+
- name: Run unit tests
312+
working-directory: cli
313+
run: |
314+
echo "Running unit tests..."
315+
go test -v ./...
316+
317+
- name: Run tests with race detector
318+
working-directory: cli
319+
run: |
320+
echo "Running tests with race detector..."
321+
go test -race ./...
322+
290323
test-source-sdks:
291324
runs-on: ubuntu-latest
292325

cli/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.idea
2-
bin
2+
bin
3+
*.out
4+
*.test

cli/cmd/apply.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,29 @@ import (
2525
"github.com/spf13/cobra"
2626
)
2727

28-
func NewApplyCommand() *cobra.Command {
28+
// applyCmdOptions holds dependencies for the apply command
29+
type applyCmdOptions struct {
30+
platformClientFactory func(namespace string) (sdk.PlatformClient, error)
31+
outputFactory func() output.TaskOutput
32+
}
33+
34+
// NewApplyCommand creates the apply command with optional dependency injection
35+
func NewApplyCommand(opts ...*applyCmdOptions) *cobra.Command {
36+
// Use the real implementation by default
37+
opt := &applyCmdOptions{
38+
platformClientFactory: func(namespace string) (sdk.PlatformClient, error) {
39+
reg, err := registry.LoadCurrentRegistrationWithNamespace(namespace)
40+
if err != nil {
41+
return nil, err
42+
}
43+
return sdk.NewPlatformClient(reg)
44+
},
45+
outputFactory: output.NewTaskOutput,
46+
}
47+
// If options are provided (from tests), use them instead
48+
if len(opts) > 0 && opts[0] != nil {
49+
opt = opts[0]
50+
}
2951
var applyCommand = &cobra.Command{
3052
Use: "apply",
3153
Short: "Create or update resources",
@@ -55,12 +77,7 @@ Usage examples:
5577
return err
5678
}
5779

58-
reg, err := registry.LoadCurrentRegistrationWithNamespace(namespace)
59-
if err != nil {
60-
return err
61-
}
62-
63-
platformClient, err := sdk.NewPlatformClient(reg)
80+
platformClient, err := opt.platformClientFactory(namespace)
6481
if err != nil {
6582
return err
6683
}
@@ -71,7 +88,7 @@ Usage examples:
7188
}
7289
defer client.Close()
7390

74-
output := output.NewTaskOutput()
91+
output := opt.outputFactory()
7592
defer output.Close()
7693

7794
err = client.Apply(manifests, output)

0 commit comments

Comments
 (0)