1

I am using the library gozk to interface my application with a production zookeeper server. I'd like to test that the application create the correct nodes, that they contain the correct content for various cases, and that the DataWatch and NodeWatch are set properly:

i.e. the application performs exactly what should based on the node and data updates.

Can I have a mock zookeeper server to be created and destroyed during unit tests only, with capability to artificially create new node and set node contents? Is there a different way than to manually create a zookeeper server and use it?

A solution already exists for java

Community
  • 1
  • 1
Lorenzo Belli
  • 1,767
  • 4
  • 25
  • 46
  • Your referenced solution in java is not actually a mock, but running real instance of Zookeeper. Since Zookeeper is written in Java, the only way doing the same thing in go is maybe using `os/exec` package to run Zookeeper instance. – ymonad Feb 08 '17 at 01:36

2 Answers2

2

I would recommend making the code of yours that calls zookeeper become an interface.

Then during testing you sub in a 'mockZookeeperConn' object that just returns values as though it was really connecting to the server (but the return values are hardcoded)

Ben Echols
  • 479
  • 2
  • 8
0

@Ben Echols 's answer is very good.

As further, you can try "build constraints".

You can configure different build tags on real-zk and mock-zk code.

For example, we configure "product" for real-zk code and "mock" for mock-zk code.

Thus there are two ways to run unittests:

  • go test -tags mock if there isn't a zk env.
  • go test -tags product if there is an available zk env.
cnby
  • 389
  • 4
  • 15
  • Yep! Thank for the hint. I ended up using this technique to enable automatic zk unit test only when a serve instance is available. – Lorenzo Belli Feb 08 '17 at 16:03