View Javadoc

1   package org.apache.continuum.buildagent.taskqueue.execution;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.continuum.buildagent.build.execution.ContinuumAgentBuildExecutor;
23  import org.apache.continuum.buildagent.build.execution.manager.BuildAgentBuildExecutorManager;
24  import org.apache.continuum.buildagent.buildcontext.BuildContext;
25  import org.apache.continuum.buildagent.buildcontext.manager.BuildContextManager;
26  import org.apache.continuum.buildagent.configuration.BuildAgentConfigurationService;
27  import org.apache.continuum.buildagent.manager.BuildAgentManager;
28  import org.apache.continuum.buildagent.model.Installation;
29  import org.apache.continuum.buildagent.model.LocalRepository;
30  import org.apache.continuum.buildagent.utils.ContinuumBuildAgentUtil;
31  import org.apache.continuum.taskqueue.BuildProjectTask;
32  import org.apache.continuum.utils.build.BuildTrigger;
33  import org.apache.maven.continuum.execution.ContinuumBuildExecutorConstants;
34  import org.apache.maven.continuum.model.project.BuildDefinition;
35  import org.apache.maven.continuum.model.scm.ScmResult;
36  import org.apache.maven.project.MavenProject;
37  import org.codehaus.plexus.spring.PlexusInSpringTestCase;
38  import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
39  import org.jmock.Expectations;
40  import org.jmock.Mockery;
41  import org.jmock.integration.junit3.JUnit3Mockery;
42  import org.jmock.lib.legacy.ClassImposteriser;
43  
44  import java.io.File;
45  import java.util.ArrayList;
46  import java.util.Calendar;
47  import java.util.HashMap;
48  import java.util.List;
49  import java.util.Map;
50  
51  public class BuildProjectTaskExecutorTest
52      extends PlexusInSpringTestCase
53  {
54      private Mockery context;
55  
56      private BuildContextManager buildContextManager;
57  
58      private BuildAgentBuildExecutorManager buildAgentBuildExecutorManager;
59  
60      private BuildAgentConfigurationService buildAgentConfigurationService;
61  
62      private BuildAgentManager buildAgentManager;
63  
64      protected void setUp()
65          throws Exception
66      {
67          super.setUp();
68  
69          context = new JUnit3Mockery();
70          context.setImposteriser( ClassImposteriser.INSTANCE );
71  
72          buildContextManager = context.mock( BuildContextManager.class );
73  
74          buildAgentBuildExecutorManager = context.mock( BuildAgentBuildExecutorManager.class );
75  
76          buildAgentConfigurationService = context.mock( BuildAgentConfigurationService.class );
77  
78          buildAgentManager = context.mock( BuildAgentManager.class );
79      }
80  
81      // CONTINUUM-2391
82      // Note: The checking of the local repo set in the context is in ContinuumActionStub. If the
83      // local repo path is incorrect, an exception will be thrown by the action stub.
84      public void testBuildProjectLocalRepository()
85          throws Exception
86      {
87          BuildProjectTaskExecutor buildProjectExecutor = (BuildProjectTaskExecutor) lookup( TaskExecutor.class,
88                                                                                             "build-agent" );
89  
90          buildProjectExecutor.setBuildAgentBuildExecutorManager( buildAgentBuildExecutorManager );
91  
92          buildProjectExecutor.setBuildAgentConfigurationService( buildAgentConfigurationService );
93  
94          buildProjectExecutor.setBuildContextManager( buildContextManager );
95  
96          buildProjectExecutor.setBuildAgentManager( buildAgentManager );
97  
98          final BuildContext buildContext = createBuildContext();
99  
100         final List<LocalRepository> localRepos = new ArrayList<LocalRepository>();
101 
102         LocalRepository localRepo = createLocalRepository( "temp", "/tmp/.m2/repository", "default" );
103         localRepos.add( localRepo );
104 
105         localRepo = createLocalRepository( "default", "/home/user/.m2/repository", "default" );
106         localRepos.add( localRepo );
107 
108         final Map<String, String> masterBuildEnvironments = new HashMap<String, String>();
109         masterBuildEnvironments.put( "M2_HOME", "/tmp/apache-maven-2.2.1" );
110 
111         final List<Installation> slaveBuildEnvironments = new ArrayList<Installation>();
112 
113         final ContinuumAgentBuildExecutor executor = context.mock( ContinuumAgentBuildExecutor.class );
114         final File workingDir = new File( "/tmp/data/working-directory/project-test" );
115         final MavenProject project = new MavenProject();
116         final File outputFile = new File( "/tmp/data/build-output-directory/output.txt" );
117 
118         context.checking( new Expectations()
119         {
120             {
121                 one( buildContextManager ).getBuildContext( 1 );
122                 will( returnValue( buildContext ) );
123 
124                 one( buildAgentManager ).getEnvironments( 1, "maven2" );
125                 will( returnValue( masterBuildEnvironments ) );
126 
127                 one( buildAgentConfigurationService ).getAvailableInstallations();
128                 will( returnValue( slaveBuildEnvironments ) );
129 
130                 one( buildAgentConfigurationService ).getLocalRepositories();
131                 will( returnValue( localRepos ) );
132 
133                 one( buildAgentManager ).shouldBuild( with( any( Map.class ) ) );
134                 will( returnValue( true ) );
135 
136                 one( buildAgentBuildExecutorManager ).getBuildExecutor(
137                     ContinuumBuildExecutorConstants.MAVEN_TWO_BUILD_EXECUTOR );
138                 will( returnValue( executor ) );
139 
140                 one( buildAgentConfigurationService ).getWorkingDirectory( 1 );
141                 will( returnValue( workingDir ) );
142 
143                 one( executor ).getMavenProject( with( any( File.class ) ), with( any( BuildDefinition.class ) ) );
144                 will( returnValue( project ) );
145 
146                 one( buildAgentManager ).startProjectBuild( 1 );
147 
148                 one( buildAgentConfigurationService ).getBuildOutputFile( 1 );
149                 will( returnValue( outputFile ) );
150 
151                 one( buildAgentManager ).returnBuildResult( with( any( Map.class ) ) );
152 
153                 one( buildContextManager ).removeBuildContext( 1 );
154             }
155         } );
156 
157         try
158         {
159             buildProjectExecutor.executeTask( createBuildProjectTask() );
160         }
161         catch ( Exception e )
162         {
163             fail( "An exception should not have been thrown!" );
164         }
165     }
166 
167     public void testBuildProjectWithConfiguredInstallationsFromBuildAgent()
168         throws Exception
169     {
170         BuildProjectTaskExecutor buildProjectExecutor = (BuildProjectTaskExecutor) lookup( TaskExecutor.class,
171                                                                                            "build-agent" );
172 
173         buildProjectExecutor.setBuildAgentBuildExecutorManager( buildAgentBuildExecutorManager );
174 
175         buildProjectExecutor.setBuildAgentConfigurationService( buildAgentConfigurationService );
176 
177         buildProjectExecutor.setBuildContextManager( buildContextManager );
178 
179         buildProjectExecutor.setBuildAgentManager( buildAgentManager );
180 
181         final BuildContext buildContext = createBuildContext();
182 
183         final List<LocalRepository> localRepos = new ArrayList<LocalRepository>();
184 
185         LocalRepository localRepo = createLocalRepository( "temp", "/tmp/.m2/repository", "default" );
186         localRepos.add( localRepo );
187 
188         localRepo = createLocalRepository( "default", "/home/user/.m2/repository", "default" );
189         localRepos.add( localRepo );
190 
191         final Map<String, String> masterBuildEnvironments = new HashMap<String, String>();
192         masterBuildEnvironments.put( "M2_HOME", "/tmp/apache-maven-2.2.1" );
193 
194         final List<Installation> slaveBuildEnvironments = new ArrayList<Installation>();
195 
196         Installation slaveBuildEnvironment = createInstallation( "M2_HOME", "/home/user/apache-maven-2.2.1" );
197         slaveBuildEnvironments.add( slaveBuildEnvironment );
198 
199         slaveBuildEnvironment = createInstallation( "EXTRA_VAR", "/home/user/extra" );
200         slaveBuildEnvironments.add( slaveBuildEnvironment );
201 
202         final ContinuumAgentBuildExecutor executor = context.mock( ContinuumAgentBuildExecutor.class );
203         final File workingDir = new File( "/tmp/data/working-directory/project-test" );
204         final MavenProject project = new MavenProject();
205         final File outputFile = new File( "/tmp/data/build-output-directory/output.txt" );
206 
207         context.checking( new Expectations()
208         {
209             {
210                 one( buildContextManager ).getBuildContext( 1 );
211                 will( returnValue( buildContext ) );
212 
213                 one( buildAgentManager ).getEnvironments( 1, "maven2" );
214                 will( returnValue( masterBuildEnvironments ) );
215 
216                 one( buildAgentConfigurationService ).getAvailableInstallations();
217                 will( returnValue( slaveBuildEnvironments ) );
218 
219                 one( buildAgentConfigurationService ).getLocalRepositories();
220                 will( returnValue( localRepos ) );
221 
222                 one( buildAgentManager ).shouldBuild( with( any( Map.class ) ) );
223                 will( returnValue( true ) );
224 
225                 one( buildAgentBuildExecutorManager ).getBuildExecutor(
226                     ContinuumBuildExecutorConstants.MAVEN_TWO_BUILD_EXECUTOR );
227                 will( returnValue( executor ) );
228 
229                 one( buildAgentConfigurationService ).getWorkingDirectory( 1 );
230                 will( returnValue( workingDir ) );
231 
232                 one( executor ).getMavenProject( with( any( File.class ) ), with( any( BuildDefinition.class ) ) );
233                 will( returnValue( project ) );
234 
235                 one( buildAgentManager ).startProjectBuild( 1 );
236 
237                 one( buildAgentConfigurationService ).getBuildOutputFile( 1 );
238                 will( returnValue( outputFile ) );
239 
240                 one( buildAgentManager ).returnBuildResult( with( any( Map.class ) ) );
241 
242                 one( buildContextManager ).removeBuildContext( 1 );
243             }
244         } );
245 
246         try
247         {
248             buildProjectExecutor.executeTask( createBuildProjectTask() );
249 
250             Map<String, String> environments = (Map<String, String>) buildContext.getActionContext().get(
251                 ContinuumBuildAgentUtil.KEY_ENVIRONMENTS );
252             assertEquals( 2, environments.size() );
253             assertTrue( environments.containsKey( "M2_HOME" ) );
254             assertTrue( environments.containsKey( "EXTRA_VAR" ) );
255             // shows that build agent's environment variables overwrites Continuum Master's environment variables
256             assertEquals( "/home/user/apache-maven-2.2.1", environments.get( "M2_HOME" ) );
257             assertEquals( "/home/user/extra", environments.get( "EXTRA_VAR" ) );
258         }
259         catch ( Exception e )
260         {
261             fail( "An exception should not have been thrown!" );
262         }
263     }
264 
265     public void testBuildProjectWithNoConfiguredInstallationsFromBuildAgent()
266         throws Exception
267     {
268         BuildProjectTaskExecutor buildProjectExecutor = (BuildProjectTaskExecutor) lookup( TaskExecutor.class,
269                                                                                            "build-agent" );
270 
271         buildProjectExecutor.setBuildAgentBuildExecutorManager( buildAgentBuildExecutorManager );
272 
273         buildProjectExecutor.setBuildAgentConfigurationService( buildAgentConfigurationService );
274 
275         buildProjectExecutor.setBuildContextManager( buildContextManager );
276 
277         buildProjectExecutor.setBuildAgentManager( buildAgentManager );
278 
279         final BuildContext buildContext = createBuildContext();
280 
281         final List<LocalRepository> localRepos = new ArrayList<LocalRepository>();
282 
283         LocalRepository localRepo = createLocalRepository( "temp", "/tmp/.m2/repository", "default" );
284         localRepos.add( localRepo );
285 
286         localRepo = createLocalRepository( "default", "/home/user/.m2/repository", "default" );
287         localRepos.add( localRepo );
288 
289         final Map<String, String> masterBuildEnvironments = new HashMap<String, String>();
290         masterBuildEnvironments.put( "M2_HOME", "/tmp/apache-maven-2.2.1" );
291 
292         final List<Installation> slaveBuildEnvironments = new ArrayList<Installation>();
293 
294         final ContinuumAgentBuildExecutor executor = context.mock( ContinuumAgentBuildExecutor.class );
295         final File workingDir = new File( "/tmp/data/working-directory/project-test" );
296         final MavenProject project = new MavenProject();
297         final File outputFile = new File( "/tmp/data/build-output-directory/output.txt" );
298 
299         context.checking( new Expectations()
300         {
301             {
302                 one( buildContextManager ).getBuildContext( 1 );
303                 will( returnValue( buildContext ) );
304 
305                 one( buildAgentManager ).getEnvironments( 1, "maven2" );
306                 will( returnValue( masterBuildEnvironments ) );
307 
308                 one( buildAgentConfigurationService ).getAvailableInstallations();
309                 will( returnValue( slaveBuildEnvironments ) );
310 
311                 one( buildAgentConfigurationService ).getLocalRepositories();
312                 will( returnValue( localRepos ) );
313 
314                 one( buildAgentManager ).shouldBuild( with( any( Map.class ) ) );
315                 will( returnValue( true ) );
316 
317                 one( buildAgentBuildExecutorManager ).getBuildExecutor(
318                     ContinuumBuildExecutorConstants.MAVEN_TWO_BUILD_EXECUTOR );
319                 will( returnValue( executor ) );
320 
321                 one( buildAgentConfigurationService ).getWorkingDirectory( 1 );
322                 will( returnValue( workingDir ) );
323 
324                 one( executor ).getMavenProject( with( any( File.class ) ), with( any( BuildDefinition.class ) ) );
325                 will( returnValue( project ) );
326 
327                 one( buildAgentManager ).startProjectBuild( 1 );
328 
329                 one( buildAgentConfigurationService ).getBuildOutputFile( 1 );
330                 will( returnValue( outputFile ) );
331 
332                 one( buildAgentManager ).returnBuildResult( with( any( Map.class ) ) );
333 
334                 one( buildContextManager ).removeBuildContext( 1 );
335             }
336         } );
337 
338         try
339         {
340             buildProjectExecutor.executeTask( createBuildProjectTask() );
341 
342             Map<String, String> environments = (Map<String, String>) buildContext.getActionContext().get(
343                 ContinuumBuildAgentUtil.KEY_ENVIRONMENTS );
344             assertEquals( 1, environments.size() );
345             assertTrue( environments.containsKey( "M2_HOME" ) );
346             assertEquals( "/tmp/apache-maven-2.2.1", environments.get( "M2_HOME" ) );
347         }
348         catch ( Exception e )
349         {
350             fail( "An exception should not have been thrown!" );
351         }
352     }
353 
354     private BuildProjectTask createBuildProjectTask()
355     {
356         BuildProjectTask task = new BuildProjectTask( 1, 1, new BuildTrigger( 1 ), "Test Project",
357                                                       "Default Build Definition", new ScmResult(), 1 );
358         return task;
359     }
360 
361     private BuildContext createBuildContext()
362     {
363         BuildContext context = new BuildContext();
364         context.setProjectId( 1 );
365         context.setProjectVersion( "1.0-SNAPSHOT" );
366         context.setBuildDefinitionId( 1 );
367         context.setBuildFile( "pom.xml" );
368         context.setExecutorId( ContinuumBuildExecutorConstants.MAVEN_TWO_BUILD_EXECUTOR );
369         context.setGoals( "clean intall" );
370         context.setArguments( "--batch --non-recursive" );
371         context.setScmUrl( "scm:svn:http://svn.example.com/repos/dummy/trunk" );
372         context.setScmUsername( "" );
373         context.setScmPassword( "" );
374         context.setBuildFresh( false );
375         context.setProjectGroupId( 1 );
376         context.setProjectGroupName( "Test Project Group" );
377         context.setScmRootAddress( "scm:svn:http://svn.example.com/repos/dummy" );
378         context.setScmRootId( 1 );
379         context.setProjectName( "Test Project" );
380         context.setProjectState( 1 );
381         context.setTrigger( 1 );
382         context.setUsername( "" );
383         context.setLocalRepository( "default" );
384         context.setBuildNumber( 1 );
385 
386         ScmResult scmResult = new ScmResult();
387         scmResult.setSuccess( true );
388 
389         context.setScmResult( scmResult );
390         context.setLatestUpdateDate( Calendar.getInstance().getTime() );
391         context.setBuildAgentUrl( "http://localhost:8181/continuum-buildagent/xmlrpc" );
392         context.setMaxExecutionTime( 7200 );
393         context.setBuildDefinitionLabel( "Default Build Definition" );
394         context.setScmTag( "scm:svn:http://svn.example.com/repos/dummy/tags" );
395 
396         return context;
397     }
398 
399     private LocalRepository createLocalRepository( String name, String locataion, String layout )
400     {
401         LocalRepository localRepository = new LocalRepository();
402         localRepository.setName( name );
403         localRepository.setLocation( locataion );
404         localRepository.setLayout( layout );
405         return localRepository;
406     }
407 
408     private Installation createInstallation( String varName, String varValue )
409     {
410         Installation installation = new Installation();
411         installation.setVarName( varName );
412         installation.setVarValue( varValue );
413         return installation;
414     }
415 }