Skip to content

(feat): Add test cases for layer.ShouldProcess() #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions internal/layer/directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,40 @@ func TestCreateDirectoryLayerValidate(t *testing.T) {
})
}
}

func TestCreateDirectoryLayerShouldProcess(t *testing.T) {
subtests := []struct {
Name string
Config *config.Config
ExpectedOutput bool
}{
{
Name: "At Least Once Device Has Mount Point Specified",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdb": {},
"/dev/xvdf": {
MountPoint: "/mnt/foo",
},
},
},
ExpectedOutput: true,
},
{
Name: "No Device Has Mount Point Specified",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdf": {},
},
},
ExpectedOutput: false,
},
}
for _, subtest := range subtests {
t.Run(subtest.Name, func(t *testing.T) {
cdl := NewCreateDirectoryLayer(nil)
value := cdl.ShouldProcess(subtest.Config)
utils.CheckOutput("cdl.ShouldProcess()", t, subtest.ExpectedOutput, value)
})
}
}
27 changes: 27 additions & 0 deletions internal/layer/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,30 @@ func TestFormatDeviceLayerValidate(t *testing.T) {
})
}
}

func TestFormatDeviceLayerShouldProcess(t *testing.T) {
subtests := []struct {
Name string
Config *config.Config
ExpectedOutput bool
}{
{
Name: "File System Declared",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdf": {
Fs: model.Xfs,
},
},
},
ExpectedOutput: true,
},
}
for _, subtest := range subtests {
t.Run(subtest.Name, func(t *testing.T) {
ld := NewFormatDeviceLayer(nil)
output := ld.ShouldProcess(subtest.Config)
utils.CheckOutput("ld.ShouldProcess()", t, subtest.ExpectedOutput, output)
})
}
}
42 changes: 42 additions & 0 deletions internal/layer/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,45 @@ func TestLabelDeviceLayerValidate(t *testing.T) {
})
}
}

func TestLabelDeviceLayerShouldProcess(t *testing.T) {
subtests := []struct {
Name string
Config *config.Config
ExpectedValue bool
}{
{
Name: "At Least Once Device Has Label Specified",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdb": {
Fs: model.Ext4,
Label: "label",
},
"/dev/xvdf": {
Fs: model.Ext4,
},
},
},
ExpectedValue: true,
},
{
Name: "No Device Has Label Specified",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdf": {
Fs: model.Ext4,
},
},
},
ExpectedValue: false,
},
}
for _, subtest := range subtests {
t.Run(subtest.Name, func(t *testing.T) {
ldl := NewLabelDeviceLayer(nil)
value := ldl.ShouldProcess(subtest.Config)
utils.CheckOutput("ldl.ShouldProcess()", t, subtest.ExpectedValue, value)
})
}
}
32 changes: 25 additions & 7 deletions internal/layer/layer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ const (
)

type MockLayer struct {
from *utils.MockIncrementError
modify *utils.MockIncrementError
validate *utils.MockIncrementError
from *utils.MockIncrementError
modify *utils.MockIncrementError
validate *utils.MockIncrementError
shouldProcess bool
}

func (ml *MockLayer) From(c *config.Config) error {
Expand All @@ -41,7 +42,7 @@ func (ml *MockLayer) Warning() string {
}

func (ml *MockLayer) ShouldProcess(c *config.Config) bool {
return true
return ml.shouldProcess
}

func TestExponentialBackoffLayerExecutor(t *testing.T) {
Expand Down Expand Up @@ -110,13 +111,30 @@ func TestExponentialBackoffLayerExecutor(t *testing.T) {
for _, subtest := range subtests {
t.Run(subtest.Name, func(t *testing.T) {
ml := &MockLayer{
from: subtest.From,
modify: subtest.Modify,
validate: subtest.Validate,
from: subtest.From,
modify: subtest.Modify,
validate: subtest.Validate,
shouldProcess: true,
}
eb := NewExponentialBackoffLayerExecutor(nil, mae, ebp)
err := eb.Execute([]Layer{ml})
utils.CheckError("eb.Execute()", t, subtest.ExpectedError, err)
})
}
}

func TestExponentialBackoffLayerExecutorShouldProcess(t *testing.T) {
// This MockLayer will fail if any of the From(), Modify() or Validate() methods
// are invoked. `shouldProcess` has been set to false so these methods would be skipped
// and there should be no error generated
ml := &MockLayer{
from: utils.NewMockIncrementError("From()", utils.SuccessUntilTrigger, 1),
modify: utils.NewMockIncrementError("Modidy()", utils.SuccessUntilTrigger, 1),
validate: utils.NewMockIncrementError("Validate()", utils.SuccessUntilTrigger, 1),
shouldProcess: false,
}
debp := DefaultExponentialBackoffParameters()
eb := NewExponentialBackoffLayerExecutor(nil, nil, debp)
err := eb.Execute([]Layer{ml})
utils.CheckError("eb.Execute()", t, nil, err)
}
45 changes: 45 additions & 0 deletions internal/layer/lv_activate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package layer

import (
"testing"

"github.com/reecetech/ebs-bootstrap/internal/config"
"github.com/reecetech/ebs-bootstrap/internal/utils"
)

func TestActivateLogicalVolumeLayerShouldProcess(t *testing.T) {
subtests := []struct {
Name string
Config *config.Config
ExpectedValue bool
}{
{
Name: "At Least Once Device Has Lvm Specified",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdb": {
Lvm: "lvm-id",
},
"/dev/xvdf": {},
},
},
ExpectedValue: true,
},
{
Name: "No Device Has Lvm Specified",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdf": {},
},
},
ExpectedValue: false,
},
}
for _, subtest := range subtests {
t.Run(subtest.Name, func(t *testing.T) {
alvl := NewActivateLogicalVolumeLayer(nil)
output := alvl.ShouldProcess(subtest.Config)
utils.CheckOutput("alvl.ShouldProcess()", t, subtest.ExpectedValue, output)
})
}
}
59 changes: 59 additions & 0 deletions internal/layer/lv_resize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package layer

import (
"testing"

"github.com/reecetech/ebs-bootstrap/internal/config"
"github.com/reecetech/ebs-bootstrap/internal/utils"
)

func TestResizeLogicalVolumeLayerShouldProcess(t *testing.T) {
subtests := []struct {
Name string
Config *config.Config
ExpectedValue bool
}{
{
Name: "At Least Once Device Has Lvm Specified and Resize Enabled",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdb": {
Lvm: "lvm-id",
Options: config.Options{
Resize: true,
},
},
"/dev/xvdf": {},
},
},
ExpectedValue: true,
},
{
Name: "Device Has Lvm Specified, but Resize Disabled",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdb": {
Lvm: "lvm-id",
},
},
},
ExpectedValue: false,
},
{
Name: "No Device Has Lvm Specified",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdf": {},
},
},
ExpectedValue: false,
},
}
for _, subtest := range subtests {
t.Run(subtest.Name, func(t *testing.T) {
rlvl := NewResizeLogicalVolumeLayer(nil)
output := rlvl.ShouldProcess(subtest.Config)
utils.CheckOutput("rlvl.ShouldProcess()", t, subtest.ExpectedValue, output)
})
}
}
45 changes: 45 additions & 0 deletions internal/layer/lv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package layer

import (
"testing"

"github.com/reecetech/ebs-bootstrap/internal/config"
"github.com/reecetech/ebs-bootstrap/internal/utils"
)

func TestCreateLogicalVolumeLayerShouldProcess(t *testing.T) {
subtests := []struct {
Name string
Config *config.Config
ExpectedValue bool
}{
{
Name: "At Least Once Device Has Lvm Specified",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdb": {
Lvm: "lvm-id",
},
"/dev/xvdf": {},
},
},
ExpectedValue: true,
},
{
Name: "No Device Has Lvm Specified",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdf": {},
},
},
ExpectedValue: false,
},
}
for _, subtest := range subtests {
t.Run(subtest.Name, func(t *testing.T) {
clvl := NewCreateLogicalVolumeLayer(nil)
output := clvl.ShouldProcess(subtest.Config)
utils.CheckOutput("clvl.ShouldProcess()", t, subtest.ExpectedValue, output)
})
}
}
37 changes: 37 additions & 0 deletions internal/layer/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,3 +565,40 @@ func TestMountDeviceLayerValidate(t *testing.T) {
})
}
}

func TestMountDeviceLayerShouldProcess(t *testing.T) {
subtests := []struct {
Name string
Config *config.Config
ExpectedOutput bool
}{
{
Name: "At Least Once Device Has Mount Point Specified",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdb": {},
"/dev/xvdf": {
MountPoint: "/mnt/foo",
},
},
},
ExpectedOutput: true,
},
{
Name: "No Device Has Mount Point Specified",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdf": {},
},
},
ExpectedOutput: false,
},
}
for _, subtest := range subtests {
t.Run(subtest.Name, func(t *testing.T) {
mdl := NewMountDeviceLayer(nil, nil)
output := mdl.ShouldProcess(subtest.Config)
utils.CheckOutput("mdl.ShouldProcess()", t, subtest.ExpectedOutput, output)
})
}
}
Loading
Loading