Disclaimer: Always test your application against a real Couchbase Cluster to ensure proper proper functionality
A little known project known as CouchbaseMock has existed for quite a while here at Couchbase. A long long time ago, back when Couchbase was still known as Membase, we needed a simple application to act as a server to test that the clients functioned properly.
CouchbaseMock still serves this purpose today; although we run an extensive suite of tests against a real cluster before each SDK release, the CouchbaseMock is still used for smoke testing (or sanity testing) before each commit is merged to the Git repository.
The ubiquity of Java and the small footprint of the mock (about a 2.5MB download) make it ideal for serving as a cluster in resource constrained systems, where a full Couchbase Cluster cannot be installed. The mock additionally contains some special commands to simulate various operations (Such as failover, rebalance, and replication) in a controlled and detereminstic manner.
In the past few weeks, I have been working on adding view support to the Mock! – you've heard correctly: the mock can now understand design documents, and run user-defined map or reduce functions against all documents in its database. View support was required so that I could integrate higher level functionality within the C SDK (libcouchbase), the details of which will be published in a different post.
MapReduce Views in CouchbaseMock
The mock responds properly to all the view related APIs that Couchbase offers. You can use the SDK of your choice (or curl) to issue the appropriate REST API calls to create design documents and query views.
The actual JSON output was also formatted to insert line breaks after each row, to make it easier to read from the terminal.
Finally, the actual beer-sample database is also bundled with the Mock. Using XZ compression, the database takes up very little room in the actual download.
Administrative REST API in CouchbaseMock
The mock also allows the creation and deletion of buckets on the fly. You can accomplish this using the same REST API that Couchbase understands. Currently only basic bucket creation, deletion, and listing are supported, but more features may be implemented in the future.
To use CouchbaseMock, first get it from the source repository:
git clone git://github.com/couchbase/CouchbaseMock
# Ensure you have Maven and a JDK installed. On debian this might look like
# sudo apt-get install openjdk-7-jdk maven
Once built, you should have a CouchbaseMock-1.0.0-
Finally, using it is as simple as running:
<span class="s1">java -jar target/CouchbaseMock-1.0.0-gd58ed25.jar</span> -S
The -S option enables the beer-sample bucket. By default the mock will run on port 8091, but this can be customized using the command line options.
Once the mock is up and running, you may interact with it from the SDK of your choosing; this includes things like performing key-value operations, defining/querying views, and creating/destroying buckets.
The CouchbaseMock is a convenient way to spin up “something like” a Couchbase cluster. It should work as a drop-in for most applications and SDKs – in fact, the view tests for the Mock itself are more or less copy/pasted from the Java SDK.
The mock is an internal project and its primary aim is SDK testing. As such, the mock is not the official way to test applications using Couchbase, nor is there any guarantee of stability regarding the mock's features and interfaces – things may be added or removed as necessary.
The above being said, the mock remains a convenient way to test basic functionality of applications using couchbase (or SDKs!) without requiring a full cluster install.
Additionally, if your application itself is written in Java, you may even wish to venture inside the Mock's source code for ways to embed it within tests.