| 1 |
// Copyright 2018 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package awsparamstore |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"errors" |
| 20 |
"testing" |
| 21 |
|
| 22 |
"github.com/aws/aws-sdk-go-v2/aws" |
| 23 |
"github.com/aws/aws-sdk-go-v2/service/ssm" |
| 24 |
"github.com/aws/smithy-go" |
| 25 |
"gocloud.dev/internal/testing/setup" |
| 26 |
"gocloud.dev/runtimevar" |
| 27 |
"gocloud.dev/runtimevar/driver" |
| 28 |
"gocloud.dev/runtimevar/drivertest" |
| 29 |
) |
| 30 |
|
| 31 |
// This constant records the region used for the last --record. |
| 32 |
// If you want to use --record mode, |
| 33 |
// 1. Update this constant to your AWS region. |
| 34 |
// TODO(issue #300): Use Terraform to get this. |
| 35 |
const region = "us-east-2" |
| 36 |
|
| 37 |
type harness struct { |
| 38 |
client *ssm.Client |
| 39 |
closer func() |
| 40 |
} |
| 41 |
|
| 42 |
func newHarness(t *testing.T) (drivertest.Harness, error) { |
| 43 |
t.Helper() |
| 44 |
|
| 45 |
cfg, _, done, _ := setup.NewAWSv2Config(context.Background(), t, region, false) |
| 46 |
return &harness{client: ssm.NewFromConfig(cfg), closer: done}, nil |
| 47 |
} |
| 48 |
|
| 49 |
func (h *harness) MakeWatcher(ctx context.Context, name string, decoder *runtimevar.Decoder) (driver.Watcher, error) { |
| 50 |
return newWatcher(h.client, name, decoder, nil), nil |
| 51 |
} |
| 52 |
|
| 53 |
func (h *harness) CreateVariable(ctx context.Context, name string, val []byte) error { |
| 54 |
_, err := h.client.PutParameter(ctx, &ssm.PutParameterInput{ |
| 55 |
Name: aws.String(name), |
| 56 |
Type: "String", |
| 57 |
Value: aws.String(string(val)), |
| 58 |
Overwrite: aws.Bool(true), |
| 59 |
}) |
| 60 |
return err |
| 61 |
} |
| 62 |
|
| 63 |
func (h *harness) UpdateVariable(ctx context.Context, name string, val []byte) error { |
| 64 |
return h.CreateVariable(ctx, name, val) |
| 65 |
} |
| 66 |
|
| 67 |
func (h *harness) DeleteVariable(ctx context.Context, name string) error { |
| 68 |
_, err := h.client.DeleteParameter(ctx, &ssm.DeleteParameterInput{Name: aws.String(name)}) |
| 69 |
return err |
| 70 |
} |
| 71 |
|
| 72 |
func (h *harness) Close() { |
| 73 |
h.closer() |
| 74 |
} |
| 75 |
|
| 76 |
func (h *harness) Mutable() bool { return true } |
| 77 |
|
| 78 |
func TestConformance(t *testing.T) { |
| 79 |
drivertest.RunConformanceTests(t, newHarness, []drivertest.AsTest{verifyAs{}}) |
| 80 |
} |
| 81 |
|
| 82 |
type verifyAs struct { |
| 83 |
} |
| 84 |
|
| 85 |
func (verifyAs) Name() string { |
| 86 |
return "verify As" |
| 87 |
} |
| 88 |
|
| 89 |
func (v verifyAs) SnapshotCheck(s *runtimevar.Snapshot) error { |
| 90 |
var getParam *ssm.GetParameterOutput |
| 91 |
if !s.As(&getParam) { |
| 92 |
return errors.New("Snapshot.As failed for GetParameterOutput") |
| 93 |
} |
| 94 |
return nil |
| 95 |
} |
| 96 |
|
| 97 |
func (va verifyAs) ErrorCheck(v *runtimevar.Variable, err error) error { |
| 98 |
var e smithy.APIError |
| 99 |
if !v.ErrorAs(err, &e) { |
| 100 |
return errors.New("Keeper.ErrorAs failed") |
| 101 |
} |
| 102 |
return nil |
| 103 |
} |
| 104 |
|
| 105 |
// Paramstore-specific tests. |
| 106 |
|
| 107 |
func TestOpenVariable(t *testing.T) { |
| 108 |
tests := []struct { |
| 109 |
URL string |
| 110 |
WantErr bool |
| 111 |
}{ |
| 112 |
// OK. |
| 113 |
{"awsparamstore://myvar", false}, |
| 114 |
// OK, setting region. |
| 115 |
{"awsparamstore://myvar?region=us-west-1", false}, |
| 116 |
// OK, setting decoder. |
| 117 |
{"awsparamstore://myvar?decoder=string", false}, |
| 118 |
// Invalid decoder. |
| 119 |
{"awsparamstore://myvar?decoder=notadecoder", true}, |
| 120 |
// OK, setting wait. |
| 121 |
{"awsparamstore://myvar?wait=2m", false}, |
| 122 |
// Invalid wait. |
| 123 |
{"awsparamstore://myvar?wait=x", true}, |
| 124 |
// Invalid parameter. |
| 125 |
{"awsparamstore://myvar?param=value", true}, |
| 126 |
} |
| 127 |
|
| 128 |
ctx := context.Background() |
| 129 |
for _, test := range tests { |
| 130 |
v, err := runtimevar.OpenVariable(ctx, test.URL) |
| 131 |
if (err != nil) != test.WantErr { |
| 132 |
t.Errorf("%s: got error %v, want error %v", test.URL, err, test.WantErr) |
| 133 |
} |
| 134 |
if err == nil { |
| 135 |
v.Close() |
| 136 |
} |
| 137 |
} |
| 138 |
} |