-
Notifications
You must be signed in to change notification settings - Fork 17
/
package_test.go
52 lines (40 loc) · 1.22 KB
/
package_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"bytes"
"os"
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
)
func RunCommand(name string, arg ...string) (bytes.Buffer, bytes.Buffer, error) {
cmd := exec.Command(name, arg...)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
return stdout, stderr, err
}
const PackageName = "summon-aws-secrets"
func TestPackage(t *testing.T) {
Path := os.Getenv("PATH")
t.Run("Compiled summon-aws-secrets package without params", func(t *testing.T) {
e := ClearEnv()
defer e.RestoreEnv()
os.Setenv("PATH", Path)
_, stderr, err := RunCommand(PackageName)
assert.Error(t, err)
assert.Contains(t, stderr.String(), "A variable ID or version flag must be given as the first and only argument!")
})
}
func Test_getValueByKey(t *testing.T) {
t.Run("Valid JSON format stored secret", func(t *testing.T) {
secret := `{ "username": "USERNAME", "password": "PASSWORD", "port": 8000 }`
stdout, err := getValueByKey("username", []byte(secret))
assert.NoError(t, err)
assert.Equal(t, "USERNAME", string(stdout))
stdout, err = getValueByKey("port", []byte(secret))
assert.NoError(t, err)
assert.Equal(t, "8000", string(stdout))
})
}