func TestGetFloat(t *testing.T) { Convey("Given an instance", t, func() { instance := new(fargo.Instance) Convey("With metadata", func() { metadata := new(fargo.InstanceMetadata) instance.Metadata = *metadata Convey("That has a float64 value", func() { key := "d" value := 1.9 metadata.Raw = []byte("<" + key + ">" + strconv.FormatFloat(value, 'f', -1, 64) + "</" + key + ">") Convey("GetFloat64 should return that value", func() { actualValue, err := metadata.GetFloat64(key) So(err, ShouldBeNil) So(actualValue, ShouldEqual, value) }) }) Convey("That has a float32 value", func() { key := "d" value := 1.9 metadata.Raw = []byte("<" + key + ">" + strconv.FormatFloat(value, 'f', -1, 32) + "</" + key + ">") Convey("GetFloat32 should return that value", func() { actualValue, err := metadata.GetFloat32(key) So(err, ShouldBeNil) So(actualValue, ShouldEqual, float32(1.9)) }) }) }) }) }
func TestGetInt(t *testing.T) { Convey("Given an instance", t, func() { instance := new(fargo.Instance) Convey("With metadata", func() { metadata := new(fargo.InstanceMetadata) instance.Metadata = *metadata Convey("That has a single integer value", func() { key := "d" value := 1 metadata.Raw = []byte("<" + key + ">" + strconv.Itoa(value) + "</" + key + ">") Convey("GetInt should return that value", func() { actualValue, err := metadata.GetInt(key) So(err, ShouldBeNil) So(actualValue, ShouldEqual, value) }) }) }) }) }
func TestSerializeMeta(t *testing.T) { Convey("Given an instance", t, func() { instance := new(fargo.Instance) Convey("With metadata", func() { instance.SetMetadataString("test", "value") Convey("Serializing results in correct JSON", func() { b, err := instance.Metadata.MarshalJSON() So(err, ShouldBeNil) So(string(b), ShouldEqual, "{\"test\":\"value\"}") }) Convey("Serializing results in correct XML", func() { b, err := xml.Marshal(instance.Metadata) So(err, ShouldBeNil) So(string(b), ShouldEqual, "<InstanceMetadata><test>value</test></InstanceMetadata>") }) Convey("Blank metadata results in blank XML", func() { metadata := new(fargo.InstanceMetadata) b, err := xml.Marshal(metadata) So(err, ShouldBeNil) So(string(b), ShouldEqual, "<InstanceMetadata></InstanceMetadata>") }) }) }) }
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") }) }) }) }
func (e *eurekaRegistry) instanceRegistered(instance *fargo.Instance) bool { _, err := e.conn.GetInstance(instance.App, instance.UniqueID(*instance)) return err == nil }