Skip to content

Commit

Permalink
Add clean diff indication for multidoc specs (#54)
Browse files Browse the repository at this point in the history
Just like this:

```
CephConfig:
~ global cephx_sign_messages true -> false
CephOSDConfig:
~ require_min_compat_client luminous -> reef
```

Depends on #53

Signed-off-by: Igor Shishkin <me@teran.dev>
  • Loading branch information
teran authored Jun 26, 2024
1 parent 77c097c commit a5bc393
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
8 changes: 8 additions & 0 deletions commands/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func Diff(ctx context.Context, ac DiffConfig) error {
return err
}

if len(descs) > 1 && len(changes) > 0 {
ac.Printer.Printf("%s:\n", desc.Kind)
}

for _, change := range changes {
log.WithFields(log.Fields{
"component": "command",
Expand All @@ -66,6 +70,10 @@ func Diff(ctx context.Context, ac DiffConfig) error {
return err
}

if len(descs) > 1 && len(changes) > 0 {
ac.Printer.Printf("%s:\n", desc.Kind)
}

for _, change := range changes {
log.WithFields(log.Fields{
"component": "command",
Expand Down
63 changes: 63 additions & 0 deletions commands/diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,66 @@ func TestDiffCephOSDConfig(t *testing.T) {
})
r.NoError(err)
}

func TestDiffMultidoc(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()

m.On("DiffCephOSDConfig", models.CephOSDConfig{
AllowCrimson: true,
BackfillfullRatio: 0.9,
FullRatio: 0.95,
NearfullRatio: 0.85,
RequireMinCompatClient: "luminous",
}).Return([]models.CephOSDConfigDifference{
{
Key: "allow_crimson",
OldValue: "false",
Value: "true",
},
}, nil).Once()

call1 := p.On("Printf", "%s:\n", []any{"CephConfig"}).Return().Once()
call2 := p.On("Green", "+ %s %s %s", []any{"mon", "test_key", "value"}).Return().NotBefore(call1).Once()
call3 := p.On("Yellow", "~ %s %s %s -> %s", []any{"osd.3", "test_key", "old_value", "value"}).Return().NotBefore(call2).Once()
call4 := p.On("Red", "- %s %s", []any{"osd", "test_key"}).Return().NotBefore(call3).Once()
call5 := p.On("Printf", "%s:\n", []any{"CephOSDConfig"}).Return().NotBefore(call4).Once()
p.On("Yellow", "~ %s %s -> %s", []any{"allow_crimson", "false", "true"}).Return().NotBefore(call5).Once()

err := Diff(context.Background(), DiffConfig{
Printer: p,
Service: m,
SpecFile: "testdata/multidoc.yaml",
})
r.NoError(err)
}
13 changes: 13 additions & 0 deletions commands/diff/testdata/multidoc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
kind: CephConfig
spec:
global:
test: value
---
kind: CephOSDConfig
spec:
allow_crimson: true
backfillfull_ratio: 0.9
full_ratio: 0.95
nearfull_ratio: 0.85
require_min_compat_client: luminous
4 changes: 4 additions & 0 deletions printer/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func (m *Mock) HiRed(format string, a ...any) {
m.Called(format, a)
}

func (m *Mock) Printf(format string, a ...any) {
m.Called(format, a)
}

func (m *Mock) Println(a ...any) {
m.Called(a)
}
Expand Down
5 changes: 5 additions & 0 deletions printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type Printer interface {
Green(format string, a ...any)
HiRed(format string, a ...any)
Printf(format string, a ...any)
Println(a ...any)
Red(format string, a ...any)
Yellow(format string, a ...any)
Expand All @@ -30,6 +31,10 @@ func (p *printer) HiRed(format string, a ...any) {
color.HiRed(format, a...)
}

func (p *printer) Printf(format string, a ...any) {
fmt.Printf(format, a...)
}

func (p *printer) Println(a ...any) {
fmt.Println(a...)
}
Expand Down

0 comments on commit a5bc393

Please sign in to comment.