1 package org.apache.maven.continuum.execution;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.framework.TestCase;
23 import org.apache.continuum.utils.shell.ExecutionResult;
24 import org.apache.continuum.utils.shell.ShellCommandHelper;
25 import org.apache.maven.continuum.configuration.ConfigurationService;
26 import org.apache.maven.continuum.configuration.DefaultConfigurationService;
27 import org.apache.maven.continuum.model.project.BuildDefinition;
28 import org.apache.maven.continuum.model.project.Project;
29 import org.apache.maven.continuum.model.project.ProjectGroup;
30 import org.apache.maven.continuum.model.scm.ScmResult;
31 import org.apache.maven.continuum.utils.ChrootJailWorkingDirectoryService;
32 import org.jmock.Expectations;
33 import org.jmock.Mockery;
34
35 import java.io.File;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39
40 public class ContinuumBuildExecutorTest
41 extends TestCase
42 {
43 protected final AbstractBuildExecutor executor = new BuildExecutorStub();
44
45 private final Mockery context = new Mockery();
46
47 private String toSystemPath( String path )
48 {
49 if ( File.separator.equals( "\\" ) )
50 {
51 String newPath = path.replaceAll( "/", "\\" + File.separator );
52 return newPath.replaceAll( "\\\\bin\\\\sh", "/bin/sh" );
53 }
54 return path;
55 }
56
57 public void testExecuteShellCommand()
58 throws Exception
59 {
60 final File chrootJailFile = new File( toSystemPath( "/home" ) );
61 final File workingDirectory = new File( toSystemPath( "/dir1/dir2/workingdir" ) );
62
63 final ShellCommandHelper helper = context.mock( ShellCommandHelper.class );
64
65 ConfigurationService configurationService = new DefaultConfigurationService()
66 {
67 @Override
68 public File getWorkingDirectory()
69 {
70 return workingDirectory;
71 }
72 };
73
74 ChrootJailWorkingDirectoryService directoryService = new ChrootJailWorkingDirectoryService();
75 directoryService.setConfigurationService( configurationService );
76 directoryService.setChrootJailDirectory( chrootJailFile );
77
78 executor.setChrootJailDirectory( chrootJailFile );
79 executor.setShellCommandHelper( helper );
80 executor.setWorkingDirectoryService( directoryService );
81
82
83 final Project project = new Project();
84 project.setId( 7 );
85 project.setGroupId( "xx" );
86 ProjectGroup projectGroup = new ProjectGroup();
87 projectGroup.setGroupId( project.getGroupId() );
88 project.setProjectGroup( projectGroup );
89
90 assertEquals( toSystemPath(
91 chrootJailFile.getPath() + "/" + project.getGroupId() + workingDirectory.getPath() + "/" +
92 project.getId() ), directoryService.getWorkingDirectory( project ).getPath() );
93
94 String executable = "mvn";
95 final String arguments = "-o clean install";
96 final File output = new File( "target/tmp" );
97 final Map<String, String> environments = new HashMap<String, String>();
98
99 final String cmd =
100 "chroot /home/xx " + " /bin/sh -c 'cd /dir1/dir2/workingdir/" + project.getId() + " && " + executable +
101 " " + arguments + "'";
102
103 final ExecutionResult result = new ExecutionResult( 0 );
104
105 context.checking( new Expectations()
106 {
107 {
108 one( helper ).executeShellCommand( chrootJailFile, "sudo", toSystemPath( cmd ), output, project.getId(),
109 environments );
110 will( returnValue( result ) );
111 }
112 } );
113
114 executor.executeShellCommand( project, executable, arguments, output, environments, null, null );
115
116 context.assertIsSatisfied();
117 }
118
119 class BuildExecutorStub
120 extends AbstractBuildExecutor
121 {
122
123 protected BuildExecutorStub()
124 {
125 super( "stub", true );
126 }
127
128 protected String findExecutable( String executable, String defaultExecutable, boolean resolveExecutable,
129 File workingDirectory )
130 {
131 return executable;
132 }
133
134 @Override
135 protected Map<String, String> getEnvironments( BuildDefinition buildDefinition )
136 {
137
138 return null;
139 }
140
141 public ContinuumBuildExecutionResult build( Project project, BuildDefinition buildDefinition, File buildOutput,
142 List<Project> projectsWithCommonScmRoot, String projectScmRootUrl )
143 throws ContinuumBuildExecutorException
144 {
145
146 return null;
147 }
148
149 public void updateProjectFromCheckOut( File workingDirectory, Project project, BuildDefinition buildDefinition,
150 ScmResult scmResult )
151 throws ContinuumBuildExecutorException
152 {
153
154
155 }
156 }
157 }