#!/usr/bin/env python
# test has been developed by Robert Harakaly and changed for SAM by Victor Galaktionov 
# logically delete a LFC file entry in the name server (lfc_delete)
# meta: proxy=true
# meta: preconfig=../../LFC-config

####
#
#	NOTE: Removes also the file which is not writeable.
#	NOTE: Error only if the parent directory is not writeable
#
####


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

global fileName

class test_ok(_test):
    def info(self):
      return "lfc_delete"

    def clean(self):
        lfc.lfc_unlink(fileName)
	lfc.lfc_rmdir(os.path.dirname(fileName))

    def prepare(self):
        lfc.lfc_mkdir(os.path.dirname(fileName),0755)
        self.guid = self.get_guid()
        ret = lfc.lfc_creatg(fileName,self.guid,0664)
        if (ret != 0):
            print "Error: cannot create testing file"
            print("Test is failed")
            return False
        return True


    def test(self):
        ret = lfc.lfc_delete(fileName) 
        statg=lfc.lfc_filestatg()
        lfc.lfc_statg(fileName,"", statg)
        return (statg,ret)
    def ret(self):
        retval=lfc.lfc_filestatg()
        retval.status='D'
        return retval
    def compare(self, testVal, retVal):
        (ret, retRetVal) = retVal
        (test, testRetVal) = testVal
        retval = True
        if (retRetVal == testRetVal):
            retval = retval & ( test.status == ret.status )
        else:
            retval = False
        return retval

class test_EACCES(_ntest):
    def info(self):
      return "lfc_delete in permission denied (EACCES)"
    def prepare(self):
        self.guid = self.get_guid()
        lfc.lfc_mkdir(os.path.dirname(fileName),0755)
        ret = lfc.lfc_creatg(fileName,self.guid,0664)
        if (ret != 0):
            print "Error: cannot create testing file"
            print("Test is failed")
            return False
        lfc.lfc_chmod(os.path.dirname(fileName), 0500)
        return True

    def clean(self):
        lfc.lfc_chmod(os.path.dirname(fileName),0700)
        lfc.lfc_unlink(fileName)
        lfc.lfc_rmdir(os.path.dirname(fileName))
 
    def test(self):
        ret = lfc.lfc_delete(fileName)
        err = lfc.cvar.serrno
        statg=lfc.lfc_filestatg()
        lfc.lfc_statg(fileName,"", statg)
        return ((statg, err), ret)

    def ret(self):
        retval=lfc.lfc_filestatg()
        retval.nlink=1
        retval.filesize=0L
        return (retval, errno.EACCES)

    def compare(self, testVal, retVal):
        ((ret, reterr), retRetVal) = retVal
        ((test, testerr), testRetVal) = testVal
        retval = True
        if ((retRetVal == testRetVal) & (reterr == testerr)):
            retval = retval & ( test.nlink == ret.nlink )
            retval = retval & ( test.filesize == ret.filesize )
        else:
            retval = False
        return retval

class test_EPERM(_ntest):
    def info(self):
        return "lfc_delete on directory (EPERM)"
    def prepare(self):
        self.guid = self.get_guid()
        lfc.lfc_mkdir(os.path.dirname(fileName),0755)
        ret = lfc.lfc_creatg(fileName,self.guid,0664)
        if (ret != 0):
            print "Error: cannot create testing file"
            print("Test is failed")
            return False
        return True

    def clean(self):
        lfc.lfc_chmod(os.path.dirname(fileName),0700)
        lfc.lfc_unlink(fileName)
        lfc.lfc_rmdir(os.path.dirname(fileName))

    def test(self):
        ret = lfc.lfc_delete(os.path.dirname(fileName))
        err = lfc.cvar.serrno
        statg=lfc.lfc_filestatg()
        lfc.lfc_statg(fileName,"", statg)
        return ((statg, err), ret)
    def ret(self):
        retval=lfc.lfc_filestatg()
        retval.nlink=1
        retval.filesize=0L
        retval.fileclass=0
        retval.status=' '
        return (retval, errno.EPERM)
    def compare(self, testVal, retVal):
        ((ret, reterr), retRetVal) = retVal
        ((test, testerr), testRetVal) = testVal
        retval = True
        if ((retRetVal == testRetVal) & (reterr == testerr)):
            retval = retval & ( test.nlink == ret.nlink )
            retval = retval & ( test.filesize == ret.filesize )
            retval = retval & ( test.fileclass == ret.fileclass )
        else:
            retval = False
        return retval

class test_ENOENT(_ntest):
    def info(self):
        return "lfc_delete nonexisting file (ENOENT)"
    def prepare(self):
        lfc.lfc_mkdir(os.path.dirname(fileName),0755)
    def clean(self):
        lfc.lfc_rmdir(os.path.dirname(fileName))

    def test(self):
        ret = lfc.lfc_delete(fileName)
        err = lfc.cvar.serrno
        statg=lfc.lfc_filestatg()
        lfc.lfc_statg(fileName,"", statg)
        return ((statg, err), ret)
    def ret(self):
        retval=lfc.lfc_filestatg()
        retval.nlink=0
        retval.filesize=0L
        retval.fileclass=0
        retval.status=' '
        return (retval, errno.ENOENT)
    def compare(self, testVal, retVal):
        ((ret, reterr), retRetVal) = retVal
        ((test, testerr), testRetVal) = testVal
        retval = True
        if ((retRetVal == testRetVal) & (reterr == testerr)):
            retval = retval & ( test.nlink == ret.nlink )
            retval = retval & ( test.filesize == ret.filesize )
            retval = retval & ( test.fileclass == ret.fileclass )
        else:
            retval = False
        return retval

class test_ENOTDIR(_ntest):
    def info(self):
        return "lfc_delete prefix not a DIR (ENOTDIR)"
    def prepare(self):
        self.guid = self.get_guid()
        lfc.lfc_creatg(os.path.dirname(fileName),self.guid,0664)
    def clean(self):
        lfc.lfc_unlink(os.path.dirname(fileName))

    def test(self):
        ret = lfc.lfc_delete(fileName)
        err = lfc.cvar.serrno
        statg=lfc.lfc_filestatg()
        lfc.lfc_statg(fileName,"", statg)
        return ((statg, err), ret)
    def ret(self):
        retval=lfc.lfc_filestatg()
        retval.nlink=0
        retval.filesize=0L
        retval.fileclass=0
        retval.status=' '
        return (retval, errno.ENOTDIR)
    def compare(self, testVal, retVal):
        ((ret, reterr), retRetVal) = retVal
        ((test, testerr), testRetVal) = testVal
        retval = True
        if ((retRetVal == testRetVal) & (reterr == testerr)):
            retval = retval & ( test.nlink == ret.nlink )
            retval = retval & ( test.filesize == ret.filesize )
            retval = retval & ( test.fileclass == ret.fileclass )
        else:
            retval = False
        return retval

class lfc_unlink_test(_testRunner):
    def __init__(self):
        global fileName
        self.name = "lfc_unlink_test"
        fileName=os.environ['LFC_HOME'] + "/python_delete_test/python_delete_test_file"
        self.tests=[test_ok,test_EACCES, test_EPERM, test_ENOENT, test_ENOTDIR]


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

