#!/usr/bin/env python
# test has been developed by Robert Harakaly and changed for SAM by Victor Galaktionov 
# add/replace   a  comment  associated  with  a  LFC file/directory in the name server (lfc_setcomment) 
# meta: proxy=true
# meta: preconfig=../../LFC-config

import os, lfc, sys
from testClass import _test, _ntest, _testRunner, SAM_Run, LFC_VO, TEST_HOME

class test_setcomment_ok(_test):
    def info(self):
        return "Set file comment existing file name"
    def prepare(self):
        guid = self.get_guid()
        self.name = LFC_VO + "/python_setcomment_test"
        ret = lfc.lfc_creatg(self.name,guid,0664)
    def clean(self):
        lfc.lfc_unlink(self.name)
        pass
    def test(self):
        self.comment="Test comment"
        comment="             "
        ret=lfc.lfc_setcomment(self.name,self.comment)
        lfc.lfc_getcomment(self.name, comment)
        return (comment.strip(),ret)
    def ret(self):
        return self.comment + '\x00'
    def compare(self, testVal, retVal):
        (ret, retRetVal) = retVal
        (test, testRetVal) = testVal
        retval = True
        if (retRetVal == testRetVal):
            retval = retval & (ret == test)
        else:
            retval = False
        return retval

class test_update_comment(_test):
    def info(self):
        return "Update comment on existing file name"
    def prepare(self):
        guid = self.get_guid()
        self.name = LFC_VO + "/python_setcomment_test"
        ret = lfc.lfc_creatg(self.name,guid,0664)
        lfc.lfc_setcomment(self.name,"Primary comment")
    def clean(self):
        lfc.lfc_unlink(self.name)
        pass
    def test(self):
        self.comment="Test comment"
        comment="             "
        ret=lfc.lfc_setcomment(self.name,self.comment)
        lfc.lfc_getcomment(self.name, comment)
        return (comment.strip(),ret)
    def ret(self):
        return self.comment + '\x00'
    def compare(self, testVal, retVal):
        (ret, retRetVal) = retVal
        (test, testRetVal) = testVal
        retval = True
        if (retRetVal == testRetVal):
            retval = retval & (ret == test)
        else:
            retval = False
        return retval

class test_nonexisting_name(_test):
    def __init__(self):
        self.retVal = -1
    def info(self):
	return "Set comment on nonexisting file name: "
    def prepare(self):
        self.name="/fhgsdjfgsagfhs"
    def test(self):
        self.comment="Test comment"
        comment="                              "
        ret=lfc.lfc_setcomment(self.name,self.comment)
        lfc.lfc_getcomment(self.name, comment)
        return (comment.strip(),ret)
    def ret(self):
        return ""

    def compare(self, testVal, retVal):
        (ret, retRetVal) = retVal
        (test, testRetVal) = testVal
        retval = True
        if (retRetVal == testRetVal):
            retval = (ret == test)
        else:
            retval = False
        return retval


class lfc_setcomment_test(_testRunner):
    def __init__(self):
        self.name = "lfc_setcomment_test"
        self.tests=[test_setcomment_ok, test_update_comment, test_nonexisting_name]


#************* Interface for SAM and Python tests ***************
SAM_Run(lfc_setcomment_test)

