#!/usr/bin/env python
# The test has been  developed by Robert Harakaly and changed for SAM by Victor Galaktionov 
# test for adding a replica for a given file (lfc_addreplica)
# meta: proxy=true
# meta: preconfig=../../LFC-config

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

class test_OK(_test):
    def info(self):
	return "Test adding replica (guid): "
    def prepare(self):
        self.guid=self.get_guid()
        self.name=LFC_VO + "/lfc_getreplica_test"
        lfc.lfc_creatg(self.name, self.guid, 0664)
    def clean(self):
        lfc.lfc_delreplica(self.guid, None, self.sfn)
        lfc.lfc_unlink(self.name)
    def test(self):
        self.sfn="sfn://test-se.cern.ch" + LFC_VO + "/hary/lfc_getreplica_test"
        lfc.lfc_addreplica(self.guid, None, "test-se.cern.ch", self.sfn, '-', 'D', "my-pool", "/pool")

        ret, list = lfc.lfc_getreplica(self.name, "", "")
        return (list,ret)
    def ret(self):
        retval=lfc.lfc_filereplica()
        retval.status="-"
        retval.sfn=self.sfn
        retval.f_type="D"
        retval.poolname="my-pool"
        retval.fs="/pool"
        return retval
    def compare(self, testVal, retVal):
        (ret, retRetVal) = retVal
        (test, testRetVal) = testVal
        retval = True
        if (retRetVal == testRetVal):
            retval = retval & ( test[0].sfn == ret.sfn )
            retval = retval & ( test[0].status == ret.status )
            retval = retval & ( test[0].f_type == ret.f_type )
            retval = retval & ( test[0].poolname == ret.poolname )
            retval = retval & ( test[0].fs == ret.fs )

        else:
            retval = False
        return retval



class test_OK_id(_test):
    def info(self):
        return "Test adding replica (fileid): "
    def prepare(self):
        self.guid=self.get_guid()
        self.name= LFC_VO + "/lfc_getreplica_test"
        lfc.lfc_creatg(self.name, self.guid, 0664)
    def clean(self):
        lfc.lfc_delreplica(self.guid, None, self.sfn)
        lfc.lfc_unlink(self.name)
    def test(self):
        self.sfn="sfn://test-se.cern.ch" + LFC_VO + "/hary/lfc_getreplica_test"
        id = lfc.lfc_fileid()
        lfc.lfc_statx(self.name, id, lfc.lfc_filestat())
        lfc.lfc_addreplica("", id, "test-se.cern.ch", self.sfn, '-', 'D', "my-pool", "/pool")

        ret, list = lfc.lfc_getreplica(self.name, "", "")
        return (list,ret)
    def ret(self):
        retval=lfc.lfc_filereplica()
        retval.status="-"
        retval.sfn=self.sfn
        retval.f_type="D"
        retval.poolname="my-pool"
        retval.fs="/pool"
        return retval
    def compare(self, testVal, retVal):
        (ret, retRetVal) = retVal
        (test, testRetVal) = testVal
        retval = True
        if (retRetVal == testRetVal):
            retval = retval & ( test[0].sfn == ret.sfn )
            retval = retval & ( test[0].status == ret.status )
            retval = retval & ( test[0].f_type == ret.f_type )
            retval = retval & ( test[0].poolname == ret.poolname )
            retval = retval & ( test[0].fs == ret.fs )

        else:
            retval = False
        return retval

class test_EINVAL1(_ntest):
    def info(self):
        return "Test existing replica nonmatching guid/fileid (EINVAL): "
    def prepare(self):
        self.sfn="sfn://test-se.cern.ch" + LFC_VO + "/hary/lfc_getreplica_test"
        self.guid=self.get_guid()
        self.name=LFC_VO + "/lfc_getreplica_test"
        lfc.lfc_creatg(self.name, self.guid, 0664)
        self.name1=LFC_VO + "/lfc_getreplica_test1"
        self.guid1=self.get_guid()
    def clean(self):
        lfc.lfc_delreplica(self.guid, None, self.sfn)
        lfc.lfc_unlink(self.name)
        lfc.lfc_unlink(self.name1)
    def test(self):
        id = lfc.lfc_fileid()
        lfc.lfc_statx(self.name1, id, lfc.lfc_filestat())
        ret = lfc.lfc_addreplica(self.guid, id, os.getenv("LFC_HOST"), self.sfn, '-', 'D', "", "")
        return (None,lfc.cvar.serrno,ret)
    def ret(self):
        return (None, errno.EINVAL)
    def compare(self, testVal, retVal):
        ((ret, reterr), retRetVal) = retVal
        (test, testerr, testRetVal) = testVal
        print reterr, testerr
        if ((retRetVal == testRetVal) & (reterr == testerr)):
            retval = True
        else:
            retval = False
        return retval

class test_EINVAL2(_ntest):
    def info(self):
        return "Test guid length exeeds CA_MAXGUILEN (EINVAL): "
    def test(self):
        guid = ""
        sfn = "sfn://test-se.cern.ch" + LFC_VO + "/hary/lfc_getreplica_test"
        for i in range (0,lfc.CA_MAXGUIDLEN+1):
            guid = guid + "a"
        ret = lfc.lfc_addreplica(guid, None, os.getenv("LFC_HOST"), sfn ,"-","D","","")
        return (None,lfc.cvar.serrno,ret)
    def ret(self):
        return (None, errno.EINVAL)
    def compare(self, testVal, retVal):
        ((ret, reterr), retRetVal) = retVal
        (test, testerr, testRetVal) = testVal
        if ((retRetVal == testRetVal) & (reterr == testerr)):
            retval = True
        else:
            retval = False
        return retval

class test_EINVAL3(_ntest):
    def info(self):
        return "Test SE name length exeeds CA_MAXGUILEN (EINVAL): "
    def prepare(self):
        self.guid=self.get_guid()
        self.name=LFC_VO + "/lfc_getreplica_test"
        lfc.lfc_creatg(self.name, self.guid, 0664)
    def clean(self):
        lfc.lfc_unlink(self.name)
    def test(self):
        se = ""
        sfn = "sfn://test-se.cern.ch" + LFC_VO + "/hary/lfc_getreplica_test"
        for i in range (0,lfc.CA_MAXNAMELEN+1):
            se = se + "a"
        ret = lfc.lfc_addreplica(self.guid, None, se, sfn,"-","D","","")
        return (None,lfc.cvar.serrno,ret)
    def ret(self):
        return (None, errno.EINVAL) 
    def compare(self, testVal, retVal):
        ((ret, reterr), retRetVal) = retVal
        (test, testerr, testRetVal) = testVal
        if ((retRetVal == testRetVal) & (reterr == testerr)):
            retval = True
        else:
            retval = False
        return retval

class test_ENOENT(_ntest):
    def info(self):
        return "Test existing replica nonexisting file (ENOENT): "
    def prepare(self):
        self.guid=self.get_guid()
    def test(self):
        sfn = "sfn://test-se.cern.ch" + LFC_VO + "/hary/lfc_getreplica_test"
        ret = lfc.lfc_addreplica(self.guid, None,os.getenv("LFC_HOST"), sfn,"-","D","","")
        return (None,lfc.cvar.serrno,ret)
    def ret(self):
        return (None, errno.ENOENT)
    def compare(self, testVal, retVal):
        ((ret, reterr), retRetVal) = retVal
        (test, testerr, testRetVal) = testVal
        if ((retRetVal == testRetVal) & (reterr == testerr)):
            retval = True
        else:
            retval = False
        return retval


class lfc_addreplica_test(_testRunner):
    def __init__(self):
      	self.name = "lfc_addreplica_test"
        self.tests=[test_OK, test_OK_id, test_EINVAL2, test_EINVAL3, test_ENOENT]


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

