github.com/jdolitsky/cnab-go@v0.7.1-beta1/credentials/credentialset_test.go (about) 1 package credentials 2 3 import ( 4 "fmt" 5 "os" 6 "runtime" 7 "strings" 8 "testing" 9 10 "github.com/deislabs/cnab-go/bundle" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestCredentialSet(t *testing.T) { 16 is := assert.New(t) 17 if err := os.Setenv("TEST_USE_VAR", "kakapu"); err != nil { 18 t.Fatal("could not setup env") 19 } 20 defer os.Unsetenv("TEST_USE_VAR") 21 22 goos := "unix" 23 if runtime.GOOS == "windows" { 24 goos = runtime.GOOS 25 } 26 credset, err := Load(fmt.Sprintf("testdata/staging-%s.yaml", goos)) 27 is.NoError(err) 28 29 results, err := credset.Resolve() 30 if err != nil { 31 t.Fatal(err) 32 } 33 count := 5 34 is.Len(results, count, "Expected %d credentials", count) 35 36 for _, tt := range []struct { 37 name string 38 key string 39 expect string 40 path string 41 }{ 42 {name: "run_program", key: "TEST_RUN_PROGRAM", expect: "wildebeest"}, 43 {name: "use_var", key: "TEST_USE_VAR", expect: "kakapu"}, 44 {name: "read_file", key: "TEST_READ_FILE", expect: "serval"}, 45 {name: "fallthrough", key: "TEST_FALLTHROUGH", expect: "quokka", path: "/animals/quokka.txt"}, 46 {name: "plain_value", key: "TEST_PLAIN_VALUE", expect: "cassowary"}, 47 } { 48 dest, ok := results[tt.name] 49 is.True(ok) 50 is.Equal(tt.expect, strings.TrimSpace(dest)) 51 } 52 } 53 54 func TestCredentialSet_Expand(t *testing.T) { 55 56 b := &bundle.Bundle{ 57 Name: "knapsack", 58 Credentials: map[string]bundle.Credential{ 59 "first": { 60 Location: bundle.Location{ 61 EnvironmentVariable: "FIRST_VAR", 62 }, 63 }, 64 "second": { 65 Location: bundle.Location{ 66 Path: "/second/path", 67 }, 68 }, 69 "third": { 70 Location: bundle.Location{ 71 EnvironmentVariable: "/THIRD_VAR", 72 Path: "/third/path", 73 }, 74 }, 75 }, 76 } 77 cs := Set{ 78 "first": "first", 79 "second": "second", 80 "third": "third", 81 } 82 83 env, path, err := cs.Expand(b, false) 84 is := assert.New(t) 85 is.NoError(err) 86 for k, v := range b.Credentials { 87 if v.EnvironmentVariable != "" { 88 is.Equal(env[v.EnvironmentVariable], cs[k]) 89 } 90 if v.Path != "" { 91 is.Equal(path[v.Path], cs[k]) 92 } 93 } 94 } 95 96 func TestCredentialSet_Merge(t *testing.T) { 97 cs := Set{ 98 "first": "first", 99 "second": "second", 100 "third": "third", 101 } 102 103 is := assert.New(t) 104 105 err := cs.Merge(Set{}) 106 is.NoError(err) 107 is.Len(cs, 3) 108 is.NotContains(cs, "fourth") 109 110 err = cs.Merge(Set{"fourth": "fourth"}) 111 is.NoError(err) 112 is.Len(cs, 4) 113 is.Contains(cs, "fourth") 114 115 err = cs.Merge(Set{"second": "bis"}) 116 is.EqualError(err, `ambiguous credential resolution: "second" is already present in base credential sets, cannot merge`) 117 118 } 119 120 func TestCredentialSetMissingRequiredCred(t *testing.T) { 121 b := &bundle.Bundle{ 122 Name: "knapsack", 123 Credentials: map[string]bundle.Credential{ 124 "first": { 125 Location: bundle.Location{ 126 EnvironmentVariable: "FIRST_VAR", 127 }, 128 Required: true, 129 }, 130 }, 131 } 132 cs := Set{} 133 _, _, err := cs.Expand(b, false) 134 assert.EqualError(t, err, `credential "first" is missing from the user-supplied credentials`) 135 _, _, err = cs.Expand(b, true) 136 assert.NoError(t, err) 137 } 138 139 func TestCredentialSetMissingOptionalCred(t *testing.T) { 140 b := &bundle.Bundle{ 141 Name: "knapsack", 142 Credentials: map[string]bundle.Credential{ 143 "first": { 144 Location: bundle.Location{ 145 EnvironmentVariable: "FIRST_VAR", 146 }, 147 }, 148 }, 149 } 150 cs := Set{} 151 _, _, err := cs.Expand(b, false) 152 assert.NoError(t, err) 153 _, _, err = cs.Expand(b, true) 154 assert.NoError(t, err) 155 }