Search in sources :

Example 1 with FileWatcher

use of net.morimekta.util.FileWatcher in project providence by morimekta.

the class ProvidenceConfigTest method testReusableObject.

@Test
public void testReusableObject() throws ProvidenceConfigException {
    FileWatcher watcher = new FileWatcher();
    ProvidenceConfig lazy = new ProvidenceConfig(registry, watcher, false);
    ProvidenceConfig strict = new ProvidenceConfig(registry, watcher, true);
    File file = copyResourceTo("/net/morimekta/providence/config/files/reusable_object.cfg", temp.getRoot());
    Service service = lazy.getConfig(file);
    assertThat(service.getAdmin().getOauthTokenKey(), is(service.getHttp().getSignatureKeys().get("app1")));
    try {
        strict.getConfig(file);
        fail("no exception");
    } catch (ProvidenceConfigException e) {
        assertThat(e.getMessage(), is("Reusable objects are not allowed in strict mode."));
        assertThat(e.getFile(), is(file.getName()));
        assertThat(e.asString(), is("Error in reusable_object.cfg on line 9, pos 27: Reusable objects are not allowed in strict mode.\n" + "        oauth_token_key & token = b64(VGVzdCBPYXV0aCBLZXkK)\n" + "--------------------------^^^^^"));
    }
}
Also used : FileWatcher(net.morimekta.util.FileWatcher) Service(net.morimekta.test.providence.config.Service) File(java.io.File) Test(org.junit.Test)

Example 2 with FileWatcher

use of net.morimekta.util.FileWatcher in project providence by morimekta.

the class ProvidenceConfigTest method testReload.

@Test
public void testReload() throws IOException {
    copyResourceTo("/net/morimekta/providence/config/files/base_service.cfg", temp.getRoot());
    File stageDb = copyResourceTo("/net/morimekta/providence/config/files/stage_db.cfg", temp.getRoot());
    File stage = copyResourceTo("/net/morimekta/providence/config/files/stage.cfg", temp.getRoot());
    FileWatcher watcher = new FileWatcher();
    ProvidenceConfig config = new ProvidenceConfig(registry, watcher, true);
    Supplier<Service> stage_service = config.resolveConfig(stage);
    assertEquals("{\n" + "  name = \"stage\"\n" + "  http = {\n" + "    port = 8080\n" + "    context = \"/app\"\n" + "    signature_keys = {\n" + "      \"app1\": b64(VGVzdCBPYXV0aCBLZXkK)\n" + "    }\n" + "    signature_override_keys = [\n" + "      \"not_really_app_1\"\n" + "    ]\n" + "  }\n" + "  db = {\n" + "    uri = \"jdbc:h2:localhost:mem\"\n" + "    driver = \"org.h2.Driver\"\n" + "    credentials = {\n" + "      username = \"myuser\"\n" + "      password = \"MyP4s5w0rd\"\n" + "    }\n" + "  }\n" + "}", debugString(stage_service.get()));
    AtomicBoolean watcherCalled = new AtomicBoolean(false);
    watcher.addWatcher(file -> watcherCalled.set(true));
    File tmp = temp.newFile();
    writeContentTo(getResourceAsString("/net/morimekta/providence/config/files/stage_db2.cfg"), tmp);
    Files.move(tmp.toPath(), stageDb.toPath(), StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
    if (FileSystems.getDefault().newWatchService().getClass().getName().equals("sun.nio.fs.PollingWatchService")) {
        // HACK: PollingWatchService uses file modification time to trigger events.  Artificially advancing the
        // modification time seems to successfully trigger events in this case.
        stageDb.setLastModified(System.currentTimeMillis() + 120 * 1000);
    }
    await().atMost(Duration.TEN_SECONDS).untilTrue(watcherCalled);
    assertThat(config.resolveConfig(stage), is(sameInstance(stage_service)));
    assertThat(stage_service.get(), is(sameInstance(stage_service.get())));
    assertEquals("{\n" + "  name = \"stage\"\n" + "  http = {\n" + "    port = 8080\n" + "    context = \"/app\"\n" + "    signature_keys = {\n" + "      \"app1\": b64(VGVzdCBPYXV0aCBLZXkK)\n" + "    }\n" + "    signature_override_keys = [\n" + "      \"not_really_app_1\"\n" + "    ]\n" + "  }\n" + "  db = {\n" + "    uri = \"jdbc:h2:localhost:mem\"\n" + "    driver = \"org.h2.Driver\"\n" + "    credentials = {\n" + "      username = \"myuser\"\n" + "      password = \"O7h3rP4ssw0rd\"\n" + "    }\n" + "  }\n" + "}", debugString(stage_service.get()));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FileWatcher(net.morimekta.util.FileWatcher) Service(net.morimekta.test.providence.config.Service) File(java.io.File) Test(org.junit.Test)

Example 3 with FileWatcher

use of net.morimekta.util.FileWatcher in project providence by morimekta.

the class ProvidenceConfigSupplierTest method testReferencedFilesOnUpdatesFromCanonicalFiles.

@Test
public void testReferencedFilesOnUpdatesFromCanonicalFiles() throws IOException {
    SimpleTypeRegistry registry = new SimpleTypeRegistry();
    registry.registerRecursively(Service.kDescriptor);
    watcher = new FileWatcher();
    parser = new ProvidenceConfigParser(registry, true);
    File service = new File(tmp.getRoot(), "service.config").getAbsoluteFile();
    File include = new File(tmp.getRoot(), "include.config").getAbsoluteFile();
    File v1 = tmp.newFolder("..dist", "v1").getAbsoluteFile();
    File service1 = new File(v1, "service.config").getAbsoluteFile();
    File include1 = new File(v1, "include.config").getAbsoluteFile();
    File v2 = tmp.newFolder("..dist", "v2").getAbsoluteFile();
    File service2 = new File(v2, "service.config").getAbsoluteFile();
    File include2 = new File(v2, "include.config").getAbsoluteFile();
    // write v1 config
    writeContentTo("config.ServicePort {\n" + "  port = 8080\n" + "}\n", include1);
    writeContentTo("include \"include.config\" as inc\n" + "config.Service {\n" + "  name = \"test\"\n" + "  http = inc\n" + "}\n", service1);
    // write v2 config
    writeContentTo("config.ServicePort {\n" + "  port = 80\n" + "}\n", include2);
    writeContentTo("include \"include.config\" as inc\n" + "config.Service {\n" + "  name = \"other\"\n" + "  http = inc\n" + "}\n", service2);
    Files.createSymbolicLink(service.toPath(), service1.toPath());
    Files.createSymbolicLink(include.toPath(), include1.toPath());
    ProvidenceConfigSupplier<Service, Service._Field> config = new ProvidenceConfigSupplier<>(service, null, watcher, parser, clock);
    Service serviceConfig = config.get();
    AtomicReference<Service> serviceRef = new AtomicReference<>(serviceConfig);
    assertThat(serviceConfig.getName(), is("test"));
    assertThat(serviceConfig.getHttp().getPort(), is((short) 8080));
    AtomicBoolean updated = new AtomicBoolean();
    ConfigListener<Service, Service._Field> listener = cf -> {
        serviceRef.set(cf);
        updated.set(true);
    };
    config.addListener(listener);
    File tmpFile = new File(tmp.getRoot(), "..tmp");
    Files.createSymbolicLink(tmpFile.toPath(), service2.toPath());
    Files.move(tmpFile.toPath(), service.toPath(), StandardCopyOption.REPLACE_EXISTING);
    waitAtMost(Duration.ONE_SECOND).untilTrue(updated);
    serviceConfig = serviceRef.get();
    assertThat(serviceConfig.getName(), is("other"));
    assertThat(serviceConfig.getHttp().getPort(), is((short) 80));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FakeClock(net.morimekta.testing.time.FakeClock) AtomicReference(java.util.concurrent.atomic.AtomicReference) Awaitility.waitAtMost(org.awaitility.Awaitility.waitAtMost) StandardCopyOption(java.nio.file.StandardCopyOption) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) Assert.assertThat(org.junit.Assert.assertThat) Pair(net.morimekta.util.Pair) ArgumentCaptor(org.mockito.ArgumentCaptor) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Mockito.atLeast(org.mockito.Mockito.atLeast) SimpleTypeRegistry(net.morimekta.providence.util.SimpleTypeRegistry) Before(org.junit.Before) CoreMatchers.sameInstance(org.hamcrest.CoreMatchers.sameInstance) ImmutableSet(com.google.common.collect.ImmutableSet) ProvidenceConfigException(net.morimekta.providence.config.ProvidenceConfigException) Duration(org.awaitility.Duration) Files(java.nio.file.Files) TestConfigSupplier(net.morimekta.providence.config.util.TestConfigSupplier) FileWatcher(net.morimekta.util.FileWatcher) Test(org.junit.Test) IOException(java.io.IOException) Mockito.doNothing(org.mockito.Mockito.doNothing) Mockito.when(org.mockito.Mockito.when) ConfigListener(net.morimekta.providence.config.ConfigListener) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) Rule(org.junit.Rule) Mockito.atMost(org.mockito.Mockito.atMost) ResourceUtils.writeContentTo(net.morimekta.testing.ResourceUtils.writeContentTo) Database(net.morimekta.test.providence.config.Database) Mockito.reset(org.mockito.Mockito.reset) TemporaryFolder(org.junit.rules.TemporaryFolder) Service(net.morimekta.test.providence.config.Service) Mockito.mock(org.mockito.Mockito.mock) SimpleTypeRegistry(net.morimekta.providence.util.SimpleTypeRegistry) Service(net.morimekta.test.providence.config.Service) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FileWatcher(net.morimekta.util.FileWatcher) File(java.io.File) Test(org.junit.Test)

Example 4 with FileWatcher

use of net.morimekta.util.FileWatcher in project providence by morimekta.

the class ProvidenceConfigTest method setUp.

@Before
public void setUp() throws IOException {
    registry = new SimpleTypeRegistry();
    registry.registerRecursively(Service.kDescriptor);
    registry.registerRecursively(Value.kDescriptor);
    registry.registerRecursively(RefMerge.kDescriptor);
    watcher = new FileWatcher();
}
Also used : FileWatcher(net.morimekta.util.FileWatcher) SimpleTypeRegistry(net.morimekta.providence.util.SimpleTypeRegistry) Before(org.junit.Before)

Aggregations

FileWatcher (net.morimekta.util.FileWatcher)4 File (java.io.File)3 Service (net.morimekta.test.providence.config.Service)3 Test (org.junit.Test)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 SimpleTypeRegistry (net.morimekta.providence.util.SimpleTypeRegistry)2 Before (org.junit.Before)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 StandardCopyOption (java.nio.file.StandardCopyOption)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ConfigListener (net.morimekta.providence.config.ConfigListener)1 ProvidenceConfigException (net.morimekta.providence.config.ProvidenceConfigException)1 TestConfigSupplier (net.morimekta.providence.config.util.TestConfigSupplier)1 Database (net.morimekta.test.providence.config.Database)1 ResourceUtils.writeContentTo (net.morimekta.testing.ResourceUtils.writeContentTo)1 FakeClock (net.morimekta.testing.time.FakeClock)1 Pair (net.morimekta.util.Pair)1 Awaitility.waitAtMost (org.awaitility.Awaitility.waitAtMost)1