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