com.yahoo.vespa.hosted.provision.testutils.MockNameResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of node-repository Show documentation
Show all versions of node-repository Show documentation
Keeps track of node assignment in a multi-application setup.
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.testutils;
import com.yahoo.vespa.hosted.provision.persistence.NameResolver;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
/**
* A mock DNS resolver. Can be configured to only answer specific lookups or any lookup.
*
* @author mpolden
*/
public class MockNameResolver implements NameResolver {
private final Map> records = new HashMap<>();
private boolean mockAnyLookup = false;
private boolean explicitReverseRecords = false;
public MockNameResolver addReverseRecord(String ipAddress, String hostname) {
addRecord(ipAddress, hostname);
return this;
}
public MockNameResolver addRecord(String hostname, String... ipAddress) {
Objects.requireNonNull(hostname, "hostname must be non-null");
Arrays.stream(ipAddress).forEach(ip -> Objects.requireNonNull(ip, "ipAddress must be non-null"));
records.compute(hostname, (ignored, ipAddresses) -> {
if (ipAddresses == null) {
ipAddresses = new LinkedHashSet<>();
}
ipAddresses.addAll(Arrays.asList(ipAddress));
return ipAddresses;
});
return this;
}
public MockNameResolver removeRecord(String name) {
records.remove(name);
return this;
}
public MockNameResolver mockAnyLookup() {
this.mockAnyLookup = true;
return this;
}
public MockNameResolver explicitReverseRecords() {
this.explicitReverseRecords = true;
return this;
}
@Override
public Set getAllByNameOrThrow(String hostname) {
if (records.containsKey(hostname)) {
return records.get(hostname);
}
if (mockAnyLookup) {
Set ipAddresses = Collections.singleton(randomIpAddress());
records.put(hostname, ipAddresses);
return ipAddresses;
}
throw new RuntimeException(new UnknownHostException("Could not resolve: " + hostname));
}
@Override
public Optional getHostname(String ipAddress) {
if (!explicitReverseRecords) {
return records.entrySet().stream()
.filter(kv -> kv.getValue().contains(ipAddress))
.map(Map.Entry::getKey)
.findFirst();
}
if (mockAnyLookup) {
String hostname = UUID.randomUUID().toString();
records.put(hostname, Collections.singleton(ipAddress));
}
return Optional.ofNullable(records.get(ipAddress))
.flatMap(values -> values.stream().findFirst());
}
private static String randomIpAddress() {
// Generate a random IP in 127/8 (loopback block)
ThreadLocalRandom random = ThreadLocalRandom.current();
return String.format("127.%d.%d.%d",
random.nextInt(1, 255 + 1),
random.nextInt(1, 255 + 1),
random.nextInt(1, 255 + 1));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy