/** @file tst_pmConfigCommand.c
 *
 *  @brief Contains the tests for pmConfigCommand.c:
 *
 * This code will test the pmConfigCommand() routine.
 *
 *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2008-01-02 20:49:09 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
 */
#include <stdio.h>
#include <string.h>
#include <pslib.h>
#include <psmodules.h>
#include "tap.h"
#include "pstap.h"
#define ERR_TRACE_LEVEL         0
#define VERBOSE			0

int main(int argc, char* argv[])
{
    psLogSetFormat("HLNM");
    psLogSetLevel(PS_LOG_INFO);
    psTraceSetLevel(".", 0);
    psTraceSetLevel("err", ERR_TRACE_LEVEL);
    plan_tests(14);


    // --------------------------------------------------------------------
    // pmConfigDatabaseCommand() tests
    // Test pmConfigDatabaseCommand() with NULL command input param
    // XXX: I think the memory leak is in pmConfigRead()
    {
        psMemId id = psMemGetId();
        psString str[3];
        str[0] = "ARGS:";
        str[1] = "-site";
        str[2] = "../dataFiles/SampleIPPConfig";
        psS32 argc = 3;
        pmConfig *config = pmConfigRead(&argc, str, "RecipeName");
        if (!config) {
            str[2] = "dataFiles/SampleIPPConfig";
            config = pmConfigRead(&argc, str, "RecipeName");
	}
        ok(config != NULL, "pmConfigRead() successful");
        ok(!pmConfigDatabaseCommand(NULL, config), "pmConfigDatabaseCommand() returned NULL with NULL command input param");
        psFree(config);
        ok(!psMemCheckLeaks (id, NULL, NULL, false), "no memory leaks");
    }


    // Test pmConfigDatabaseCommand() with NULL pmConfig input param
    // XXX: I think the memory leak is in pmConfigRead()
    {
        psMemId id = psMemGetId();
        psString testCmd = psStringCopy("my command");
        ok(!pmConfigDatabaseCommand(&testCmd, NULL), "pmConfigDatabaseCommand() returned NULL with NULL command input param");
        ok(!psMemCheckLeaks (id, NULL, NULL, false), "no memory leaks");
    }


    // Test pmConfigDatabaseCommand() with command arg generated by other than psStringDup()
    // XXX: This seg-faults because pmConfigDatabaseCommand(), or whichever function,
    // does not verify that the command was allocated as a psString.
    if (0) {
        psMemId id = psMemGetId();
        psString str[3];
        str[0] = "ARGS:";
        str[1] = "-site";
        str[2] = "../dataFiles/SampleIPPConfig";
        psS32 argc = 3;
        pmConfig *config = pmConfigRead(&argc, str, "RecipeName");
        if (!config) {
            str[2] = "dataFiles/SampleIPPConfig";
            config = pmConfigRead(&argc, str, "RecipeName");
	}
        ok(config != NULL, "pmConfigRead() successful");
        char *testCmd = "command";
        ok(!pmConfigDatabaseCommand(&testCmd, config), "pmConfigDatabaseCommand() returned NULL with NULL command input param");
        ok(!psMemCheckLeaks (id, NULL, NULL, false), "no memory leaks");
    }


    // Test pmConfigDatabaseCommand() with acceptable input params
    // XXX: I think the memory leak is in pmConfigRead()
    {
        psMemId id = psMemGetId();
        psString str[3];
        str[0] = "ARGS:";
        str[1] = "-site";
        str[2] = "../dataFiles/SampleIPPConfig";
        psS32 argc = 3;
        pmConfig *config = pmConfigRead(&argc, str, "RecipeName");
        if (!config) {
            str[2] = "dataFiles/SampleIPPConfig";
            config = pmConfigRead(&argc, str, "RecipeName");
	}
        ok(config != NULL, "pmConfigRead() successful");
        psString testCmd = psStringCopy("my command");
        psString verifyCmd = psStringCopy(testCmd);
        // Generate the verify test string
        {
            bool mdok;
            psString dbserver = psMetadataLookupStr(&mdok, config->site, "DBSERVER");
            psString dbname = psMetadataLookupStr(&mdok, config->site, "DBNAME");
            psString dbuser = psMetadataLookupStr(&mdok, config->site, "DBUSER");
            psString dbpassword = psMetadataLookupStr(&mdok, config->site, "DBPASSWORD");
            psStringAppend(&verifyCmd, " -dbserver %s -dbname %s -dbuser %s -dbpassword %s",
                           dbserver, dbname, dbuser, dbpassword);
	}

        ok(pmConfigDatabaseCommand(&testCmd, config), "pmConfigDatabaseCommand() returned non-NULL with acceptable input params");
        ok(!strcmp(testCmd, verifyCmd), "pmConfigDatabaseCommand() generated the correct command string");
        psFree(config);
        ok(!psMemCheckLeaks (id, NULL, NULL, false), "no memory leaks");
    }


    // --------------------------------------------------------------------
    // pmConfigTraceCommand() tests
    // Test pmConfigTraceCommand() with NULL command input param
    {
        psMemId id = psMemGetId();
        ok(!pmConfigTraceCommand(NULL), "pmConfigTraceCommand() returned NULL with NULL command input param");
        ok(!psMemCheckLeaks (id, NULL, NULL, false), "no memory leaks");
    }


    // Test pmConfigTraceCommand() with acceptable input command input param
    {
        psMemId id = psMemGetId();
        psTraceSetLevel("dummyTraceLevel", 0);
        psString testCmd = psStringCopy("my command");
        psString verifyCmd = psStringCopy(testCmd);
        // Generate the verify test string
        {
            psMetadata *levels = psTraceLevels();
            psMetadataIterator *iter = psMetadataIteratorAlloc(levels, PS_LIST_HEAD, NULL);
            psMetadataItem *item;
            while ((item = psMetadataGetAndIncrement(iter))) {
                assert(item->type == PS_TYPE_S32);
                psStringAppend(&verifyCmd, " -trace %s %d", item->name, item->data.S32);
            }
            psFree(iter);
            psFree(levels);
	}
        ok(pmConfigTraceCommand(&testCmd), "pmConfigTraceCommand() returned non-NULL with acceptable input param");
        ok(!strcmp(testCmd, verifyCmd), "pmConfigTraceCommand() generated the correct command string");
        psFree(testCmd);
        psFree(verifyCmd);
        ok(!psMemCheckLeaks (id, NULL, NULL, false), "no memory leaks");
    }
}
    
