Skip to content

Commit

Permalink
add platform conversion checks and update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Dec 11, 2024
1 parent ed31c21 commit d37d494
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
11 changes: 11 additions & 0 deletions util/backup_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ func hclDecodeConfig(filePath string) (*BackupConfig, error) {
return nil, errors.New("cloud_config block absent")
}

// validate platform
platform := backupConfig.CloudConfig.Platform
if platform != AWS && platform != GCP {
log.Printf("PLATFORM %s is not supported", platform)
return nil, errors.New("unsupported platform")
}

return &backupConfig, nil
}

Expand Down Expand Up @@ -101,6 +108,10 @@ func envImportConfig() (*BackupConfig, error) {
log.Print("CONTAINER and PLATFORM are both required input values, and one or both was unspecified as an environment variable")
return nil, errors.New("environment variable absent")
}
if platform != AWS && platform != GCP {
log.Printf("PLATFORM %s is not supported", platform)
return nil, errors.New("unsupported platform")
}

return &BackupConfig{
CloudConfig: &CloudConfig{
Expand Down
25 changes: 15 additions & 10 deletions util/backup_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestHclDecodeConfig(test *testing.T) {
}
expectedCloudConfig := CloudConfig{
Container: Container,
Platform: "aws",
Platform: AWS,
Prefix: Prefix,
}

Expand Down Expand Up @@ -55,18 +55,18 @@ func TestHclDecodeConfig(test *testing.T) {
func TestOSImportConfig(test *testing.T) {
// source of truth for values
const (
platform string = "gcp"
addr string = "https://127.0.0.1:8234"
skipVerify string = "false"
authEngine string = "token"
token string = "abcdefg"
awsMount string = "gcp"
awsRole string = "my_role"
snapshotPath string = "/tmp/my_vault.backup"
platform platform = GCP
addr string = "https://127.0.0.1:8234"
skipVerify string = "false"
authEngine string = "token"
token string = "abcdefg"
awsMount string = "gcp"
awsRole string = "my_role"
snapshotPath string = "/tmp/my_vault.backup"
)

os.Setenv("CONTAINER", Container)
os.Setenv("PLATFORM", platform)
os.Setenv("PLATFORM", string(platform))
os.Setenv("PREFIX", Prefix)
os.Setenv("VAULT_ADDR", addr)
os.Setenv("VAULT_SKIP_VERIFY", skipVerify)
Expand Down Expand Up @@ -109,6 +109,11 @@ func TestOSImportConfig(test *testing.T) {
test.Errorf("actual snapshot cleanup: %t", config.SnapshotCleanup)
}

os.Setenv("PLATFORM", "foo")
if _, err := envImportConfig(); err == nil || err.Error() != "unsupported platform" {
test.Errorf("expected error: unsupported platform, actual: %s", err)
}

os.Unsetenv("CONTAINER")
if _, err = envImportConfig(); err == nil || err.Error() != "environment variable absent" {
test.Errorf("expected error: environment variable absent, actual: %s", err)
Expand Down

0 comments on commit d37d494

Please sign in to comment.