Shopify / toxiproxy

:alarm_clock: :fire: A TCP proxy to simulate network and system conditions for chaos and resiliency testing

Home Page:https://github.com/shopify/toxiproxy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ToxiproxyContainer.ContainerProxy deprecated

azevedoedison opened this issue · comments

Hi, I did an implementation using ToxiproxyContainer.ContainerProxy and it works easily. However, the method is deprecated and I didn't find an example of how to use of new way. How can I create a proxy?

Look my example:

public abstract class CassandraTestBase {

	protected CassandraTestBase() {
	}

	private static final Network network = Network.newNetwork();

	@Container
	public static final CassandraContainer<?> cassandra = new CassandraContainer<>("cassandra:3.11.2")
			.withNetwork(network).withReuse(true);

	@Container
	public static final ToxiproxyContainer toxiproxy = new ToxiproxyContainer("ghcr.io/shopify/toxiproxy:2.5.0")
			.withNetwork(network);
	
	public static ToxiproxyContainer.ContainerProxy proxy;

	private static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS local WITH replication = { 'class': 'SimpleStrategy', 'replication_factor':'1' };";
	private static final String KEYSPACE_ACTIVATE_QUERY = "USE local;";
	private static final String LOCAL = "local";
	private static CqlSession session;
	private static String keyspace = "";
	/** Datacenter used for local-test profile. */
	private static String datacenter = "datacenter1";
	private static final int REQUEST_TIMEOUT = 12000;

	@BeforeAll
	public static void startCassandraContainer() {
		proxy = toxiproxy.getProxy(cassandra, 9042);
		TestcontainersConfiguration.getInstance().updateUserConfig("testcontainers.reuse.enable", "true");
		System.setProperty("spring.data.cassandra.contact-points", proxy.getContainerIpAddress());
		System.setProperty("spring.data.cassandra.port", String.valueOf(proxy.getProxyPort()));
		System.setProperty("spring.data.cassandra.local-datacenter", datacenter);
		assumeTrue(DockerClientFactory.instance().isDockerAvailable());

		session = CqlSession.builder()
				.addContactPoint(new InetSocketAddress(proxy.getContainerIpAddress(), proxy.getProxyPort()))
				.withLocalDatacenter(datacenter)
				.withConfigLoader(DriverConfigLoader.programmaticBuilder()
						.withDuration(DefaultDriverOption.METADATA_SCHEMA_REQUEST_TIMEOUT,
								Duration.ofMillis(REQUEST_TIMEOUT))
						.withDuration(DefaultDriverOption.CONNECTION_INIT_QUERY_TIMEOUT,
								Duration.ofMillis(REQUEST_TIMEOUT))
						.withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofMillis(REQUEST_TIMEOUT)).build())
				.build();

	}

	/**
	 * Creates the keyspace from the test resource file of the api
	 * 
	 * @param profile - Active profile used
	 * @param loader  Class
	 */
	public static void createKeyspace(String profile, Class<?> loader) {
		try (InputStream input = loader.getClassLoader()
				.getResourceAsStream("application-" + profile + ".properties")) {
			Properties applicationPoperties = new Properties();
			applicationPoperties.load(input);

			keyspace = applicationPoperties.getProperty("spring.data.cassandra.keyspace-name");

		} catch (IOException e) {
			log.error(e.getMessage(), e);
		}
		getSession().execute(KEYSPACE_CREATION_QUERY.replace(LOCAL, keyspace));
		getSession().execute(KEYSPACE_ACTIVATE_QUERY.replace(LOCAL, keyspace));
	}

	public static CqlSession getSession() {
		return session;
	}

}

And my test (toxics are deprecated too):

@DisplayName("Shouldn't update Timestamps when connection will be lost")
void testInsertDataWithToxic() throws Exception {	
		proxy.toxics().bandwidth("CUT_CONNECTION_DOWNSTREAM", ToxicDirection.DOWNSTREAM, 0);
		proxy.toxics().bandwidth("CUT_CONNECTION_UPSTREAM", ToxicDirection.UPSTREAM, 0);

		assertThrows(CassandraUncategorizedException.class, () -> {
			userRepo.updateTimestamps();
		});

		proxy.toxics().get("CUT_CONNECTION_DOWNSTREAM").remove();
		proxy.toxics().get("CUT_CONNECTION_UPSTREAM").remove();
	}
}

You can check the docs

I am closing this, as it was provided links to the doc in Testcontainers and library.