|
| 1 | +import { ToxiProxyContainer, TPClient } from "./toxiproxy-container"; |
| 2 | +import { GenericContainer, Network } from "testcontainers"; |
| 3 | +import { createClient } from "redis"; |
| 4 | + |
| 5 | +describe("ToxiProxyContainer", () => { |
| 6 | + jest.setTimeout(240_000); |
| 7 | + |
| 8 | + // Helper to connect to redis |
| 9 | + async function connectTo(url: string) { |
| 10 | + const client = createClient({ |
| 11 | + url, |
| 12 | + }); |
| 13 | + client.on("error", () => {}); // Ignore errors |
| 14 | + await client.connect(); |
| 15 | + expect(client.isOpen).toBeTruthy(); |
| 16 | + return client; |
| 17 | + } |
| 18 | + |
| 19 | + // create_proxy { |
| 20 | + it("Should create a proxy to an endpoint", async () => { |
| 21 | + const containerNetwork = await new Network().start(); |
| 22 | + const redisContainer = await new GenericContainer("redis:7.2") |
| 23 | + .withNetwork(containerNetwork) |
| 24 | + .withNetworkAliases("redis") |
| 25 | + .start(); |
| 26 | + |
| 27 | + const toxiproxyContainer = await new ToxiProxyContainer().withNetwork(containerNetwork).start(); |
| 28 | + |
| 29 | + // Create the proxy between Toxiproxy and Redis |
| 30 | + const redisProxy = await toxiproxyContainer.createProxy({ |
| 31 | + name: "redis", |
| 32 | + upstream: "redis:6379", |
| 33 | + }); |
| 34 | + |
| 35 | + const url = `redis://${redisProxy.host}:${redisProxy.port}`; |
| 36 | + const client = await connectTo(url); |
| 37 | + await client.set("key", "val"); |
| 38 | + expect(await client.get("key")).toBe("val"); |
| 39 | + |
| 40 | + await client.disconnect(); |
| 41 | + await toxiproxyContainer.stop(); |
| 42 | + await redisContainer.stop(); |
| 43 | + }); |
| 44 | + // } |
| 45 | + |
| 46 | + // enabled_disabled { |
| 47 | + it("Should enable and disable a proxy", async () => { |
| 48 | + const containerNetwork = await new Network().start(); |
| 49 | + const redisContainer = await new GenericContainer("redis:7.2") |
| 50 | + .withNetwork(containerNetwork) |
| 51 | + .withNetworkAliases("redis") |
| 52 | + .start(); |
| 53 | + |
| 54 | + const toxiproxyContainer = await new ToxiProxyContainer().withNetwork(containerNetwork).start(); |
| 55 | + |
| 56 | + // Create the proxy between Toxiproxy and Redis |
| 57 | + const redisProxy = await toxiproxyContainer.createProxy({ |
| 58 | + name: "redis", |
| 59 | + upstream: "redis:6379", |
| 60 | + }); |
| 61 | + |
| 62 | + const url = `redis://${redisProxy.host}:${redisProxy.port}`; |
| 63 | + const client = await connectTo(url); |
| 64 | + |
| 65 | + await client.set("key", "val"); |
| 66 | + expect(await client.get("key")).toBe("val"); |
| 67 | + |
| 68 | + // Disable any new connections to the proxy |
| 69 | + await redisProxy.setEnabled(false); |
| 70 | + |
| 71 | + await expect(client.ping()).rejects.toThrow(); |
| 72 | + |
| 73 | + // Enable the proxy again |
| 74 | + await redisProxy.setEnabled(true); |
| 75 | + |
| 76 | + expect(await client.ping()).toBe("PONG"); |
| 77 | + |
| 78 | + await client.disconnect(); |
| 79 | + await toxiproxyContainer.stop(); |
| 80 | + await redisContainer.stop(); |
| 81 | + }); |
| 82 | + // } |
| 83 | + |
| 84 | + // adding_toxic { |
| 85 | + it("Should add a toxic to a proxy and then remove", async () => { |
| 86 | + const containerNetwork = await new Network().start(); |
| 87 | + const redisContainer = await new GenericContainer("redis:7.2") |
| 88 | + .withNetwork(containerNetwork) |
| 89 | + .withNetworkAliases("redis") |
| 90 | + .start(); |
| 91 | + |
| 92 | + const toxiproxyContainer = await new ToxiProxyContainer().withNetwork(containerNetwork).start(); |
| 93 | + |
| 94 | + // Create the proxy between Toxiproxy and Redis |
| 95 | + const redisProxy = await toxiproxyContainer.createProxy({ |
| 96 | + name: "redis", |
| 97 | + upstream: "redis:6379", |
| 98 | + }); |
| 99 | + |
| 100 | + const url = `redis://${redisProxy.host}:${redisProxy.port}`; |
| 101 | + const client = await connectTo(url); |
| 102 | + |
| 103 | + // See https://github.com/ihsw/toxiproxy-node-client for details on the instance interface |
| 104 | + const toxic = await redisProxy.instance.addToxic<TPClient.Latency>({ |
| 105 | + attributes: { |
| 106 | + jitter: 50, |
| 107 | + latency: 1500, |
| 108 | + }, |
| 109 | + name: "upstream-latency", |
| 110 | + stream: "upstream", |
| 111 | + toxicity: 1, // 1 is 100% |
| 112 | + type: "latency", |
| 113 | + }); |
| 114 | + |
| 115 | + const before = Date.now(); |
| 116 | + await client.ping(); |
| 117 | + const after = Date.now(); |
| 118 | + expect(after - before).toBeGreaterThan(1000); |
| 119 | + |
| 120 | + await toxic.remove(); |
| 121 | + |
| 122 | + await client.disconnect(); |
| 123 | + await toxiproxyContainer.stop(); |
| 124 | + await redisContainer.stop(); |
| 125 | + }); |
| 126 | + // } |
| 127 | + |
| 128 | + it("Should create multiple proxies", async () => { |
| 129 | + const containerNetwork = await new Network().start(); |
| 130 | + const redisContainer = await new GenericContainer("redis:7.2") |
| 131 | + .withNetwork(containerNetwork) |
| 132 | + .withNetworkAliases("redis") |
| 133 | + .start(); |
| 134 | + |
| 135 | + const toxiproxyContainer = await new ToxiProxyContainer().withNetwork(containerNetwork).start(); |
| 136 | + |
| 137 | + // Create the proxy between Toxiproxy and Redis |
| 138 | + const redisProxy = await toxiproxyContainer.createProxy({ |
| 139 | + name: "redis", |
| 140 | + upstream: "redis:6379", |
| 141 | + }); |
| 142 | + |
| 143 | + // Create the proxy between Toxiproxy and Redis |
| 144 | + const redisProxy2 = await toxiproxyContainer.createProxy({ |
| 145 | + name: "redis2", |
| 146 | + upstream: "redis:6379", |
| 147 | + }); |
| 148 | + |
| 149 | + const url = `redis://${redisProxy.host}:${redisProxy.port}`; |
| 150 | + const client = await connectTo(url); |
| 151 | + await client.set("key", "val"); |
| 152 | + expect(await client.get("key")).toBe("val"); |
| 153 | + |
| 154 | + const url2 = `redis://${redisProxy2.host}:${redisProxy2.port}`; |
| 155 | + const client2 = await connectTo(url2); |
| 156 | + expect(await client2.get("key")).toBe("val"); |
| 157 | + |
| 158 | + await client.disconnect(); |
| 159 | + await client2.disconnect(); |
| 160 | + await toxiproxyContainer.stop(); |
| 161 | + await redisContainer.stop(); |
| 162 | + }); |
| 163 | + |
| 164 | + it("Throws an error when too many proxies are created", async () => { |
| 165 | + const toxiproxyContainer = await new ToxiProxyContainer().start(); |
| 166 | + |
| 167 | + for (let i = 0; i < 32; i++) { |
| 168 | + await toxiproxyContainer.createProxy({ |
| 169 | + name: "test-" + i, |
| 170 | + upstream: `google.com:80`, |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | + await expect( |
| 175 | + toxiproxyContainer.createProxy({ |
| 176 | + name: "test-32", |
| 177 | + upstream: `google.com:80`, |
| 178 | + }) |
| 179 | + ).rejects.toThrow(); |
| 180 | + |
| 181 | + await toxiproxyContainer.stop(); |
| 182 | + }); |
| 183 | +}); |
0 commit comments