Search in sources :

Example 6 with VcsRootSshKeyManager

use of jetbrains.buildServer.ssh.VcsRootSshKeyManager in project teamcity-git by JetBrains.

the class GitVcsSupportTest method getCurrentVersion_should_not_do_fetch.

@TestFor(issues = "TW-17435")
@Test
public void getCurrentVersion_should_not_do_fetch() throws Exception {
    ServerPluginConfig config = myConfigBuilder.build();
    VcsRootSshKeyManager manager = new EmptyVcsRootSshKeyManager();
    FetchCommand fetchCommand = new FetchCommandImpl(config, new TransportFactoryImpl(config, manager), new FetcherProperties(config), manager);
    FetchCommandCountDecorator fetchCounter = new FetchCommandCountDecorator(fetchCommand);
    GitVcsSupport git = gitSupport().withPluginConfig(myConfigBuilder).withResetCacheManager(myResetCacheManager).withFetchCommand(fetchCounter).build();
    File remoteRepositoryDir = new File(myTmpDir, "repo_for_fetch");
    copyRepository(dataFile("repo_for_fetch.1"), remoteRepositoryDir);
    VcsRootImpl root = getRoot("master", false, remoteRepositoryDir);
    git.getCurrentState(root);
    assertEquals(0, fetchCounter.getFetchCount());
    git.getCurrentState(root);
    assertEquals(0, fetchCounter.getFetchCount());
}
Also used : VcsRootImpl(jetbrains.buildServer.vcs.impl.VcsRootImpl) VcsRootSshKeyManager(jetbrains.buildServer.ssh.VcsRootSshKeyManager) GitTestUtil.dataFile(jetbrains.buildServer.buildTriggers.vcs.git.tests.GitTestUtil.dataFile) FileUtil.writeFile(jetbrains.buildServer.util.FileUtil.writeFile) File(java.io.File) LockFile(org.eclipse.jgit.internal.storage.file.LockFile) Test(org.testng.annotations.Test) TestFor(jetbrains.buildServer.util.TestFor)

Example 7 with VcsRootSshKeyManager

use of jetbrains.buildServer.ssh.VcsRootSshKeyManager in project teamcity-git by JetBrains.

the class GitVcsSupportTest method should_retry_getCurrentState_if_it_fails.

@Test
@TestFor(issues = "TW-24084")
public void should_retry_getCurrentState_if_it_fails() throws Exception {
    VcsRootImpl root = vcsRoot().withFetchUrl("ssh://some.org/repo.git").withAuthMethod(AuthenticationMethod.PRIVATE_KEY_DEFAULT).build();
    final FetchConnection connection = myContext.mock(FetchConnection.class);
    final Ref masterRef = myContext.mock(Ref.class, "master");
    final Ref topicRef = myContext.mock(Ref.class, "topic");
    myContext.checking(new Expectations() {

        {
            allowing(masterRef).getName();
            will(returnValue("refs/heads/master"));
            allowing(masterRef).getObjectId();
            will(returnValue(ObjectId.fromString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")));
            allowing(topicRef).getName();
            will(returnValue("refs/heads/topic"));
            allowing(topicRef).getObjectId();
            will(returnValue(ObjectId.fromString("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")));
            allowing(connection).getRefsMap();
            will(returnValue(map("refs/heads/master", masterRef, "refs/heads/topic", topicRef)));
            allowing(connection).close();
        }
    });
    // setup TransportFactory so that it fails to get connection several times with well known exceptions
    // and then successfully gets it on the last call
    final AtomicInteger failCount = new AtomicInteger(0);
    final List<TransportException> recoverableErrors = Arrays.asList(new TransportException("Session.connect: java.net.SocketException: Connection reset", new JSchException("test")), new TransportException("Session.connect: java.net.SocketException: Software caused connection abort", new JSchException("test")), new TransportException("com.jcraft.jsch.JSchException: connection is closed by foreign host", new JSchException("test")), new TransportException("java.net.UnknownHostException: some.org", new JSchException("test")), new TransportException("com.jcraft.jsch.JSchException: verify: false", new JSchException("test")), new TransportException("com.jcraft.jsch.JSchException: channel is not opened.", new JSchException("test")));
    ServerPluginConfig config = myConfigBuilder.withGetConnectionRetryAttempts(recoverableErrors.size() + 1).withConnectionRetryIntervalMillis(0).setCurrentStateTimeoutSeconds(1).build();
    VcsRootSshKeyManager manager = new EmptyVcsRootSshKeyManager();
    TransportFactory transportFactory = new TransportFactoryImpl(config, manager) {

        @Override
        public Transport createTransport(@NotNull Repository r, @NotNull URIish url, @NotNull AuthSettings authSettings, int timeoutSeconds) throws NotSupportedException, VcsException {
            return new Transport(r, url) {

                @Override
                public FetchConnection openFetch() throws NotSupportedException, TransportException {
                    if (failCount.get() < recoverableErrors.size()) {
                        TransportException error = recoverableErrors.get(failCount.get());
                        failCount.incrementAndGet();
                        throw error;
                    } else {
                        return connection;
                    }
                }

                @Override
                public PushConnection openPush() throws NotSupportedException, TransportException {
                    return null;
                }

                @Override
                public void close() {
                }
            };
        }
    };
    FetchCommand fetchCommand = new FetchCommandImpl(config, transportFactory, new FetcherProperties(config), manager);
    FetchCommandCountDecorator fetchCounter = new FetchCommandCountDecorator(fetchCommand);
    GitVcsSupport git = gitSupport().withPluginConfig(config).withTransportFactory(transportFactory).withFetchCommand(fetchCounter).build();
    RepositoryStateData state = git.getCurrentState(root);
    assertEquals(state.getBranchRevisions(), map("refs/heads/master", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "refs/heads/topic", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"));
}
Also used : Expectations(org.jmock.Expectations) JSchException(com.jcraft.jsch.JSchException) URIish(org.eclipse.jgit.transport.URIish) FetchConnection(org.eclipse.jgit.transport.FetchConnection) TransportException(org.eclipse.jgit.errors.TransportException) NotNull(org.jetbrains.annotations.NotNull) Ref(org.eclipse.jgit.lib.Ref) GitTestUtil.copyRepository(jetbrains.buildServer.buildTriggers.vcs.git.tests.GitTestUtil.copyRepository) Repository(org.eclipse.jgit.lib.Repository) VcsRootImpl(jetbrains.buildServer.vcs.impl.VcsRootImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) VcsRootSshKeyManager(jetbrains.buildServer.ssh.VcsRootSshKeyManager) Transport(org.eclipse.jgit.transport.Transport) Test(org.testng.annotations.Test) TestFor(jetbrains.buildServer.util.TestFor)

Example 8 with VcsRootSshKeyManager

use of jetbrains.buildServer.ssh.VcsRootSshKeyManager in project teamcity-git by JetBrains.

the class TransportFactoryTest method transport_should_have_timeout_specified_in_config.

public void transport_should_have_timeout_specified_in_config() throws Exception {
    myConfigBuilder.setIdleTimeoutSeconds(20);
    ServerPluginConfig config = myConfigBuilder.build();
    VcsRootSshKeyManager manager = new EmptyVcsRootSshKeyManager();
    TransportFactory transportFactory = new TransportFactoryImpl(config, manager);
    Transport transport = createTransport(transportFactory);
    assertEquals(20, transport.getTimeout());
}
Also used : VcsRootSshKeyManager(jetbrains.buildServer.ssh.VcsRootSshKeyManager) Transport(org.eclipse.jgit.transport.Transport)

Example 9 with VcsRootSshKeyManager

use of jetbrains.buildServer.ssh.VcsRootSshKeyManager in project teamcity-git by JetBrains.

the class CollectChangesTest method test_no_second_fetch_if_from_revision_missing.

@TestFor(issues = "TW-64455")
public void test_no_second_fetch_if_from_revision_missing() throws Exception {
    final ServerPluginConfig config = myConfig.build();
    final VcsRootSshKeyManager manager = new EmptyVcsRootSshKeyManager();
    final String[] fetchExpected = { "refs/heads/b1;refs/heads/b3;refs/heads/b4;refs/heads/master", "refs/heads/master", "refs/heads/b1;refs/heads/b3;refs/heads/b4" };
    final FetchCommand fetchCommand = new FetchCommandImpl(config, new TransportFactoryImpl(config, manager), new FetcherProperties(config), manager) {

        final AtomicInteger fetchHappened = new AtomicInteger(0);

        @Override
        public void fetch(@NotNull Repository db, @NotNull URIish fetchURI, @NotNull FetchSettings settings) throws IOException, VcsException {
            final String refSpecStr = settings.getRefSpecs().stream().map(RefSpec::getSource).sorted(Comparator.naturalOrder()).collect(Collectors.joining(";"));
            if (!fetchExpected[fetchHappened.getAndIncrement()].equals(refSpecStr)) {
                fail("Unexpected fetch happened: " + refSpecStr);
            }
            super.fetch(db, fetchURI, settings);
        }
    };
    final GitVcsSupport git = gitSupport().withPluginConfig(myConfig).withFetchCommand(fetchCommand).build();
    final File repo = getRemoteRepositoryDir("TW-64455-no_second_fetch_if_from_revision_missing_1");
    final VcsRoot root = vcsRoot().withFetchUrl(repo).build();
    // clone repository on server
    final RepositoryStateData versionState = createVersionState("refs/heads/master", map("refs/heads/master", "3fe70d1959d56bf478a7ee01f2b5e57bf12e10df", "refs/heads/b1", "7afe31b738eb97b69c9b0ca033548d5a3eabe597", "refs/heads/b3", "27a2e7b1004f3a69fad996f7fd18a14f9a4c4eec", "refs/heads/b4", "65f49c6f9c0970a893ec8e7645a35d8b7d1d7b36"));
    git.getCollectChangesPolicy().collectChanges(root, createVersionState("refs/heads/master", map("refs/heads/master", "3fe70d1959d56bf478a7ee01f2b5e57bf12e10df", "refs/heads/b1", null, "refs/heads/b3", null, "refs/heads/b4", null)), versionState, CheckoutRules.DEFAULT);
    // update remote repository:
    File updatedRepo = getRemoteRepositoryDir("TW-64455-no_second_fetch_if_from_revision_missing_2");
    FileUtil.delete(repo);
    repo.mkdirs();
    FileUtil.copyDir(updatedRepo, repo);
    // delete clone on server
    MirrorManagerImpl mirrors = new MirrorManagerImpl(myConfig.build(), new HashCalculatorImpl(), new RemoteRepositoryUrlInvestigatorImpl());
    File cloneOnServer = mirrors.getMirrorDir(repo.getCanonicalPath());
    FileUtil.delete(cloneOnServer);
    // collect changes in master to clone the repository
    VcsRoot rootMaster = vcsRoot().withFetchUrl(repo).withBranch("master").build();
    git.getCollectChangesPolicy().collectChanges(rootMaster, createVersionState("refs/heads/master", "3fe70d1959d56bf478a7ee01f2b5e57bf12e10df"), createVersionState("refs/heads/master", "db31739d5d13aef2785189fe4f79b823b5dc6ab6"), CheckoutRules.DEFAULT);
    // collect changes
    git.getCollectChangesPolicy().collectChanges(root, versionState, createVersionState("refs/heads/master", map("refs/heads/master", "db31739d5d13aef2785189fe4f79b823b5dc6ab6", "refs/heads/b1", "95ff8476374914c77c658f876dd1548b93d88d58", "refs/heads/b3", "6821229bee5b5cf5030e938caa1cb703a0edbc86", "refs/heads/b4", null)), CheckoutRules.DEFAULT);
}
Also used : URIish(org.eclipse.jgit.transport.URIish) NotNull(org.jetbrains.annotations.NotNull) GitTestUtil.copyRepository(jetbrains.buildServer.buildTriggers.vcs.git.tests.GitTestUtil.copyRepository) Repository(org.eclipse.jgit.lib.Repository) RefSpec(org.eclipse.jgit.transport.RefSpec) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) VcsRootSshKeyManager(jetbrains.buildServer.ssh.VcsRootSshKeyManager) GitTestUtil.dataFile(jetbrains.buildServer.buildTriggers.vcs.git.tests.GitTestUtil.dataFile) File(java.io.File) TestFor(jetbrains.buildServer.util.TestFor)

Example 10 with VcsRootSshKeyManager

use of jetbrains.buildServer.ssh.VcsRootSshKeyManager in project teamcity-git by JetBrains.

the class GitPerformanceTests method incrementalIntellijFetch.

@Test
public // @Test(invocationCount = 100)
void incrementalIntellijFetch() throws Exception {
    final VcsRootImpl root = VcsRootBuilder.vcsRoot().withBranchSpec("+:refs/heads/*").withFetchUrl("ssh://git@git.jetbrains.team/intellij.git").withAuthMethod(AuthenticationMethod.PRIVATE_KEY_DEFAULT).build();
    final ServerPaths sp = new ServerPaths("/Users/victory/Tests/server_paths");
    final ServerPluginConfig config = new PluginConfigBuilder(sp).setSeparateProcessForFetch(true).build();
    final GitSupportBuilder builder = GitSupportBuilder.gitSupport().withServerPaths(sp).withPluginConfig(config).withFetchCommand(new NativeGitCommands(config, () -> new GitExec("git", new GitVersion(2, 34, 0)), new VcsRootSshKeyManager() {

        @Nullable
        @Override
        public TeamCitySshKey getKey(@NotNull VcsRoot root) {
            return null;
        }
    }));
    GitVcsSupport support = builder.build();
    final RepositoryStateData currentState = support.getCurrentState(root);
    final long startTime = new Date().getTime();
    System.out.println("Fetching repository...");
    final OperationContext ctx = support.createContext(root, "fetch");
    try {
        support.getCollectChangesPolicy().ensureRepositoryStateLoadedFor(ctx, currentState, true);
    } finally {
        ctx.close();
    }
    final long totalTime = new Date().getTime() - startTime;
    System.out.println("Fetch time: " + totalTime + "ms");
}
Also used : NativeGitCommands(jetbrains.buildServer.buildTriggers.vcs.git.command.NativeGitCommands) GitExec(jetbrains.buildServer.buildTriggers.vcs.git.command.GitExec) VcsRoot(jetbrains.buildServer.vcs.VcsRoot) NotNull(org.jetbrains.annotations.NotNull) Date(java.util.Date) VcsRootImpl(jetbrains.buildServer.vcs.impl.VcsRootImpl) RepositoryStateData(jetbrains.buildServer.vcs.RepositoryStateData) VcsRootSshKeyManager(jetbrains.buildServer.ssh.VcsRootSshKeyManager) ServerPaths(jetbrains.buildServer.serverSide.ServerPaths) Test(org.testng.annotations.Test)

Aggregations

VcsRootSshKeyManager (jetbrains.buildServer.ssh.VcsRootSshKeyManager)10 TestFor (jetbrains.buildServer.util.TestFor)7 Test (org.testng.annotations.Test)6 NotNull (org.jetbrains.annotations.NotNull)5 File (java.io.File)4 GitTestUtil.dataFile (jetbrains.buildServer.buildTriggers.vcs.git.tests.GitTestUtil.dataFile)4 Repository (org.eclipse.jgit.lib.Repository)4 URIish (org.eclipse.jgit.transport.URIish)4 GitTestUtil.copyRepository (jetbrains.buildServer.buildTriggers.vcs.git.tests.GitTestUtil.copyRepository)3 VcsRootImpl (jetbrains.buildServer.vcs.impl.VcsRootImpl)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 GitRepoOperationsImpl (jetbrains.buildServer.buildTriggers.vcs.git.command.impl.GitRepoOperationsImpl)2 ServerPaths (jetbrains.buildServer.serverSide.ServerPaths)2 Ref (org.eclipse.jgit.lib.Ref)2 RefSpec (org.eclipse.jgit.transport.RefSpec)2 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)1 SystemInfo (com.intellij.openapi.util.SystemInfo)1 JSch (com.jcraft.jsch.JSch)1 JSchException (com.jcraft.jsch.JSchException)1 IOException (java.io.IOException)1