Merge pull request #82 from bpgray/test-discovery-acl-for-pr

* test-discovery-acl-for-pr:
  add acl tests and setup test instance of consul to support acl testing
This commit is contained in:
Spencer Gibb
2016-02-09 18:04:35 -07:00
8 changed files with 124 additions and 5 deletions

View File

@@ -14,9 +14,9 @@ install:
- ./mvnw --settings .settings.xml install -P docs -q -U -DskipTests=true -Dmaven.test.redirectTestOutputToFile=true
- ./docs/src/main/asciidoc/ghpages.sh
script:
- ./consul agent -server -advertise 127.0.0.1 -bootstrap-expect 1 -data-dir /tmp/consul -ui-dir ./src/test/resources/consul_ui &
- '[ "${TRAVIS_PULL_REQUEST}" != "false" ] || ./mvnw --settings .settings.xml deploy -nsu -Dmaven.test.redirectTestOutputToFile=true'
- '[ "${TRAVIS_PULL_REQUEST}" = "false" ] || ./mvnw --settings .settings.xml install -nsu -Dmaven.test.redirectTestOutputToFile=true'
- ./src/test/bash/travis_run_consul.sh
- '[ "${TRAVIS_PULL_REQUEST}" != "false" ] || ./mvnw -P ci --settings .settings.xml deploy -nsu -Dmaven.test.redirectTestOutputToFile=true'
- '[ "${TRAVIS_PULL_REQUEST}" = "false" ] || ./mvnw -P ci --settings .settings.xml install -nsu -Dmaven.test.redirectTestOutputToFile=true'
env:
global:
- GIT_NAME="Spencer Gibb"

29
pom.xml
View File

@@ -121,6 +121,35 @@
</snapshots>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/ConsulDiscoveryClientAclTests.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>ci</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/ConsulPropertySourceLocatorTests.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.consul.discovery;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.consul.ConsulAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
/**
* @author Spencer Gibb
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ConsulDiscoveryClientAclTests.MyTestConfig.class)
@WebIntegrationTest(value = {"spring.application.name=testConsulDiscoveryAcl",
"spring.cloud.consul.discovery.preferIpAddress=true",
"spring.cloud.consul.discovery.aclToken=2d2e6b3b-1c82-40ab-8171-54609d8ad304"}, randomPort = true)
public class ConsulDiscoveryClientAclTests {
@Autowired
private ConsulDiscoveryClient discoveryClient;
@Test
public void getInstancesForThisServiceWorks() {
List<ServiceInstance> instances = discoveryClient.getInstances("testConsulDiscovery");
assertNotNull("instances was null", instances);
assertFalse("instances was empty", instances.isEmpty());
}
@Configuration
@EnableDiscoveryClient
@EnableAutoConfiguration
@Import({ ConsulAutoConfiguration.class, ConsulDiscoveryClientConfiguration.class })
public static class MyTestConfig {
}
}

View File

@@ -38,7 +38,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ConsulDiscoveryClientTests.MyTestConfig.class)
@WebIntegrationTest(value = {"spring.application.name=testConsulDiscovery", "spring.cloud.consul.discovery.preferIpAddress=true"}, randomPort = true)
@WebIntegrationTest(value = {"spring.application.name=testConsulDiscovery",
"spring.cloud.consul.discovery.preferIpAddress=true"}, randomPort = true)
public class ConsulDiscoveryClientTests {
@Autowired

View File

@@ -1,3 +1,4 @@
#!/bin/bash
mkdir /tmp/consul-config
consul agent -server -bootstrap-expect 1 -advertise 127.0.0.1 -data-dir /tmp/consul -config-dir=/tmp/consul-config -ui-dir `dirname $0`/../../../src/test/resources/consul_ui
BASEDIR=`dirname $0`/../../..
consul agent -server -bootstrap-expect 1 -advertise 127.0.0.1 -data-dir /tmp/consul -config-dir=/tmp/consul-config -ui-dir ${BASEDIR}/src/test/resources/consul_ui

View File

@@ -0,0 +1,6 @@
#!/bin/bash
BASEDIR=`dirname $0`/../../..
${BASEDIR}/consul agent -server -bootstrap-expect 1 -advertise 127.0.0.1 -data-dir /tmp/consul -ui-dir ${BASEDIR}/src/test/resources/consul_ui -config-dir ${BASEDIR}/src/test/resources/consul_config &
# wait for consul to elect a leader before sending acl
sleep 5
curl -X PUT -d @`dirname $0`/../../test/resources/consul_acl/consul_discovery_client_acl.json http://localhost:8500/v1/acl/create?token=2ee647bd-bd69-4118-9f34-b9a6e9e60746

View File

@@ -0,0 +1,12 @@
{
"ID": "2d2e6b3b-1c82-40ab-8171-54609d8ad304",
"Name": "discover_client_test_acl",
"Type": "client",
"Rules": "{
\"service\": {
\"testConsulDiscoveryAcl\": {
\"policy\": \"write\"
}
}
}"
}

View File

@@ -0,0 +1,6 @@
{
"datacenter": "local",
"acl_datacenter": "local",
"acl_master_token": "2ee647bd-bd69-4118-9f34-b9a6e9e60746",
"acl_default_policy": "deny"
}