func TestHostNetworking(t *testing.T) { listener, st, dc := setup("192.168.5.135") st.AddService("blorp-svc", data.Service{}) addGroup(st, "blorp-svc", &data.AddressSpec{ Type: data.FIXED, Port: 8080, }, "image", "blorp-image") dc.startContainers(container{ NetworkMode: "host", ID: "blorp-instance", IPAddress: "", Image: "blorp-image:tag", }) listener.ReadInServices() listener.ReadExistingContainers() require.Len(t, allInstances(st), 1) store.ForeachInstance(st, "blorp-svc", func(_, _ string, inst data.Instance) error { require.Equal(t, listener.hostIP, inst.Address) require.Equal(t, 8080, inst.Port) require.Equal(t, data.LIVE, inst.State) return nil }) }
func TestNoAddress(t *testing.T) { listener, st, dc := setup("192.168.3.4") st.AddService("important-svc", data.Service{}) addGroup(st, "important-svc", &data.AddressSpec{ Type: data.MAPPED, Port: 8080, }, "image", "important-image") dc.startContainers(container{ ID: "oops-instance", IPAddress: "10.13.14.15", Image: "important-image:greatest", // No published port }) listener.ReadInServices() listener.ReadExistingContainers() require.Len(t, allInstances(st), 1) store.ForeachInstance(st, "blorp-svc", func(_, _ string, inst data.Instance) error { require.Equal(t, listener.hostIP, inst.Address) require.Equal(t, 3456, inst.Port) require.Equal(t, data.NOADDR, inst.State) return nil }) }
func TestMappedPort(t *testing.T) { listener, st, dc := setup("11.98.99.98") st.AddService("blorp-svc", data.Service{}) addGroup(st, "blorp-svc", &data.AddressSpec{ Type: data.MAPPED, Port: 8080, }, "image", "blorp-image") dc.startContainers(container{ ID: "blorp-instance", IPAddress: "10.13.14.15", Image: "blorp-image:tag", Ports: map[string]string{ "8080/tcp": "3456", }, }) listener.ReadInServices() listener.ReadExistingContainers() require.Len(t, allInstances(st), 1) store.ForeachInstance(st, "blorp-svc", func(_, _ string, inst data.Instance) error { require.Equal(t, listener.hostIP, inst.Address) require.Equal(t, 3456, inst.Port) require.Equal(t, data.LIVE, inst.State) return nil }) }
func testInstances(s store.Store, t *testing.T) { require.Nil(t, s.AddService("svc", testService)) require.Nil(t, s.AddInstance("svc", "inst", testInst)) instances := func() map[string]data.Instance { insts := make(map[string]data.Instance) require.Nil(t, store.ForeachInstance(s, "svc", func(_, n string, inst data.Instance) error { insts[n] = inst return nil })) return insts } require.Equal(t, map[string]data.Instance{"inst": testInst}, instances()) serviceInstances := func() map[string]data.Instance { insts := make(map[string]data.Instance) require.Nil(t, store.ForeachServiceInstance(s, nil, func(sn string, in string, inst data.Instance) error { insts[sn+" "+in] = inst return nil })) return insts } require.Equal(t, map[string]data.Instance{"svc inst": testInst}, serviceInstances()) require.Nil(t, s.RemoveInstance("svc", "inst")) require.Equal(t, map[string]data.Instance{}, instances()) require.Equal(t, map[string]data.Instance{}, serviceInstances()) }
// The service has been changed; re-evaluate which containers belong, // and which don't. Assume we have a correct list of containers. func (l *Listener) redefineService(serviceName string, newService *store.ServiceInfo) error { svc, found := l.services[serviceName] if !found { svc = &service{} l.services[serviceName] = svc } svc.ServiceInfo = newService svc.localInstances = make(instanceSet) var err error for _, container := range l.containers { if _, err = l.evaluate(container, svc); err != nil { return err } } // remove any instances for this service that do not match return store.ForeachInstance(l.store, serviceName, func(_, instanceName string, inst data.Instance) error { if !svc.includes(instanceName) && l.owns(inst) { return l.store.RemoveInstance(serviceName, instanceName) } return nil }) }