Skip to content

Commit

Permalink
cleanup else if and unify cloud config test const
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Nov 13, 2024
1 parent 66dd61d commit b154622
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
4 changes: 3 additions & 1 deletion storage/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"os"
"strings"
"testing"

"github.com/mschuchard/vault-raft-backup/util"
)

func TestSnapshotS3Upload(test *testing.T) {
Expand All @@ -16,7 +18,7 @@ func TestSnapshotS3Upload(test *testing.T) {
defer fooFile.Close()
defer os.Remove("./foo")

if err := snapshotS3Upload(object, fooFile, "prefix-foo"); err == nil || !strings.Contains(err.Error(), "NoCredentialProviders: no valid providers in chain") {
if err := snapshotS3Upload(util.Container, fooFile, "prefix-foo"); err == nil || !strings.Contains(err.Error(), "NoCredentialProviders: no valid providers in chain") {
test.Errorf("expected error (contains): NoCredentialProviders: no valid providers in chain, actual: %v", err)
}
}
4 changes: 3 additions & 1 deletion storage/gcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package storage
import (
"os"
"testing"

"github.com/mschuchard/vault-raft-backup/util"
)

func TestSnapshotCSUpload(test *testing.T) {
Expand All @@ -14,7 +16,7 @@ func TestSnapshotCSUpload(test *testing.T) {
defer fooFile.Close()
defer os.Remove("./foo")

if err := snapshotCSUpload(object, fooFile, "prefix-foo"); err == nil || err.Error() != "dialing: google: could not find default credentials. See https://cloud.google.com/docs/authentication/external/set-up-adc for more information" {
if err := snapshotCSUpload(util.Container, fooFile, "prefix-foo"); err == nil || err.Error() != "dialing: google: could not find default credentials. See https://cloud.google.com/docs/authentication/external/set-up-adc for more information" {
test.Errorf("expected error: dialing: google: could not find default credentials. See https://cloud.google.com/docs/authentication/external/set-up-adc for more information, actual: %v", err)
}
}
2 changes: 0 additions & 2 deletions storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"github.com/mschuchard/vault-raft-backup/util"
)

const object = "my_bucket"

func TestStorageTransfer(test *testing.T) {
if err := StorageTransfer(&util.CloudConfig{}, "/foo", true); err == nil {
test.Error("did not return error as expected for nonexistent snapshot file")
Expand Down
14 changes: 6 additions & 8 deletions util/backup_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func TestHclDecodeConfig(test *testing.T) {
SnapshotPath: "/path/to/vault.bak",
}
expectedCloudConfig := CloudConfig{
Container: "my_bucket",
Container: Container,
Platform: "aws",
Prefix: "prefix",
Prefix: Prefix,
}

if vaultConfig != expectedVaultConfig || cloudConfig != expectedCloudConfig || !config.SnapshotCleanup {
Expand Down Expand Up @@ -55,9 +55,7 @@ func TestHclDecodeConfig(test *testing.T) {
func TestOSImportConfig(test *testing.T) {
// source of truth for values
const (
bucket string = "my_bucket"
platform string = "gcp"
prefix string = "my_prefix"
addr string = "https://127.0.0.1:8234"
skipVerify string = "false"
authEngine string = "token"
Expand All @@ -67,9 +65,9 @@ func TestOSImportConfig(test *testing.T) {
snapshotPath string = "/tmp/my_vault.backup"
)

os.Setenv("CONTAINER", bucket)
os.Setenv("CONTAINER", Container)
os.Setenv("PLATFORM", platform)
os.Setenv("PREFIX", prefix)
os.Setenv("PREFIX", Prefix)
os.Setenv("VAULT_ADDR", addr)
os.Setenv("VAULT_SKIP_VERIFY", skipVerify)
os.Setenv("VAULT_AUTH_ENGINE", authEngine)
Expand All @@ -88,9 +86,9 @@ func TestOSImportConfig(test *testing.T) {
vaultConfig := config.VaultConfig
cloudConfig := config.CloudConfig
expectedCloudConfig := CloudConfig{
Container: bucket,
Container: Container,
Platform: platform,
Prefix: prefix,
Prefix: Prefix,
}
expectedVaultConfig := VaultConfig{
Address: addr,
Expand Down
2 changes: 2 additions & 0 deletions util/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import vault "github.com/hashicorp/vault/api"
const (
VaultAddress = "http://127.0.0.1:8200"
VaultToken = "abcdefghijklmnopqrstuvwxyz09"
Container = "my_bucket"
Prefix = "prefix"
)

var (
Expand Down
8 changes: 3 additions & 5 deletions vault/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,9 @@ func NewVaultConfig(backupVaultConfig *util.VaultConfig) (*vaultConfig, error) {
log.Print("token authentication will be utilized with the Vault client")
engine = vaultToken
}
} else { // validate engine if unspecified
if engine != awsIam && engine != vaultToken {
log.Printf("%v was input as an authentication engine, but only token and aws are supported", engine)
return nil, errors.New("invalid Vault authentication engine")
}
} else if engine != awsIam && engine != vaultToken { // validate engine if unspecified
log.Printf("%v was input as an authentication engine, but only token and aws are supported", engine)
return nil, errors.New("invalid Vault authentication engine")
}

// validate vault token
Expand Down

0 comments on commit b154622

Please sign in to comment.