Ejemplo n.º 1
0
func (s *EtcdSuite) TestSetMeta(c *C) {
	events := make(chan *discoverd.Event, 1)
	s.state.Subscribe("a", false, discoverd.EventKindServiceMeta, events)

	c.Assert(s.backend.AddService("a", nil), IsNil)
	c.Assert(s.backend.StartSync(), IsNil)

	// with service that doesn't exist
	err := s.backend.SetServiceMeta("b", &discoverd.ServiceMeta{Data: []byte("foo")})
	c.Assert(err, FitsTypeOf, NotFoundError{})

	// new with wrong index
	err = s.backend.SetServiceMeta("a", &discoverd.ServiceMeta{Data: []byte("foo"), Index: 1})
	c.Assert(hh.IsPreconditionFailedError(err), Equals, true)

	// new
	meta := &discoverd.ServiceMeta{Data: []byte("foo"), Index: 0}
	c.Assert(s.backend.SetServiceMeta("a", meta), IsNil)
	assertMetaEvent(c, events, "a", meta)

	// index=0 set with existing
	err = s.backend.SetServiceMeta("a", &discoverd.ServiceMeta{Data: []byte("foo"), Index: 0})
	c.Assert(hh.IsObjectExistsError(err), Equals, true)

	// set with existing, valid index
	meta.Data = []byte("bar")
	c.Assert(s.backend.SetServiceMeta("a", meta), IsNil)
	assertMetaEvent(c, events, "a", meta)

	// set with existing, low index
	meta.Index--
	meta.Data = []byte("baz")
	err = s.backend.SetServiceMeta("a", meta)
	c.Assert(hh.IsPreconditionFailedError(err), Equals, true)
}
Ejemplo n.º 2
0
func (s *HTTPSuite) TestServiceMeta(c *C) {
	srv := s.client.Service("a")
	events := make(chan *discoverd.Event)
	stream, err := srv.Watch(events)
	c.Assert(err, IsNil)
	defer stream.Close()
	assertEvent(c, events, "a", discoverd.EventKindCurrent, nil)

	// Get non-existent meta
	_, err = srv.GetMeta()
	c.Assert(discoverd.IsNotFound(err), Equals, true)

	// Set new meta
	meta := &discoverd.ServiceMeta{Data: []byte("1")}
	c.Assert(srv.SetMeta(meta), IsNil)
	c.Assert(meta.Index > 0, Equals, true)
	assertMetaEvent(c, events, "a", meta)

	res, err := srv.GetMeta()
	c.Assert(err, IsNil)
	c.Assert(res, DeepEquals, meta)

	// Set meta on non-existent service
	err = s.client.Service("foo").SetMeta(meta)
	c.Assert(discoverd.IsNotFound(err), Equals, true, Commentf("err = %#v", err))

	// Update meta
	meta.Data = []byte("2")
	c.Assert(srv.SetMeta(meta), IsNil)
	c.Assert(meta.Index > res.Index, Equals, true, Commentf("old = %d, new = %d", res.Index, meta.Index))
	assertMetaEvent(c, events, "a", meta)

	// Update meta with wrong index
	meta.Index--
	err = srv.SetMeta(meta)
	c.Assert(err, NotNil)
	c.Assert(hh.IsPreconditionFailedError(err), Equals, true)
}