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
20
21
22
23
24
25
26
27
28
29
30
31 public abstract class InteractiveTest extends JobBaseTest {
32
33 protected InteractiveTest(String jobprotocol) throws Exception {
34 super(jobprotocol);
35 }
36
37
38
39
40 @Test
41 public void test_setStdin() throws Exception {
42
43
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
50 Job job = createJob(desc);
51
52
53 OutputStream stdin = job.getStdin();
54 stdin.write("Test 1".getBytes());
55 stdin.close();
56
57
58 job.run();
59
60
61 job.waitFor();
62
63
64 checkStatus(job.getState(), State.DONE);
65
66
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
77 assertEquals(
78 stringToTestInStdout,
79 stdout);
80 }
81
82
83
84
85 @Test
86 public void test_getStdout() throws Exception {
87
88
89 String stringToTestInStdout = "Test";
90 JobDescription desc = createWriteJob(stringToTestInStdout);
91 desc.setAttribute(JobDescription.INTERACTIVE, JobDescription.TRUE);
92
93
94 Job job = runJob(desc);
95
96
97 job.waitFor();
98
99
100 checkStatus(job.getState(), State.DONE);
101
102
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
115 assertEquals(
116 stringToTestInStdout,
117 stdout);
118 }
119
120
121
122
123 @Test
124 public void test_getStderr() throws Exception {
125
126
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
133 Job job = runJob(desc);
134
135
136 job.waitFor();
137
138 String stderrEmpty = "";
139
140 BufferedReader jobStdoutReader = new BufferedReader( new InputStreamReader( job.getStderr()));
141 String firstLine = jobStdoutReader.readLine();
142 if(firstLine != null) {
143 stderrEmpty = firstLine;
144 }
145
146
147 assertNotSame(
148 "",
149 stderrEmpty);
150 }
151
152
153
154
155
156
157 @Test
158 public void test_run_environnement() throws Exception {
159
160
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
174 Job job = runJob(desc);
175
176
177 job.waitFor();
178
179 checkStatus(job.getState(), State.DONE);
180
181
182 BufferedReader jobStdoutReader = new BufferedReader( new InputStreamReader(job.getStdout()));
183
184 assertEquals(
185 myvar0 + ":" + myvar1,
186 jobStdoutReader.readLine());
187 }
188
189
190
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
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
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
210 OutputStream stdin = jobs[index].getStdin();
211 stdin.write(new String("Test "+index).getBytes());
212 stdin.close();
213
214
215 taskContainer.add(jobs[index]);
216 }
217
218
219 taskContainer.run();
220
221
222 taskContainer.waitFor(WaitMode.ALL);
223
224
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
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 }