View Javadoc

1   package org.ogf.saga.resource;
2   
3   import org.apache.log4j.Logger;
4   import org.junit.Assert;
5   import org.junit.Test;
6   import org.ogf.saga.error.AuthenticationFailedException;
7   import org.ogf.saga.error.AuthorizationFailedException;
8   import org.ogf.saga.error.BadParameterException;
9   import org.ogf.saga.error.NoSuccessException;
10  import org.ogf.saga.error.NotImplementedException;
11  import org.ogf.saga.error.TimeoutException;
12  import org.ogf.saga.job.Job;
13  import org.ogf.saga.job.JobDescription;
14  import org.ogf.saga.job.JobFactory;
15  import org.ogf.saga.job.JobService;
16  import org.ogf.saga.resource.description.ComputeDescription;
17  import org.ogf.saga.resource.description.ResourceDescription;
18  import org.ogf.saga.resource.instance.Compute;
19  import org.ogf.saga.resource.instance.Resource;
20  import org.ogf.saga.url.URL;
21  import org.ogf.saga.url.URLFactory;
22  
23  public abstract class ComputeTest extends ResourceBaseTest {
24  
25      private Logger m_logger = Logger.getLogger(this.getClass());
26  
27      public ComputeTest(String resourceprotocol) throws Exception {
28          super(resourceprotocol, Type.COMPUTE);
29      }
30  
31      @Override
32      protected Resource acquire(ResourceDescription rd) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, BadParameterException, TimeoutException, NoSuccessException {
33          return m_rm.acquireCompute((ComputeDescription) rd);
34      }
35      
36      @Test 
37      public void acquireWithFlavorRequirements() throws Exception {
38          int ram = 1024;
39          int cpu = 2;
40          ComputeDescription cd = (ComputeDescription) ResourceFactory.createResourceDescription(Type.COMPUTE);
41          // image only
42          cd.setVectorAttribute(ComputeDescription.TEMPLATE, 
43                  new String[]{m_templatesForAcquire.get(0)}
44          );
45          // add memory and size
46          cd.setAttribute(ComputeDescription.MEMORY, Integer.toString(ram));
47          cd.setAttribute(ComputeDescription.SIZE, Integer.toString(cpu));
48          m_currentResource = this.acquireResourceFromDescReadyForUse(cd);
49          ComputeDescription desc = ((Compute)m_currentResource).getDescription();
50          assertTrue(desc.existsAttribute(ComputeDescription.MEMORY));
51          assertTrue(Integer.parseInt(desc.getAttribute(ComputeDescription.MEMORY)) >= ram);
52          assertTrue(desc.existsAttribute(ComputeDescription.SIZE));
53          assertTrue(Integer.parseInt(desc.getAttribute(ComputeDescription.SIZE)) >= cpu);
54      }
55      
56      //////////
57      // run job
58      //////////
59      @Test
60      public void launchAndSubmitJobAndDeleteVM() throws Exception {
61          m_currentResource = this.acquireResourceReadyForUse();
62          org.ogf.saga.task.State jobState = null;
63          URL jobservice = URLFactory.createURL(m_currentResource.getAccess()[0]);
64          JobService service = JobFactory.createJobService(m_session, jobservice);
65          JobDescription desc = JobFactory.createJobDescription();
66          desc.setAttribute(JobDescription.EXECUTABLE, "/bin/date");
67          desc.setAttribute(JobDescription.OUTPUT, "stdout.txt");
68          desc.setAttribute(JobDescription.ERROR, "stderr.txt");
69          Job job = service.createJob(desc);
70          job.run();
71          m_logger.info(job.getAttribute(Job.JOBID));   // for detecting hang in run()
72  
73          // wait for the END
74          job.waitFor();
75          m_logger.info("Job finished.");               // for detecting hang in waitFor()
76  
77          // check job status
78          jobState = job.getState();
79          // check job status
80          Assert.assertEquals(
81                  org.ogf.saga.task.State.DONE,
82                  jobState);
83      }
84      
85  }