예제 #1
0
func ListenToZooKeeper(config c.Zookeeper, deb bool) (chan zk.Event, chan bool) {
	c, _, err := zk.Connect(config.ConnectionString(), time.Second)

	if err != nil {
		panic(err)
	}

	return ListenToConn(c, config.Path, deb, config.Delay())
}
예제 #2
0
func createAndListen(conf configuration.Zookeeper) (chan zk.Event, *zk.Conn) {
	conn, _, err := zk.Connect(conf.ConnectionString(), time.Second*10)

	if err != nil {
		log.Panic(err)
	}

	ch, _ := qzk.ListenToConn(conn, conf.Path, true, conf.Delay())
	return ch, conn
}
예제 #3
0
func TestZKStorage(t *testing.T) {
	conn, _, err := zk.Connect(zkConf.ConnectionString(), zkTimeout)
	orPanic(err)

	Convey("#NewZKStorage", t, func() {
		cleanZK(conn)
		s, err := NewZKStorage(conn, zkConf)

		So(err, ShouldBeNil)

		Convey("it should implement the Storage interface", func() {
			_, ok := interface{}(s).(Storage)
			So(ok, ShouldBeTrue)
		})

		Convey("it should have created the base path", func() {
			exists, _, err := conn.Exists(zkConf.Path)

			So(err, ShouldBeNil)
			So(exists, ShouldBeTrue)
		})
	})

	Convey("#ZKStorage.All", t, func() {
		s, err := NewZKStorage(conn, zkConf)
		So(err, ShouldBeNil)

		Convey("when I get all in an empty zookeeper", func() {
			cleanZK(conn)
			entries, err := s.All()

			So(err, ShouldBeNil)

			Convey("there should be no entries", func() {
				So(len(entries), ShouldEqual, 0)
			})
		})

		Convey("when I get all in a legacy/v1 zookeeper", func() {
			cleanZK(conn)
			loadToZK(conn, [][2]string{
				[2]string{zkConf.Path, ""},
				[2]string{zkConf.Path + "/test", "hdr(host) -i foo"},
				[2]string{zkConf.Path + "/test2", "fozbaz"},
			})

			entries, err := s.All()

			So(err, ShouldBeNil)

			Convey("there should be the correct number of entries", func() {
				So(len(entries), ShouldEqual, 2)
			})

			Convey("there should be an entry with the id test", func() {
				var test Service
				found := false
				for _, i := range entries {
					if i.Id == "test" {
						test = i
						found = true
						break
					}
				}
				So(found, ShouldBeTrue)

				Convey("which should have an acl 'hdr(host) -i foo'", func() {
					So(test.Acl, ShouldEqual, "hdr(host) -i foo")
					So(test.Config["Acl"], ShouldEqual, "hdr(host) -i foo")
				})
			})
		})

		Convey("when I get all in a mixed v1/v2 zookeeper", func() {
			cleanZK(conn)
			loadToZK(conn, [][2]string{
				[2]string{zkConf.Path, ""},
				[2]string{zkConf.Path + "/test", `{"version": "2", "config": {"Acl": "foo", "arb": "barb"}}`},
				[2]string{zkConf.Path + "/test2", "fozbaz"},
			})

			entries, err := s.All()

			So(err, ShouldBeNil)

			Convey("there should be the correct number of entries", func() {
				So(len(entries), ShouldEqual, 2)
			})

			Convey("there should be an entry with the id test", func() {
				var test Service
				found := false
				for _, i := range entries {
					if i.Id == "test" {
						test = i
						found = true
						break
					}
				}
				So(found, ShouldBeTrue)

				Convey("which should have an acl 'foo'", func() {
					So(test.Acl, ShouldEqual, "foo")
					So(test.Config["Acl"], ShouldEqual, "foo")
				})

				Convey("which should have a config entry 'arb'", func() {
					So(test.Config["arb"], ShouldEqual, "barb")
				})
			})
		})
	})

	Convey("#ZKStorage.Upsert", t, func() {
		s, err := NewZKStorage(conn, zkConf)
		So(err, ShouldBeNil)

		testService := Service{
			Id: "test",
			Config: map[string]string{
				"Acl":   "foo",
				"barst": "carst",
			},
		}

		Convey("when I insert into an empty key", func() {
			cleanZK(conn)
			err := s.Upsert(testService)
			So(err, ShouldBeNil)

			entries, err := s.All()
			So(err, ShouldBeNil)

			Convey("there should be 1 entry", func() {
				So(len(entries), ShouldEqual, 1)

				readEntry := entries[0]

				Convey("which should have an Acl 'foo'", func() {
					So(readEntry.Acl, ShouldEqual, "foo")
					So(readEntry.Config["Acl"], ShouldEqual, "foo")
				})

				Convey("which should have a config entry 'barst'", func() {
					So(readEntry.Config["barst"], ShouldEqual, "carst")
				})
			})
		})

		Convey("when I insert into an existing key", func() {
			cleanZK(conn)
			loadToZK(conn, [][2]string{
				[2]string{zkConf.Path, ""},
				[2]string{zkConf.Path + "/test", "fozbaz"},
			})

			err := s.Upsert(testService)
			So(err, ShouldBeNil)

			entries, err := s.All()
			So(err, ShouldBeNil)

			Convey("there should be 1 entry", func() {
				So(len(entries), ShouldEqual, 1)

				readEntry := entries[0]

				Convey("which should have an Acl 'foo'", func() {
					So(readEntry.Acl, ShouldEqual, "foo")
					So(readEntry.Config["Acl"], ShouldEqual, "foo")
				})

				Convey("which should have a config entry 'barst'", func() {
					So(readEntry.Config["barst"], ShouldEqual, "carst")
				})
			})
		})
	})

	Convey("#ZKStorage.Delete", t, func() {
		s, err := NewZKStorage(conn, zkConf)
		So(err, ShouldBeNil)

		Convey("when I delete the only service", func() {
			cleanZK(conn)
			loadToZK(conn, [][2]string{
				[2]string{zkConf.Path, ""},
				[2]string{zkConf.Path + "/test", "fozbaz"},
			})

			err := s.Delete("test")
			So(err, ShouldBeNil)

			Convey("there should be zero entries", func() {
				entries, err := s.All()
				So(err, ShouldBeNil)

				So(len(entries), ShouldEqual, 0)
			})
		})

		Convey("when I delete an non-existant service", func() {
			cleanZK(conn)

			err := s.Delete("test")

			Convey("it should error", func() {
				So(err, ShouldNotBeNil)
			})
			So(err, ShouldNotBeNil)

			Convey("there should be zero entries", func() {
				entries, err := s.All()
				So(err, ShouldBeNil)

				So(len(entries), ShouldEqual, 0)
			})
		})
	})
}