func TestMetadataReading(t *testing.T) { e, _ := fargo.NewConnFromConfigFile("./config_sample/local.gcfg") for _, j := range []bool{false, true} { e.UseJson = j Convey("Read empty instance metadata", t, func() { a, err := e.GetApp("EUREKA") So(err, ShouldBeNil) i := a.Instances[0] _, err = i.Metadata.GetString("SomeProp") So(err, ShouldBeNil) }) Convey("Read valid instance metadata", t, func() { a, err := e.GetApp("TESTAPP") So(err, ShouldBeNil) So(len(a.Instances), ShouldBeGreaterThan, 0) if len(a.Instances) == 0 { return } i := a.Instances[0] err = e.AddMetadataString(i, "SomeProp", "AValue") So(err, ShouldBeNil) v, err := i.Metadata.GetString("SomeProp") So(err, ShouldBeNil) So(v, ShouldEqual, "AValue") }) } }
func DontTestDeregistration(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, } Convey("Register a TESTAPP instance", t, func() { Convey("Instance registers correctly", func() { err := e.RegisterInstance(&i) So(err, ShouldBeNil) }) }) Convey("Deregister the TESTAPP instance", t, func() { Convey("Instance deregisters correctly", func() { err := e.DeregisterInstance(&i) So(err, ShouldBeNil) }) Convey("Instance cannot check in", func() { err := e.HeartBeatInstance(&i) So(err, ShouldNotBeNil) }) }) }
func TestUpdateStatus(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 Convey("Register an instance to TESTAPP", t, func() { Convey("Instance registers correctly", func() { err := e.RegisterInstance(&i) So(err, ShouldBeNil) }) }) Convey("Update an instance status", t, func() { Convey("Instance updates to OUT_OF_SERVICE correctly", func() { err := e.UpdateInstanceStatus(&i, fargo.OUTOFSERVICE) So(err, ShouldBeNil) }) Convey("Instance updates to UP corectly", func() { err := e.UpdateInstanceStatus(&i, fargo.UP) So(err, ShouldBeNil) }) }) } }
func TestGetMultipleInstancesByVIPAddress(t *testing.T) { if testing.Short() { t.SkipNow() } e, _ := fargo.NewConnFromConfigFile("./config_sample/local.gcfg") cacheDelay := 35 * time.Second for _, e.UseJson = range []bool{false, true} { Convey("When the VIP address has one instance", t, withRegisteredInstance(&e, func(instance *fargo.Instance) { Convey("when the VIP address has two instances", withCustomRegisteredInstance(&e, "TESTAPP2", "i-234567", func(_ *fargo.Instance) { Convey("requesting the instances by that VIP address should provide them", func() { time.Sleep(cacheDelay) vipAddress := "app" instances, err := e.GetInstancesByVIPAddress(vipAddress) So(err, ShouldBeNil) So(instances, ShouldHaveLength, 2) for _, ins := range instances { So(ins.VipAddress, ShouldEqual, vipAddress) } So(instances[0], ShouldNotEqual, instances[1]) }) })) })) time.Sleep(cacheDelay) } }
func TestGetSingleInstanceByVIPAddress(t *testing.T) { if testing.Short() { t.SkipNow() } e, _ := fargo.NewConnFromConfigFile("./config_sample/local.gcfg") cacheDelay := 35 * time.Second vipAddress := "app" for _, e.UseJson = range []bool{false, true} { Convey("When the VIP address has one instance", t, withRegisteredInstance(&e, func(instance *fargo.Instance) { time.Sleep(cacheDelay) instances, err := e.GetInstancesByVIPAddress(vipAddress) So(err, ShouldBeNil) So(instances, ShouldHaveLength, 1) So(instances[0].VipAddress, ShouldEqual, vipAddress) Convey("requesting the instances by that VIP address with status UP should provide that one", func() { instances, err := e.GetInstancesByVIPAddress(vipAddress, fargo.ThatAreUp) So(err, ShouldBeNil) So(instances, ShouldHaveLength, 1) So(instances[0].VipAddress, ShouldEqual, vipAddress) Convey("and when the instance has a status other than UP", func() { otherStatus := fargo.OUTOFSERVICE So(otherStatus, ShouldNotEqual, fargo.UP) err := e.UpdateInstanceStatus(instance, otherStatus) So(err, ShouldBeNil) Convey("selecting instances with that other status should provide that one", func() { time.Sleep(cacheDelay) instances, err := e.GetInstancesByVIPAddress(vipAddress, fargo.WithStatus(otherStatus)) So(err, ShouldBeNil) So(instances, ShouldHaveLength, 1) Convey("And selecting instances with status UP should provide none", func() { // Ensure that we tolerate a nil option safely. instances, err := e.GetInstancesByVIPAddress(vipAddress, fargo.ThatAreUp, nil) So(err, ShouldBeNil) So(instances, ShouldBeEmpty) }) }) }) }) })) Convey("When the secure VIP address has one instance", t, withRegisteredInstance(&e, func(_ *fargo.Instance) { Convey("requesting the instances by that VIP address should provide that one", func() { time.Sleep(cacheDelay) // Ensure that we tolerate a nil option safely. instances, err := e.GetInstancesBySecureVIPAddress(vipAddress, nil) So(err, ShouldBeNil) So(instances, ShouldHaveLength, 1) So(instances[0].SecureVipAddress, ShouldEqual, vipAddress) }) })) time.Sleep(cacheDelay) } }
func TestGetApps(t *testing.T) { e, _ := fargo.NewConnFromConfigFile("./config_sample/local.gcfg") for _, j := range []bool{false, true} { e.UseJson = j Convey("Pull applications", t, func() { a, _ := e.GetApps() So(len(a["EUREKA"].Instances), ShouldEqual, 2) }) Convey("Pull single application", t, func() { a, _ := e.GetApp("EUREKA") So(len(a.Instances), ShouldEqual, 2) for _, ins := range a.Instances { So(ins.IPAddr, ShouldBeIn, []string{"172.17.0.2", "172.17.0.3"}) } }) } }
func TestGetInstancesByNonexistentVIPAddress(t *testing.T) { e, _ := fargo.NewConnFromConfigFile("./config_sample/local.gcfg") for _, e.UseJson = range []bool{false, true} { Convey("Get instances by VIP address", t, func() { Convey("when the VIP address has no instances", func() { instances, err := e.GetInstancesByVIPAddress("nonexistent") So(err, ShouldBeNil) So(instances, ShouldBeEmpty) }) Convey("when the secure VIP address has no instances", func() { instances, err := e.GetInstancesBySecureVIPAddress("nonexistent") So(err, ShouldBeNil) So(instances, ShouldBeEmpty) }) }) } }
func TestReregistration(t *testing.T) { e, _ := fargo.NewConnFromConfigFile("./config_sample/local.gcfg") 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) So(ii.App, ShouldEqual, i.App) So(ii.HostName, ShouldEqual, i.HostName) }) }) } }
func TestRegistration(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 Convey("Fail to heartbeat a non-existent instance", t, func() { j := fargo.Instance{ HostName: "i-6543", 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, } err := e.HeartBeatInstance(&j) So(err, ShouldNotBeNil) So(err, shouldBearHTTPStatusCode, http.StatusNotFound) }) Convey("Register an instance to TESTAPP", t, func() { Convey("Instance registers correctly", func() { err := e.RegisterInstance(&i) So(err, ShouldBeNil) }) Convey("Instance can check in", func() { err := e.HeartBeatInstance(&i) So(err, ShouldBeNil) }) }) } }
func TestGetApps(t *testing.T) { e, _ := fargo.NewConnFromConfigFile("./config_sample/local.gcfg") for _, j := range []bool{false, true} { e.UseJson = j Convey("Pull applications", t, func() { a, _ := e.GetApps() So(len(a["EUREKA"].Instances), ShouldEqual, 2) }) Convey("Pull single application", t, func() { a, _ := e.GetApp("EUREKA") So(len(a.Instances), ShouldEqual, 2) for idx, ins := range a.Instances { if ins.HostName == "node1.localdomain" { So(a.Instances[idx].IPAddr, ShouldEqual, "172.16.0.11") So(a.Instances[idx].HostName, ShouldEqual, "node1.localdomain") } else { So(a.Instances[idx].IPAddr, ShouldEqual, "172.16.0.22") So(a.Instances[idx].HostName, ShouldEqual, "node2.localdomain") } } }) } }
// Register and keep the eureka connection func registerToEureka(configFile string) (fargo.EurekaConnection, fargo.Instance) { eurekaConn, _ = fargo.NewConnFromConfigFile(configFile) hostname, _ := os.Hostname() i := fargo.Instance{ HostName: hostname, Port: 8000, App: config.Eureka_app_name, IPAddr: hostname, VipAddress: config.Eureka_app_name, DataCenterInfo: fargo.DataCenterInfo{Name: fargo.Amazon}, SecureVipAddress: config.Eureka_ip_addr, Status: fargo.UP, HealthCheckUrl: "http://" + hostname + ":" + config.Server_port + "/dynamic-pricing-api/1.0/health", StatusPageUrl: "http://" + hostname + ":" + config.Server_port + "/dynamic-pricing-api/1.0/health", HomePageUrl: "http://" + hostname + ":" + config.Server_port + "/dynamic-pricing-api/1.0/health", } err := eurekaConn.RegisterInstance(&i) if err != nil { log.Error("%v", err) } return eurekaConn, i }