Skip to content

Commit

Permalink
add annotation path test and uncomment tests
Browse files Browse the repository at this point in the history
Signed-off-by: Omri Shiv <327609+OmriShiv@users.noreply.github.com>
  • Loading branch information
omrishiv committed May 18, 2023
1 parent 1afb288 commit 611b4be
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 85 deletions.
170 changes: 85 additions & 85 deletions cmd/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,91 +25,91 @@ func TestMain(t *testing.T) {
os.Setenv("AVP_SECRET_ID", secretid)
os.Setenv("AVP_ROLE_ID", roleid)
os.Setenv("VAULT_SKIP_VERIFY", "true")
//
//t.Run("will throw an error expecting arguments", func(t *testing.T) {
// args := []string{}
// cmd := NewGenerateCommand()
//
// c := bytes.NewBufferString("")
// cmd.SetArgs(args)
// cmd.SetErr(c)
// cmd.SetOut(bytes.NewBufferString(""))
// cmd.Execute()
// out, err := ioutil.ReadAll(c) // Read buffer to bytes
// if err != nil {
// t.Fatal(err)
// }
//
// expected := "<path> argument required to generate manifests"
// if !strings.Contains(string(out), expected) {
// t.Fatalf("expected to contain: %s but got %s", expected, out)
// }
//})
//
//t.Run("will return that couldn't find yamls", func(t *testing.T) {
// args := []string{"./fixtures/input/empty/"}
// cmd := NewGenerateCommand()
//
// b := bytes.NewBufferString("")
// cmd.SetArgs(args)
// cmd.SetErr(b)
// cmd.SetOut(bytes.NewBufferString(""))
// cmd.Execute()
// out, err := ioutil.ReadAll(b) // Read buffer to bytes
// if err != nil {
// t.Fatal(err)
// }
//
// expected := "no YAML or JSON files were found in ./fixtures/input/empty/"
// if !strings.Contains(string(out), expected) {
// t.Fatalf("expected to contain: %s but got %s", expected, out)
// }
//})
//
//t.Run("returns error for empty manifests", func(t *testing.T) {
// // From path
// args := []string{"../fixtures/input/empty/file.yaml"}
// cmd := NewGenerateCommand()
//
// b := bytes.NewBufferString("")
// cmd.SetArgs(args)
// cmd.SetErr(b)
// cmd.SetOut(bytes.NewBufferString(""))
// cmd.Execute()
// out, err := ioutil.ReadAll(b) // Read buffer to bytes
// if err != nil {
// t.Fatal(err)
// }
//
// expected := ""
// if !strings.Contains(string(out), expected) {
// t.Fatalf("expected to contain: %s but got %s", expected, out)
// }
//
// // From stdin
// args = []string{"-"}
// stdin := bytes.NewBufferString("")
// inputBuf, err := ioutil.ReadFile("../fixtures/input/empty/file.yaml")
// if err != nil {
// t.Fatal(err)
// }
// stdin.Write(inputBuf)
//
// b = bytes.NewBufferString("")
// cmd.SetIn(stdin)
// cmd.SetArgs(args)
// cmd.SetErr(b)
// cmd.SetOut(bytes.NewBufferString(""))
// cmd.Execute()
// out, err = ioutil.ReadAll(b) // Read buffer to bytes
// if err != nil {
// t.Fatal(err)
// }
//
// if !strings.Contains(string(out), expected) {
// t.Fatalf("expected to contain: %s but got %s", expected, out)
// }
//})

t.Run("will throw an error expecting arguments", func(t *testing.T) {
args := []string{}
cmd := NewGenerateCommand()

c := bytes.NewBufferString("")
cmd.SetArgs(args)
cmd.SetErr(c)
cmd.SetOut(bytes.NewBufferString(""))
cmd.Execute()
out, err := ioutil.ReadAll(c) // Read buffer to bytes
if err != nil {
t.Fatal(err)
}

expected := "<path> argument required to generate manifests"
if !strings.Contains(string(out), expected) {
t.Fatalf("expected to contain: %s but got %s", expected, out)
}
})

t.Run("will return that couldn't find yamls", func(t *testing.T) {
args := []string{"./fixtures/input/empty/"}
cmd := NewGenerateCommand()

b := bytes.NewBufferString("")
cmd.SetArgs(args)
cmd.SetErr(b)
cmd.SetOut(bytes.NewBufferString(""))
cmd.Execute()
out, err := ioutil.ReadAll(b) // Read buffer to bytes
if err != nil {
t.Fatal(err)
}

expected := "no YAML or JSON files were found in ./fixtures/input/empty/"
if !strings.Contains(string(out), expected) {
t.Fatalf("expected to contain: %s but got %s", expected, out)
}
})

t.Run("returns error for empty manifests", func(t *testing.T) {
// From path
args := []string{"../fixtures/input/empty/file.yaml"}
cmd := NewGenerateCommand()

b := bytes.NewBufferString("")
cmd.SetArgs(args)
cmd.SetErr(b)
cmd.SetOut(bytes.NewBufferString(""))
cmd.Execute()
out, err := ioutil.ReadAll(b) // Read buffer to bytes
if err != nil {
t.Fatal(err)
}

expected := ""
if !strings.Contains(string(out), expected) {
t.Fatalf("expected to contain: %s but got %s", expected, out)
}

// From stdin
args = []string{"-"}
stdin := bytes.NewBufferString("")
inputBuf, err := ioutil.ReadFile("../fixtures/input/empty/file.yaml")
if err != nil {
t.Fatal(err)
}
stdin.Write(inputBuf)

b = bytes.NewBufferString("")
cmd.SetIn(stdin)
cmd.SetArgs(args)
cmd.SetErr(b)
cmd.SetOut(bytes.NewBufferString(""))
cmd.Execute()
out, err = ioutil.ReadAll(b) // Read buffer to bytes
if err != nil {
t.Fatal(err)
}

if !strings.Contains(string(out), expected) {
t.Fatalf("expected to contain: %s but got %s", expected, out)
}
})

t.Run("will replace templates from local vault", func(t *testing.T) {
args := []string{"../fixtures/input/nonempty"}
Expand Down
13 changes: 13 additions & 0 deletions fixtures/input/nonempty/secret_path_annotation_key_value.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Secret
metadata:
annotations:
avp.kubernetes.io/path: secret/testing
avp.kubernetes.io/kv-version: "1"
name: example-secret-key-value-annotation-path
namespace: <namespace>
type: Opaque
data:
SECRET_KEY: <secret-var-value>
<secret-var-value>: SECRET_VAL
<secret-var-value2>: <secret-var-value>
14 changes: 14 additions & 0 deletions fixtures/output/all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,20 @@ metadata:
type: Opaque
---
apiVersion: v1
data:
SECRET_KEY: dGVzdC1wYXNzd29yZA==
dGVzdC1wYXNzd29yZA==: SECRET_VAL
dGVzdC1wYXNzd29yZDI=: dGVzdC1wYXNzd29yZA==
kind: Secret
metadata:
annotations:
avp.kubernetes.io/kv-version: "1"
avp.kubernetes.io/path: secret/testing
name: example-secret-key-value-annotation-path
namespace: test-namespace
type: Opaque
---
apiVersion: v1
data:
SECRET_VAR: dGVzdC1wYXNzd29yZA==
kind: Secret
Expand Down

0 comments on commit 611b4be

Please sign in to comment.