forked from hudl/fargo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstance_test.go
95 lines (75 loc) · 2.54 KB
/
instance_test.go
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
87
88
89
90
91
92
93
94
95
package fargo_test
// MIT Licensed (see README.md)
import (
"fmt"
"testing"
"github.com/hudl/fargo"
. "github.com/smartystreets/goconvey/convey"
)
func TestInstanceID(t *testing.T) {
i := fargo.Instance{
HostName: "i-6543",
Port: 9090,
App: "TESTAPP",
IPAddr: "127.0.0.10",
VipAddress: "127.0.0.10",
SecureVipAddress: "127.0.0.10",
Status: fargo.UP,
}
Convey("Given an instance with DataCenterInfo.Name set to Amazon", t, func() {
i.DataCenterInfo = fargo.DataCenterInfo{Name: fargo.Amazon}
Convey("When UniqueID function has NOT been set", func() {
i.UniqueID = nil
Convey("And InstanceID has been set in AmazonMetadata", func() {
i.DataCenterInfo.Metadata.InstanceID = "EXPECTED-ID"
Convey("Id() should return the provided InstanceID", func() {
So(i.Id(), ShouldEqual, "EXPECTED-ID")
})
})
Convey("And InstanceID has NOT been set in AmazonMetadata", func() {
i.DataCenterInfo.Metadata.InstanceID = ""
Convey("Id() should return an empty string", func() {
So(i.Id(), ShouldEqual, "")
})
})
})
Convey("When UniqueID function has been set", func() {
i.UniqueID = func(i fargo.Instance) string {
return fmt.Sprintf("%s:%d", i.App, 123)
}
Convey("And InstanceID has been set in AmazonMetadata", func() {
i.DataCenterInfo.Metadata.InstanceID = "UNEXPECTED"
Convey("Id() should return the ID that is provided by UniqueID", func() {
So(i.Id(), ShouldEqual, "TESTAPP:123")
})
})
Convey("And InstanceID has not been set in AmazonMetadata", func() {
i.DataCenterInfo.Metadata.InstanceID = ""
Convey("Id() should return the ID that is provided by UniqueID", func() {
So(i.Id(), ShouldEqual, "TESTAPP:123")
})
})
})
})
Convey("Given an instance with DataCenterInfo.Name set to MyOwn", t, func() {
i.DataCenterInfo = fargo.DataCenterInfo{Name: fargo.MyOwn}
Convey("When UniqueID function has NOT been set", func() {
i.UniqueID = nil
Convey("Id() should return the host name", func() {
So(i.Id(), ShouldEqual, "i-6543")
})
})
Convey("When UniqueID function has been set", func() {
i.Metadata.Raw = []byte(`{"instanceId": "unique-id"}`)
i.UniqueID = func(i fargo.Instance) string {
if id, err := i.Metadata.GetString("instanceId"); err == nil {
return fmt.Sprintf("%s:%s", i.HostName, id)
}
return i.HostName
}
Convey("Id() should return the ID that is provided by UniqueID", func() {
So(i.Id(), ShouldEqual, "i-6543:unique-id")
})
})
})
}