Search in sources :

Example 6 with DataStore

use of io.fabric8.api.DataStore in project fabric8 by jboss-fuse.

the class ContainerPlaceholderResolverTest method testResolveAttributeCase.

@Test
public void testResolveAttributeCase() throws Exception {
    PlaceholderResolver resolver = getContainerPlaceholderResolver();
    assertEquals(ip, resolver.resolve(fabricService, null, null, null, "container:root/IP"));
    assertEquals(localhostname, resolver.resolve(fabricService, null, null, null, "container:root/LocalHostName"));
    assertEquals(bindaddress, resolver.resolve(fabricService, null, null, null, "container:root/Bindaddress"));
    assertEquals(containerResolver, resolver.resolve(fabricService, null, null, null, "container:root/Resolver"));
    verify(fabricService);
    verify(dataStore);
}
Also used : PlaceholderResolver(io.fabric8.api.PlaceholderResolver) Test(org.junit.Test)

Example 7 with DataStore

use of io.fabric8.api.DataStore in project fabric8 by jboss-fuse.

the class ContainerPlaceholderResolverTest method testResolveCurrentName.

@Test
@Ignore("[FABRIC-1110] Mocked test makes invalid assumption on the implementation")
public void testResolveCurrentName() throws Exception {
    final FabricService fabricService = createMock(FabricService.class);
    final DataStore dataStore = createMock(DataStore.class);
    expect(fabricService.getCurrentContainerName()).andReturn("root").anyTimes();
    expect(fabricService.getContainer(EasyMock.<String>anyObject())).andReturn(new ContainerImpl(null, "root", fabricService));
    replay(fabricService);
    replay(dataStore);
    PlaceholderResolver resolver = getContainerPlaceholderResolver();
    assertEquals("root", resolver.resolve(fabricService, null, null, null, "container:name"));
    verify(fabricService);
    verify(dataStore);
}
Also used : FabricService(io.fabric8.api.FabricService) ContainerImpl(io.fabric8.internal.ContainerImpl) DataStore(io.fabric8.api.DataStore) PlaceholderResolver(io.fabric8.api.PlaceholderResolver) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with DataStore

use of io.fabric8.api.DataStore in project fabric8 by jboss-fuse.

the class GitDataStoreImplProfilesIT method init.

@Before
public void init() throws NoSuchFieldException, IllegalAccessException, IOException, GitAPIException {
    File repo = new File("target/fabric-git");
    FileUtils.deleteDirectory(repo);
    repo.mkdirs();
    git = Git.init().setDirectory(repo).setGitDir(new File(repo, ".git")).call();
    git.commit().setMessage("init").call();
    git.tag().setName(GitHelpers.ROOT_TAG).call();
    dataStore = mock(DataStore.class);
    when(dataStore.getDefaultVersion()).thenReturn("1.0");
    gitService = mock(GitService.class);
    when(gitService.getGit()).thenReturn(git);
    runtimeProperties = mock(RuntimeProperties.class);
    when(runtimeProperties.getRuntimeIdentity()).thenReturn("root");
    curator = mock(CuratorFramework.class);
    pullPushPolicy = mock(PullPushPolicy.class);
    PullPushPolicy.PushPolicyResult ppResult = mock(PullPushPolicy.PushPolicyResult.class);
    when(ppResult.getPushResults()).thenReturn(new ArrayList<PushResult>());
    when(pullPushPolicy.doPush(any(GitContext.class), any(CredentialsProvider.class))).thenReturn(ppResult);
    mockStatic(ZooKeeperUtils.class);
    PowerMockito.when(ZooKeeperUtils.generateContainerToken(runtimeProperties, curator)).thenReturn("token");
    gdsi = new GitDataStoreImpl();
    this.<ValidationSupport>getField(gdsi, "active", ValidationSupport.class).setValid();
    this.<ValidatingReference<DataStore>>getField(gdsi, "dataStore", ValidatingReference.class).bind(dataStore);
    this.<ValidatingReference<GitService>>getField(gdsi, "gitService", ValidatingReference.class).bind(gitService);
    this.<ValidatingReference<RuntimeProperties>>getField(gdsi, "runtimeProperties", ValidatingReference.class).bind(runtimeProperties);
    this.<ValidatingReference<CuratorFramework>>getField(gdsi, "curator", ValidatingReference.class).bind(curator);
    setField(gdsi, "dataStoreProperties", Map.class, new HashMap<String, Object>());
    setField(gdsi, "pullPushPolicy", PullPushPolicy.class, pullPushPolicy);
    profileRegistry = gdsi;
}
Also used : ValidatingReference(io.fabric8.api.scr.ValidatingReference) ValidationSupport(io.fabric8.api.scr.ValidationSupport) PullPushPolicy(io.fabric8.git.PullPushPolicy) PushResult(org.eclipse.jgit.transport.PushResult) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) CuratorFramework(org.apache.curator.framework.CuratorFramework) GitContext(io.fabric8.api.GitContext) DataStore(io.fabric8.api.DataStore) GitService(io.fabric8.git.GitService) File(java.io.File) RuntimeProperties(io.fabric8.api.RuntimeProperties) Before(org.junit.Before)

Example 9 with DataStore

use of io.fabric8.api.DataStore in project fabric8 by jboss-fuse.

the class DummyBatchingProgressMonitor method activateInternal.

private void activateInternal() throws Exception {
    LOGGER.info("Starting up GitDataStore " + this);
    // Call the bootstrap {@link DataStoreTemplate}
    DataStoreTemplate template = runtimeProperties.get().removeRuntimeAttribute(DataStoreTemplate.class);
    if (template != null) {
        // Do the initial commit and set the root tag
        Ref rootTag = getGit().getRepository().getRef(GitHelpers.ROOT_TAG);
        if (rootTag == null) {
            getGit().commit().setMessage("First Commit").setCommitter("fabric", "user@fabric").call();
            getGit().tag().setName(GitHelpers.ROOT_TAG).setMessage("Tag the root commit").call();
        }
        LOGGER.debug("Running datastore bootstrap template: " + template);
        template.doWith(this, dataStore.get());
    }
    // Setup proxy service
    GitProxyService proxyService = gitProxyService.get();
    defaultProxySelector = ProxySelector.getDefault();
    // authenticator disabled, until properly tested it does not affect others, as Authenticator is static in the JVM
    // Authenticator.setDefault(new FabricGitLocalHostAuthenticator(proxyService));
    ProxySelector fabricProxySelector = new FabricGitLocalHostProxySelector(defaultProxySelector, proxyService);
    ProxySelector.setDefault(fabricProxySelector);
    LOGGER.debug("Setting up FabricProxySelector: {}", fabricProxySelector);
    if (gitRemoteUrl != null) {
        gitListener.runRemoteUrlChanged(gitRemoteUrl);
        remoteUrl = gitRemoteUrl;
    } else {
        gitService.get().addGitListener(gitListener);
        remoteUrl = gitService.get().getRemoteUrl();
        if (remoteUrl != null) {
            gitListener.runRemoteUrlChanged(remoteUrl);
        }
    }
    // Get initial versions
    getInitialVersions();
    // poll logic in case of remote git repo
    if (gitRemoteUrl != null) {
        // i need this old logic in case of remote repos
        LOGGER.info("Starting to pull from remote git repository every {} millis", gitRemotePollInterval);
        threadPool.scheduleWithFixedDelay(new Runnable() {

            @Override
            public void run() {
                LockHandle writeLock = aquireWriteLock();
                try {
                    LOGGER.trace("Performing timed pull");
                    doPullInternal();
                    LOGGER.debug("Performed timed pull from external git repo");
                } catch (Throwable e) {
                    LOGGER.debug("Error during performed timed pull/push due " + e.getMessage(), e);
                    LOGGER.warn("Error during performed timed pull/push due " + e.getMessage() + ". This exception is ignored.");
                } finally {
                    writeLock.unlock();
                }
            }

            @Override
            public String toString() {
                return "TimedPushTask";
            }
        }, 1000, gitRemotePollInterval, TimeUnit.MILLISECONDS);
    }
    LOGGER.info("Using ZooKeeper SharedCount to react when master git repo is changed, so we can do a git pull to the local git repo.");
    counter = new SharedCount(curator.get(), ZkPath.GIT_TRIGGER.getPath(), 0);
    counter.addListener(new SharedCountListener() {

        @Override
        public void countHasChanged(final SharedCountReader sharedCountReader, final int value) throws Exception {
            Runnable task = new Runnable() {

                @Override
                public void run() {
                    doPullInternal();
                }
            };
            if (gitRandomFetchDelay == 0) {
                LOGGER.debug("Watch counter updated to " + value + ", scheduling immediate pull");
                threadPool.submit(task);
            } else {
                int delay = RND.nextInt(gitRandomFetchDelay) + 1;
                LOGGER.debug("Watch counter updated to " + value + ", scheduling pull with random delay=" + delay + "s");
                threadPool.schedule(task, delay, TimeUnit.SECONDS);
            }
        }

        @Override
        public void stateChanged(CuratorFramework curatorFramework, ConnectionState connectionState) {
            switch(connectionState) {
                case SUSPENDED:
                case READ_ONLY:
                case LOST:
                    // do nothing
                    break;
                case CONNECTED:
                case RECONNECTED:
                    LOGGER.info("Shared Counter (Re)connected, doing a pull");
                    doPullInternal();
                    break;
            }
        }
    });
    try {
        counter.start();
    } catch (KeeperException.NotReadOnlyException ex) {
    // In read only mode the counter is not going to start.
    // If the connection is reestablished the component will reactivate.
    // We need to catch this error so that the component gets activated.
    }
    if (gitGcOnLoad) {
        LockHandle writeLock = aquireWriteLock();
        try {
            GitOperation<Void> gitop = new GitOperation<Void>() {

                public Void call(Git git, GitContext context) throws Exception {
                    long before = System.currentTimeMillis();
                    try {
                        git.gc().call();
                        LOGGER.debug("git gc took " + ((System.currentTimeMillis() - before)) + " ms.");
                    } catch (GitAPIException e) {
                        LOGGER.debug("git gc threw an exception!", e);
                    }
                    return null;
                }
            };
            executeInternal(new GitContext(), null, gitop);
        } finally {
            writeLock.unlock();
        }
    }
    // failing to activate the component
    if (readWriteLock.isWriteLockedByCurrentThread() || readWriteLock.getWriteHoldCount() == 0) {
        try {
            // let's delegate to other thread in order to free MCF thread
            // ENTESB-7843: there's a problem when jetty is configured with TLS and keystore location using
            // profile: URI. while Jetty continues to be configured if profile:jetty.xml isn't available
            // (and it isn't initially), it fails trying to access keystore via profile: URI.
            // we should optimistically assume we can pull, but there's nothing wrong if we can't
            // - we'll be able to pull later
            threadPoolInitial.execute(new Runnable() {

                @Override
                public void run() {
                    doPullInternal(5);
                }
            });
        } catch (IllegalStateException e) {
            LOGGER.info("Another thread acquired write lock and GitDataStore can't pull from remote repository.");
        }
    } else {
        LOGGER.info("Another thread is keeping git write lock. GitDataStore will continue activation.");
    }
}
Also used : SharedCount(org.apache.curator.framework.recipes.shared.SharedCount) ProxySelector(java.net.ProxySelector) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) CuratorFramework(org.apache.curator.framework.CuratorFramework) DataStoreTemplate(io.fabric8.api.DataStoreTemplate) SharedCountListener(org.apache.curator.framework.recipes.shared.SharedCountListener) GitProxyService(io.fabric8.git.GitProxyService) LockHandle(io.fabric8.api.LockHandle) SharedCountReader(org.apache.curator.framework.recipes.shared.SharedCountReader) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) FabricException(io.fabric8.api.FabricException) KeeperException(org.apache.zookeeper.KeeperException) MalformedURLException(java.net.MalformedURLException) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) GitContext(io.fabric8.api.GitContext) ConnectionState(org.apache.curator.framework.state.ConnectionState) KeeperException(org.apache.zookeeper.KeeperException)

Example 10 with DataStore

use of io.fabric8.api.DataStore in project fabric8 by jboss-fuse.

the class CachingGitDataStoreTest method testDataStore.

@Test
public void testDataStore() throws Exception {
    // dataStore.getDefaultVersion();
    String defaultVersion = null;
    assertEquals("defaultVersion", "1.0", defaultVersion);
    // now lets import some data - using the old non-git file layout...
    String importPath = basedir + "/../fabric8-karaf/src/main/resources/distro/fabric";
    if (useOldImportFormat) {
        assertFolderExists(importPath);
        try {
            dataStore.importFromFileSystem(importPath);
        } catch (Exception e) {
        // Ignore exception about missing url handlers. Not needed for this test anyway.
        }
        assertHasVersion(defaultVersion);
    } else {
        String prefix = importPath + "/fabric";
        String profileImport = prefix + "/configs/versions/1.0/profiles";
        assertFolderExists(profileImport);
        // dataStore.importFromFileSystem(new File(profileImport), "fabric",
        // "1.0", true);
        assertHasVersion(defaultVersion);
    }
    remote.checkout().setName("1.0").call();
    String importedProfile = "example-dozer";
    String profile = importedProfile;
    assertProfileExists(defaultVersion, profile);
    // assertFolderExists("Should have imported an mq/ReadMe.md file!",
    // getLocalGitFile("fabric/profiles/mq/ReadMe.md"));
    String version = "1.1";
    assertCreateVersion("1.0", version);
    assertProfileConfiguration(version, importedProfile, Constants.AGENT_PID, "attribute.parents", "feature-camel");
    assertProfileTextFileConfigurationContains(version, "example-camel-mq", "camel.xml", "http://camel.apache.org/schema/blueprint");
    /*
         * List<String> fileNames = dataStore.getConfigurationFileNames(version,
         * "example-camel-mq"); assertNotNull("Should have some file names",
         * fileNames); assertTrue("Should have some file names",
         * fileNames.size() > 0); assertTrue("Should contain 'came",
         * fileNames.size() > 0);
         * assertCollectionContains("configurationFileNames", fileNames,
         * "camel.xml");
         */
    // lets test the profile attributes
    // dataStore.getProfileAttributes(version, importedProfile);
    Map<String, String> profileAttributes = Collections.emptyMap();
    String parent = profileAttributes.get("parents");
    assertEquals(importedProfile + ".profileAttributes[parent]", "feature-camel", parent);
    System.out.println("Profile attributes: " + profileAttributes);
    String profileAttributeKey = "myKey";
    String expectedProfileAttributeValue = "myValue";
    // dataStore.setProfileAttribute(version, importedProfile, profileAttributeKey, expectedProfileAttributeValue);
    // profileAttributes = dataStore.getProfileAttributes(version, importedProfile);
    System.out.println("Profile attributes: " + profileAttributes);
    assertMapContains("Profile attribute[" + profileAttributeKey + "]", profileAttributes, profileAttributeKey, expectedProfileAttributeValue);
    String hawtioRepoKey = "repository.hawtio";
    // dataStore.getConfiguration(version, "hawtio", Constants.AGENT_PID);
    Map<String, String> hawtioAttrbutes = Collections.emptyMap();
    String currentHawtRepo = hawtioAttrbutes.get(hawtioRepoKey);
    System.out.println("Current repository.hawtio: " + currentHawtRepo);
    // now lets write via the hawtio API
    FabricGitFacade hawtio = new FabricGitFacade();
    hawtio.bindGitDataStoreForTesting(dataStore);
    hawtio.activateForTesting();
    /* String hawtioPropertyFile = "/fabric/profiles/" + dataStore.convertProfileIdToDirectory("hawtio") + "/" + Constants.AGENT_PID + ".properties";
        hawtio.write(version, hawtioPropertyFile, "My commit message", "me", "me@apache.org", "# new file\n" + hawtioRepoKey + " = "
                + "mvn\\:io.hawt/hawtio-karaf/myNewVersion/xml/features" + "\n");
        */
    // dataStore.getConfiguration(version, "hawtio", Constants.AGENT_PID);
    hawtioAttrbutes = Collections.emptyMap();
    String actual = hawtioAttrbutes.get(hawtioRepoKey);
    assertEquals("should have found the updated hawtio repo key", "mvn:io.hawt/hawtio-karaf/myNewVersion/xml/features", actual);
    // lets check that the file configurations recurses into folders
    // Map<String, byte[]> tomcatFileConfigurations = dataStore.getFileConfigurations("1.0", "controller-tomcat");
    // assertHasFileConfiguration(tomcatFileConfigurations, "tomcat/conf/server.xml.mvel");
    /* 
        Collection<String> schemas = dataStore.listFiles("1.0", Arrays.asList("example-dozer"), "schemas");
        assertNotNull(schemas);
        assertContainerEquals("schemas for example-dozer", Arrays.asList("invoice.xsd"), new ArrayList<String>(schemas));
         */
    // check we don't accidentally create a profile
    String profileNotCreated = "shouldNotBeCreated";
    // assertEquals("Should not create profile: " + profileNotCreated, null, dataStore.getProfile(version, profileNotCreated, false));
    assertProfileNotExists(defaultVersion, profileNotCreated);
    // assertFolderNotExists(getLocalGitFile("fabric/profiles/" +
    // dataStore.convertProfileIdToDirectory(profileNotCreated)));
    // now lets create some profiles in this new version
    String newProfile = "myNewProfile";
    // dataStore.createProfile(version, newProfile);
    assertProfileExists(version, newProfile);
    // lazy create a profile
    String anotherNewProfile = "anotherNewProfile";
    // dataStore.getProfile(version, anotherNewProfile, true);
    assertProfileExists(version, anotherNewProfile);
    version = "1.2";
    assertCreateVersion("1.1", version);
    // check this version has the profile too
    assertProfileExists(version, newProfile);
    assertProfileExists(version, profile);
    // now lets delete a profile
    dataStore.deleteProfile(version, newProfile);
    assertProfileNotExists(version, newProfile);
    // lets check the remote repo
    remote.checkout().setName("1.1").call();
    assertProfileExists("1.1", profile);
    assertProfileExists("1.1", newProfile);
    // assertFolderExists(getRemoteGitFile("fabric/profiles/" +
    // dataStore.convertProfileIdToDirectory(profile)));
    // assertFolderExists(getRemoteGitFile("fabric/profiles/" +
    // dataStore.convertProfileIdToDirectory(newProfile)));
    remote.checkout().setName("1.2").call();
    assertProfileExists("1.2", profile);
    assertProfileNotExists("1.2", newProfile);
    // assertFolderExists(getRemoteGitFile("fabric/profiles/" +
    // dataStore.convertProfileIdToDirectory(profile)));
    // assertFolderNotExists(getRemoteGitFile("fabric/profiles/" +
    // dataStore.convertProfileIdToDirectory(newProfile)));
    remote.checkout().setName("1.0").call();
    // assertFolderExists(getRemoteGitFile("fabric/profiles/" +
    // dataStore.convertProfileIdToDirectory(profile)));
    // assertFolderNotExists(getRemoteGitFile("fabric/profiles/" +
    // dataStore.convertProfileIdToDirectory(newProfile)));
    // delete version 1.2
    assertHasVersion("1.1");
    assertHasVersion("1.2");
    // dataStore.removeVersion("1.2");
    assertHasVersion("1.1");
    assertHasNotVersion("1.2");
    Collection<String> remoteBranches = RepositoryUtils.getBranches(remote.getRepository());
    System.out.println("Remote branches after delete: " + remoteBranches);
}
Also used : FabricGitFacade(io.fabric8.git.hawtio.FabricGitFacade) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)11 Version (io.fabric8.api.Version)8 Ignore (org.junit.Ignore)7 DataStore (io.fabric8.api.DataStore)6 Profile (io.fabric8.api.Profile)5 Before (org.junit.Before)5 FabricService (io.fabric8.api.FabricService)4 PlaceholderResolver (io.fabric8.api.PlaceholderResolver)4 ContainerImpl (io.fabric8.internal.ContainerImpl)4 File (java.io.File)4 IOException (java.io.IOException)4 DataStoreTemplate (io.fabric8.api.DataStoreTemplate)3 ProfileBuilder (io.fabric8.api.ProfileBuilder)3 RuntimeProperties (io.fabric8.api.RuntimeProperties)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 CuratorFramework (org.apache.curator.framework.CuratorFramework)3 Container (io.fabric8.api.Container)2 FabricException (io.fabric8.api.FabricException)2 GitContext (io.fabric8.api.GitContext)2