func EnsurePath(z *zk.Conn, pathParts []string, perm []zk.ACL) (err error) { path := BuildPath(pathParts) stat, err := z.Exists(path) if err != nil || stat != nil { return } currentParts := make([]string, 0, len(pathParts)) for _, part := range pathParts { currentParts = append(currentParts, part) _, err = z.Create(BuildPath(currentParts), "", 0, perm) if err != nil { if zkErr, ok := err.(*zk.Error); ok && zkErr.Code == zk.ZNODEEXISTS { err = nil } else { return } } } return }
. "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "strconv" ) var _ = Describe("ZkUtils", func() { Describe("BuildPath", func() { It("should build path from parts", func() { Expect(BuildPath([]string{"part1", "part2"})).To(Equal("/part1/part2")) }) }) Describe("EnsurePath", func() { var s *TestServer var z *zk.Conn BeforeEach(func() { port, err := netutils.UnusedPort() Expect(err).NotTo(HaveOccurred()) s, err = NewTestServer(port) Expect(err).NotTo(HaveOccurred()) if err != nil { return } zz, session, err := zk.Dial("localhost:"+strconv.Itoa(port), 5e9) Expect(err).NotTo(HaveOccurred())
package serviceregistry_test import ( "github.com/koofr/go-netutils" . "github.com/koofr/go-serviceregistry" "github.com/koofr/go-zkutils" zk "github.com/koofr/gozk" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "strconv" ) var _ = Describe("Zkregistry", func() { var s *zkutils.TestServer var r *ZkRegistry var z *zk.Conn BeforeEach(func() { port, err := netutils.UnusedPort() Expect(err).NotTo(HaveOccurred()) s, err = zkutils.NewTestServer(port) Expect(err).NotTo(HaveOccurred()) if err != nil { return } zz, session, err := zk.Dial("localhost:"+strconv.Itoa(port), 5e9) Expect(err).NotTo(HaveOccurred())