Skip to content

Commit

Permalink
(feat): Add test coverage for layer.FormatDeviceLayer()
Browse files Browse the repository at this point in the history
  • Loading branch information
lasith-kg committed Jan 3, 2024
1 parent b753368 commit 4921ddf
Show file tree
Hide file tree
Showing 20 changed files with 423 additions and 218 deletions.
92 changes: 46 additions & 46 deletions internal/action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,67 +52,67 @@ func TestDefaultActionExecutor(t *testing.T) {
}

subtests := []struct {
Name string
Read func(buffer *string) error
Error error
Mode model.Mode
ExpectedErr error
Name string
Read func(buffer *string) error
Error error
Mode model.Mode
ExpectedError error
}{
{
Name: "Mode=Empty + Read=Disabled + Action=Success",
Read: fail("🔴 Standard Input Disabled"),
Error: nil,
Mode: model.Empty,
ExpectedErr: fmt.Errorf("🔴 Unsupported mode was encountered. Refused to execute action"),
Name: "Mode=Empty + Read=Disabled + Action=Success",
Read: fail("🔴 Standard Input Disabled"),
Error: nil,
Mode: model.Empty,
ExpectedError: fmt.Errorf("🔴 Unsupported mode was encountered. Refused to execute action"),
},
{
Name: "Mode=Healthcheck + Read=Avoid + Action=Success",
Read: fail("🔴 Standard Input Disabled"),
Error: nil,
Mode: model.Healthcheck,
ExpectedErr: fmt.Errorf("🔴 Healthcheck mode enabled. Refused to execute action"),
Name: "Mode=Healthcheck + Read=Avoid + Action=Success",
Read: fail("🔴 Standard Input Disabled"),
Error: nil,
Mode: model.Healthcheck,
ExpectedError: fmt.Errorf("🔴 Healthcheck mode enabled. Refused to execute action"),
},
{
Name: "Mode=Prompt + Read=Input<y> + Action=Success",
Read: input("y"),
Error: nil,
Mode: model.Prompt,
ExpectedErr: nil,
Name: "Mode=Prompt + Read=Input<y> + Action=Success",
Read: input("y"),
Error: nil,
Mode: model.Prompt,
ExpectedError: nil,
},
{
Name: "Mode=Prompt + Read=Input<yes> + Action=Success",
Read: input("yes"),
Error: nil,
Mode: model.Prompt,
ExpectedErr: nil,
Name: "Mode=Prompt + Read=Input<yes> + Action=Success",
Read: input("yes"),
Error: nil,
Mode: model.Prompt,
ExpectedError: nil,
},
{
Name: "Mode=Prompt + Read=Failure + Action=Success",
Read: fail("🔴 Failed to Read From Standard Input"),
Error: nil,
Mode: model.Prompt,
ExpectedErr: fmt.Errorf("🔴 Action rejected. Refused to execute action"),
Name: "Mode=Prompt + Read=Failure + Action=Success",
Read: fail("🔴 Failed to Read From Standard Input"),
Error: nil,
Mode: model.Prompt,
ExpectedError: fmt.Errorf("🔴 Action rejected. Refused to execute action"),
},
{
Name: "Mode=Prompt + Read=Input<n> + Action=Success",
Read: input("n"),
Error: nil,
Mode: model.Prompt,
ExpectedErr: fmt.Errorf("🔴 Action rejected. Refused to execute action"),
Name: "Mode=Prompt + Read=Input<n> + Action=Success",
Read: input("n"),
Error: nil,
Mode: model.Prompt,
ExpectedError: fmt.Errorf("🔴 Action rejected. Refused to execute action"),
},
{
Name: "Mode=Force + Read=Disabled + Action=Success",
Read: fail("🔴 Standard Input Disabled"),
Error: nil,
Mode: model.Force,
ExpectedErr: nil,
Name: "Mode=Force + Read=Disabled + Action=Success",
Read: fail("🔴 Standard Input Disabled"),
Error: nil,
Mode: model.Force,
ExpectedError: nil,
},
{
Name: "Mode=Force + Read=Disabled + Action=Failure",
Read: fail("🔴 Standard Input Disabled"),
Error: fmt.Errorf("🔴 Error encountered while executing action"),
Mode: model.Force,
ExpectedErr: fmt.Errorf("🔴 Error encountered while executing action"),
Name: "Mode=Force + Read=Disabled + Action=Failure",
Read: fail("🔴 Standard Input Disabled"),
Error: fmt.Errorf("🔴 Error encountered while executing action"),
Mode: model.Force,
ExpectedError: fmt.Errorf("🔴 Error encountered while executing action"),
},
}
for _, subtest := range subtests {
Expand All @@ -125,7 +125,7 @@ func TestDefaultActionExecutor(t *testing.T) {
mode: subtest.Mode,
}
err := dae.Execute([]Action{a})
utils.CheckError("dae.Execute()", t, subtest.ExpectedErr, err)
utils.CheckError("dae.Execute()", t, subtest.ExpectedError, err)
})
}
}
2 changes: 1 addition & 1 deletion internal/action/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ func (a *FormatDeviceAction) Refuse() string {
}

func (a *FormatDeviceAction) Success() string {
return fmt.Sprintf("Successfully formated to %s", a.fileSystemService.GetFileSystem())
return fmt.Sprintf("Successfully formated to %s", a.fileSystemService.GetFileSystem().String())
}
4 changes: 2 additions & 2 deletions internal/backend/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (db *LinuxDeviceBackend) Label(bd *model.BlockDevice, label string) ([]acti
return nil, fmt.Errorf("🔴 %s: %s", bd.Name, err)
}
if ml := fss.GetMaximumLabelLength(); len(label) > ml {
return nil, fmt.Errorf("🔴 %s: Label '%s' exceeds the maximum %d character length for the %s file system", bd.Name, label, ml, fss.GetFileSystem())
return nil, fmt.Errorf("🔴 %s: Label '%s' exceeds the maximum %d character length for the %s file system", bd.Name, label, ml, fss.GetFileSystem().String())
}
if fss.DoesLabelRequireUnmount() && len(bd.MountPoint) > 0 {
a := db.Umount(bd)
Expand All @@ -82,7 +82,7 @@ func (db *LinuxDeviceBackend) Resize(bd *model.BlockDevice) (action.Action, erro
target := bd.Name
if fss.DoesResizeRequireMount() {
if len(bd.MountPoint) == 0 {
return nil, fmt.Errorf("🔴 %s: To resize the %s file system, device must be mounted", bd.Name, fss.GetFileSystem())
return nil, fmt.Errorf("🔴 %s: To resize the %s file system, device must be mounted", bd.Name, fss.GetFileSystem().String())
}
target = bd.MountPoint
}
Expand Down
48 changes: 24 additions & 24 deletions internal/backend/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestGetBlockDevice(t *testing.T) {
Device string
BlockDevices map[string]*model.BlockDevice
ExpectedOutput *model.BlockDevice
ExpectedErr error
ExpectedError error
}{
{
Name: "Valid Block Device",
Expand All @@ -34,7 +34,7 @@ func TestGetBlockDevice(t *testing.T) {
Name: "/dev/xvdf",
FileSystem: model.Xfs,
},
ExpectedErr: nil,
ExpectedError: nil,
},
{
Name: "Non-existent Block Device",
Expand All @@ -46,14 +46,14 @@ func TestGetBlockDevice(t *testing.T) {
},
},
ExpectedOutput: nil,
ExpectedErr: fmt.Errorf("🔴 /dev/sdb: Could not find block device"),
ExpectedError: fmt.Errorf("🔴 /dev/sdb: Could not find block device"),
},
}
for _, subtest := range subtests {
t.Run(subtest.Name, func(t *testing.T) {
ldb := NewMockLinuxDeviceBackend(subtest.BlockDevices)
bd, err := ldb.GetBlockDevice(subtest.Device)
utils.CheckError("ldb.From()", t, subtest.ExpectedErr, err)
utils.CheckError("ldb.From()", t, subtest.ExpectedError, err)
utils.CheckOutput("ldb.From()", t, subtest.ExpectedOutput, bd)
})
}
Expand All @@ -67,7 +67,7 @@ func TestLabel(t *testing.T) {
Label string
CmpOption cmp.Option
ExpectedOutput []action.Action
ExpectedErr error
ExpectedError error
}{
{
Name: "Labelling Valid Block Device",
Expand All @@ -86,7 +86,7 @@ func TestLabel(t *testing.T) {
ExpectedOutput: []action.Action{
action.NewLabelDeviceAction("/dev/xvdf", "label", service.NewExt4Service(nil)),
},
ExpectedErr: nil,
ExpectedError: nil,
},
{
Name: "Labelling Valid Block Device + Requires Unmount",
Expand All @@ -108,7 +108,7 @@ func TestLabel(t *testing.T) {
action.NewUnmountDeviceAction("/dev/xvdf", "/mnt/app", nil),
action.NewLabelDeviceAction("/dev/xvdf", "label", service.NewXfsService(nil)),
},
ExpectedErr: nil,
ExpectedError: nil,
},
{
Name: "Fail To Label + Exceeding Maximum Label Length",
Expand All @@ -125,7 +125,7 @@ func TestLabel(t *testing.T) {
service.Ext4Service{},
),
ExpectedOutput: nil,
ExpectedErr: fmt.Errorf("🔴 /dev/xvdf: Label 'exceptionally-long-label' exceeds the maximum 16 character length for the ext4 file system"),
ExpectedError: fmt.Errorf("🔴 /dev/xvdf: Label 'exceptionally-long-label' exceeds the maximum 16 character length for the ext4 file system"),
},
{
Name: "Fail To Label + Unformatted Device",
Expand All @@ -141,7 +141,7 @@ func TestLabel(t *testing.T) {
action.LabelDeviceAction{},
),
ExpectedOutput: nil,
ExpectedErr: fmt.Errorf("🔴 /dev/xvdf: An unformatted file system can not be queried/modified"),
ExpectedError: fmt.Errorf("🔴 /dev/xvdf: An unformatted file system can not be queried/modified"),
},
}
for _, subtest := range subtests {
Expand All @@ -151,7 +151,7 @@ func TestLabel(t *testing.T) {
utils.ExpectErr("ldb.GetBlockDevice()", t, false, err)

actions, err := ldb.Label(bd, subtest.Label)
utils.CheckError("ldb.Label()", t, subtest.ExpectedErr, err)
utils.CheckError("ldb.Label()", t, subtest.ExpectedError, err)
utils.CheckOutput("ldb.Label()", t, subtest.ExpectedOutput, actions, subtest.CmpOption)
})
}
Expand All @@ -164,7 +164,7 @@ func TestResize(t *testing.T) {
Device string
CmpOption cmp.Option
ExpectedOutput action.Action
ExpectedErr error
ExpectedError error
}{
{
Name: "Valid Block Device",
Expand All @@ -180,7 +180,7 @@ func TestResize(t *testing.T) {
action.ResizeDeviceAction{},
),
ExpectedOutput: action.NewResizeDeviceAction("/dev/xvdf", "/dev/xvdf", service.NewExt4Service(nil)),
ExpectedErr: nil,
ExpectedError: nil,
},
{
Name: "Valid Block Device + Requires Mount",
Expand All @@ -197,7 +197,7 @@ func TestResize(t *testing.T) {
action.ResizeDeviceAction{},
),
ExpectedOutput: action.NewResizeDeviceAction("/dev/xvdf", "/mnt/app", service.NewXfsService(nil)),
ExpectedErr: nil,
ExpectedError: nil,
},
{
Name: "Fail to Resize + Requires Mount, but not Mounted",
Expand All @@ -210,7 +210,7 @@ func TestResize(t *testing.T) {
Device: "/dev/xvdf",
CmpOption: cmp.AllowUnexported(),
ExpectedOutput: nil,
ExpectedErr: fmt.Errorf("🔴 /dev/xvdf: To resize the xfs file system, device must be mounted"),
ExpectedError: fmt.Errorf("🔴 /dev/xvdf: To resize the xfs file system, device must be mounted"),
},
{
Name: "Fail To Resize + Unformatted Device",
Expand All @@ -223,7 +223,7 @@ func TestResize(t *testing.T) {
Device: "/dev/xvdf",
CmpOption: cmp.AllowUnexported(),
ExpectedOutput: nil,
ExpectedErr: fmt.Errorf("🔴 /dev/xvdf: An unformatted file system can not be queried/modified"),
ExpectedError: fmt.Errorf("🔴 /dev/xvdf: An unformatted file system can not be queried/modified"),
},
}
for _, subtest := range subtests {
Expand All @@ -233,7 +233,7 @@ func TestResize(t *testing.T) {
utils.ExpectErr("ldb.GetBlockDevice()", t, false, err)

action, err := ldb.Resize(bd)
utils.CheckError("ldb.Resize()", t, subtest.ExpectedErr, err)
utils.CheckError("ldb.Resize()", t, subtest.ExpectedError, err)
utils.CheckOutput("ldb.Resize()", t, subtest.ExpectedOutput, action, subtest.CmpOption)
})
}
Expand All @@ -247,7 +247,7 @@ func TestFormat(t *testing.T) {
model.FileSystem
CmpOption cmp.Option
ExpectedOutput action.Action
ExpectedErr error
ExpectedError error
}{
{
Name: "Valid Block Device",
Expand All @@ -264,7 +264,7 @@ func TestFormat(t *testing.T) {
action.FormatDeviceAction{},
),
ExpectedOutput: action.NewFormatDeviceAction("/dev/xvdf", service.NewExt4Service(nil)),
ExpectedErr: nil,
ExpectedError: nil,
},
{
Name: "Invalid File System + Attempting to Erase File System",
Expand All @@ -278,7 +278,7 @@ func TestFormat(t *testing.T) {
FileSystem: model.Unformatted,
CmpOption: cmp.AllowUnexported(),
ExpectedOutput: nil,
ExpectedErr: fmt.Errorf("🔴 /dev/xvdf: An unformatted file system can not be queried/modified"),
ExpectedError: fmt.Errorf("🔴 /dev/xvdf: An unformatted file system can not be queried/modified"),
},
}
for _, subtest := range subtests {
Expand All @@ -288,7 +288,7 @@ func TestFormat(t *testing.T) {
utils.ExpectErr("ldb.GetBlockDevice()", t, false, err)

action, err := ldb.Format(bd, subtest.FileSystem)
utils.CheckError("ldb.Format()", t, subtest.ExpectedErr, err)
utils.CheckError("ldb.Format()", t, subtest.ExpectedError, err)
utils.CheckOutput("ldb.Format()", t, subtest.ExpectedOutput, action, subtest.CmpOption)
})
}
Expand Down Expand Up @@ -408,7 +408,7 @@ func TestLinuxDeviceBackendFrom(t *testing.T) {
Config *config.Config
GetBlockDevice func(name string) (*model.BlockDevice, error)
ExpectedOutput map[string]*model.BlockDevice
ExpectedErr error
ExpectedError error
}{
{
Name: "Valid Block Device",
Expand All @@ -429,7 +429,7 @@ func TestLinuxDeviceBackendFrom(t *testing.T) {
FileSystem: model.Unformatted,
},
},
ExpectedErr: nil,
ExpectedError: nil,
},
{
Name: "Invalid Block Device",
Expand All @@ -442,7 +442,7 @@ func TestLinuxDeviceBackendFrom(t *testing.T) {
return nil, fmt.Errorf("🔴 %s: 'jfs' is not a supported file system", name)
},
ExpectedOutput: nil,
ExpectedErr: fmt.Errorf("🔴 /dev/xvdf: 'jfs' is not a supported file system"),
ExpectedError: fmt.Errorf("🔴 /dev/xvdf: 'jfs' is not a supported file system"),
},
}
for _, subtest := range subtests {
Expand All @@ -454,7 +454,7 @@ func TestLinuxDeviceBackendFrom(t *testing.T) {

ldb := NewLinuxDeviceBackend(mds, nil)
err := ldb.From(subtest.Config)
utils.CheckError("ldb.From()", t, subtest.ExpectedErr, err)
utils.CheckError("ldb.From()", t, subtest.ExpectedError, err)
utils.CheckOutput("ldb.From()", t, subtest.ExpectedOutput, ldb.blockDevices)
})
}
Expand Down
Loading

0 comments on commit 4921ddf

Please sign in to comment.