Skip to content

Commit

Permalink
Read isSecure also from tags when tag merging into meta is enabled. A…
Browse files Browse the repository at this point in the history
…dd test. Fixes spring-cloud#699
  • Loading branch information
hamigam committed Apr 6, 2022
1 parent 6a2a550 commit baa22a6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,21 @@ public class ConsulServiceInstance extends DefaultServiceInstance {

private HealthService healthService;

public ConsulServiceInstance(HealthService healthService, String serviceId) {
this(healthService.getService().getId(), serviceId, findHost(healthService),
healthService.getService().getPort(), getSecure(healthService, false), getMetadata(healthService),
healthService.getService().getTags());
this.healthService = healthService;
}

public ConsulServiceInstance(String instanceId, String serviceId, String host, int port, boolean secure,
Map<String, String> metadata, List<String> tags) {
this(instanceId, serviceId, host, port, secure, metadata, tags, false);
}

public ConsulServiceInstance(HealthService healthService, String serviceId, boolean mergeTags) {
this(healthService.getService().getId(), serviceId, findHost(healthService),
healthService.getService().getPort(), getSecure(healthService), getMetadata(healthService),
healthService.getService().getPort(), getSecure(healthService, mergeTags), getMetadata(healthService),
healthService.getService().getTags(), mergeTags);
this.healthService = healthService;
}
Expand Down Expand Up @@ -99,10 +106,12 @@ private static Map<String, String> getMetadata(HealthService healthService) {
return metadata;
}

private static boolean getSecure(HealthService healthService) {
private static boolean getSecure(HealthService healthService, boolean mergeTags) {
boolean secure = false;
Map<String, String> metadata = getMetadata(healthService);
// getMetadata() above returns an empty Map if meta is null
if (mergeTags) {
metadata = mergeTags(metadata, healthService.getService().getTags());
}
if (metadata.containsKey("secure")) {
secure = Boolean.parseBoolean(metadata.get("secure"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2013-2019 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
*
* https://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 com.ecwid.consul.v1.ConsulClient;
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.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.consul.test.ConsulTestcontainers;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

/**
* @author Maciej Hamiga
**/
@RunWith(SpringRunner.class)
@SpringBootTest(properties = { "spring.application.name=testConsulDiscovery",
"spring.cloud.consul.discovery.prefer-ip-address=true", "spring.cloud.consul.discovery.metadata[foo]=bar",
"spring.cloud.consul.discovery.merge-tags-enabled=true", "spring.cloud.consul.discovery.tags=foo2=bar2,secure=true"},
classes = ConsulDiscoveryClientMergeTagsAndMetadataTests.MyTestConfig.class, webEnvironment = RANDOM_PORT)
@ContextConfiguration(initializers = ConsulTestcontainers.class)
public class ConsulDiscoveryClientMergeTagsAndMetadataTests {

@Autowired
private ConsulDiscoveryClient discoveryClient;

@Autowired
private ConsulClient consulClient;

@Test
public void bothTagsAndMetadataAreReturnedAsInstanceMetadata() {
List<ServiceInstance> instances = this.discoveryClient.getInstances("testConsulDiscovery");
assertThat(instances).as("instances was null").isNotNull();
assertThat(instances.isEmpty()).as("instances was empty").isFalse();

ServiceInstance instance = instances.get(0);
assertThat(instance.isSecure()).as("instance was not secure (http)").isTrue();
assertThat(instance.getMetadata()).containsEntry("foo", "bar");
assertThat(instance.getMetadata()).containsEntry("foo2", "bar2");
}

@Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration
@EnableDiscoveryClient
public static class MyTestConfig {

}

}

0 comments on commit baa22a6

Please sign in to comment.