View Javadoc

1   package org.ogf.saga.job.run;
2   
3   import org.junit.Test;
4   import org.ogf.saga.error.NoSuccessException;
5   import org.ogf.saga.job.Job;
6   import org.ogf.saga.job.JobDescription;
7   import org.ogf.saga.job.JobFactory;
8   import org.ogf.saga.job.JobService;
9   import org.ogf.saga.job.abstracts.AttributeVector;
10  import org.ogf.saga.job.base.JobBaseTest;
11  import org.ogf.saga.task.State;
12  import org.ogf.saga.task.TaskContainer;
13  import org.ogf.saga.task.TaskFactory;
14  import org.ogf.saga.task.WaitMode;
15  
16  import java.io.*;
17  
18  /* ***************************************************
19  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
20  * ***             http://cc.in2p3.fr/             ***
21  * ***************************************************
22  * File:   JobRunOptionalTest
23  * Author: Nicolas DEMESY (nicolas.demesy@bt.com)
24  * Author: lionel.schwarz@in2p3.fr
25  * Date:   7 NOPV 2013
26  * ***************************************************
27  * Description: 
28  * This test suite is made to be test  
29  * interactive jobs
30  */
31  public abstract class InteractiveTest extends JobBaseTest {
32  	
33  	protected InteractiveTest(String jobprotocol) throws Exception {
34          super(jobprotocol);
35      }
36  
37  	/**
38       * Runs a job,write in stdin, wait for done and check stdout
39       */
40  	@Test
41      public void test_setStdin() throws Exception {
42          
43      	// prepare interactive job
44      	String stringToTestInStdout = "     1\tTest 1";
45      	JobDescription desc = createJob("/bin/cat", null, null);
46      	desc.setAttribute(JobDescription.INTERACTIVE, JobDescription.TRUE);
47      	desc.setVectorAttribute(JobDescription.ARGUMENTS, new String[]{"-n"});
48      	
49          // create
50          Job job = createJob(desc);
51          
52          // get stdin
53          OutputStream stdin = job.getStdin();
54          stdin.write("Test 1".getBytes());
55          stdin.close();
56          
57          // run
58          job.run();
59          
60          // wait for the end
61          job.waitFor();
62  
63          // check job for DONE status
64          checkStatus(job.getState(), State.DONE);
65  
66          // check stdout
67      	String stdout = "";
68      	BufferedReader jobStdoutReader = new BufferedReader(new InputStreamReader(job.getStdout()));
69          String input;
70          if(jobStdoutReader != null) {
71  	        while ((input = jobStdoutReader.readLine()) !=null ){
72  	        	stdout += input;
73  	        }
74          }
75          
76      	// check
77          assertEquals(
78          		stringToTestInStdout,
79          		stdout);
80      }
81      
82      /**
83       * Runs a job, waits for done and check stdout
84       */
85  	@Test
86      public void test_getStdout() throws Exception {
87          
88      	// prepare interactive job
89      	String stringToTestInStdout = "Test";
90      	JobDescription desc = createWriteJob(stringToTestInStdout);
91      	desc.setAttribute(JobDescription.INTERACTIVE, JobDescription.TRUE);
92      	
93          // submit
94          Job job = runJob(desc);
95          
96          // wait for the end
97          job.waitFor();
98  
99          // check job for DONE status
100         checkStatus(job.getState(), State.DONE);
101 
102         // check stdout
103     	String stdout = "";
104     	BufferedReader jobStdoutReader = new BufferedReader(new InputStreamReader(job.getStdout()));
105         String input;
106         if(jobStdoutReader != null) {
107 	        while ((input = jobStdoutReader.readLine()) !=null ){
108 	        	if(input.indexOf(stringToTestInStdout) > -1) {
109 	        		stdout = input;
110 	        	}
111 	        }
112         }
113         
114     	// check
115         assertEquals(
116         		stringToTestInStdout,
117         		stdout);
118     }
119     
120     /**
121      * Runs a job, waits for failed and check stderr
122      */
123 	@Test
124     public void test_getStderr() throws Exception {
125         
126     	// prepare interactive job
127     	AttributeVector[] attributesV = new AttributeVector[1];
128     	attributesV[0] = new AttributeVector(JobDescription.ARGUMENTS,new String[]{"-true"});    	
129     	JobDescription desc = createJob("/bin/cat", null, attributesV);
130     	desc.setAttribute(JobDescription.INTERACTIVE, JobDescription.TRUE);
131     	
132         // submit
133         Job job = runJob(desc);
134 
135         // wait for the end
136         job.waitFor();
137 
138        	String stderrEmpty = "";
139         //test stderr
140         BufferedReader jobStdoutReader = new BufferedReader( new InputStreamReader( job.getStderr()));
141         String firstLine = jobStdoutReader.readLine();
142         if(firstLine != null) {
143         	stderrEmpty = firstLine;
144         }
145         	
146         // check
147         assertNotSame(
148                 "",
149                 stderrEmpty);
150     }
151 	
152 	
153 	/**
154 	 * check the environment in job stdout 
155 	 * Runs job with requested environment variables
156 	 */
157 	@Test
158 	public void test_run_environnement() throws Exception {
159 	    
160 		// prepare
161 		String myvar0="Testing0";
162 		String myvar1="\"Testing 1\"";
163         String[] _myenv = new String[] {
164                 "MYVAR0="+myvar0,
165                 "MYVAR1="+myvar1
166         };
167 		AttributeVector[] attributesV = new AttributeVector[2];
168 		attributesV[0] = new AttributeVector(JobDescription.ENVIRONMENT, _myenv);
169     	attributesV[1] = new AttributeVector(JobDescription.ARGUMENTS,new String[]{"$MYVAR0:$MYVAR1"});    	
170 		JobDescription desc =  createJob("/bin/echo", null, attributesV);
171 		desc.setAttribute(JobDescription.INTERACTIVE, JobDescription.TRUE);
172     	
173 		// submit
174 	    Job job = runJob(desc);
175 	    
176 	    // wait for the end
177 	    job.waitFor();  
178 	    
179 	    checkStatus(job.getState(), State.DONE);
180 	    
181 	    // check stdout
182     	BufferedReader jobStdoutReader = new BufferedReader( new InputStreamReader(job.getStdout()));       
183 	    // check
184         assertEquals(
185         		myvar0 + ":" + myvar1,
186         		jobStdoutReader.readLine());
187 	}
188 	
189 	/**
190      * Runs short jobs on the same time with one task container
191      */
192 	@Test
193     public void test_simultaneousStdin() throws Exception {
194         
195     	int numberOfJobs = Integer.parseInt(SIMULTANEOUS_JOB_NUMBER);
196 
197     	TaskContainer taskContainer = TaskFactory.createTaskContainer();
198     	    	
199     	// create and start jobs
200     	JobService m_service = JobFactory.createJobService(m_session, m_jobservice);
201     	Job[] jobs = new Job[numberOfJobs];
202 		for (int index = 0; index < numberOfJobs; index++) {
203 			// create description
204 	    	JobDescription desc = JobFactory.createJobDescription();
205     		desc.setAttribute(JobDescription.EXECUTABLE, "/bin/cat");
206 	        desc.setAttribute(JobDescription.INTERACTIVE, JobDescription.TRUE);
207 	    	
208 	        jobs[index] = m_service.createJob(desc);
209 	        // get stdin
210 	        OutputStream stdin = jobs[index].getStdin();
211 	        stdin.write(new String("Test "+index).getBytes());
212 	        stdin.close();
213 	        
214 	        // add job to task			
215 			taskContainer.add(jobs[index]);
216 		}
217     
218 		// run
219 		taskContainer.run();
220 		
221 		// wait the end 
222 		taskContainer.waitFor(WaitMode.ALL);
223     	
224 		// get failed jobs
225 		int numberOfFailed = 0;
226 		for (int j = 0; j < jobs.length; j++) {
227 			if(jobs[j].getState().getValue() == State.FAILED.getValue()) {
228 				numberOfFailed ++;
229 			}
230 		}
231 		
232 		if(numberOfFailed > 1) 
233 			throw new NoSuccessException(numberOfFailed + "jobs of "+numberOfJobs+" are failed.");
234 		if(numberOfFailed > 0) 
235 			throw new NoSuccessException(numberOfFailed + "job of "+numberOfJobs+" is failed.");
236 		
237 		int numberOfWrongStdout = 0;
238 		for (int j = 0; j < jobs.length; j++) {
239 			// check stdout
240 			String stdout = "";
241 	    	BufferedReader jobStdoutReader = new BufferedReader(new InputStreamReader(jobs[j].getStdout()));
242 	        String input;
243 	        if(jobStdoutReader != null) {
244 		        while ((input = jobStdoutReader.readLine()) !=null ){
245 		        	stdout += input;		        	
246 		        }
247 	        }
248 	        
249 	        if(!stdout.equals("Test "+j)) {
250 	        	numberOfWrongStdout ++;
251 	        }
252 		}
253 		
254     	assertEquals(
255 	                0,
256 	                numberOfWrongStdout);
257     }
258 }