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
|
package aws
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func must(r any, err error) any {
if err != nil {
panic(err)
}
return r
}
func TestMeta_MissingKey(t *testing.T) {
ec2meta := MockEC2Meta(nil, nil, "")
assert.Empty(t, must(ec2meta.Meta("foo")))
assert.Equal(t, "default", must(ec2meta.Meta("foo", "default")))
}
func TestMeta_ValidKey(t *testing.T) {
ec2meta := MockEC2Meta(map[string]string{"instance-id": "i-1234"}, nil, "")
assert.Equal(t, "i-1234", must(ec2meta.Meta("instance-id")))
assert.Equal(t, "i-1234", must(ec2meta.Meta("instance-id", "unused default")))
}
func TestDynamic_MissingKey(t *testing.T) {
ec2meta := MockEC2Meta(nil, nil, "")
assert.Empty(t, must(ec2meta.Dynamic("foo")))
assert.Equal(t, "default", must(ec2meta.Dynamic("foo", "default")))
}
func TestDynamic_ValidKey(t *testing.T) {
ec2meta := MockEC2Meta(nil, map[string]string{"instance-id": "i-1234"}, "")
assert.Equal(t, "i-1234", must(ec2meta.Dynamic("instance-id")))
assert.Equal(t, "i-1234", must(ec2meta.Dynamic("instance-id", "unused default")))
}
func TestRegion_NoRegion(t *testing.T) {
ec2meta := MockEC2Meta(nil, nil, "")
assert.Equal(t, "unknown", must(ec2meta.Region()))
}
func TestRegion_NoRegionWithDefault(t *testing.T) {
ec2meta := MockEC2Meta(nil, nil, "")
assert.Equal(t, "foo", must(ec2meta.Region("foo")))
}
func TestRegion_KnownRegion(t *testing.T) {
ec2meta := MockEC2Meta(nil, nil, "us-east-1")
assert.Equal(t, "us-east-1", must(ec2meta.Region()))
}
func TestUnreachable(t *testing.T) {
assert.False(t, unreachable(errors.New("foo")))
assert.True(t, unreachable(errors.New("host is down")))
assert.True(t, unreachable(errors.New("request canceled")))
assert.True(t, unreachable(errors.New("no route to host")))
}
func TestRetrieveMetadata_NonEC2(t *testing.T) {
ec2meta := NewEc2Meta(ClientOptions{})
ec2meta.nonAWS = true
assert.Equal(t, "foo", must(ec2meta.retrieveMetadata("", "foo")))
}
|