Skip to content

Commit

Permalink
Add tests for commands (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
teran authored Jun 14, 2024
1 parent d59a194 commit bcd959b
Show file tree
Hide file tree
Showing 14 changed files with 341 additions and 27 deletions.
22 changes: 15 additions & 7 deletions cmd/cephctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
dumpCephConfigCmd "github.com/teran/cephctl/commands/dump/cephconfig"
healthcheckCmd "github.com/teran/cephctl/commands/healthcheck"
"github.com/teran/cephctl/differ"
"github.com/teran/cephctl/printer"
"github.com/teran/cephctl/service"
)

Expand All @@ -35,16 +36,18 @@ var (
Envar("CEPHCTL_TRACE").
Bool()

apply = app.Command("apply", "Apply ceph configuration")
applySpecFile = apply.Arg("filename", "Filename with configuration specification").Required().String()

diff = app.Command("diff", "Show difference between running and desired configurations")
diffColor = diff.
colorize = app.
Flag("color", "Colorize diff output").
Short('c').
Envar("CEPHCTL_COLOR").
Default("true").
Bool()

apply = app.Command("apply", "Apply ceph configuration")
applySpecFile = apply.Arg("filename", "Filename with configuration specification").Required().String()

diff = app.Command("diff", "Show difference between running and desired configurations")

diffSpecFile = diff.Arg("filename", "Filename with configuration specification").Required().String()

dump = app.Command("dump", "Dump runtime configuration")
Expand All @@ -68,6 +71,7 @@ func main() {
}

svc := service.New(ceph.New(*cephBinary), differ.New())
prntr := printer.New(*colorize)

switch appCmd {
case apply.FullCommand():
Expand All @@ -81,7 +85,7 @@ func main() {

case diff.FullCommand():
if err := diffCmd.Diff(ctx, diffCmd.DiffConfig{
Colorize: *diffColor,
Printer: prntr,
Service: svc,
SpecFile: *diffSpecFile,
}); err != nil {
Expand All @@ -91,13 +95,17 @@ func main() {
case dumpCephConfig.FullCommand():
log.Tracef("running dump command")
if err := dumpCephConfigCmd.DumpCephConfig(ctx, dumpCephConfigCmd.DumpCephConfigConfig{
Printer: prntr,
Service: svc,
}); err != nil {
panic(err)
}

case healthcheck.FullCommand():
if err := healthcheckCmd.Healthcheck(ctx, svc); err != nil {
if err := healthcheckCmd.Healthcheck(ctx, healthcheckCmd.HealthcheckConfig{
Printer: prntr,
Service: svc,
}); err != nil {
panic(err)
}

Expand Down
2 changes: 1 addition & 1 deletion commands/apply/apply.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package commands
package apply

import (
"context"
Expand Down
29 changes: 29 additions & 0 deletions commands/apply/apply_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package apply

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"github.com/teran/cephctl/models"
"github.com/teran/cephctl/service"
)

func TestApply(t *testing.T) {
r := require.New(t)

m := service.NewMock()
defer m.AssertExpectations(t)

m.On("ApplyCephConfig", models.CephConfig{
"global": {
"test": "value",
},
}).Return(nil).Once()

err := Apply(context.Background(), ApplyConfig{
Service: m,
SpecFile: "testdata/cephconfig.yaml",
})
r.NoError(err)
}
5 changes: 5 additions & 0 deletions commands/apply/testdata/cephconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
kind: CephConfig
spec:
global:
test: value
14 changes: 6 additions & 8 deletions commands/diff/diff.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package commands
package diff

import (
"context"
"strings"

"github.com/fatih/color"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"github.com/teran/cephctl/ceph/config/spec"
"github.com/teran/cephctl/ceph/config/spec/cephconfig"
"github.com/teran/cephctl/models"
"github.com/teran/cephctl/printer"
"github.com/teran/cephctl/service"
)

type DiffConfig struct {
Service service.Service
Colorize bool
Printer printer.Printer
SpecFile string
}

Expand All @@ -26,8 +26,6 @@ func Diff(ctx context.Context, ac DiffConfig) error {
return err
}

color.NoColor = !ac.Colorize

switch strings.ToLower(kind) {
case "cephconfig":
cfg, err := cephconfig.New(specData)
Expand All @@ -45,11 +43,11 @@ func Diff(ctx context.Context, ac DiffConfig) error {

switch change.Kind {
case models.CephConfigDifferenceKindAdd:
color.Green("+ %s %s %s", change.Section, change.Key, *change.Value)
ac.Printer.Green("+ %s %s %s", change.Section, change.Key, *change.Value)
case models.CephConfigDifferenceKindChange:
color.Yellow("~ %s %s %s -> %s", change.Section, change.Key, *change.OldValue, *change.Value)
ac.Printer.Yellow("~ %s %s %s -> %s", change.Section, change.Key, *change.OldValue, *change.Value)
case models.CephConfigDifferenceKindRemove:
color.Red("- %s %s", change.Section, change.Key)
ac.Printer.Red("- %s %s", change.Section, change.Key)
}
}

Expand Down
58 changes: 58 additions & 0 deletions commands/diff/diff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package diff

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"github.com/teran/cephctl/models"
"github.com/teran/cephctl/printer"
"github.com/teran/cephctl/service"
"github.com/teran/go-ptr"
)

func TestDiff(t *testing.T) {
r := require.New(t)

m := service.NewMock()
defer m.AssertExpectations(t)

p := printer.NewMock()
defer p.AssertExpectations(t)

m.On("DiffCephConfig", models.CephConfig{
"global": {
"test": "value",
},
}).Return([]models.CephConfigDifference{
{
Kind: models.CephConfigDifferenceKindAdd,
Section: "mon",
Key: "test_key",
Value: ptr.String("value"),
},
{
Kind: models.CephConfigDifferenceKindChange,
Section: "osd.3",
Key: "test_key",
OldValue: ptr.String("old_value"),
Value: ptr.String("value"),
},
{
Kind: models.CephConfigDifferenceKindRemove,
Section: "osd",
Key: "test_key",
},
}, nil).Once()

p.On("Green", "+ %s %s %s", []any{"mon", "test_key", "value"}).Return().Once()
p.On("Yellow", "~ %s %s %s -> %s", []any{"osd.3", "test_key", "old_value", "value"}).Return().Once()
p.On("Red", "- %s %s", []any{"osd", "test_key"}).Return().Once()

err := Diff(context.Background(), DiffConfig{
Printer: p,
Service: m,
SpecFile: "testdata/cephconfig.yaml",
})
r.NoError(err)
}
5 changes: 5 additions & 0 deletions commands/diff/testdata/cephconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
kind: CephConfig
spec:
global:
test: value
8 changes: 5 additions & 3 deletions commands/dump/cephconfig/cephconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package cephconfig

import (
"context"
"fmt"

yaml "gopkg.in/yaml.v3"

"github.com/teran/cephctl/models"
"github.com/teran/cephctl/printer"
"github.com/teran/cephctl/service"
yaml "gopkg.in/yaml.v3"
)

type DumpCephConfigConfig struct {
Printer printer.Printer
Service service.Service
}

Expand All @@ -34,6 +36,6 @@ func DumpCephConfig(ctx context.Context, dc DumpCephConfigConfig) error {
return err
}

fmt.Println(string(data))
dc.Printer.Println(string(data))
return nil
}
35 changes: 35 additions & 0 deletions commands/dump/cephconfig/cephconfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cephconfig

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"github.com/teran/cephctl/models"
"github.com/teran/cephctl/printer"
"github.com/teran/cephctl/service"
)

func TestDumpCephConfig(t *testing.T) {
r := require.New(t)

m := service.NewMock()
defer m.AssertExpectations(t)

p := printer.NewMock()
defer p.AssertExpectations(t)

m.On("DumpConfig").Return(models.CephConfig{
"global": {
"key": "value",
},
}, nil).Once()

p.On("Println", []any{"kind: CephConfig\nspec:\n global:\n key: value\n"}).Return().Once()

err := DumpCephConfig(context.Background(), DumpCephConfigConfig{
Printer: p,
Service: m,
})
r.NoError(err)
}
21 changes: 13 additions & 8 deletions commands/healthcheck/healthcheck.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
package status
package healthcheck

import (
"context"

"github.com/fatih/color"
"github.com/teran/cephctl/models"
"github.com/teran/cephctl/printer"
"github.com/teran/cephctl/service"
)

func Healthcheck(ctx context.Context, svc service.Service) error {
indicators, err := svc.CheckClusterHealth(ctx)
type HealthcheckConfig struct {
Service service.Service
Printer printer.Printer
}

func Healthcheck(ctx context.Context, hc HealthcheckConfig) error {
indicators, err := hc.Service.CheckClusterHealth(ctx)
if err != nil {
return err
}

for _, indicator := range indicators {
var printFn func(string, ...interface{}) = color.HiRed
var printFn func(string, ...any) = hc.Printer.HiRed

switch indicator.CurrentValueStatus {
case models.ClusterHealthIndicatorStatusGood:
printFn = color.Green
printFn = hc.Printer.Green
case models.ClusterHealthIndicatorStatusAtRisk:
printFn = color.Yellow
printFn = hc.Printer.Yellow
case models.ClusterHealthIndicatorStatusDangerous:
printFn = color.Red
printFn = hc.Printer.Red
}

printFn(
Expand Down
55 changes: 55 additions & 0 deletions commands/healthcheck/healthcheck_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package healthcheck

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"github.com/teran/cephctl/models"
"github.com/teran/cephctl/printer"
"github.com/teran/cephctl/service"
)

func TestHealthcheck(t *testing.T) {
r := require.New(t)

m := service.NewMock()
defer m.AssertExpectations(t)

p := printer.NewMock()
defer p.AssertExpectations(t)

m.On("CheckClusterHealth").Return([]models.ClusterHealthIndicator{
{
Indicator: models.ClusterHealthIndicatorTypeClusterStatus,
CurrentValue: "HEALTH_OK",
CurrentValueStatus: models.ClusterHealthIndicatorStatusGood,
},
{
Indicator: models.ClusterHealthIndicatorTypeQuorum,
CurrentValue: "5 of 5",
CurrentValueStatus: models.ClusterHealthIndicatorStatusGood,
},
{
Indicator: models.ClusterHealthIndicatorTypeOSDsDown,
CurrentValue: "0 of 15",
CurrentValueStatus: models.ClusterHealthIndicatorStatusGood,
},
}, nil).Once()

p.On("Green", "[%s] %s = %s", []any{
" GOOD", models.ClusterHealthIndicatorTypeClusterStatus, "HEALTH_OK",
}).Return().Once()
p.On("Green", "[%s] %s = %s", []any{
" GOOD", models.ClusterHealthIndicatorTypeQuorum, "5 of 5",
}).Return().Once()
p.On("Green", "[%s] %s = %s", []any{
" GOOD", models.ClusterHealthIndicatorTypeOSDsDown, "0 of 15",
}).Return().Once()

err := Healthcheck(context.Background(), HealthcheckConfig{
Printer: p,
Service: m,
})
r.NoError(err)
}
Loading

0 comments on commit bcd959b

Please sign in to comment.