Skip to content

Commit

Permalink
(feat): Add test coverage for layer.LabelDeviceLayer()
Browse files Browse the repository at this point in the history
  • Loading branch information
lasith-kg committed Jan 3, 2024
1 parent 4921ddf commit a98c72f
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 12 deletions.
2 changes: 1 addition & 1 deletion internal/layer/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (fdl *FormatDeviceLayer) Modify(c *config.Config) ([]action.Action, error)
return nil, fmt.Errorf("🔴 %s: Can not erase the file system of a device", bd.Name)
}
if bd.FileSystem != model.Unformatted {
return nil, fmt.Errorf("🔴 %s: Can not format a device that already has a %s file system", bd.Name, bd.FileSystem.String())
return nil, fmt.Errorf("🔴 %s: Can not format a device with an existing %s file system", bd.Name, bd.FileSystem.String())
}
a, err := fdl.deviceBackend.Format(bd, cd.Fs)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/layer/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestFormatDeviceLayerModify(t *testing.T) {
ExpectedError: fmt.Errorf("🔴 /dev/xvdf: Can not erase the file system of a device"),
},
{
Name: "Attempting to Change the File System of a Block Device",
Name: "Attempting to Change the Existing File System of a Block Device",
Config: &config.Config{
Defaults: config.Options{Mode: model.Force},
Devices: map[string]config.Device{
Expand All @@ -105,7 +105,7 @@ func TestFormatDeviceLayerModify(t *testing.T) {
},
CmpOption: cmp.AllowUnexported(),
ExpectedOutput: nil,
ExpectedError: fmt.Errorf("🔴 /dev/xvdf: Can not format a device that already has a xfs file system"),
ExpectedError: fmt.Errorf("🔴 /dev/xvdf: Can not format a device with an existing xfs file system"),
},
}
for _, subtest := range subtests {
Expand All @@ -127,7 +127,7 @@ func TestFormatDeviceLayerValidate(t *testing.T) {
ExpectedOutput error
}{
{
Name: "Valid Block Device",
Name: "File System Matches Requested File System",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdf": {
Expand All @@ -144,7 +144,7 @@ func TestFormatDeviceLayerValidate(t *testing.T) {
ExpectedOutput: nil,
},
{
Name: "Invalid Block Device",
Name: "File System Does Not Match Requested File System",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdf": {
Expand Down
6 changes: 0 additions & 6 deletions internal/layer/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/reecetech/ebs-bootstrap/internal/action"
"github.com/reecetech/ebs-bootstrap/internal/backend"
"github.com/reecetech/ebs-bootstrap/internal/config"
"github.com/reecetech/ebs-bootstrap/internal/model"
)

type LabelDeviceLayer struct {
Expand Down Expand Up @@ -36,12 +35,7 @@ func (fdl *LabelDeviceLayer) Modify(c *config.Config) ([]action.Action, error) {
if bd.Label == cd.Label {
continue
}
if bd.FileSystem == model.Unformatted {
return nil, fmt.Errorf("🔴 %s: Can not label a device with no file system", bd.Name)
}
mode := c.GetMode(name)
// Labelling a device can potentially require unmounting it first
// Therefore, multiple actions may be returned: Label Actions (las)
las, err := fdl.deviceBackend.Label(bd, cd.Label)
if err != nil {
return nil, err
Expand Down
226 changes: 226 additions & 0 deletions internal/layer/label_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
package layer

import (
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/reecetech/ebs-bootstrap/internal/action"
"github.com/reecetech/ebs-bootstrap/internal/backend"
"github.com/reecetech/ebs-bootstrap/internal/config"
"github.com/reecetech/ebs-bootstrap/internal/model"
"github.com/reecetech/ebs-bootstrap/internal/service"
"github.com/reecetech/ebs-bootstrap/internal/utils"
)

func TestLabelDeviceLayerModify(t *testing.T) {
subtests := []struct {
Name string
Config *config.Config
Devices map[string]*model.BlockDevice
CmpOption cmp.Option
ExpectedOuput []action.Action
ExpectedError error
}{
{
Name: "Label Device With File System That Avoids Unmounting",
Config: &config.Config{
Defaults: config.Options{Mode: model.Force},
Devices: map[string]config.Device{
"/dev/xvdf": {
Fs: model.Ext4,
Label: "label",
},
},
},
Devices: map[string]*model.BlockDevice{
"/dev/xvdf": {
Name: "/dev/xvdf",
FileSystem: model.Ext4,
},
},
CmpOption: cmp.AllowUnexported(
action.LabelDeviceAction{},
service.Ext4Service{},
),
ExpectedOuput: []action.Action{
action.NewLabelDeviceAction("/dev/xvdf", "label", service.NewExt4Service(nil)).SetMode(model.Force),
},
ExpectedError: nil,
},
{
Name: "Label Device With File System That Requires Unmounting",
Config: &config.Config{
Defaults: config.Options{Mode: model.Force},
Devices: map[string]config.Device{
"/dev/xvdf": {
Fs: model.Xfs,
Label: "label",
MountPoint: "/mnt/foo",
},
},
},
Devices: map[string]*model.BlockDevice{
"/dev/xvdf": {
Name: "/dev/xvdf",
FileSystem: model.Xfs,
MountPoint: "/mnt/foo",
},
},
CmpOption: cmp.AllowUnexported(
action.UnmountDeviceAction{},
action.LabelDeviceAction{},
service.XfsService{},
),
ExpectedOuput: []action.Action{
action.NewUnmountDeviceAction("/dev/xvdf", "/mnt/foo", nil).SetMode(model.Force),
action.NewLabelDeviceAction("/dev/xvdf", "label", service.NewXfsService(nil)).SetMode(model.Force),
},
ExpectedError: nil,
},
{
Name: "Label Matches Requested Label",
Config: &config.Config{
Defaults: config.Options{Mode: model.Force},
Devices: map[string]config.Device{
"/dev/xvdf": {
Fs: model.Ext4,
Label: "label",
},
},
},
Devices: map[string]*model.BlockDevice{
"/dev/xvdf": {
Name: "/dev/xvdf",
FileSystem: model.Ext4,
Label: "label",
},
},
CmpOption: cmp.AllowUnexported(),
ExpectedOuput: []action.Action{},
ExpectedError: nil,
},
{
Name: "Skip Labelling",
Config: &config.Config{
Defaults: config.Options{Mode: model.Force},
Devices: map[string]config.Device{
"/dev/xvdf": {
Fs: model.Ext4,
},
},
},
Devices: map[string]*model.BlockDevice{
"/dev/xvdf": {
Name: "/dev/xvdf",
FileSystem: model.Ext4,
},
},
CmpOption: cmp.AllowUnexported(),
ExpectedOuput: []action.Action{},
ExpectedError: nil,
},
{
Name: "Attempting to Label Block Device With No File System",
Config: &config.Config{
Defaults: config.Options{Mode: model.Force},
Devices: map[string]config.Device{
"/dev/xvdf": {
Fs: model.Ext4,
Label: "label",
},
},
},
Devices: map[string]*model.BlockDevice{
"/dev/xvdf": {
Name: "/dev/xvdf",
},
},
CmpOption: cmp.AllowUnexported(),
ExpectedOuput: nil,
ExpectedError: fmt.Errorf("🔴 /dev/xvdf: An unformatted file system can not be queried/modified"),
},
}
for _, subtest := range subtests {
t.Run(subtest.Name, func(t *testing.T) {
ldb := backend.NewMockLinuxDeviceBackend(subtest.Devices)
layer := NewLabelDeviceLayer(ldb)
actions, err := layer.Modify(subtest.Config)
utils.CheckError("LabelDeviceLayer.Modify()", t, subtest.ExpectedError, err)
utils.CheckOutput("LabelDeviceLayer.Modify()", t, subtest.ExpectedOuput, actions, subtest.CmpOption)
})
}
}

func TestLabelDeviceLayerValidate(t *testing.T) {
subtests := []struct {
Name string
Config *config.Config
Devices map[string]*model.BlockDevice
ExpectedError error
}{
{
Name: "Label Matches Requested Label",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdf": {
Fs: model.Ext4,
Label: "label",
},
},
},
Devices: map[string]*model.BlockDevice{
"/dev/xvdf": {
Name: "/dev/xvdf",
FileSystem: model.Ext4,
Label: "label",
},
},
ExpectedError: nil,
},
{
Name: "Skipping Validation",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdf": {
Fs: model.Ext4,
},
},
},
Devices: map[string]*model.BlockDevice{
"/dev/xvdf": {
Name: "/dev/xvdf",
FileSystem: model.Ext4,
},
},
ExpectedError: nil,
},
{
Name: "Label Does Not Match Requested Label",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdf": {
Fs: model.Ext4,
Label: "label",
},
},
},
Devices: map[string]*model.BlockDevice{
"/dev/xvdf": {
Name: "/dev/xvdf",
FileSystem: model.Ext4,
Label: "not-label",
},
},
ExpectedError: fmt.Errorf("🔴 /dev/xvdf: Failed label validation checks. Expected=label, Actual=not-label"),
},
}
for _, subtest := range subtests {
t.Run(subtest.Name, func(t *testing.T) {
ldb := backend.NewMockLinuxDeviceBackend(subtest.Devices)
ldl := NewLabelDeviceLayer(ldb)
err := ldl.Validate(subtest.Config)
utils.CheckError("ldl.Validate()", t, subtest.ExpectedError, err)
})
}
}
2 changes: 1 addition & 1 deletion internal/layer/layer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (ml *MockLayer) Warning() string {
func TestExponentialBackoffLayerExecutor(t *testing.T) {
mae := action.NewDefaultActionExecutor()
// Lets generate ExponentialBackoffParameters with a custom
// InitialInterval. We do not want to slow down the test suite
// InitialInterval of 10 ms. We do not want to slow down the test suite
// with an excessively long initial interval
debp := DefaultExponentialBackoffParameters()
ebp := &ExponentialBackoffParameters{
Expand Down

0 comments on commit a98c72f

Please sign in to comment.