0
0
mirror of https://github.com/thegeeklab/wp-opentofu.git synced 2024-11-21 14:20:40 +00:00

refactoring plugin_test.go

This commit is contained in:
Neemias Junior 2020-02-09 23:55:31 +01:00 committed by Jacob McCann
parent 731f1c021f
commit 0fcb773369
No known key found for this signature in database
GPG Key ID: B5A476DE32B9AE72

View File

@ -1,27 +1,13 @@
package main package main
import ( import (
"log"
"os" "os"
"os/exec" "os/exec"
"testing" "testing"
. "github.com/franela/goblin" . "github.com/franela/goblin"
"github.com/joho/godotenv"
) )
func loadEnv(keyValue string) {
const FileName = "./.env_example"
env, err := godotenv.Unmarshal(keyValue)
err = godotenv.Write(env, FileName)
if err != nil {
log.Fatal(err)
}
defer os.Remove(FileName)
_ = godotenv.Load(FileName)
}
func TestPlugin(t *testing.T) { func TestPlugin(t *testing.T) {
g := Goblin(t) g := Goblin(t)
@ -42,57 +28,38 @@ func TestPlugin(t *testing.T) {
}) })
g.Describe("credsSet", func() { g.Describe("credsSet", func() {
var awsAccessKeyID string
var awsSecretAccessKey string
var awsSessionToken string
g.Before(func() {
awsAccessKeyID = os.Getenv("AWS_ACCESS_KEY_ID")
awsSecretAccessKey = os.Getenv("AWS_SECRET_ACCESS_KEY")
awsSessionToken = os.Getenv("AWS_SESSION_TOKEN")
})
// Restoring all credentials after running the credsSet test
g.After(func() {
os.Setenv("AWS_ACCESS_KEY_ID", awsAccessKeyID)
os.Setenv("AWS_SECRET_ACCESS_KEY", awsSecretAccessKey)
os.Setenv("AWS_SESSION_TOKEN", awsSessionToken)
})
type args struct {
config string
}
tests := []struct { tests := []struct {
name string name string
args args args map[string]string
want bool want bool
}{ }{
{ {
"Should return true when all credentials were set", "Should return true when all credentials were set",
args{config: "AWS_ACCESS_KEY_ID=access_key_id1\nAWS_SECRET_ACCESS_KEY=secret_access_key1\nAWS_SESSION_TOKEN=session_token1"}, map[string]string{"AWS_ACCESS_KEY_ID": "x", "AWS_SECRET_ACCESS_KEY": "x", "AWS_SESSION_TOKEN": "x"},
true, true,
}, },
{ {
"Should return false when access key id is missing", "Should return false when access key id is missing",
args{config: "AWS_SECRET_ACCESS_KEY=secret_access_key2\nAWS_SESSION_TOKEN=session_token2"}, map[string]string{"AWS_SECRET_ACCESS_KEY": "x", "AWS_SESSION_TOKEN": "x"},
false, false,
}, },
{ {
"Should return false when secret access key is missing", "Should return false when secret access key is missing",
args{config: "AWS_ACCESS_KEY_ID=access_key_id3\nAWS_SESSION_TOKEN=session_token3"}, map[string]string{"AWS_ACCESS_KEY_ID": "x", "AWS_SESSION_TOKEN": "x"},
false, false,
}, },
{ {
"Should return false when session token is missing", "Should return false when session token is missing",
args{config: "AWS_ACCESS_KEY_ID=access_key_id4\nAWS_SECRET_ACCESS_KEY=secret_access_key4"}, map[string]string{"AWS_ACCESS_KEY_ID": "x", "AWS_SECRET_ACCESS_KEY": "x"},
false, false,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
g.It(tt.name, func() { g.It(tt.name, func() {
loadEnv(tt.args.config) for k, v := range tt.args {
os.Setenv(k, v)
}
g.Assert(credsSet()).Equal(tt.want) g.Assert(credsSet()).Equal(tt.want)
}) })
} }