From b169fc67bb1ab6b206a050f3ae40a8900e6be4fd Mon Sep 17 00:00:00 2001 From: Stev Witzel Date: Sat, 5 Sep 2015 12:54:46 +0100 Subject: [PATCH 1/6] Fix missing artifact in provisioning script --- provision_eureka.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/provision_eureka.sh b/provision_eureka.sh index fb6392c..7d5fe37 100644 --- a/provision_eureka.sh +++ b/provision_eureka.sh @@ -19,7 +19,9 @@ echo "127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdom 172.16.0.22 node2 node2.localdomain " > /etc/hosts -wget -O /var/lib/tomcat/webapps/eureka.zip https://netflixoss.ci.cloudbees.com/job/eureka-master/lastSuccessfulBuild/artifact/eureka-server/build/libs/eureka-server-1.1.135-SNAPSHOT.war +ARTIFACT="https://netflixoss.ci.cloudbees.com/job/eureka-master/lastSuccessfulBuild/artifact/eureka-server/build/libs/eureka-server-1.1.147-SNAPSHOT.war" + +wget -O /var/lib/tomcat/webapps/eureka.zip ${ARTIFACT} unzip -o -d /var/lib/tomcat/webapps/eureka /var/lib/tomcat/webapps/eureka.zip cp /vagrant/tests/eureka_properties/*.properties /var/lib/tomcat/webapps/eureka/WEB-INF/classes/ chown -R tomcat:tomcat /var/lib/tomcat/webapps/eureka From 674e7b44ca95d70062d688a743dc03d6d5fdc9c4 Mon Sep 17 00:00:00 2001 From: Stev Witzel Date: Sat, 5 Sep 2015 12:56:50 +0100 Subject: [PATCH 2/6] Rename metadata_test suites to fix goconvey panics Convey panics when multiple test suites have the same name. See https://github.com/smartystreets/goconvey/commit/52529b --- tests/metadata_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/metadata_test.go b/tests/metadata_test.go index 47f17b7..8d9addb 100644 --- a/tests/metadata_test.go +++ b/tests/metadata_test.go @@ -35,7 +35,7 @@ func TestGetFloat(t *testing.T) { Convey("With metadata", func() { metadata := new(fargo.InstanceMetadata) instance.Metadata = *metadata - Convey("That has a float value", func() { + Convey("That has a float64 value", func() { key := "d" value := 1.9 metadata.Raw = []byte("<" + key + ">" + strconv.FormatFloat(value, 'f', -1, 64) + "") @@ -45,7 +45,7 @@ func TestGetFloat(t *testing.T) { So(actualValue, ShouldEqual, value) }) }) - Convey("That has a float value", func() { + Convey("That has a float32 value", func() { key := "d" value := 1.9 metadata.Raw = []byte("<" + key + ">" + strconv.FormatFloat(value, 'f', -1, 32) + "") From b1b67319089314f2572e8106d2ac60b498a4e296 Mon Sep 17 00:00:00 2001 From: Stev Witzel Date: Sat, 5 Sep 2015 23:49:21 +0100 Subject: [PATCH 3/6] Handle Raw == nil in InstanceMetadata.MarshalJSON InstanceMetadata.MarshalJSON must return proper JSON; i.e. an empty JSON object should be returned when Raw is not set. This fixes failing test case TestReregistration in net_test.go --- marshal.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/marshal.go b/marshal.go index 3466148..9af0290 100644 --- a/marshal.go +++ b/marshal.go @@ -113,5 +113,10 @@ func (i *InstanceMetadata) MarshalJSON() ([]byte, error) { if i.parsed != nil { return json.Marshal(i.parsed) } + + if i.Raw == nil { + i.Raw = []byte("{}") + } + return i.Raw, nil } From 68711f8400b77d61b3a9098ac08f55647642081b Mon Sep 17 00:00:00 2001 From: Stev Witzel Date: Sun, 6 Sep 2015 00:28:13 +0100 Subject: [PATCH 4/6] Prevent XML/JSON mixup in TestReregistration Don't re-use instance between XML and JSON tests as InstanceMetadata.Raw does not seem to handle this at the moment. --- tests/net_test.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/tests/net_test.go b/tests/net_test.go index 8e227b9..283e9f2 100644 --- a/tests/net_test.go +++ b/tests/net_test.go @@ -86,33 +86,39 @@ func TestRegistration(t *testing.T) { func TestReregistration(t *testing.T) { e, _ := fargo.NewConnFromConfigFile("./config_sample/local.gcfg") - i := fargo.Instance{ - HostName: "i-123456", - Port: 9090, - App: "TESTAPP", - IPAddr: "127.0.0.10", - VipAddress: "127.0.0.10", - DataCenterInfo: fargo.DataCenterInfo{Name: fargo.MyOwn}, - SecureVipAddress: "127.0.0.10", - Status: fargo.UP, - } + for _, j := range []bool{false, true} { e.UseJson = j + + i := fargo.Instance{ + HostName: "i-123456", + Port: 9090, + App: "TESTAPP", + IPAddr: "127.0.0.10", + VipAddress: "127.0.0.10", + DataCenterInfo: fargo.DataCenterInfo{Name: fargo.MyOwn}, + SecureVipAddress: "127.0.0.10", + Status: fargo.UP, + } + Convey("Register a TESTAPP instance", t, func() { Convey("Instance registers correctly", func() { err := e.RegisterInstance(&i) So(err, ShouldBeNil) }) }) + Convey("Reregister the TESTAPP instance", t, func() { Convey("Instance reregisters correctly", func() { err := e.ReregisterInstance(&i) So(err, ShouldBeNil) }) + Convey("Instance can check in", func() { err := e.HeartBeatInstance(&i) So(err, ShouldBeNil) }) + Convey("Instance can be gotten correctly", func() { ii, err := e.GetInstance(i.App, i.HostName) So(err, ShouldBeNil) From fdd2ee47366159263dfe23ac49ebd1c2f760f2d0 Mon Sep 17 00:00:00 2001 From: Stev Witzel Date: Fri, 4 Sep 2015 00:05:14 +0100 Subject: [PATCH 5/6] Allow for customizable instance IDs For context: https://github.com/Netflix/eureka/pull/85 https://github.com/Netflix/eureka/issues/24 --- net.go | 6 ++- struct.go | 2 + tests/instance_test.go | 95 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 tests/instance_test.go diff --git a/net.go b/net.go index 17d632d..bde59dc 100644 --- a/net.go +++ b/net.go @@ -291,9 +291,13 @@ func (e *EurekaConnection) HeartBeatInstance(ins *Instance) error { } func (i *Instance) Id() string { + if i.UniqueID != nil { + return i.UniqueID(*i) + } + if i.DataCenterInfo.Name == "Amazon" { return i.DataCenterInfo.Metadata.InstanceID } - return i.HostName + return i.HostName } diff --git a/struct.go b/struct.go index 0ce6e0b..19b0570 100644 --- a/struct.go +++ b/struct.go @@ -98,6 +98,8 @@ type Instance struct { LeaseInfo LeaseInfo `xml:"leaseInfo" json:"leaseInfo"` Metadata InstanceMetadata `xml:"metadata" json:"metadata"` + + UniqueID func(i Instance) string `xml:"-" json:"-"` } // Port struct used for JSON [un]marshaling only diff --git a/tests/instance_test.go b/tests/instance_test.go new file mode 100644 index 0000000..da01ad9 --- /dev/null +++ b/tests/instance_test.go @@ -0,0 +1,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") + }) + }) + }) +} From 8dc2081cf81a87c517b2726f73b9ceabc7015d4b Mon Sep 17 00:00:00 2001 From: Stev Witzel Date: Mon, 7 Sep 2015 21:48:48 +0100 Subject: [PATCH 6/6] Keep UniqueID around when refreshing the instance --- net.go | 1 + 1 file changed, 1 insertion(+) diff --git a/net.go b/net.go index bde59dc..85ea8fe 100644 --- a/net.go +++ b/net.go @@ -199,6 +199,7 @@ func (e *EurekaConnection) GetInstance(app, insId string) (*Instance, error) { func (e *EurekaConnection) readInstanceInto(ins *Instance) error { tins, err := e.GetInstance(ins.App, ins.Id()) if err == nil { + tins.UniqueID = ins.UniqueID *ins = *tins } return err