Mercurial > hg > mercurial
view mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/AgentMirrorCleanerTest.java @ 892:c28e68e22272
fix tests to support flow logger
author | Dmitry Neverov <dmitry.neverov@jetbrains.com> |
---|---|
date | Fri, 07 Nov 2014 16:04:52 +0100 |
parents | a11bcfb63f4f |
children | 7bf4d943d5bb |
line wrap: on
line source
/* * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jetbrains.buildServer.buildTriggers.vcs.mercurial; import jetbrains.buildServer.agent.*; import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.CommandSettingsForRootImpl; import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.CommandlineViaFileWrapperWeaver; import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.ExtensionsWeaver; import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.TestCommandSettingsFactory; import jetbrains.buildServer.buildTriggers.vcs.mercurial.ext.MercurialExtensionManager; import jetbrains.buildServer.vcs.*; import jetbrains.buildServer.vcs.impl.VcsRootImpl; import org.jetbrains.annotations.NotNull; import org.jmock.Expectations; import org.jmock.Mockery; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.io.File; import java.util.Date; import java.util.HashMap; import static java.util.Arrays.asList; import static jetbrains.buildServer.buildTriggers.vcs.mercurial.Util.copyRepository; import static jetbrains.buildServer.buildTriggers.vcs.mercurial.VcsRootBuilder.vcsRoot; /** * @author dmitry.neverov */ @Test public class AgentMirrorCleanerTest extends BaseMercurialTestCase { private Mockery myContext; private MercurialAgentSideVcsSupport myVcsSupport; private AgentMirrorCleaner myCleaner; private BuildProgressLogger myLogger; private File myWorkDir; private int myBuildCounter; private MirrorManager myMirrorManager; @BeforeMethod public void setUp() throws Exception { super.setUp(); myContext = new Mockery(); myWorkDir = myTempFiles.createTempDir(); final BuildAgentConfiguration agentConfig = myContext.mock(BuildAgentConfiguration.class); myContext.checking(new Expectations() {{ allowing(agentConfig).getCacheDirectory("mercurial"); will(returnValue(myTempFiles.createTempDir())); allowing(agentConfig).getParametersResolver(); will(returnValue(new HgPathResolver())); }}); AgentPluginConfigImpl pluginConfig = new AgentPluginConfigImpl(agentConfig); myMirrorManager = new MirrorManagerImpl(pluginConfig); AgentRepoFactory repoFactory = new AgentRepoFactory(pluginConfig, new CommandSettingsForRootImpl(new TestCommandSettingsFactory(), new ExtensionsWeaver(), new CommandlineViaFileWrapperWeaver()), new AgentHgPathProvider(agentConfig)); myVcsSupport = new MercurialAgentSideVcsSupport(pluginConfig, myMirrorManager, repoFactory, new MercurialExtensionManager()); myCleaner = new AgentMirrorCleaner(myMirrorManager); myLogger = myContext.mock(FlowLogger.class); myContext.checking(new Expectations() {{ allowing(myLogger).message(with(any(String.class))); allowing(myLogger).warning(with(any(String.class))); allowing(myLogger).activityStarted(with(any(String.class)), with(any(String.class))); allowing(myLogger).activityFinished(with(any(String.class)), with(any(String.class))); allowing(myLogger).getFlowLogger(with(any(String.class))); will(returnValue(myLogger)); }}); } public void should_add_cleaners_only_for_roots_not_used_in_build() throws Exception { //setup mirrors for 2 roots on an agent: final File repo1 = myTempFiles.createTempDir(); final File repo2 = myTempFiles.createTempDir(); copyRepository(new File("mercurial-tests/testData/rep1"), repo1); copyRepository(new File("mercurial-tests/testData/rep2"), repo2); final VcsRoot root1 = vcsRoot().withUrl(repo1.getAbsolutePath()).build(); final VcsRoot root2 = vcsRoot().withUrl(repo2.getAbsolutePath()).build(); //update will initialize mirrors doUpdate(root1, "4:b06a290a363b", myWorkDir); doUpdate(root2, "8:b6e2d176fe8e", new File(myWorkDir, "subdir")); final File mirrorDir1 = myMirrorManager.getMirrorDir(repo1.getAbsolutePath()); final File mirrorDir2 = myMirrorManager.getMirrorDir(repo2.getAbsolutePath()); final Date mirrorDir1LastUsedTime = new Date(myMirrorManager.getLastUsedTime(mirrorDir1)); final Date mirrorDir2LastUsedTime = new Date(myMirrorManager.getLastUsedTime(mirrorDir2)); //run build which uses root1: final DirectoryCleanersProviderContext ctx = myContext.mock(DirectoryCleanersProviderContext.class); myContext.checking(new Expectations(){{ AgentRunningBuild build = myContext.mock(AgentRunningBuild.class, "build" + myBuildCounter++); allowing(build).getVcsRootEntries(); will(returnValue(asList(new VcsRootEntry(root1, CheckoutRules.DEFAULT)))); allowing(ctx).getRunningBuild(); will(returnValue(build)); }}); //cleaner should add cleaners only for roots which are not used in the running build final DirectoryCleanersRegistry registry = myContext.mock(DirectoryCleanersRegistry.class); myContext.checking(new Expectations() {{ never(registry).addCleaner(with(mirrorDir1), with(mirrorDir1LastUsedTime)); one(registry).addCleaner(with(mirrorDir2), with(mirrorDir2LastUsedTime)); }}); myCleaner.registerDirectoryCleaners(ctx, registry); myContext.assertIsSatisfied(); } public void no_errors_for_non_mercurial_roots() throws Exception { final File repo1 = myTempFiles.createTempDir(); copyRepository(new File("mercurial-tests/testData/rep1"), repo1); final VcsRoot root1 = vcsRoot().withUrl(repo1.getAbsolutePath()).build(); final VcsRoot nonHgRoot = new VcsRootImpl(20, "some.vcs"); final DirectoryCleanersProviderContext ctx = myContext.mock(DirectoryCleanersProviderContext.class); myContext.checking(new Expectations(){{ AgentRunningBuild build = myContext.mock(AgentRunningBuild.class, "build" + myBuildCounter++); allowing(build).getVcsRootEntries(); will(returnValue(asList(new VcsRootEntry(root1, CheckoutRules.DEFAULT), new VcsRootEntry(nonHgRoot, CheckoutRules.DEFAULT)))); allowing(ctx).getRunningBuild(); will(returnValue(build)); }}); final DirectoryCleanersRegistry registry = myContext.mock(DirectoryCleanersRegistry.class); myCleaner.registerDirectoryCleaners(ctx, registry); myContext.assertIsSatisfied(); } private void doUpdate(@NotNull VcsRoot vcsRoot, @NotNull String toVersion, @NotNull File workDir) throws VcsException { final AgentRunningBuild build = myContext.mock(AgentRunningBuild.class, "build" + myBuildCounter++); myContext.checking(new Expectations() {{ allowing(build).getBuildLogger(); will(returnValue(myLogger)); allowing(build).getSharedConfigParameters(); will(returnValue(new HashMap<String, String>() {{ put("teamcity.hg.use.local.mirrors", "true"); }})); }}); myVcsSupport.getUpdater(vcsRoot, CheckoutRules.DEFAULT, toVersion, workDir, build, false).process(IncludeRule.createDefaultInstance(), workDir); } }