{"id":25776111,"url":"https://github.com/pszymczyk/embedded-consul","last_synced_at":"2025-02-27T06:05:35.743Z","repository":{"id":9124039,"uuid":"60956229","full_name":"pszymczyk/embedded-consul","owner":"pszymczyk","description":"Embedded Consul provides easy way to run Consul in integration tests. ","archived":true,"fork":false,"pushed_at":"2022-10-07T13:21:09.000Z","size":336,"stargazers_count":94,"open_issues_count":20,"forks_count":29,"subscribers_count":11,"default_branch":"master","last_synced_at":"2024-04-16T07:04:47.262Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Groovy","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pszymczyk.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-06-12T08:32:35.000Z","updated_at":"2024-03-31T14:17:55.000Z","dependencies_parsed_at":"2022-09-16T00:11:03.506Z","dependency_job_id":null,"html_url":"https://github.com/pszymczyk/embedded-consul","commit_stats":null,"previous_names":[],"tags_count":36,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pszymczyk%2Fembedded-consul","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pszymczyk%2Fembedded-consul/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pszymczyk%2Fembedded-consul/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pszymczyk%2Fembedded-consul/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pszymczyk","download_url":"https://codeload.github.com/pszymczyk/embedded-consul/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240987435,"owners_count":19889333,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-27T06:01:18.015Z","updated_at":"2025-02-27T06:05:35.732Z","avatar_url":"https://github.com/pszymczyk.png","language":"Groovy","funding_links":[],"categories":["测试"],"sub_categories":[],"readme":"# Public archive\n\nProject has been deprecated in favour of [Testcontainers Hashicorp Consul Module](https://www.testcontainers.org/modules/consul/)\n\n# Embedded Consul\n\nEmbedded Consul provides easy way to run Consul in integration tests.\n\n[![Build Status](https://travis-ci.org/pszymczyk/embedded-consul.svg?branch=master)](https://travis-ci.org/pszymczyk/embedded-consul)\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.pszymczyk.consul/embedded-consul/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.pszymczyk.consul/embedded-consul/)\n\nBuilt on Consul 1.11.2 \u003cbr /\u003e\nCompatible with jdk1.8+. \u003cbr /\u003e\nWorking on all operating systems: Mac, Linux, Windows.\n\n### How to get it?\n\n```xml\n    \u003cdependency\u003e\n      \u003cgroupId\u003ecom.pszymczyk.consul\u003c/groupId\u003e\n      \u003cartifactId\u003eembedded-consul\u003c/artifactId\u003e\n      \u003cversion\u003e2.3.1\u003c/version\u003e\n      \u003cscope\u003etest\u003c/scope\u003e\n    \u003c/dependency\u003e\n```\n\n```groovy\n    testImplementation 'com.pszymczyk.consul:embedded-consul:2.3.1'\n```\n\n### Usage\n#### Spring Boot setup\n\n```java\n@RunWith(SpringRunner.class)\n@SpringBootTest\npublic class SpringBootIntegrationTest {\n\n    @BeforeClass\n    public static void setup() {\n        consul = ConsulStarterBuilder.consulStarter().buildAndStart();\n\n        System.setProperty(\"spring.cloud.consul.enabled\", \"true\");\n        System.setProperty(\"spring.cloud.consul.host\", \"localhost\");\n        System.setProperty(\"spring.cloud.consul.port\", String.valueOf(consul.getHttpPort()));\n    }\n\n    @Test\n    public void doSomethingWithSpring(){\n        //use your spring beans\n    }    \n}\n```\n\n#### JUnit Rule\n\nIf JUnit is on classpath, simplest way to use `embedded-consul` is via\n[JUnit rules](https://github.com/junit-team/junit4/wiki/Rules).\n\n```java\npublic class IntegrationTest {\n\n    @ClassRule\n    public static final ConsulResource consul = new ConsulResource();\n\n    private OkHttpClient client = new OkHttpClient();\n\n    @Test\n    public void shouldStartConsul() throws Throwable {\n        await().atMost(30, TimeUnit.SECONDS).until(() -\u003e {\n            Request request = new Request.Builder()\n                    .url(\"http://localhost:\" + consul.getHttpPort() + \"/v1/agent/self\")\n                    .build();\n\n            return client.newCall(request).execute().code() == 200;\n        });\n    }\n}\n```\n\n#### JUnit 5 Extension\n\nThere is also an [extension](https://junit.org/junit5/docs/current/user-guide/#extensions) for JUnit 5.\nUnlike in Junit 4 with `@Rule` and `@ClassRule` there is no option to control the scope of `ConsulProcess`.\n`ConsulExtension` starts Consul for a whole class just like `@ClassRule`, but it resets the Consul state between tests.\n\n```java\nclass IntegrationTest {\n\n    @RegisterExtension\n    ConsulExtension consul = new ConsulExtension();\n\n    private OkHttpClient client = new OkHttpClient();\n\n    @Test\n    void shouldStartConsul() throws Throwable {\n        await().atMost(30, TimeUnit.SECONDS).until(() -\u003e {\n            Request request = new Request.Builder()\n                    .url(\"http://localhost:\" + consul.getHttpPort() + \"/v1/agent/self\")\n                    .build();\n\n            return client.newCall(request).execute().code() == 200;\n        });\n    }\n}\n```\n\nThe extension can also be registered via `@ExtendWith` annotation and provided to test as a method argument.\n\n```java\n@ExtendWith(ConsulExtension.class)\nclass IntegrationTest {\n\n    @Test\n    void test(ConsulProcess consul) {\n        OkHttpClient client = new OkHttpClient();\n        await().atMost(30, TimeUnit.SECONDS).until(() -\u003e {\n            Request request = new Request.Builder()\n                    .url(\"http://localhost:\" + consul.getHttpPort() + \"/v1/agent/self\")\n                    .build();\n\n            return client.newCall(request).execute().code() == 200;\n        });\n    }\n}\n```\n\n#### Manual\n```java\n\npublic class IntegrationTest {\n\n    private ConsulProcess consul;\n\n    @Before\n    public void setup() {\n        consul = ConsulStarterBuilder.consulStarter().buildAndStart();\n    }\n\n    @After\n    public void cleanup() throws Exception {\n        consul.close();\n    }\n\n    /* tests as in example above */\n```\n\n### Register Services\n\nYou can automatically register Services in Consul via `ConsulStarterBuilder`.\n```java\n    \n    ConsulStarterBuilder.consulStarter()\n        .withService(\n                new Service(\"a service\"), \n                new Service(\"another service\", \"127.0.0.1\", 8000));\n        .buildAndStart();\n\n``` \n\n### Reset Consul state\n\nThe ConsulProcess can be reset at any time. Reset method do few operations:\n- removing all registered services\n- removes all registered checks\n- removes all data from kv store\n- destroy all active sessions\n\nInvoking `reset` method is faster than starting new Consul process.\n\n```java\n\n    consulClient.setKVBinaryValue(\"foo\", \"bar\".getBytes())\n\n    // do sth\n\n    consul.reset()\n\n    assert consulClient.getKVBinaryValue(\"foo\").getValue() == null\n```\n\n### Passing custom configuration\n\nIf you want to pass custom property which is not covered by `ConsulStartrBuilder` you can pass JSON configuration.\n\n```java\n\nString customConfiguration =\n                \"{\" +\n                    \"\\\"datacenter\\\": \\\"test-dc\\\",\" +                    \n                    \"\\\"log_level\\\": \\\"INFO\\\",\" +\n                    \"\\\"node_name\\\": \\\"foobar\\\"\" +\n                \"}\";\n\nConsulProcess consul = ConsulStarterBuilder.consulStarter().withCustomConfig(customConfiguration).buildAndStart();    \n\n```\n\nGiven JSON configuration will be saved in addition configuration file `extra_config.json` and processed after base\nconfiguration (with highest priority).\n\n### Changing download directory\n\nAn environment variable can be set to change the consul CDN.\n\n```bash\n# default\nexport CONSUL_BINARY_CDN=https://releases.hashicorp.com/consul/\n```\nProxy can be used if necessary.\n```\nhttps.proxyHost=localhost \nhttps.proxyPort=3128\n```\n\n\n### Files structure\n\n```\n\n    ├─$temp-directory\n    │ \n    ├── embedded-consul-$consul-version\n    │   ├── consul\n    │   └── consul.zip\n    ├── embedded-consul-data-dir + randomNumber\n    │   ├── raft\n    │   │   ├── peers.json\n    │   │   ├── raft.db\n    │   │   └── snapshots\n    │   └── serf\n    │       ├── local.snapshot\n    │       └── remote.snapshot\n    ├── embedded-consul-config-dir + randomNumber\n    │   ├── basic_config.json   \n    │   └── extra_config.json\n```\n\nTo avoid unnecessary downloads Consul binary is downloaded into static named directory `/$tmp/embedded-consul-$consul-version`.\nAnother stuff (ports config, raft, serf) is created in dynamically named temp directories.\n\nAt the moment files are not deleted!.\n\n### Simultaneous running\n\nEmbedded Consul overrides all default [ports used by Consul](https://www.consul.io/docs/agent/options.html#ports).\nPorts are randomized so it's possible to run multiple Consul Agents in single machine.\nConfiguration file is stored in `/$tmp/embedded-consul-config-dir$randomNumber/basic_config.json`, sample content:\n\n```javascript\n\n    {\n        \"ports\": {\n            \"dns\": 50084,\n            \"serf_lan\": 50085,\n            \"serf_wan\": 50086,\n            \"server\": 50087\n        },\n        \"disable_update_check\": true,\n        \"performance\": {\n            \"raft_multiplier\": 1\n        }\n    }\n\n```\n\n### JDK 1.7.x support\n\nSupport for older Java versions has been dropped throughout release `2.0.0`. \nIf you'd like to use `embedded-consul` with JDK 1.7 the `1.1.1` is the last  appropriate version.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpszymczyk%2Fembedded-consul","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpszymczyk%2Fembedded-consul","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpszymczyk%2Fembedded-consul/lists"}