-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat): Add Config.Modifier test coverage
- Loading branch information
Showing
3 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/reecetech/ebs-bootstrap/internal/service" | ||
"github.com/reecetech/ebs-bootstrap/internal/utils" | ||
) | ||
|
||
func TestAwsNitroNVMeModifier(t *testing.T) { | ||
ns := service.NewMockNVMeService() | ||
ds := service.NewMockDeviceService() | ||
|
||
subtests := []struct { | ||
Name string | ||
Config *Config | ||
GetBlockDevices func() ([]string, error) | ||
GetBlockDeviceMapping func(name string) (string, error) | ||
ExpectedOutput *Config | ||
ExpectedErr error | ||
}{ | ||
{ | ||
Name: "Root Device + EBS Device (Non-Nitro Instance)", | ||
Config: &Config{ | ||
Devices: map[string]Device{ | ||
"/dev/sdb": {}, | ||
}, | ||
}, | ||
GetBlockDevices: func() ([]string, error) { | ||
return []string{"/dev/sda1", "/dev/sdb"}, nil | ||
}, | ||
GetBlockDeviceMapping: func(name string) (string, error) { | ||
return "", fmt.Errorf("🔴 GetBlockDeviceMapping() should not be called") | ||
}, | ||
ExpectedOutput: &Config{ | ||
Devices: map[string]Device{ | ||
"/dev/sdb": {}, | ||
}, | ||
}, | ||
ExpectedErr: nil, | ||
}, | ||
{ | ||
Name: "Root Device + EBS/Instance Store Device (Nitro Instance)", | ||
Config: &Config{ | ||
Devices: map[string]Device{ | ||
"/dev/sdb": {}, | ||
}, | ||
}, | ||
GetBlockDevices: func() ([]string, error) { | ||
return []string{"/dev/nvme0n1", "/dev/nvme1n1"}, nil | ||
}, | ||
GetBlockDeviceMapping: func(name string) (string, error) { | ||
switch name { | ||
case "/dev/nvme0n1": // Root Device | ||
return "/dev/sda1", nil | ||
default: // EBS/Instance Store | ||
return "/dev/sdb", nil | ||
} | ||
}, | ||
// Config will be left unchanged when error is encountered during modification stage | ||
ExpectedOutput: &Config{ | ||
Devices: map[string]Device{ | ||
"/dev/nvme1n1": {}, | ||
}, | ||
}, | ||
ExpectedErr: nil, | ||
}, | ||
{ | ||
Name: "NVMe Device that is not AWS-managed", | ||
Config: &Config{ | ||
Devices: map[string]Device{ | ||
"/dev/sdb": {}, | ||
}, | ||
}, | ||
GetBlockDevices: func() ([]string, error) { | ||
return []string{"/dev/nvme0n1"}, nil | ||
}, | ||
GetBlockDeviceMapping: func(name string) (string, error) { | ||
return "", fmt.Errorf("🔴 %s is not an AWS-managed NVME device", name) | ||
}, | ||
ExpectedOutput: &Config{ | ||
Devices: map[string]Device{ | ||
"/dev/sdb": {}, | ||
}, | ||
}, | ||
ExpectedErr: fmt.Errorf("🔴 /dev/nvme0n1 is not an AWS-managed NVME device"), | ||
}, | ||
{ | ||
Name: "Failure to Retrieve Block Devices", | ||
Config: &Config{ | ||
Devices: map[string]Device{ | ||
"/dev/sdb": {}, | ||
}, | ||
}, | ||
GetBlockDevices: func() ([]string, error) { | ||
return nil, fmt.Errorf("🔴 lsblk: Could not retrieve block devices") | ||
}, | ||
GetBlockDeviceMapping: func(name string) (string, error) { | ||
return "", fmt.Errorf("🔴 GetBlockDeviceMapping() should not be called") | ||
}, | ||
// Config will be left unchanged when error is encountered during modification stage | ||
ExpectedOutput: &Config{ | ||
Devices: map[string]Device{ | ||
"/dev/sdb": {}, | ||
}, | ||
}, | ||
ExpectedErr: fmt.Errorf("🔴 lsblk: Could not retrieve block devices"), | ||
}, | ||
} | ||
|
||
for _, subtest := range subtests { | ||
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{})) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters