diff options
author | Harald Musum <musum@verizonmedia.com> | 2020-07-12 08:32:59 +0200 |
---|---|---|
committer | Harald Musum <musum@verizonmedia.com> | 2020-07-12 08:32:59 +0200 |
commit | 3ba6a9df0d1264486d575e83b404927f3eb177c1 (patch) | |
tree | ff5cae4e40ff4b298133403a1b4668f972e7dc22 | |
parent | e9656bd66a67c38add039e7feccbd1fbc39614fb (diff) |
Do not hardcode entries and source host in mock
* Use hostname of localhost if host not found and equal to localhost
instead of hardcoded name for tests
4 files changed, 35 insertions, 43 deletions
diff --git a/config-application-package/src/main/java/com/yahoo/config/model/application/provider/MockFileRegistry.java b/config-application-package/src/main/java/com/yahoo/config/model/application/provider/MockFileRegistry.java index e9c100938dd..bba0c762558 100644 --- a/config-application-package/src/main/java/com/yahoo/config/model/application/provider/MockFileRegistry.java +++ b/config-application-package/src/main/java/com/yahoo/config/model/application/provider/MockFileRegistry.java @@ -3,37 +3,30 @@ package com.yahoo.config.model.application.provider; import com.yahoo.config.FileReference; import com.yahoo.config.application.api.FileRegistry; +import com.yahoo.net.HostName; import java.util.ArrayList; -import java.util.Collections; import java.util.List; -import java.util.Set; /** * A file registry for testing, and, it seems, doubling as a null registry in some code paths. * * @author Tony Vaagenes + * @author hmusum */ public class MockFileRegistry implements FileRegistry { + private final List<Entry> entries = new ArrayList<>(); public FileReference addFile(String relativePath) { - return new FileReference("0123456789abcdef"); + FileReference fileReference = new FileReference(relativePath); + entries.add(new Entry("", fileReference)); + return fileReference; } @Override - public String fileSourceHost() { - return "localhost.fortestingpurposesonly"; - } - - public static final Entry entry1 = new Entry("component/path1", new FileReference("1234")); - public static final Entry entry2 = new Entry("component/path2", new FileReference("56789")); + public String fileSourceHost() { return HostName.getLocalhost(); } - public List<Entry> export() { - List<Entry> result = new ArrayList<>(); - result.add(entry1); - result.add(entry2); - return result; - } + public List<Entry> export() { return entries; } @Override public FileReference addUri(String uri) { diff --git a/config-application-package/src/test/java/com/yahoo/config/model/application/provider/PreGeneratedFileRegistryTestCase.java b/config-application-package/src/test/java/com/yahoo/config/model/application/provider/PreGeneratedFileRegistryTestCase.java index e2b75542a5f..da8300fd300 100644 --- a/config-application-package/src/test/java/com/yahoo/config/model/application/provider/PreGeneratedFileRegistryTestCase.java +++ b/config-application-package/src/test/java/com/yahoo/config/model/application/provider/PreGeneratedFileRegistryTestCase.java @@ -1,11 +1,12 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.config.model.application.provider; +import com.yahoo.config.FileReference; import com.yahoo.config.application.api.FileRegistry; import org.junit.Test; import java.io.StringReader; -import java.util.Arrays; +import java.util.List; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -14,27 +15,22 @@ import static org.junit.Assert.assertTrue; * @author Tony Vaagenes */ public class PreGeneratedFileRegistryTestCase { + @Test public void importAndExport() { FileRegistry fileRegistry = new MockFileRegistry(); + fileRegistry.addFile("1234"); String serializedRegistry = PreGeneratedFileRegistry.exportRegistry(fileRegistry); - PreGeneratedFileRegistry importedRegistry = - PreGeneratedFileRegistry.importRegistry( - new StringReader(serializedRegistry)); - - assertTrue(importedRegistry.getPaths().containsAll( - Arrays.asList( - MockFileRegistry.entry1.relativePath, - MockFileRegistry.entry2.relativePath))); + PreGeneratedFileRegistry importedRegistry = PreGeneratedFileRegistry.importRegistry(new StringReader(serializedRegistry)); - assertEquals(2, importedRegistry.getPaths().size()); + FileReference fileReference = new FileReference("1234"); + assertTrue(importedRegistry.getPaths().containsAll(List.of("1234", fileReference))); - checkConsistentEntry(MockFileRegistry.entry1, importedRegistry); - checkConsistentEntry(MockFileRegistry.entry2, importedRegistry); + assertEquals(1, importedRegistry.getPaths().size()); - assertEquals(fileRegistry.fileSourceHost(), - importedRegistry.fileSourceHost()); + checkConsistentEntry(fileRegistry.export().get(0), importedRegistry); + assertEquals(fileRegistry.fileSourceHost(), importedRegistry.fileSourceHost()); } void checkConsistentEntry(FileRegistry.Entry entry, FileRegistry registry) { diff --git a/config-model/src/main/java/com/yahoo/vespa/model/HostSystem.java b/config-model/src/main/java/com/yahoo/vespa/model/HostSystem.java index 3d2918e0ee1..f7a04c36da0 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/HostSystem.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/HostSystem.java @@ -9,6 +9,7 @@ import com.yahoo.config.provision.ClusterMembership; import com.yahoo.config.provision.ClusterSpec; import com.yahoo.config.provision.HostSpec; import com.yahoo.config.provision.ProvisionLogger; +import com.yahoo.net.HostName; import java.net.UnknownHostException; import java.util.LinkedHashMap; @@ -31,7 +32,7 @@ import static java.util.logging.Level.FINE; */ public class HostSystem extends AbstractConfigProducer<Host> { - private static Logger log = Logger.getLogger(HostSystem.class.getName()); + private static final Logger log = Logger.getLogger(HostSystem.class.getName()); private final Map<String, HostResource> hostname2host = new LinkedHashMap<>(); private final HostProvisioner provisioner; @@ -65,15 +66,19 @@ public class HostSystem extends AbstractConfigProducer<Host> { * @return the host with the given hostname, or null if no such host */ public HostResource getHostByHostname(String name) { - // TODO: please eliminate the following ugly hack - if ("localhost.fortestingpurposesonly".equals(name)) { - String localhost = "localhost"; - if ( ! getChildren().containsKey(localhost)) { - new Host(this, localhost); + System.out.println("Getting name=" + name + " all hosts: " + hostname2host); + HostResource hostResource = hostname2host.get(name); + if (hostResource == null) { + // Create a new HostResource if this is the host this code is running on (as when running tests) + if (HostName.getLocalhost().equals(name)) { + String localhost = "localhost"; + if (! getChildren().containsKey(localhost)) { + new Host(this, localhost); + } + hostResource = new HostResource(getChildren().get(localhost)); } - return new HostResource(getChildren().get(localhost)); } - return hostname2host.get(name); + return hostResource; } @Override diff --git a/config-model/src/main/java/com/yahoo/vespa/model/filedistribution/FileDistributor.java b/config-model/src/main/java/com/yahoo/vespa/model/filedistribution/FileDistributor.java index 5ccf86f9ba8..fb9402614f6 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/filedistribution/FileDistributor.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/filedistribution/FileDistributor.java @@ -11,10 +11,10 @@ import com.yahoo.vespa.model.Host; import java.util.*; /** - * Sends RPC requests to hosts (tenant hosts and config servers) to start download of files. This is used during prepare - * of an application. Services themselves will also request files, the work done in this class is done so that hosts can - * start downloading files before services gets new config that needs these files. This also tries to make sure that - * all config servers (not just the one where the application was deployed) have the files available. + * Sends RPC requests to hosts (tenant hosts and config servers) asking them to start download of files. This is used + * during prepare of an application. Services themselves will also request files, the methods in this class are used + * so that hosts can start downloading files before services gets new config that needs these files. It also tries + * to make sure that all config servers (not just the one where the application was deployed) have the files available. * * @author Tony Vaagenes */ @@ -36,7 +36,6 @@ public class FileDistributor { /** * Adds the given file to the associated application packages' registry of file and marks the file * for distribution to the given host. - * <b>Note: This class receives ownership of the given collection.</b> * * @return the reference to the file, created by the application package */ @@ -47,7 +46,6 @@ public class FileDistributor { /** * Adds the given file to the associated application packages' registry of file and marks the file * for distribution to the given host. - * <b>Note: This class receives ownership of the given collection.</b> * * @return the reference to the file, created by the application package */ |