influxdb/testing/secret.go

519 lines
11 KiB
Go
Raw Normal View History

package testing
import (
feat(vault): add vault implementation of secret service test(platform): run testcontainer integration tests for nightly release Integration tests for the vault secret service using testcontiners should not run along with unit tests, however, they should run on some regular schedule. This commit introduces `make test-integration` which runs integration tests for vault using testcontainers. The command introduced relies on docker being available on the host it is executed on. chore(platform): make go modules tidy chore: try to fix go mod chore(platform): remove explicit logrus dependency chore(platform): run go mod tidy chore(platform): replace github.com/Sirupsen/logrus with github.com/sirupsen/logrus chore(platform): update docker dependency feat(vault): add vault implementation of secret service test(platform): run testcontainer integration tests for nightly release Integration tests for the vault secret service using testcontiners should not run along with unit tests, however, they should run on some regular schedule. This commit introduces `make test-integration` which runs integration tests for vault using testcontainers. The command introduced relies on docker being available on the host it is executed on. chore(platform): make go modules tidy chore: try to fix go mod chore(platform): run go mod tidy feat(vault): add vault implementation of secret service chore(platform): make go modules tidy feat(platform): add Put/Patch/Delete methods on secret service feat(vault): add Put/Patch/Delete methods on vault secret service feat(http): add http handler methods for secret service feat(bolt): add Put/Delete/Patch methods to bolt secret service feat(testing): add tests for Put/Patch/Delete methods in secret service feat(mock): add mock secret service feat(http): add tests for secrets endpoints feat(http): update swagger for secrets endpoints chore: run go mod tidy
2018-11-16 16:45:00 +00:00
"bytes"
"context"
feat(vault): add vault implementation of secret service test(platform): run testcontainer integration tests for nightly release Integration tests for the vault secret service using testcontiners should not run along with unit tests, however, they should run on some regular schedule. This commit introduces `make test-integration` which runs integration tests for vault using testcontainers. The command introduced relies on docker being available on the host it is executed on. chore(platform): make go modules tidy chore: try to fix go mod chore(platform): remove explicit logrus dependency chore(platform): run go mod tidy chore(platform): replace github.com/Sirupsen/logrus with github.com/sirupsen/logrus chore(platform): update docker dependency feat(vault): add vault implementation of secret service test(platform): run testcontainer integration tests for nightly release Integration tests for the vault secret service using testcontiners should not run along with unit tests, however, they should run on some regular schedule. This commit introduces `make test-integration` which runs integration tests for vault using testcontainers. The command introduced relies on docker being available on the host it is executed on. chore(platform): make go modules tidy chore: try to fix go mod chore(platform): run go mod tidy feat(vault): add vault implementation of secret service chore(platform): make go modules tidy feat(platform): add Put/Patch/Delete methods on secret service feat(vault): add Put/Patch/Delete methods on vault secret service feat(http): add http handler methods for secret service feat(bolt): add Put/Delete/Patch methods to bolt secret service feat(testing): add tests for Put/Patch/Delete methods in secret service feat(mock): add mock secret service feat(http): add tests for secrets endpoints feat(http): update swagger for secrets endpoints chore: run go mod tidy
2018-11-16 16:45:00 +00:00
"sort"
"testing"
"github.com/google/go-cmp/cmp"
platform "github.com/influxdata/influxdb"
)
feat(vault): add vault implementation of secret service test(platform): run testcontainer integration tests for nightly release Integration tests for the vault secret service using testcontiners should not run along with unit tests, however, they should run on some regular schedule. This commit introduces `make test-integration` which runs integration tests for vault using testcontainers. The command introduced relies on docker being available on the host it is executed on. chore(platform): make go modules tidy chore: try to fix go mod chore(platform): remove explicit logrus dependency chore(platform): run go mod tidy chore(platform): replace github.com/Sirupsen/logrus with github.com/sirupsen/logrus chore(platform): update docker dependency feat(vault): add vault implementation of secret service test(platform): run testcontainer integration tests for nightly release Integration tests for the vault secret service using testcontiners should not run along with unit tests, however, they should run on some regular schedule. This commit introduces `make test-integration` which runs integration tests for vault using testcontainers. The command introduced relies on docker being available on the host it is executed on. chore(platform): make go modules tidy chore: try to fix go mod chore(platform): run go mod tidy feat(vault): add vault implementation of secret service chore(platform): make go modules tidy feat(platform): add Put/Patch/Delete methods on secret service feat(vault): add Put/Patch/Delete methods on vault secret service feat(http): add http handler methods for secret service feat(bolt): add Put/Delete/Patch methods to bolt secret service feat(testing): add tests for Put/Patch/Delete methods in secret service feat(mock): add mock secret service feat(http): add tests for secrets endpoints feat(http): update swagger for secrets endpoints chore: run go mod tidy
2018-11-16 16:45:00 +00:00
var secretCmpOptions = cmp.Options{
cmp.Comparer(func(x, y []byte) bool {
return bytes.Equal(x, y)
}),
cmp.Transformer("Sort", func(in []string) []string {
out := append([]string(nil), in...) // Copy input to avoid mutating it
sort.Strings(out)
return out
}),
}
// A secret is a comparable data structure that is used for testing
type Secret struct {
OrganizationID platform.ID
Env map[string]string
}
// SecretServiceFields contain the
type SecretServiceFields struct {
Secrets []Secret
}
// SecretService will test all methods for the secrets service.
func SecretService(
init func(SecretServiceFields, *testing.T) (platform.SecretService, func()),
t *testing.T,
) {
tests := []struct {
name string
fn func(
init func(SecretServiceFields, *testing.T) (platform.SecretService, func()),
t *testing.T,
)
}{
{
name: "LoadSecret",
fn: LoadSecret,
},
{
name: "PutSecret",
fn: PutSecret,
},
feat(vault): add vault implementation of secret service test(platform): run testcontainer integration tests for nightly release Integration tests for the vault secret service using testcontiners should not run along with unit tests, however, they should run on some regular schedule. This commit introduces `make test-integration` which runs integration tests for vault using testcontainers. The command introduced relies on docker being available on the host it is executed on. chore(platform): make go modules tidy chore: try to fix go mod chore(platform): remove explicit logrus dependency chore(platform): run go mod tidy chore(platform): replace github.com/Sirupsen/logrus with github.com/sirupsen/logrus chore(platform): update docker dependency feat(vault): add vault implementation of secret service test(platform): run testcontainer integration tests for nightly release Integration tests for the vault secret service using testcontiners should not run along with unit tests, however, they should run on some regular schedule. This commit introduces `make test-integration` which runs integration tests for vault using testcontainers. The command introduced relies on docker being available on the host it is executed on. chore(platform): make go modules tidy chore: try to fix go mod chore(platform): run go mod tidy feat(vault): add vault implementation of secret service chore(platform): make go modules tidy feat(platform): add Put/Patch/Delete methods on secret service feat(vault): add Put/Patch/Delete methods on vault secret service feat(http): add http handler methods for secret service feat(bolt): add Put/Delete/Patch methods to bolt secret service feat(testing): add tests for Put/Patch/Delete methods in secret service feat(mock): add mock secret service feat(http): add tests for secrets endpoints feat(http): update swagger for secrets endpoints chore: run go mod tidy
2018-11-16 16:45:00 +00:00
{
name: "PutSecrets",
fn: PutSecrets,
},
{
name: "PatchSecrets",
fn: PatchSecrets,
},
{
name: "GetSecretKeys",
fn: GetSecretKeys,
},
feat(vault): add vault implementation of secret service test(platform): run testcontainer integration tests for nightly release Integration tests for the vault secret service using testcontiners should not run along with unit tests, however, they should run on some regular schedule. This commit introduces `make test-integration` which runs integration tests for vault using testcontainers. The command introduced relies on docker being available on the host it is executed on. chore(platform): make go modules tidy chore: try to fix go mod chore(platform): remove explicit logrus dependency chore(platform): run go mod tidy chore(platform): replace github.com/Sirupsen/logrus with github.com/sirupsen/logrus chore(platform): update docker dependency feat(vault): add vault implementation of secret service test(platform): run testcontainer integration tests for nightly release Integration tests for the vault secret service using testcontiners should not run along with unit tests, however, they should run on some regular schedule. This commit introduces `make test-integration` which runs integration tests for vault using testcontainers. The command introduced relies on docker being available on the host it is executed on. chore(platform): make go modules tidy chore: try to fix go mod chore(platform): run go mod tidy feat(vault): add vault implementation of secret service chore(platform): make go modules tidy feat(platform): add Put/Patch/Delete methods on secret service feat(vault): add Put/Patch/Delete methods on vault secret service feat(http): add http handler methods for secret service feat(bolt): add Put/Delete/Patch methods to bolt secret service feat(testing): add tests for Put/Patch/Delete methods in secret service feat(mock): add mock secret service feat(http): add tests for secrets endpoints feat(http): update swagger for secrets endpoints chore: run go mod tidy
2018-11-16 16:45:00 +00:00
{
name: "DeleteSecrets",
fn: DeleteSecrets,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.fn(init, t)
})
}
}
// LoadSecret tests the LoadSecret method for the SecretService interface.
func LoadSecret(
init func(f SecretServiceFields, t *testing.T) (platform.SecretService, func()),
t *testing.T,
) {
type args struct {
orgID platform.ID
key string
}
type wants struct {
value string
err error
}
tests := []struct {
name string
fields SecretServiceFields
args args
wants wants
}{
{
name: "load secret field",
fields: SecretServiceFields{
Secrets: []Secret{
{
OrganizationID: platform.ID(1),
Env: map[string]string{
"api_key": "abc123xyz",
},
},
},
},
args: args{
orgID: platform.ID(1),
key: "api_key",
},
wants: wants{
value: "abc123xyz",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, done := init(tt.fields, t)
defer done()
ctx := context.Background()
val, err := s.LoadSecret(ctx, tt.args.orgID, tt.args.key)
if (err != nil) != (tt.wants.err != nil) {
t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
}
if err != nil && tt.wants.err != nil {
if err.Error() != tt.wants.err.Error() {
t.Fatalf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error())
}
}
if want, got := tt.wants.value, val; want != got {
t.Errorf("expected value to be %s, got %s", want, got)
}
})
}
}
// PutSecret tests the PutSecret method for the SecretService interface.
func PutSecret(
init func(f SecretServiceFields, t *testing.T) (platform.SecretService, func()),
t *testing.T,
) {
type args struct {
orgID platform.ID
key string
value string
}
type wants struct {
err error
}
tests := []struct {
name string
fields SecretServiceFields
args args
wants wants
}{
{
name: "put secret",
fields: SecretServiceFields{},
args: args{
orgID: platform.ID(1),
key: "api_key",
value: "abc123xyz",
},
wants: wants{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, done := init(tt.fields, t)
defer done()
ctx := context.Background()
err := s.PutSecret(ctx, tt.args.orgID, tt.args.key, tt.args.value)
if (err != nil) != (tt.wants.err != nil) {
t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
}
if err != nil && tt.wants.err != nil {
if err.Error() != tt.wants.err.Error() {
t.Fatalf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error())
}
}
val, err := s.LoadSecret(ctx, tt.args.orgID, tt.args.key)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
if want, got := tt.args.value, val; want != got {
t.Errorf("expected value to be %s, got %s", want, got)
}
})
}
}
feat(vault): add vault implementation of secret service test(platform): run testcontainer integration tests for nightly release Integration tests for the vault secret service using testcontiners should not run along with unit tests, however, they should run on some regular schedule. This commit introduces `make test-integration` which runs integration tests for vault using testcontainers. The command introduced relies on docker being available on the host it is executed on. chore(platform): make go modules tidy chore: try to fix go mod chore(platform): remove explicit logrus dependency chore(platform): run go mod tidy chore(platform): replace github.com/Sirupsen/logrus with github.com/sirupsen/logrus chore(platform): update docker dependency feat(vault): add vault implementation of secret service test(platform): run testcontainer integration tests for nightly release Integration tests for the vault secret service using testcontiners should not run along with unit tests, however, they should run on some regular schedule. This commit introduces `make test-integration` which runs integration tests for vault using testcontainers. The command introduced relies on docker being available on the host it is executed on. chore(platform): make go modules tidy chore: try to fix go mod chore(platform): run go mod tidy feat(vault): add vault implementation of secret service chore(platform): make go modules tidy feat(platform): add Put/Patch/Delete methods on secret service feat(vault): add Put/Patch/Delete methods on vault secret service feat(http): add http handler methods for secret service feat(bolt): add Put/Delete/Patch methods to bolt secret service feat(testing): add tests for Put/Patch/Delete methods in secret service feat(mock): add mock secret service feat(http): add tests for secrets endpoints feat(http): update swagger for secrets endpoints chore: run go mod tidy
2018-11-16 16:45:00 +00:00
// PutSecrets tests the PutSecrets method for the SecretService interface.
func PutSecrets(
init func(f SecretServiceFields, t *testing.T) (platform.SecretService, func()),
t *testing.T,
) {
type args struct {
orgID platform.ID
secrets map[string]string
}
type wants struct {
err error
keys []string
}
tests := []struct {
name string
fields SecretServiceFields
args args
wants wants
}{
{
name: "put secrets",
fields: SecretServiceFields{
Secrets: []Secret{
{
OrganizationID: platform.ID(1),
Env: map[string]string{
"api_key": "abc123xyz",
},
},
},
},
args: args{
orgID: platform.ID(1),
secrets: map[string]string{
"api_key2": "abc123xyz",
"batman": "potato",
},
},
wants: wants{
keys: []string{"api_key2", "batman"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, done := init(tt.fields, t)
defer done()
ctx := context.Background()
err := s.PutSecrets(ctx, tt.args.orgID, tt.args.secrets)
if (err != nil) != (tt.wants.err != nil) {
t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
}
if err != nil && tt.wants.err != nil {
if err.Error() != tt.wants.err.Error() {
t.Fatalf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error())
}
}
for k, v := range tt.args.secrets {
val, err := s.LoadSecret(ctx, tt.args.orgID, k)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
if want, got := v, val; want != got {
t.Errorf("expected value to be %s, got %s", want, got)
}
}
keys, err := s.GetSecretKeys(ctx, tt.args.orgID)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
if diff := cmp.Diff(keys, tt.wants.keys, secretCmpOptions...); diff != "" {
t.Errorf("keys are different -got/+want\ndiff %s", diff)
}
})
}
}
// PatchSecrets tests the PatchSecrets method for the SecretService interface.
func PatchSecrets(
init func(f SecretServiceFields, t *testing.T) (platform.SecretService, func()),
t *testing.T,
) {
type args struct {
orgID platform.ID
secrets map[string]string
}
type wants struct {
err error
keys []string
}
tests := []struct {
name string
fields SecretServiceFields
args args
wants wants
}{
{
name: "patch secrets",
fields: SecretServiceFields{
Secrets: []Secret{
{
OrganizationID: platform.ID(1),
Env: map[string]string{
"api_key": "abc123xyz",
},
},
},
},
args: args{
orgID: platform.ID(1),
secrets: map[string]string{
"api_key2": "abc123xyz",
"batman": "potato",
},
},
wants: wants{
keys: []string{"api_key", "api_key2", "batman"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, done := init(tt.fields, t)
defer done()
ctx := context.Background()
err := s.PatchSecrets(ctx, tt.args.orgID, tt.args.secrets)
if (err != nil) != (tt.wants.err != nil) {
t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
}
if err != nil && tt.wants.err != nil {
if err.Error() != tt.wants.err.Error() {
t.Fatalf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error())
}
}
for k, v := range tt.args.secrets {
val, err := s.LoadSecret(ctx, tt.args.orgID, k)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
if want, got := v, val; want != got {
t.Errorf("expected value to be %s, got %s", want, got)
}
}
keys, err := s.GetSecretKeys(ctx, tt.args.orgID)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
if diff := cmp.Diff(keys, tt.wants.keys, secretCmpOptions...); diff != "" {
t.Errorf("keys are different -got/+want\ndiff %s", diff)
}
})
}
}
// GetSecretKeys tests the GetSecretKeys method for the SecretService interface.
func GetSecretKeys(
init func(f SecretServiceFields, t *testing.T) (platform.SecretService, func()),
t *testing.T,
) {
type args struct {
orgID platform.ID
}
type wants struct {
keys []string
err error
}
tests := []struct {
name string
fields SecretServiceFields
args args
wants wants
}{
{
name: "get secret keys for one org",
fields: SecretServiceFields{
Secrets: []Secret{
{
OrganizationID: platform.ID(1),
Env: map[string]string{
"api_key": "abc123xyz",
},
},
{
OrganizationID: platform.ID(2),
Env: map[string]string{
"api_key": "zyx321cba",
},
},
},
},
args: args{
orgID: platform.ID(1),
},
wants: wants{
keys: []string{"api_key"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, done := init(tt.fields, t)
defer done()
ctx := context.Background()
keys, err := s.GetSecretKeys(ctx, tt.args.orgID)
if (err != nil) != (tt.wants.err != nil) {
t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
}
if err != nil && tt.wants.err != nil {
if err.Error() != tt.wants.err.Error() {
t.Fatalf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error())
}
}
feat(vault): add vault implementation of secret service test(platform): run testcontainer integration tests for nightly release Integration tests for the vault secret service using testcontiners should not run along with unit tests, however, they should run on some regular schedule. This commit introduces `make test-integration` which runs integration tests for vault using testcontainers. The command introduced relies on docker being available on the host it is executed on. chore(platform): make go modules tidy chore: try to fix go mod chore(platform): remove explicit logrus dependency chore(platform): run go mod tidy chore(platform): replace github.com/Sirupsen/logrus with github.com/sirupsen/logrus chore(platform): update docker dependency feat(vault): add vault implementation of secret service test(platform): run testcontainer integration tests for nightly release Integration tests for the vault secret service using testcontiners should not run along with unit tests, however, they should run on some regular schedule. This commit introduces `make test-integration` which runs integration tests for vault using testcontainers. The command introduced relies on docker being available on the host it is executed on. chore(platform): make go modules tidy chore: try to fix go mod chore(platform): run go mod tidy feat(vault): add vault implementation of secret service chore(platform): make go modules tidy feat(platform): add Put/Patch/Delete methods on secret service feat(vault): add Put/Patch/Delete methods on vault secret service feat(http): add http handler methods for secret service feat(bolt): add Put/Delete/Patch methods to bolt secret service feat(testing): add tests for Put/Patch/Delete methods in secret service feat(mock): add mock secret service feat(http): add tests for secrets endpoints feat(http): update swagger for secrets endpoints chore: run go mod tidy
2018-11-16 16:45:00 +00:00
if diff := cmp.Diff(keys, tt.wants.keys, secretCmpOptions...); diff != "" {
t.Errorf("keys are different -got/+want\ndiff %s", diff)
}
})
}
}
// DeleteSecrets tests the DeleteSecrets method for the SecretService interface.
func DeleteSecrets(
init func(f SecretServiceFields, t *testing.T) (platform.SecretService, func()),
t *testing.T,
) {
type args struct {
orgID platform.ID
keys []string
}
type wants struct {
keys []string
err error
}
tests := []struct {
name string
fields SecretServiceFields
args args
wants wants
}{
{
name: "delete secret keys",
fields: SecretServiceFields{
Secrets: []Secret{
{
OrganizationID: platform.ID(1),
Env: map[string]string{
"api_key": "abc123xyz",
"api_key2": "potato",
"batman": "foo",
},
},
},
},
args: args{
orgID: platform.ID(1),
keys: []string{"api_key2", "batman"},
},
wants: wants{
keys: []string{"api_key"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, done := init(tt.fields, t)
defer done()
ctx := context.Background()
err := s.DeleteSecret(ctx, tt.args.orgID, tt.args.keys...)
if (err != nil) != (tt.wants.err != nil) {
t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
}
if err != nil && tt.wants.err != nil {
if err.Error() != tt.wants.err.Error() {
t.Fatalf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error())
}
}
keys, err := s.GetSecretKeys(ctx, tt.args.orgID)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
if diff := cmp.Diff(keys, tt.wants.keys, secretCmpOptions...); diff != "" {
t.Errorf("keys are different -got/+want\ndiff %s", diff)
}
})
}
}