package main import ( "secretsmanager/config" "testing" "time" "secretsmanager/secretsmanager" kv "github.com/hashicorp/vault-plugin-secrets-kv" "github.com/hashicorp/vault/api" "github.com/hashicorp/vault/http" "github.com/hashicorp/vault/sdk/logical" "github.com/hashicorp/vault/vault" ) // CreateTestVault spins up a Vault server and tests against // an actual Vault instance. Currently, this is only set up for kv v2 func createTestVault(t testing.TB) *vault.TestCluster { t.Helper() // CoreConfig parameterizes the Vault core config coreConfig := &vault.CoreConfig{ LogicalBackends: map[string]logical.Factory{ "kv": kv.Factory, }, } cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{ // Handler returns an http.Handler for the API. This can be used on // its own to mount the Vault API within another web server. HandlerFunc: http.Handler, }) cluster.Start() // Create KV V2 mount on the path /test // It starts in cluster mode, so you just pick one of the three clients // In this case, Cores[0] is just always picking the first one if err := cluster.Cores[0].Client.Sys().Mount("test", &api.MountInput{ Type: "kv", Options: map[string]string{ "version": "2", }, }); err != nil { t.Fatal(err) } return cluster } func Test_exportToPipeline(t *testing.T) { type args struct { key string value interface{} } tests := []struct { name string args args wantErr bool }{ { name: "happy path", args: args{ key: "TEST_KEY", value: "TEST_VALUE", }, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { cluster := createTestVault(t) defer cluster.Cleanup() vaultClient := cluster.Cores[0].Client // only need a client from 1 of 3 clusters _, err := secretsmanager.New(config.Config{ VaultAddr: vaultClient.Address(), VaultUsername: "", VaultPassword: "", VaultSecretsManagerID: "", VaultPath: "", Debug: false, }) if err != nil { t.Fatal(err) } // time buffer required after new mount // https://github.com/hashicorp/terraform-provider-vault/issues/677#issuecomment-609116328 // Code 400: Errors: Upgrading from non-versioned to versioned data. This backend will be unavailable for a brief period and will resume service shortly. time.Sleep(2 * time.Second) if err := exportToPipeline(tt.args.key, tt.args.value); (err != nil) != tt.wantErr { t.Errorf("exportToPipeline() error = %v, wantErr %v", err, tt.wantErr) } }) } }