| 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 gcpruntimeconfig |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"errors" |
| 20 |
"fmt" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"gocloud.dev/internal/testing/setup" |
| 24 |
"gocloud.dev/runtimevar" |
| 25 |
"gocloud.dev/runtimevar/driver" |
| 26 |
"gocloud.dev/runtimevar/drivertest" |
| 27 |
pb "google.golang.org/genproto/googleapis/cloud/runtimeconfig/v1beta1" |
| 28 |
"google.golang.org/grpc/codes" |
| 29 |
"google.golang.org/grpc/status" |
| 30 |
) |
| 31 |
|
| 32 |
// This constant records the project used for the last --record. |
| 33 |
// If you want to use --record mode, |
| 34 |
// 1. Update this constant to your GCP project ID. |
| 35 |
// 2. Ensure that the "Runtime Configuration API" is enabled for your project. |
| 36 |
// TODO(issue #300): Use Terraform to get this. |
| 37 |
const projectID = "go-cloud-test-216917" |
| 38 |
|
| 39 |
const ( |
| 40 |
// configID is the runtimeconfig high-level config that variables sit under. |
| 41 |
configID = "go_cloud_runtimeconfigurator_test" |
| 42 |
) |
| 43 |
|
| 44 |
func configPath() string { |
| 45 |
return fmt.Sprintf("projects/%s/configs/%s", projectID, configID) |
| 46 |
} |
| 47 |
|
| 48 |
func variableKey(variableName string) string { |
| 49 |
return VariableKey(projectID, configID, variableName) |
| 50 |
} |
| 51 |
|
| 52 |
type harness struct { |
| 53 |
client pb.RuntimeConfigManagerClient |
| 54 |
closer func() |
| 55 |
} |
| 56 |
|
| 57 |
func newHarness(t *testing.T) (drivertest.Harness, error) { |
| 58 |
t.Helper() |
| 59 |
|
| 60 |
ctx := context.Background() |
| 61 |
conn, done := setup.NewGCPgRPCConn(ctx, t, endPoint, "runtimevar") |
| 62 |
client := pb.NewRuntimeConfigManagerClient(conn) |
| 63 |
// Ignore errors if the config already exists. |
| 64 |
_, _ = client.CreateConfig(ctx, &pb.CreateConfigRequest{ |
| 65 |
Parent: "projects/" + projectID, |
| 66 |
Config: &pb.RuntimeConfig{ |
| 67 |
Name: configPath(), |
| 68 |
Description: t.Name(), |
| 69 |
}, |
| 70 |
}) |
| 71 |
return &harness{ |
| 72 |
client: client, |
| 73 |
closer: func() { |
| 74 |
_, _ = client.DeleteConfig(ctx, &pb.DeleteConfigRequest{Name: configPath()}) |
| 75 |
done() |
| 76 |
}, |
| 77 |
}, nil |
| 78 |
} |
| 79 |
|
| 80 |
func (h *harness) MakeWatcher(ctx context.Context, name string, decoder *runtimevar.Decoder) (driver.Watcher, error) { |
| 81 |
return newWatcher(h.client, variableKey(name), decoder, nil) |
| 82 |
} |
| 83 |
|
| 84 |
func (h *harness) CreateVariable(ctx context.Context, name string, val []byte) error { |
| 85 |
_, err := h.client.CreateVariable(ctx, &pb.CreateVariableRequest{ |
| 86 |
Parent: configPath(), |
| 87 |
Variable: &pb.Variable{ |
| 88 |
Name: variableKey(name), |
| 89 |
Contents: &pb.Variable_Value{Value: val}, |
| 90 |
}, |
| 91 |
}) |
| 92 |
return err |
| 93 |
} |
| 94 |
|
| 95 |
func (h *harness) UpdateVariable(ctx context.Context, name string, val []byte) error { |
| 96 |
_, err := h.client.UpdateVariable(ctx, &pb.UpdateVariableRequest{ |
| 97 |
Name: variableKey(name), |
| 98 |
Variable: &pb.Variable{ |
| 99 |
Contents: &pb.Variable_Value{Value: val}, |
| 100 |
}, |
| 101 |
}) |
| 102 |
return err |
| 103 |
} |
| 104 |
|
| 105 |
func (h *harness) DeleteVariable(ctx context.Context, name string) error { |
| 106 |
_, err := h.client.DeleteVariable(ctx, &pb.DeleteVariableRequest{Name: variableKey(name)}) |
| 107 |
return err |
| 108 |
} |
| 109 |
|
| 110 |
func (h *harness) Close() { |
| 111 |
h.closer() |
| 112 |
} |
| 113 |
|
| 114 |
func (h *harness) Mutable() bool { return true } |
| 115 |
|
| 116 |
func TestConformance(t *testing.T) { |
| 117 |
drivertest.RunConformanceTests(t, newHarness, []drivertest.AsTest{verifyAs{}}) |
| 118 |
} |
| 119 |
|
| 120 |
type verifyAs struct{} |
| 121 |
|
| 122 |
func (verifyAs) Name() string { |
| 123 |
return "verify As" |
| 124 |
} |
| 125 |
|
| 126 |
func (verifyAs) SnapshotCheck(s *runtimevar.Snapshot) error { |
| 127 |
var v *pb.Variable |
| 128 |
if !s.As(&v) { |
| 129 |
return errors.New("Snapshot.As failed") |
| 130 |
} |
| 131 |
return nil |
| 132 |
} |
| 133 |
|
| 134 |
func (verifyAs) ErrorCheck(v *runtimevar.Variable, err error) error { |
| 135 |
var s *status.Status |
| 136 |
if !v.ErrorAs(err, &s) { |
| 137 |
return errors.New("runtimevar.ErrorAs failed") |
| 138 |
} |
| 139 |
return nil |
| 140 |
} |
| 141 |
|
| 142 |
// Runtimeconfigurator-specific tests. |
| 143 |
|
| 144 |
func TestEquivalentError(t *testing.T) { |
| 145 |
tests := []struct { |
| 146 |
Err1, Err2 error |
| 147 |
Want bool |
| 148 |
}{ |
| 149 |
{Err1: errors.New("not grpc"), Err2: errors.New("not grpc"), Want: true}, |
| 150 |
{Err1: errors.New("not grpc"), Err2: errors.New("not grpc but different")}, |
| 151 |
{Err1: errors.New("not grpc"), Err2: status.Errorf(codes.Internal, "fail")}, |
| 152 |
{Err1: status.Errorf(codes.Internal, "fail"), Err2: status.Errorf(codes.InvalidArgument, "fail")}, |
| 153 |
{Err1: status.Errorf(codes.Internal, "fail"), Err2: status.Errorf(codes.Internal, "fail"), Want: true}, |
| 154 |
} |
| 155 |
|
| 156 |
for _, test := range tests { |
| 157 |
got := equivalentError(test.Err1, test.Err2) |
| 158 |
if got != test.Want { |
| 159 |
t.Errorf("%v vs %v: got %v want %v", test.Err1, test.Err2, got, test.Want) |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
func TestNoConnectionError(t *testing.T) { |
| 165 |
ctx := context.Background() |
| 166 |
creds, err := setup.FakeGCPCredentials(ctx) |
| 167 |
if err != nil { |
| 168 |
t.Fatal(err) |
| 169 |
} |
| 170 |
|
| 171 |
// Connect to the Runtime Configurator service. |
| 172 |
client, cleanup, err := Dial(ctx, creds.TokenSource) |
| 173 |
if err != nil { |
| 174 |
t.Fatal(err) |
| 175 |
} |
| 176 |
defer cleanup() |
| 177 |
|
| 178 |
variableKey := VariableKey("gcp-project-id", "cfg-name", "cfg-variable-name") |
| 179 |
v, err := OpenVariable(client, variableKey, nil, nil) |
| 180 |
if err != nil { |
| 181 |
t.Fatal(err) |
| 182 |
} |
| 183 |
defer v.Close() |
| 184 |
_, err = v.Watch(context.Background()) |
| 185 |
if err == nil { |
| 186 |
t.Error("got nil want error") |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
func TestOpenVariable(t *testing.T) { |
| 191 |
cleanup := setup.FakeGCPDefaultCredentials(t) |
| 192 |
defer cleanup() |
| 193 |
|
| 194 |
tests := []struct { |
| 195 |
URL string |
| 196 |
WantErr bool |
| 197 |
}{ |
| 198 |
// OK. |
| 199 |
{"gcpruntimeconfig://projects/myproject/configs/mycfg/variables/myvar", false}, |
| 200 |
// OK, hierarchical key name. |
| 201 |
{"gcpruntimeconfig://projects/myproject/configs/mycfg/variables/myvar1/myvar2", false}, |
| 202 |
// OK, setting decoder. |
| 203 |
{"gcpruntimeconfig://projects/myproject/configs/mycfg/variables/myvar?decoder=string", false}, |
| 204 |
// Missing projects prefix. |
| 205 |
{"gcpruntimeconfig://project/myproject/configs/mycfg/variables/myvar", true}, |
| 206 |
// Missing project. |
| 207 |
{"gcpruntimeconfig://projects//configs/mycfg/variables/myvar", true}, |
| 208 |
// Missing configs. |
| 209 |
{"gcpruntimeconfig://projects/myproject/mycfg/variables/myvar", true}, |
| 210 |
// Missing configID. |
| 211 |
{"gcpruntimeconfig://projects/myproject/configs//variables/myvar", true}, |
| 212 |
// Missing variables. |
| 213 |
{"gcpruntimeconfig://projects/myproject/configs/mycfg//myvar", true}, |
| 214 |
// Missing variable name. |
| 215 |
{"gcpruntimeconfig://projects/myproject/configs/mycfg/variables/", true}, |
| 216 |
// Invalid decoder. |
| 217 |
{"gcpruntimeconfig://myproject/mycfg/myvar?decoder=notadecoder", true}, |
| 218 |
// Invalid param. |
| 219 |
{"gcpruntimeconfig://myproject/mycfg/myvar?param=value", true}, |
| 220 |
// OK, setting wait. |
| 221 |
{"gcpruntimeconfig://projects/myproject/configs/mycfg/variables/myvar?wait=1m", false}, |
| 222 |
// Invalid wait. |
| 223 |
{"gcpruntimeconfig://projects/myproject/configs/mycfg/variables/myvar?wait=xx", true}, |
| 224 |
} |
| 225 |
|
| 226 |
ctx := context.Background() |
| 227 |
for _, test := range tests { |
| 228 |
v, err := runtimevar.OpenVariable(ctx, test.URL) |
| 229 |
if v != nil { |
| 230 |
defer v.Close() |
| 231 |
} |
| 232 |
if (err != nil) != test.WantErr { |
| 233 |
t.Errorf("%s: got error %v, want error %v", test.URL, err, test.WantErr) |
| 234 |
} |
| 235 |
} |
| 236 |
} |