Skip to content

Commit

Permalink
(feat): Ensuring that all tests are run using t.Run()
Browse files Browse the repository at this point in the history
  • Loading branch information
lasith-kg committed Dec 29, 2023
1 parent 26831d3 commit a771318
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 112 deletions.
20 changes: 11 additions & 9 deletions internal/action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,16 @@ func TestDefaultActionExecutor(t *testing.T) {
},
}
for _, subtest := range subtests {
dae := &DefaultActionExecutor{
read: subtest.Read,
}
a := &MockAction{
execute: func() error { return subtest.Error },
mode: subtest.Mode,
}
err := dae.Execute([]Action{a})
utils.CheckError("dae.Execute()", t, subtest.ExpectedErr, err)
t.Run(subtest.Name, func(t *testing.T) {
dae := &DefaultActionExecutor{
read: subtest.Read,
}
a := &MockAction{
execute: func() error { return subtest.Error },
mode: subtest.Mode,
}
err := dae.Execute([]Action{a})
utils.CheckError("dae.Execute()", t, subtest.ExpectedErr, err)
})
}
}
15 changes: 8 additions & 7 deletions internal/config/modifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,15 @@ func TestAwsNitroNVMeModifier(t *testing.T) {
ExpectedErr: fmt.Errorf("🔴 lsblk: Could not retrieve block devices"),
},
}

for _, subtest := range subtests {
ds.StubGetBlockDevices = subtest.GetBlockDevices
ns.StubGetBlockDeviceMapping = subtest.GetBlockDeviceMapping
t.Run(subtest.Name, func(t *testing.T) {
ds.StubGetBlockDevices = subtest.GetBlockDevices
ns.StubGetBlockDeviceMapping = subtest.GetBlockDeviceMapping

andm := NewAwsNVMeDriverModifier(ns, ds)
err := andm.Modify(subtest.Config)
utils.CheckError("andm.Modify()", t, subtest.ExpectedErr, err)
utils.CheckOutput("andm.Modify()", t, subtest.ExpectedOutput, subtest.Config, cmp.AllowUnexported(Config{}))
andm := NewAwsNVMeDriverModifier(ns, ds)
err := andm.Modify(subtest.Config)
utils.CheckError("andm.Modify()", t, subtest.ExpectedErr, err)
utils.CheckOutput("andm.Modify()", t, subtest.ExpectedOutput, subtest.Config, cmp.AllowUnexported(Config{}))
})
}
}
20 changes: 12 additions & 8 deletions internal/config/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,14 @@ func TestOwnerValidator(t *testing.T) {
},
}
for _, subtest := range subtests {
os.StubGetUser = subtest.GetUser
os.StubGetGroup = subtest.GetGroup
t.Run(subtest.Name, func(t *testing.T) {
os.StubGetUser = subtest.GetUser
os.StubGetGroup = subtest.GetGroup

ov := NewOwnerValidator(os)
err := ov.Validate(subtest.Config)
utils.CheckError("ov.Validate()", t, subtest.ExpectedErr, err)
ov := NewOwnerValidator(os)
err := ov.Validate(subtest.Config)
utils.CheckError("ov.Validate()", t, subtest.ExpectedErr, err)
})
}
}

Expand Down Expand Up @@ -454,8 +456,10 @@ func TestResizeThresholdValidator(t *testing.T) {
},
}
for _, subtest := range subtests {
rtv := NewResizeThresholdValidator()
err := rtv.Validate(subtest.Config)
utils.CheckError("rtv.Validate()", t, subtest.ExpectedErr, err)
t.Run(subtest.Name, func(t *testing.T) {
rtv := NewResizeThresholdValidator()
err := rtv.Validate(subtest.Config)
utils.CheckError("rtv.Validate()", t, subtest.ExpectedErr, err)
})
}
}
9 changes: 5 additions & 4 deletions internal/model/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

func TestParseMode(t *testing.T) {
subtests := []struct {
Name string
Mode string
ExpectedOutput Mode
ExpectedError error
Expand Down Expand Up @@ -41,8 +40,10 @@ func TestParseMode(t *testing.T) {
},
}
for _, subtest := range subtests {
m, err := ParseMode(subtest.Mode)
utils.CheckError("ParseMode()", t, subtest.ExpectedError, err)
utils.CheckOutput("ParseMode()", t, subtest.ExpectedOutput, m)
t.Run(subtest.Mode, func(t *testing.T) {
m, err := ParseMode(subtest.Mode)
utils.CheckError("ParseMode()", t, subtest.ExpectedError, err)
utils.CheckOutput("ParseMode()", t, subtest.ExpectedOutput, m)
})
}
}
18 changes: 10 additions & 8 deletions internal/model/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,24 @@ func TestShouldResize(t *testing.T) {
ResizeThreshold: 85,
FileSystemSize: 90,
BlockDeviceSize: 100,
ExpectedOutput: true,
ExpectedOutput: false,
},
{
Name: "Resize=❌",
Name: "Resize=❌ <alt>",
ResizeThreshold: 100,
FileSystemSize: 100,
BlockDeviceSize: 100,
ExpectedOutput: false,
},
}
for _, subtest := range subtests {
bdm := &BlockDeviceMetrics{
subtest.FileSystemSize,
subtest.BlockDeviceSize,
}
sr := bdm.ShouldResize(subtest.ResizeThreshold)
utils.CheckOutput("bdm.ShouldResize()", t, subtest.ExpectedOutput, sr)
t.Run(subtest.Name, func(t *testing.T) {
bdm := &BlockDeviceMetrics{
subtest.FileSystemSize,
subtest.BlockDeviceSize,
}
sr := bdm.ShouldResize(subtest.ResizeThreshold)
utils.CheckOutput("bdm.ShouldResize()", t, subtest.ExpectedOutput, sr)
})
}
}
14 changes: 9 additions & 5 deletions internal/model/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ func TestUnmarshalYAML(t *testing.T) {
},
}
for _, subtest := range subtests {
var fp FilePermissions
err := yaml.Unmarshal(subtest.Yaml, &fp)
utils.CheckError("yaml.Unmarshal()", t, subtest.ExpectedErr, err)
utils.CheckOutput("yaml.Unmarshal()", t, subtest.ExpectedOutput, fp)
t.Run(subtest.Name, func(t *testing.T) {
var fp FilePermissions
err := yaml.Unmarshal(subtest.Yaml, &fp)
utils.CheckError("yaml.Unmarshal()", t, subtest.ExpectedErr, err)
utils.CheckOutput("yaml.Unmarshal()", t, subtest.ExpectedOutput, fp)
})
}
}

Expand All @@ -68,6 +70,8 @@ func TestPerm(t *testing.T) {
},
}
for _, subtest := range subtests {
utils.CheckOutput("FilePermission.Perm()", t, subtest.ExpectedOutput, subtest.FilePermission.Perm())
t.Run(subtest.Name, func(t *testing.T) {
utils.CheckOutput("FilePermission.Perm()", t, subtest.ExpectedOutput, subtest.FilePermission.Perm())
})
}
}
8 changes: 5 additions & 3 deletions internal/model/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ func TestParseFileSystem(t *testing.T) {
},
}
for _, subtest := range subtests {
fs, err := ParseFileSystem(subtest.FileSystem)
utils.CheckError("ParseFileSystem()", t, subtest.ExpectedError, err)
utils.CheckOutput("ParseFileSystem()", t, subtest.ExpectedOutput, fs)
t.Run(subtest.FileSystem, func(t *testing.T) {
fs, err := ParseFileSystem(subtest.FileSystem)
utils.CheckError("ParseFileSystem()", t, subtest.ExpectedError, err)
utils.CheckOutput("ParseFileSystem()", t, subtest.ExpectedOutput, fs)
})
}
}
56 changes: 33 additions & 23 deletions internal/service/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ func TestGetSize(t *testing.T) {
},
}
for _, subtest := range subtests {
mrf := utils.NewMockRunnerFactory(subtest.RunnerBinary, subtest.RunnerArgs, subtest.RunnerOutput, subtest.RunnerError)
lds := NewLinuxDeviceService(mrf)
size, err := lds.GetSize(subtest.Device)
utils.CheckError("lds.GetSize()", t, subtest.ExpectedError, err)
utils.CheckOutput("lds.GetSize()", t, subtest.ExpectedOutput, size)
t.Run(subtest.Name, func(t *testing.T) {
mrf := utils.NewMockRunnerFactory(subtest.RunnerBinary, subtest.RunnerArgs, subtest.RunnerOutput, subtest.RunnerError)
lds := NewLinuxDeviceService(mrf)
size, err := lds.GetSize(subtest.Device)
utils.CheckError("lds.GetSize()", t, subtest.ExpectedError, err)
utils.CheckOutput("lds.GetSize()", t, subtest.ExpectedOutput, size)
})
}
}

Expand Down Expand Up @@ -101,11 +103,13 @@ func TestGetBlockDevices(t *testing.T) {
},
}
for _, subtest := range subtests {
mrf := utils.NewMockRunnerFactory(subtest.RunnerBinary, subtest.RunnerArgs, subtest.RunnerOutput, subtest.RunnerError)
lds := NewLinuxDeviceService(mrf)
size, err := lds.GetBlockDevices()
utils.CheckErrorGlob("lds.GetBlockDevices()", t, subtest.ExpectedError, err)
utils.CheckOutput("lds.GetBlockDevices()", t, subtest.ExpectedOutput, size)
t.Run(subtest.Name, func(t *testing.T) {
mrf := utils.NewMockRunnerFactory(subtest.RunnerBinary, subtest.RunnerArgs, subtest.RunnerOutput, subtest.RunnerError)
lds := NewLinuxDeviceService(mrf)
size, err := lds.GetBlockDevices()
utils.CheckErrorGlob("lds.GetBlockDevices()", t, subtest.ExpectedError, err)
utils.CheckOutput("lds.GetBlockDevices()", t, subtest.ExpectedOutput, size)
})
}
}

Expand Down Expand Up @@ -230,11 +234,13 @@ func TestGetBlockDevice(t *testing.T) {
},
}
for _, subtest := range subtests {
mrf := utils.NewMockRunnerFactory(subtest.RunnerBinary, subtest.RunnerArgs, subtest.RunnerOutput, subtest.RunnerError)
lds := NewLinuxDeviceService(mrf)
size, err := lds.GetBlockDevice(subtest.Device)
utils.CheckErrorGlob("lds.GetBlockDevice()", t, subtest.ExpectedError, err)
utils.CheckOutput("lds.GetBlockDevice()", t, subtest.ExpectedOutput, size)
t.Run(subtest.Name, func(t *testing.T) {
mrf := utils.NewMockRunnerFactory(subtest.RunnerBinary, subtest.RunnerArgs, subtest.RunnerOutput, subtest.RunnerError)
lds := NewLinuxDeviceService(mrf)
size, err := lds.GetBlockDevice(subtest.Device)
utils.CheckErrorGlob("lds.GetBlockDevice()", t, subtest.ExpectedError, err)
utils.CheckOutput("lds.GetBlockDevice()", t, subtest.ExpectedOutput, size)
})
}
}

Expand Down Expand Up @@ -277,10 +283,12 @@ func TestMount(t *testing.T) {
},
}
for _, subtest := range subtests {
mrf := utils.NewMockRunnerFactory(subtest.RunnerBinary, subtest.RunnerArgs, subtest.RunnerOutput, subtest.RunnerError)
lds := NewLinuxDeviceService(mrf)
err := lds.Mount(subtest.Source, subtest.Target, subtest.Fs, subtest.Options)
utils.CheckErrorGlob("lds.Mount()", t, subtest.ExpectedError, err)
t.Run(subtest.Name, func(t *testing.T) {
mrf := utils.NewMockRunnerFactory(subtest.RunnerBinary, subtest.RunnerArgs, subtest.RunnerOutput, subtest.RunnerError)
lds := NewLinuxDeviceService(mrf)
err := lds.Mount(subtest.Source, subtest.Target, subtest.Fs, subtest.Options)
utils.CheckErrorGlob("lds.Mount()", t, subtest.ExpectedError, err)
})
}
}

Expand Down Expand Up @@ -317,9 +325,11 @@ func TestUmount(t *testing.T) {
},
}
for _, subtest := range subtests {
mrf := utils.NewMockRunnerFactory(subtest.RunnerBinary, subtest.RunnerArgs, subtest.RunnerOutput, subtest.RunnerError)
lds := NewLinuxDeviceService(mrf)
err := lds.Umount(subtest.Source, subtest.Target)
utils.CheckErrorGlob("lds.Umount()", t, subtest.ExpectedError, err)
t.Run(subtest.Name, func(t *testing.T) {
mrf := utils.NewMockRunnerFactory(subtest.RunnerBinary, subtest.RunnerArgs, subtest.RunnerOutput, subtest.RunnerError)
lds := NewLinuxDeviceService(mrf)
err := lds.Umount(subtest.Source, subtest.Target)
utils.CheckErrorGlob("lds.Umount()", t, subtest.ExpectedError, err)
})
}
}
8 changes: 5 additions & 3 deletions internal/service/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ func TestGetFile(t *testing.T) {
},
}
for _, subtest := range subtests {
output, err := ufs.GetFile(subtest.Path)
utils.ExpectErr("ufs.GetFile()", t, subtest.ShouldExpectErr, err)
utils.CheckOutput("ufs.GetFile()", t, subtest.ExpectedOutput, output)
t.Run(subtest.Name, func(t *testing.T) {
output, err := ufs.GetFile(subtest.Path)
utils.ExpectErr("ufs.GetFile()", t, subtest.ShouldExpectErr, err)
utils.CheckOutput("ufs.GetFile()", t, subtest.ExpectedOutput, output)
})
}
}

Expand Down
Loading

0 comments on commit a771318

Please sign in to comment.