92039146866893d0e37976a65a41e9e06fb7af5f
[teamcity/dotNetPackagesSupport.git] / nuget-tests / src / jetbrains / buildServer / nuget / tests / integration / InstallPackageIntegtatoinTest.java
1 /*\r
2  * Copyright 2000-2011 JetBrains s.r.o.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package jetbrains.buildServer.nuget.tests.integration;\r
18 \r
19 import com.intellij.execution.configurations.GeneralCommandLine;\r
20 import jetbrains.buildServer.ExecResult;\r
21 import jetbrains.buildServer.RunBuildException;\r
22 import jetbrains.buildServer.SimpleCommandLineProcessRunner;\r
23 import jetbrains.buildServer.agent.*;\r
24 import jetbrains.buildServer.nuget.agent.install.PackageUsages;\r
25 import jetbrains.buildServer.nuget.agent.install.PackagesInstallerRunner;\r
26 import jetbrains.buildServer.nuget.agent.install.impl.NuGetActionFactoryImpl;\r
27 import jetbrains.buildServer.nuget.agent.parameters.NuGetParameters;\r
28 import jetbrains.buildServer.nuget.agent.parameters.PackagesInstallParameters;\r
29 import jetbrains.buildServer.nuget.agent.parameters.PackagesParametersFactory;\r
30 import jetbrains.buildServer.nuget.agent.parameters.PackagesUpdateParameters;\r
31 import jetbrains.buildServer.nuget.agent.util.BuildProcessBase;\r
32 import jetbrains.buildServer.nuget.agent.util.CommandlineBuildProcessFactory;\r
33 import jetbrains.buildServer.nuget.common.PackagesUpdateMode;\r
34 import jetbrains.buildServer.nuget.tests.util.BuildProcessTestCase;\r
35 import jetbrains.buildServer.util.ArchiveUtil;\r
36 import junit.framework.Assert;\r
37 import org.jetbrains.annotations.NotNull;\r
38 import org.jmock.Expectations;\r
39 import org.jmock.Mockery;\r
40 import org.testng.annotations.BeforeMethod;\r
41 import org.testng.annotations.Test;\r
42 \r
43 import java.io.File;\r
44 import java.util.Arrays;\r
45 import java.util.Collection;\r
46 import java.util.Collections;\r
47 import java.util.List;\r
48 \r
49 /**\r
50  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
51  * Date: 08.07.11 2:15\r
52  */\r
53 public class InstallPackageIntegtatoinTest extends BuildProcessTestCase {\r
54   private File myRoot;\r
55   private Mockery m;\r
56   private AgentRunningBuild myBuild;\r
57   private BuildRunnerContext myContext;\r
58   private BuildProgressLogger myLogger;\r
59   private PackagesParametersFactory myParametersFactory;\r
60   private PackagesInstallParameters myInstall;\r
61   private PackagesUpdateParameters myUpdate;\r
62   private NuGetParameters myNuGet;\r
63   private BuildProcess myMockProcess;\r
64 \r
65   @BeforeMethod\r
66   @Override\r
67   protected void setUp() throws Exception {\r
68     super.setUp();\r
69     myRoot = createTempDir();\r
70     m = new Mockery();\r
71     myBuild = m.mock(AgentRunningBuild.class);\r
72     myContext = m.mock(BuildRunnerContext.class);\r
73     myLogger = m.mock(BuildProgressLogger.class);\r
74     myParametersFactory = m.mock(PackagesParametersFactory.class);\r
75     myInstall = m.mock(PackagesInstallParameters.class);\r
76     myUpdate = m.mock(PackagesUpdateParameters.class);\r
77     myMockProcess = m.mock(BuildProcess.class);\r
78     myNuGet = m.mock(NuGetParameters.class);\r
79 \r
80     m.checking(new Expectations() {{\r
81       allowing(myContext).getBuild();  will(returnValue(myBuild));\r
82       allowing(myBuild).getBuildLogger(); will(returnValue(myLogger));\r
83       allowing(myBuild).getCheckoutDirectory(); will(returnValue(myRoot));\r
84 \r
85       allowing(myMockProcess).start();\r
86       allowing(myMockProcess).waitFor(); will(returnValue(BuildFinishedStatus.FINISHED_SUCCESS));\r
87 \r
88       allowing(myLogger).message(with(any(String.class)));\r
89       allowing(myLogger).activityStarted(with(equal("install")), with(any(String.class)), with(any(String.class)));\r
90       allowing(myLogger).activityFinished(with(equal("install")), with(any(String.class)));\r
91 \r
92       allowing(myInstall).getNuGetParameters(); will(returnValue(myNuGet));\r
93       allowing(myUpdate).getNuGetParameters(); will(returnValue(myNuGet));\r
94     }});\r
95   }\r
96 \r
97   @Test\r
98   public void test_01_online_sources() throws RunBuildException {\r
99     ArchiveUtil.unpackZip(Paths.getTestDataPath("test-01.zip"), "", myRoot);\r
100 \r
101     fetchPackages(new File(myRoot, "sln1-lib.sln"), Collections.<String>emptyList(), false, false);\r
102 \r
103     List<File> packageses = Arrays.asList(new File(myRoot, "packages").listFiles());\r
104     System.out.println("installed packageses = " + packageses);\r
105 \r
106     Assert.assertTrue(new File(myRoot, "packages/NUnit.2.5.7.10213").isDirectory());\r
107     Assert.assertTrue(new File(myRoot, "packages/NInject.2.2.1.4").isDirectory());\r
108     Assert.assertTrue(new File(myRoot, "packages/Machine.Specifications.0.4.13.0").isDirectory());\r
109     Assert.assertEquals(4, packageses.size());\r
110   }\r
111 \r
112   @Test\r
113   public void test_01_online_sources_update() throws RunBuildException {\r
114     ArchiveUtil.unpackZip(Paths.getTestDataPath("test-01.zip"), "", myRoot);\r
115 \r
116     m.checking(new Expectations(){{\r
117       allowing(myLogger).activityStarted(with(equal("update")), with(any(String.class)), with(equal("nuget")));\r
118       allowing(myLogger).activityFinished(with(equal("update")), with(equal("nuget")));\r
119 \r
120       allowing(myUpdate).getUseSafeUpdate(); will(returnValue(false));\r
121       allowing(myUpdate).getPackagesToUpdate(); will(returnValue(Collections.<String>emptyList()));\r
122       allowing(myUpdate).getUpdateMode(); will(returnValue(PackagesUpdateMode.FOR_EACH_PACKAGES_CONFIG));\r
123     }});\r
124 \r
125     fetchPackages(new File(myRoot, "sln1-lib.sln"), Collections.<String>emptyList(), false, true);\r
126 \r
127 \r
128     List<File> packageses = Arrays.asList(new File(myRoot, "packages").listFiles());\r
129     System.out.println("installed packageses = " + packageses);\r
130 \r
131     Assert.assertTrue(new File(myRoot, "packages/NUnit.2.5.7.10213").isDirectory());\r
132     Assert.assertTrue(new File(myRoot, "packages/NUnit.2.5.10.11092").isDirectory());\r
133     Assert.assertTrue(new File(myRoot, "packages/NInject.2.2.1.4").isDirectory());\r
134     Assert.assertTrue(new File(myRoot, "packages/Machine.Specifications.0.4.13.0").isDirectory());\r
135     Assert.assertEquals(5, packageses.size());\r
136   }\r
137 \r
138   @Test\r
139   public void test_01_online_sources_update_forSln() throws RunBuildException {\r
140     ArchiveUtil.unpackZip(Paths.getTestDataPath("test-01.zip"), "", myRoot);\r
141 \r
142     m.checking(new Expectations(){{\r
143       allowing(myLogger).activityStarted(with(equal("update")), with(any(String.class)), with(equal("nuget")));\r
144       allowing(myLogger).activityFinished(with(equal("update")), with(equal("nuget")));\r
145 \r
146       allowing(myUpdate).getUseSafeUpdate(); will(returnValue(false));\r
147       allowing(myUpdate).getPackagesToUpdate(); will(returnValue(Collections.<String>emptyList()));\r
148       allowing(myUpdate).getUpdateMode(); will(returnValue(PackagesUpdateMode.FOR_SLN));\r
149     }});\r
150 \r
151     fetchPackages(new File(myRoot, "sln1-lib.sln"), Collections.<String>emptyList(), false, true);\r
152 \r
153 \r
154     List<File> packageses = Arrays.asList(new File(myRoot, "packages").listFiles());\r
155     System.out.println("installed packageses = " + packageses);\r
156 \r
157     Assert.assertTrue(new File(myRoot, "packages/NUnit.2.5.7.10213").isDirectory());\r
158     Assert.assertTrue(new File(myRoot, "packages/NUnit.2.5.10.11092").isDirectory());\r
159     Assert.assertTrue(new File(myRoot, "packages/NInject.2.2.1.4").isDirectory());\r
160     Assert.assertTrue(new File(myRoot, "packages/Machine.Specifications.0.4.13.0").isDirectory());\r
161     Assert.assertEquals(5, packageses.size());\r
162   }\r
163 \r
164   @Test\r
165   public void test_01_online_sources_update_safe() throws RunBuildException {\r
166     ArchiveUtil.unpackZip(Paths.getTestDataPath("test-01.zip"), "", myRoot);\r
167 \r
168     m.checking(new Expectations(){{\r
169       allowing(myLogger).activityStarted(with(equal("update")), with(any(String.class)), with(equal("nuget")));\r
170       allowing(myLogger).activityFinished(with(equal("update")), with(equal("nuget")));\r
171 \r
172       allowing(myUpdate).getUseSafeUpdate(); will(returnValue(true));\r
173       allowing(myUpdate).getPackagesToUpdate(); will(returnValue(Collections.<String>emptyList()));\r
174       allowing(myUpdate).getUpdateMode(); will(returnValue(PackagesUpdateMode.FOR_EACH_PACKAGES_CONFIG));\r
175     }});\r
176 \r
177     fetchPackages(new File(myRoot, "sln1-lib.sln"), Collections.<String>emptyList(), false, true);\r
178 \r
179 \r
180     List<File> packageses = Arrays.asList(new File(myRoot, "packages").listFiles());\r
181     System.out.println("installed packageses = " + packageses);\r
182 \r
183     Assert.assertTrue(new File(myRoot, "packages/NUnit.2.5.7.10213").isDirectory());\r
184     Assert.assertTrue(new File(myRoot, "packages/NUnit.2.5.10.11092").isDirectory());\r
185     Assert.assertTrue(new File(myRoot, "packages/NInject.2.2.1.4").isDirectory());\r
186     Assert.assertTrue(new File(myRoot, "packages/Machine.Specifications.0.4.13.0").isDirectory());\r
187     Assert.assertEquals(5, packageses.size());\r
188   }\r
189 \r
190   @Test\r
191   public void test_01_online_sources_ecludeVersion() throws RunBuildException {\r
192     ArchiveUtil.unpackZip(Paths.getTestDataPath("test-01.zip"), "", myRoot);\r
193 \r
194     fetchPackages(new File(myRoot, "sln1-lib.sln"), Collections.<String>emptyList(), true, false);\r
195 \r
196     List<File> packageses = Arrays.asList(new File(myRoot, "packages").listFiles());\r
197     System.out.println("installed packageses = " + packageses);\r
198 \r
199     Assert.assertTrue(new File(myRoot, "packages/NUnit").isDirectory());\r
200     Assert.assertTrue(new File(myRoot, "packages/NInject").isDirectory());\r
201     Assert.assertTrue(new File(myRoot, "packages/Machine.Specifications").isDirectory());\r
202     Assert.assertEquals(4, packageses.size());\r
203   }\r
204 \r
205   @Test(enabled = false, dependsOnGroups = "Need to understand how to check NuGet uses only specified sources")\r
206   public void test_01_local_sources() throws RunBuildException {\r
207     ArchiveUtil.unpackZip(Paths.getTestDataPath("test-01.zip"), "", myRoot);\r
208     File sourcesDir = new File(myRoot, "js");\r
209     ArchiveUtil.unpackZip(Paths.getTestDataPath("test-01-sources.zip"), "", sourcesDir);\r
210 \r
211     fetchPackages(new File(myRoot, "sln1-lib.sln"), Arrays.asList("file:///" + sourcesDir.getPath()), false, false);\r
212 \r
213     List<File> packageses = Arrays.asList(new File(myRoot, "packages").listFiles());\r
214     System.out.println("installed packageses = " + packageses);\r
215 \r
216     Assert.assertTrue(new File(myRoot, "packages/NUnit.2.5.7.10213").isDirectory());\r
217     Assert.assertTrue(new File(myRoot, "packages/NInject.2.2.1.4").isDirectory());\r
218     Assert.assertTrue(new File(myRoot, "packages/Machine.Specifications.0.4.13.0").isDirectory());\r
219     Assert.assertEquals(4, packageses.size());\r
220   }\r
221 \r
222   private void fetchPackages(final File sln,\r
223                              final List<String> sources,\r
224                              final boolean excludeVersion,\r
225                              final boolean update) throws RunBuildException {\r
226     final PackageUsages pu = m.mock(PackageUsages.class);\r
227     final BuildProcess pup = m.mock(BuildProcess.class, "report usages");\r
228     m.checking(new Expectations() {{\r
229       allowing(myParametersFactory).loadNuGetParameters(myContext);  will(returnValue(myNuGet));\r
230       allowing(myParametersFactory).loadInstallPackagesParameters(myContext, myNuGet);  will(returnValue(myInstall));\r
231 \r
232       allowing(myNuGet).getNuGetExeFile();\r
233       will(returnValue(Paths.getPathToNuGet()));\r
234       allowing(myNuGet).getSolutionFile(); will(returnValue(sln));\r
235       allowing(myNuGet).getNuGetPackageSources(); will(returnValue(sources));\r
236       allowing(myInstall).getExcludeVersion(); will(returnValue(excludeVersion));\r
237       allowing(myParametersFactory).loadUpdatePackagesParameters(myContext, myNuGet);  will(returnValue(update ? myUpdate : null));\r
238 \r
239       allowing(pu).createReport(with(any(File.class)));\r
240       will(returnValue(pup));\r
241 \r
242       allowing(pup).start();\r
243       allowing(pup).waitFor(); will(returnValue(BuildFinishedStatus.FINISHED_SUCCESS));\r
244     }});\r
245 \r
246     BuildProcess proc = new PackagesInstallerRunner(\r
247             new NuGetActionFactoryImpl(executingFactory(), pu),\r
248             myParametersFactory\r
249     ).createBuildProcess(myBuild, myContext);\r
250 \r
251     assertRunSuccessfully(proc, BuildFinishedStatus.FINISHED_SUCCESS);\r
252 \r
253     m.assertIsSatisfied();\r
254   }\r
255 \r
256   private CommandlineBuildProcessFactory executingFactory() {\r
257     return new CommandlineBuildProcessFactory() {\r
258       public BuildProcess executeCommandLine(@NotNull final BuildRunnerContext hostContext,\r
259                                              @NotNull final File program,\r
260                                              @NotNull final Collection<String> argz,\r
261                                              @NotNull final File workingDir) throws RunBuildException {\r
262         return new BuildProcessBase() {\r
263           @NotNull\r
264           @Override\r
265           protected BuildFinishedStatus waitForImpl() throws RunBuildException {\r
266             GeneralCommandLine cmd = new GeneralCommandLine();\r
267             cmd.setExePath(program.getPath());\r
268             for (String arg : argz) {\r
269               cmd.addParameter(arg);\r
270             }\r
271             cmd.setWorkingDirectory(workingDir);\r
272 \r
273             System.out.println("Run: " + cmd.getCommandLineString());\r
274 \r
275             ExecResult result = SimpleCommandLineProcessRunner.runCommand(cmd, new byte[0]);\r
276 \r
277             System.out.println(result.getStdout());\r
278             System.out.println(result.getStderr());\r
279 \r
280             return result.getExitCode() == 0\r
281                     ? BuildFinishedStatus.FINISHED_SUCCESS\r
282                     : BuildFinishedStatus.FINISHED_FAILED;\r
283           }\r
284         };\r
285       }\r
286     };\r
287   }\r
288 }\r