GridLab
Grid Application Toolkit

A simple API for Grid Applications
GAT

Menu



Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

jobdescription_serialisation_tests.c

Go to the documentation of this file.
00001 /** @file jobdescription_serialisation_tests.c
00002  *  Test GATResource serialisation code.
00003  * 
00004  *  @date Wed Dec 11 2003
00005  * 
00006  *  @version $Header: /export/cvs-gridlab/wp-1/Codes/GATEngine/C-reference/test/internal/jobdescription_serialisation_tests.c,v 1.6 2004/04/20 17:04:43 hartmutkaiser Exp $
00007  *
00008  *  Copyright (C) Hartmut Kaiser
00009  *  This file is part of the GAT Engine.
00010  *  Contributed by Hartmut Kaiser <hartmutkaiser [at] t-online [dot] de>.
00011  *
00012  *  Use, modification and distribution is subject to the Gridlab Software
00013  *  License. (See accompanying file GLlicense.txt or copy at
00014  *  http://www.gridlab.org/GLlicense.txt)
00015  */
00016 
00017 static const char *rcsid = "$Header: /export/cvs-gridlab/wp-1/Codes/GATEngine/C-reference/test/internal/jobdescription_serialisation_tests.c,v 1.6 2004/04/20 17:04:43 hartmutkaiser Exp $";
00018 
00019 /* System Header Files */
00020 
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <sys/utsname.h>
00024 
00025 /* GAT Header Files */
00026 
00027 #include "GAT.h"
00028 #include "GATTestUtils.h"
00029 
00030 #include "GATMemoryStream.h"
00031 
00032 /* Macros */
00033 
00034 /* Structures, unions and enums */
00035 
00036 /* Static function prototypes */
00037 static GATJobDescription
00038   create_jobdescription_description(GATContext context);
00039 static GATHardwareResourceDescription
00040   create_hardware_resource_description(GATContext context);
00041 static GATSoftwareDescription
00042   create_software_description(GATContext context);
00043 static GATFile
00044   create_gatfile(GATContext context, char const *name);
00045 
00046 static GATResult
00047   verify_direct_serialisation(GATContext context, GATJobDescription resource);
00048 static GATResult
00049   verify_generic_serialisation(GATContext context, GATJobDescription resource);
00050 
00051 /* File scope variables */
00052 
00053 /* External functions */
00054 
00055 int main(void)
00056 {
00057   GATResult retval  = GAT_FAIL;
00058   GATContext context = NULL;
00059   GATJobDescription jobdescription = NULL;
00060   
00061   GAT_TEST_SUITE("Job Description Serialization");
00062 
00063   context = GATContext_Create();
00064   GAT_TEST(NULL != context);
00065 
00066   /* the following test makes sure, that there were no errors during the 
00067      creation of the GATContext object */
00068   GAT_TEST_TRACE(GATType_GATContext == GATContext_GetType(context), context);
00069 
00070   GAT_TEST_START("Job Description Serialization Tests");
00071 
00072   /* create a hardware resource description describing the required resource */ 
00073   jobdescription = create_jobdescription_description(context);
00074   GAT_TEST_TRACE(NULL != jobdescription, context);
00075 
00076   GAT_TEST_STOP();
00077   
00078   /* test serialisation trough the direct GATResource functions */
00079   retval = verify_direct_serialisation(context, jobdescription);
00080   
00081   /* test serialisation trough the generic GATObject functions */
00082   retval = verify_generic_serialisation(context, jobdescription);
00083   
00084   /* free up all allocated memory */
00085   GATJobDescription_Destroy(&jobdescription);
00086   GATContext_Destroy(&context);
00087 
00088   GAT_TEST_FINISH();
00089 
00090   return (0);
00091 }
00092 
00093 /* Local functions */
00094 
00095 /* test serialisation trough the direct GATJobDescription functions */
00096 static GATResult
00097 verify_direct_serialisation(GATContext context, GATJobDescription jd)
00098 {
00099   GATResult retcode = GAT_FAIL;
00100   GATJobDescription new_jd = NULL;
00101   GATMemoryStream stream   = GATMemoryStream_Create(0, 0, GATFalse);
00102   GATBool  isequal  = GATFalse;
00103   char  *buffer = NULL;
00104   GATuint32  buffer_size = 0;
00105   
00106   GAT_TEST_START("verify_direct_serialisation");
00107   
00108   retcode = GATJobDescription_Serialise(jd, 
00109     GATMemoryStream_ToGATObject (stream), GATFalse);
00110   GAT_TEST_TRACE(GAT_SUCCEEDED(retcode), context);
00111 
00112   buffer = GATMemoryStream_GetBuffer(stream, &buffer_size, GATFalse);
00113   GAT_TEST_TRACE(NULL != buffer, context);
00114   GAT_TEST_TRACE(buffer_size > 0, context);
00115 
00116   /* reposition the stream */
00117   GATMemoryStream_Seek(stream, GATOrigin_Set, 0, 0);
00118   
00119   new_jd = GATJobDescription_DeSerialise(context,
00120     GATMemoryStream_ToGATObject(stream), &retcode);
00121   GAT_TEST_TRACE(NULL != new_jd, context);
00122   
00123   retcode = GATJobDescription_Equals(jd, new_jd, &isequal);
00124   GAT_TEST_TRACE(GAT_SUCCEEDED(retcode), context);
00125   GAT_TEST_TRACE(GATTrue == isequal, context);
00126 
00127   GATJobDescription_Destroy(&new_jd);
00128   GATMemoryStream_Destroy(&stream);
00129   
00130   GAT_TEST_STOP();
00131   return retcode;
00132 }
00133 
00134 /* test serialisation trough the generic GATObject functions */
00135 static GATResult 
00136 verify_generic_serialisation(GATContext context, GATJobDescription jd)
00137 {
00138   GATResult retcode = GAT_FAIL;
00139   GATJobDescription new_jd = NULL;
00140   GATMemoryStream stream  = GATMemoryStream_Create (0, 0, GATFalse);
00141   GATBool isequal = GATFalse;
00142   char *buffer = NULL;
00143   GATuint32 buffer_size = 0;
00144 
00145   GAT_TEST_START("verify_generic_serialisation");
00146   
00147   retcode = GATSerialisable_Serialise(GATJobDescription_ToGATObject(jd), 
00148     GATMemoryStream_ToGATObject(stream), GATTrue);
00149   GAT_TEST_TRACE(GAT_SUCCEEDED(retcode), context);
00150 
00151   buffer = GATMemoryStream_GetBuffer(stream, &buffer_size, GATFalse);
00152   GAT_TEST_TRACE(NULL != buffer, context);
00153   GAT_TEST_TRACE(buffer_size > 0, context);
00154 
00155   /* reposition the stream */
00156   GATMemoryStream_Seek(stream, GATOrigin_Set, 0, 0);
00157   
00158   new_jd = (GATJobDescription) GATSerialisable_DeSerialise(context, 
00159     GATMemoryStream_ToGATObject(stream), &retcode);
00160   GAT_TEST_TRACE(GAT_SUCCEEDED(retcode), context);
00161   GAT_TEST_TRACE(NULL != new_jd, context);
00162   
00163   retcode = GATJobDescription_Equals(jd, new_jd, &isequal);
00164   GAT_TEST_TRACE(GAT_SUCCEEDED(retcode), context);
00165   GAT_TEST_TRACE(GATTrue == isequal, context);
00166 
00167   GATJobDescription_Destroy(&new_jd);
00168   GATMemoryStream_Destroy(&stream);
00169   
00170   GAT_TEST_STOP ();
00171   return retcode;
00172 }
00173  
00174 /* create an arbitrary job description object */
00175 static GATJobDescription 
00176 create_jobdescription_description(GATContext context)
00177 {
00178   GATJobDescription jd = NULL;
00179   GATSoftwareDescription sd = NULL;
00180   GATHardwareResourceDescription hrd = NULL;
00181   
00182   GAT_TEST_START("create_hardware_resource_description");
00183   
00184   sd = create_software_description(context);
00185   GAT_TEST_TRACE(NULL != sd, context);
00186   
00187   hrd = create_hardware_resource_description(context);
00188   GAT_TEST_TRACE(NULL != hrd, context);
00189   
00190   jd = GATJobDescription_Create_Description(context, sd, 
00191     GATHardwareResourceDescription_ToGATResourceDescription_const(hrd));
00192   GAT_TEST_TRACE(NULL != jd, context);
00193   
00194   GATHardwareResourceDescription_Destroy(&hrd);
00195   GATSoftwareDescription_Destroy(&sd);
00196 
00197   GAT_TEST_STOP();
00198 
00199   return jd;
00200 }
00201 
00202 /* create a hardware resource description describing the required resource */ 
00203 static GATHardwareResourceDescription
00204 create_hardware_resource_description (GATContext context)
00205 {
00206   GATHardwareResourceDescription hw_desc = NULL;
00207   GATTable  requirements = NULL;
00208   struct utsname sysinfo;
00209   
00210   GAT_TEST_START("create_hardware_resource_description");
00211 
00212   /* create and fill the requirements table */
00213   requirements = GATTable_Create();
00214   GAT_TEST_TRACE(NULL != requirements, context);
00215 
00216   /* query for the system information */
00217   GAT_TEST_TRACE(uname(&sysinfo) >= 0, context);
00218   
00219   /* add required OS parameters to the requirements table */
00220   GATTable_Add_float  (requirements, "memory.size", 0.256f);
00221   GATTable_Add_String (requirements, "machine.type", sysinfo.machine);
00222   GATTable_Add_String (requirements, "machine.node", sysinfo.nodename);
00223   GATTable_Add_String (requirements, "cpu.type", "unknown");
00224   GATTable_Add_float  (requirements, "cpu.speed", 1.f);
00225   GATTable_Add_float  (requirements, "disk.size", 10.f);
00226 
00227   /* create the software Resource description */
00228   hw_desc = GATHardwareResourceDescription_Create (requirements);
00229   GAT_TEST_TRACE(NULL != hw_desc, context);
00230 
00231   GATTable_Destroy (&requirements);
00232 
00233   GAT_TEST_STOP ();
00234   
00235   return hw_desc;
00236 }
00237 
00238 static GATFile
00239 create_gatfile(GATContext context, char const *name)
00240 {
00241   GATLocation location = NULL;
00242   GATFile file = NULL;
00243   
00244   GAT_TEST_START("create_gatfile");
00245   
00246   location = GATLocation_Create(name);
00247   GAT_TEST_TRACE(NULL != location, context);
00248   
00249   file = GATFile_Create(context, location, 0);
00250   GAT_TEST_TRACE(NULL != file, context);
00251   
00252   GATLocation_Destroy(&location);
00253 
00254   GAT_TEST_STOP ();
00255   
00256   return file;
00257 }
00258 
00259 /* create an arbitrary software description for testing purposes */
00260 static GATSoftwareDescription
00261 create_software_description(GATContext context)
00262 {
00263   GATSoftwareDescription sw_desc = NULL;
00264   GATLocation location = NULL;
00265   GATList_String arguments = NULL;
00266   GATTable environment = NULL;
00267   GATFile file = NULL;
00268   char *env_string = NULL;
00269   int i = 0;
00270   GATTable attributes = NULL;
00271   
00272   GAT_TEST_START("create_software_description");
00273 
00274   /* create and fill the attribute table */
00275   attributes = GATTable_Create();
00276   GAT_TEST_TRACE(NULL != attributes, context);
00277 
00278   /* create "location" attribute" */
00279   location = GATLocation_Create("echo");
00280   GAT_TEST_TRACE(NULL != location, context);
00281   
00282   /* create and fill the "arguments" attribute */
00283   arguments = GATList_String_Create();
00284   GAT_TEST_TRACE(NULL != arguments, context);
00285   
00286   for (i = 1; i < 4; ++i)
00287   {
00288     char buffer[32];
00289     GATResult retval = GAT_SUCCESS;
00290     
00291     sprintf(buffer, "argument %d", i);
00292     retval = (GATList_String_Insert(arguments, 
00293       GATList_String_End(arguments), buffer)) ? 
00294         GAT_SUCCESS : GAT_MEMORYFAILURE;
00295     GAT_TEST_TRACE(GAT_SUCCEEDED(retval), context);
00296   }
00297   
00298   /* create and fill the "environment" attribute */
00299   environment = GATTable_Create();
00300   GAT_TEST_TRACE(NULL != environment, context);
00301   
00302   env_string = getenv("INCLUDE");
00303   if (NULL != env_string)
00304   {
00305     GATTable_Add_String(environment, "INCLUDE", env_string);
00306   }
00307   env_string = getenv("PATH");
00308   if (NULL != env_string)
00309   {
00310     GATTable_Add_String(environment, "PATH", env_string);
00311   }
00312   env_string = getenv("HOME");
00313   if (NULL != env_string)
00314   {
00315     GATTable_Add_String(environment, "HOME", env_string);
00316   }
00317   
00318   /* add required OS parameters to the requirements table */
00319   GATTable_Add_GATObject(attributes, "location", 
00320     GATLocation_ToGATObject_const(location));    
00321   GATTable_Add_GATObject(attributes, "arguments", 
00322     GATList_String_ToGATObject_const(arguments));
00323   GATTable_Add_GATObject(attributes, "environment", 
00324     GATTable_ToGATObject_const(environment));
00325   
00326   file = create_gatfile(context, "stdin");
00327   GATTable_Add_GATObject(attributes, "stdin", 
00328     GATFile_ToGATObject_const(file));
00329   GATFile_Destroy(&file);
00330   
00331   file = create_gatfile(context, "stdout");
00332   GATTable_Add_GATObject(attributes, "stdout", 
00333     GATFile_ToGATObject_const(file));
00334   GATFile_Destroy(&file);
00335   
00336   file = create_gatfile(context, "stderr");
00337   GATTable_Add_GATObject(attributes, "stderr", 
00338     GATFile_ToGATObject_const(file));
00339   GATFile_Destroy(&file);
00340 
00341   /* create the software description object */
00342   sw_desc = GATSoftwareDescription_Create(attributes);
00343   GAT_TEST_TRACE(NULL != sw_desc, context);
00344   
00345   GATTable_Destroy(&environment);
00346   GATList_String_Destroy(&arguments);
00347   GATLocation_Destroy(&location);
00348   GATTable_Destroy(&attributes);
00349 
00350   GAT_TEST_STOP();
00351   
00352   return sw_desc;
00353 }
00354