-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve unit tests for internal/summon/command
- Loading branch information
1 parent
4909a67
commit e9eeaf1
Showing
7 changed files
with
334 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package v1 | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
) | ||
|
||
// MockProvider conforms to, and allows testing of, both the singleValueProvider and | ||
// Provider interfaces | ||
type MockProvider struct { | ||
GetValueCallArgs []string // keeps track of args for each call to getValue | ||
} | ||
|
||
// GetValue returns | ||
// 0. If [id] has prefix 'err_', returns (nil, errors.New(id + "_value")) | ||
// 1. Otherwise, returns ([]byte(id + "_value"), nil) | ||
func (p *MockProvider) GetValue(id string) ([]byte, error) { | ||
p.GetValueCallArgs = append(p.GetValueCallArgs, id) | ||
|
||
if strings.HasPrefix(id, "err_") { | ||
return nil, errors.New(id + "_value") | ||
} | ||
return []byte(id + "_value"), nil | ||
} | ||
|
||
// GetValues sequentially get values for unique ids by calling GetValue | ||
// | ||
// If there exists any id with the prefix 'global_err_', the function will return | ||
// (nil, errors.New(id + "_value")) | ||
func (p *MockProvider) GetValues(ids ...string) ( | ||
map[string]ProviderResponse, | ||
error, | ||
) { | ||
responses := map[string]ProviderResponse{} | ||
|
||
for _, id := range ids { | ||
if _, ok := responses[id]; ok { | ||
continue | ||
} | ||
|
||
if strings.HasPrefix(id, "global_err_") { | ||
return nil, errors.New(id + "_value") | ||
} | ||
|
||
pr := ProviderResponse{} | ||
pr.Value, pr.Error = p.GetValue(id) | ||
|
||
responses[id] = pr | ||
} | ||
|
||
return responses, nil | ||
} | ||
|
||
// GetName simply returns "mock-provider" | ||
func (p *MockProvider) GetName() string { | ||
return "mock-provider" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package v1 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMockProvider_GetName(t *testing.T) { | ||
p := &MockProvider{} | ||
|
||
assert.Equal(t, p.GetName(), "mock-provider") | ||
} | ||
|
||
func TestMockProvider_GetValue(t *testing.T) { | ||
p := &MockProvider{} | ||
|
||
t.Run("Get value", func(t *testing.T) { | ||
val, err := p.GetValue("foo") | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
assert.Equal(t, string(val), "foo_value") | ||
}) | ||
|
||
t.Run("Reports error", func(t *testing.T) { | ||
val, err := p.GetValue("err_foo") | ||
assert.EqualError(t, err, "err_foo_value") | ||
assert.Nil(t, val) | ||
}) | ||
} | ||
|
||
func TestMockProvider_GetValues(t *testing.T) { | ||
p := &MockProvider{} | ||
|
||
t.Run("Sequentially calls GetValue", func(t *testing.T) { | ||
_, _ = p.GetValues("a", "b", "c") | ||
|
||
assert.Equal(t, []string{"a", "b", "c"}, p.GetValueCallArgs) | ||
}) | ||
|
||
t.Run("Returns global error", func(t *testing.T) { | ||
res, err := p.GetValues("a", "b", "global_err_example", "c") | ||
assert.EqualError(t, err, "global_err_example_value") | ||
assert.Nil(t, res) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package command | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cyberark/summon/secretsyml" | ||
"github.com/stretchr/testify/assert" | ||
|
||
plugin_v1 "github.com/cyberark/secretless-broker/internal/plugin/v1" | ||
) | ||
|
||
func Test_resolveSecrets(t *testing.T) { | ||
p := plugin_v1.MockProvider{} | ||
secretsMap := func() secretsyml.SecretsMap { | ||
return secretsyml.SecretsMap{ | ||
"bar": secretsyml.SecretSpec{ | ||
Tags: []secretsyml.YamlTag{secretsyml.Literal}, | ||
Path: "bar_path", | ||
}, | ||
"baz": secretsyml.SecretSpec{ | ||
Tags: []secretsyml.YamlTag{secretsyml.Var}, | ||
Path: "baz_path", | ||
}, | ||
"foo": secretsyml.SecretSpec{ | ||
Tags: []secretsyml.YamlTag{secretsyml.Var, secretsyml.File}, | ||
Path: "foo_path", | ||
}, | ||
} | ||
} | ||
|
||
t.Run("Resolves variable and literal secrets", func(t *testing.T) { | ||
secrets, err := resolveSecrets( | ||
&p, | ||
secretsMap(), | ||
) | ||
|
||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
assert.Equal( | ||
t, | ||
secrets, | ||
map[string]string{ | ||
"bar": "bar_path", | ||
"baz": "baz_path_value", | ||
"foo": "foo_path_value", | ||
}) | ||
}) | ||
|
||
t.Run("Reports errors", func(t *testing.T) { | ||
sm := secretsMap() | ||
sm["err1"] = secretsyml.SecretSpec{ | ||
Tags: []secretsyml.YamlTag{secretsyml.Var, secretsyml.File}, | ||
Path: "err_first", | ||
} | ||
sm["err2"] = secretsyml.SecretSpec{ | ||
Tags: []secretsyml.YamlTag{secretsyml.Var}, | ||
Path: "err_second", | ||
} | ||
|
||
secrets, err := resolveSecrets( | ||
&p, | ||
sm, | ||
) | ||
|
||
if !assert.EqualError(t, err, "err_first_value; err_second_value") { | ||
return | ||
} | ||
assert.Nil(t, secrets) | ||
}) | ||
} | ||
|
||
func Test_buildEnvironment(t *testing.T) { | ||
tempFactory := NewTempFactory("") | ||
|
||
defer tempFactory.Cleanup() | ||
|
||
env, err := buildEnvironment( | ||
map[string]string{ | ||
"bar": "bar_val", | ||
"baz": "baz_val", | ||
"foo": "foo_val", | ||
}, | ||
secretsyml.SecretsMap{ | ||
"bar": secretsyml.SecretSpec{ | ||
Tags: []secretsyml.YamlTag{secretsyml.Literal, secretsyml.File}, | ||
}, | ||
"baz": secretsyml.SecretSpec{ | ||
Tags: []secretsyml.YamlTag{secretsyml.Literal, secretsyml.File}, | ||
}, | ||
"foo": secretsyml.SecretSpec{ | ||
Tags: []secretsyml.YamlTag{secretsyml.Literal}, | ||
}, | ||
}, | ||
&tempFactory, | ||
) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
assert.Equal( | ||
t, | ||
env, | ||
[]string{ | ||
"bar=" + tempFactory.files[0], | ||
"baz=" + tempFactory.files[1], | ||
"foo=foo_val", | ||
}, | ||
) | ||
} |
Oops, something went wrong.