Skip to content

Etcd Module

Etcd is a strongly consistent, distributed key-value store that provides a reliable way to store data that needs to be accessed by a distributed system or cluster of machines.

Install

npm install @testcontainers/etcd --save-dev

Examples

it("should connect and perform read/write operations", async () => {
  const container = await new EtcdContainer().start();
  const client = new Etcd3({
    hosts: container.getClientEndpoint(),
  });
  const key = "foo";
  const value = "bar";

  await client.put(key).value(value);
  const result = await client.get(key).string();
  expect(result).toEqual(value);

  await container.stop();
});
it("should subscribe to key changes", async () => {
  const subscriber = vi.fn();
  const container = await new EtcdContainer().start();
  const client = new Etcd3({
    hosts: container.getClientEndpoint(),
  });
  const key = "foo";
  const value = "bar";
  const watcher = await client.watch().key(key).create();
  watcher.on("put", subscriber);
  await client.put(key).value(value);
  await setTimeout(1_000);
  expect(subscriber).toHaveBeenCalled();
  await watcher.cancel();
  await container.stop();
});