-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
86 lines (77 loc) · 2.4 KB
/
Copy pathmain_test.go
File metadata and controls
86 lines (77 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"context"
"strings"
"testing"
)
func TestVpcConfigFrom(t *testing.T) {
if got := vpcConfigFrom("", "", ""); got != nil {
t.Errorf("empty inputs should yield nil, got %+v", got)
}
if got := vpcConfigFrom("vpc-1", "subnet-1", ""); got != nil {
t.Errorf("partial inputs should yield nil, got %+v", got)
}
got := vpcConfigFrom("vpc-1", "subnet-1", "sg-1")
if got == nil {
t.Fatal("complete inputs should yield a config")
}
if got.VpcId != "vpc-1" || got.SubnetIds[0] != "subnet-1" || got.SecurityGroupIds[0] != "sg-1" {
t.Errorf("unexpected config: %+v", got)
}
}
// runningEnvs returns a handler that lists the given ids, each RUNNING.
func runningEnvs(ids ...string) handler {
return func(action string, body []byte) (int, string) {
switch action {
case "describeEnvironments":
items := make([]string, len(ids))
for i, id := range ids {
items[i] = `{"EnvironmentId":"` + id + `"}`
}
return 200, `{"Environments":[` + strings.Join(items, ",") + `]}`
case "getEnvironmentStatus":
return 200, `{"Status":"RUNNING"}`
}
return 500, ""
}
}
func TestResolveTargetSingle(t *testing.T) {
c := testClient(runningEnvs("only-one"))
target, err := resolveTarget(context.Background(), c, "")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if target == nil || target.EnvironmentId != "only-one" {
t.Fatalf("expected the single environment, got %+v", target)
}
}
func TestResolveTargetNone(t *testing.T) {
c := testClient(runningEnvs())
target, err := resolveTarget(context.Background(), c, "")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if target != nil {
t.Fatalf("expected nil target when none exist, got %+v", target)
}
}
func TestResolveTargetMultipleNeedsID(t *testing.T) {
c := testClient(runningEnvs("a", "b"))
_, err := resolveTarget(context.Background(), c, "")
if err == nil || !strings.Contains(err.Error(), "multiple") {
t.Fatalf("expected 'multiple environments' error, got %v", err)
}
}
func TestResolveTargetByID(t *testing.T) {
c := testClient(runningEnvs("a", "b"))
target, err := resolveTarget(context.Background(), c, "b")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if target == nil || target.EnvironmentId != "b" {
t.Fatalf("expected environment b, got %+v", target)
}
if _, err := resolveTarget(context.Background(), c, "missing"); err == nil {
t.Fatal("expected not-found error for unknown id")
}
}