Skip to content

Commit

Permalink
convert possible content to pass the validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ticoann committed Dec 26, 2014
1 parent c18e310 commit e861874
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 42 deletions.
30 changes: 6 additions & 24 deletions src/python/WMCore/ReqMgr/Utils/Validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,15 @@
ReqMgr request handling.
"""

import time
import cherrypy
from datetime import datetime, timedelta

import WMCore.Lexicon
from WMCore.REST.Error import InvalidParameter
from WMCore.Database.CMSCouch import CouchError
from WMCore.WMSpec.WMWorkload import WMWorkloadHelper
from WMCore.WMSpec.StdSpecs.StdBase import WMSpecFactoryException
from WMCore.WMSpec.WMWorkloadTools import loadSpecByType
from WMCore.Wrappers import JsonWrapper

from WMCore.REST.Server import RESTEntity, restcall, rows
from WMCore.REST.Auth import authz_match
from WMCore.REST.Tools import tools
from WMCore.REST.Validation import validate_str, validate_strlist

import WMCore.ReqMgr.Service.RegExp as rx
from WMCore.ReqMgr.Auth import getWritePermission
from WMCore.ReqMgr.DataStructs.Request import initialize_request_args, generateRequestName
from WMCore.ReqMgr.DataStructs.RequestStatus import REQUEST_STATE_LIST, check_allowed_transition
from WMCore.ReqMgr.DataStructs.RequestStatus import REQUEST_STATE_TRANSITION
from WMCore.ReqMgr.DataStructs.RequestType import REQUEST_TYPES
from WMCore.ReqMgr.DataStructs.Request import initialize_request_args
from WMCore.ReqMgr.DataStructs.RequestStatus import check_allowed_transition
from WMCore.ReqMgr.DataStructs.RequestError import InvalidStateTransition

from WMCore.Services.RequestDB.RequestDBWriter import RequestDBWriter

def validate_request_update_args(request_args, config, reqmgr_db_service, param):
"""
param and safe structure is RESTArgs structure: named tuple
Expand Down Expand Up @@ -62,7 +43,6 @@ def validate_request_update_args(request_args, config, reqmgr_db_service, param)
authz_match(permission['role'], permission['group'])
del request_args["RequestType"]


#validate the status
if request_args.has_key("RequestStatus"):
validate_state_transition(reqmgr_db_service, request_name, request_args["RequestStatus"])
Expand All @@ -86,9 +66,11 @@ def validate_request_create_args(request_args, config, *args, **kwargs):
3. convert data from body to arguments (spec instance, argument with default setting)
TODO: rasie right kind of error with clear message
"""
print request_args
initialize_request_args(request_args, config)

initialize_request_args(request_args, config)
print "xxxxx"
from pprint import pprint
pprint(request_args)
#check the permission for creating the request
permission = getWritePermission(request_args)
authz_match(permission['role'], permission['group'])
Expand Down
4 changes: 2 additions & 2 deletions src/python/WMCore/WMSpec/StdSpecs/StdBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,10 +953,10 @@ def getWorkloadArguments():
"validate" : lambda x: all([cmsname(y) for y in x])},
# should be Low, Normal, High
"SubscriptionPriority" : {"default" : "Low", "type" : str,
"validate" : lambda x: all([cmsname(y) for y in x])},
"validate" : lambda x: x in ["Low", "Normal", "High"]},
# shouldbe Move Replica
"CustodialSubType" : {"default" : "Move", "type" : str,
"validate" : lambda x: all([cmsname(y) for y in x])},
"validate" : lambda x: x in ["Move", "Replica"]},

# Block closing informaiont
"BlockCloseMaxWaitTime" : {"default" : 66400, "type" : int, "validate" : lambda x : x > 0},
Expand Down
40 changes: 25 additions & 15 deletions src/python/WMCore/WMSpec/WMWorkloadTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class WMWorkloadToolsException(WMException):
"""
pass

class InvlaidSpecArgumentError(WMException):
pass

def makeLumiList(lumiDict):
try:
ll = LumiList(compactList = lumiDict)
Expand Down Expand Up @@ -66,10 +69,16 @@ def strToBool(string):
def checkDBSUrl(dbsUrl):
# use the import statement here since this is packed and used in RunTime code.
# dbs client is not shipped with it.
from WMCore.Services.DBS.DBS3Reader import DBS3Reader

if dbsUrl:
try:
DBS3Reader(dbsUrl).dbs.serverinfo()
#from WMCore.Services.DBS.DBS3Reader import DBS3Reader
#DBS3Reader(dbsUrl).dbs.serverinfo()
from WMCore.Services.Requests import JSONRequests
jsonSender = JSONRequests(dbsUrl)
result = jsonSender.get("/serverinfo")
if not result[1] == 200:
raise WMWorkloadToolsException("DBS is not connected: %s : %s" % (dbsUrl, str(result)))
except:
raise WMWorkloadToolsException("DBS is not responding: %s" % dbsUrl)

Expand All @@ -93,24 +102,24 @@ def parsePileupConfig(mcPileup, dataPileup):
def _validateArgument(argument, value, argumentDefinition):
validNull = argumentDefinition[argument]["null"]
if not validNull and value is None:
return "Argument %s can't be None" % argument
raise InvlaidSpecArgumentError("Argument %s can't be None" % argument)
elif validNull and value is None:
return

return value
try:
argType = argumentDefinition[argument]["type"]
argType(value)
value = argumentDefinition[argument]["type"](value)
except Exception:
return "Argument %s type is incorrect in schema." % argument
raise InvlaidSpecArgumentError("Argument %s type is incorrect in schema." % argument)

validateFunction = argumentDefinition[argument]["validate"]
if validateFunction is not None:
try:
if not validateFunction(argType(value)):
raise Exception
except:
if not validateFunction(value):
raise InvlaidSpecArgumentError("Argument %s doesn't pass the validation function." % argument)
except Exception, ex:
# Some validation functions (e.g. Lexicon) will raise errors instead of returning False
return "Argument %s doesn't pass validation." % argument
return
raise InvlaidSpecArgumentError("Validation failed: %s, %s, %s" % (argument, value, str(ex)))
return value

def validateArgumentsCreate(arguments, argumentDefinition):
"""
Expand All @@ -128,7 +137,8 @@ def validateArgumentsCreate(arguments, argumentDefinition):
return "Argument %s is required." % argument
elif optional and argument not in arguments:
continue
_validateArgument(argument, arguments[argument], argumentDefinition)
arguments[argument] = _validateArgument(argument, arguments[argument], argumentDefinition)

return

def validateArgumentsUpdate(arguments, argumentDefinition):
Expand All @@ -141,7 +151,7 @@ def validateArgumentsUpdate(arguments, argumentDefinition):
otherwise returns None
"""
for argument in arguments:
_validateArgument(argument, arguments[argument], argumentDefinition)
arguments[argument] = _validateArgument(argument, arguments[argument], argumentDefinition)
return

def setArgumentsNoneValueWithDefault(arguments, argumentDefinition):
Expand Down
5 changes: 4 additions & 1 deletion test/python/WMCore_t/ReqMgr_t/Service_t/ReqMgr_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def getAuthHeader(hmacData, reqAuth):
for role in reqAuth['role']:
roles[role] = {'group': reqAuth['group']}

return fake_authz_headers(hmacData, roles = roles, format = "dict")
return fake_authz_headers(hmacData, dn = "/TEST/DN/CN", roles = roles, format = "dict")


class ReqMgrTest(RESTBaseUnitTestWithDBBackend):
Expand Down Expand Up @@ -70,11 +70,14 @@ def setUp(self):
rerecoArgs = JsonWrapper.load(rerecoFile)
self.rerecoCreateArgs = rerecoArgs["createRequest"]
self.rerecoAssignArgs = rerecoArgs["assignRequest"]
# overwrite rereco args
self.rerecoAssignArgs["AcquisitionEra"] = "test_aqc"

lheFile = open(os.path.join(requestPath, "LHEStep0.json"), 'r')
lheArgs = JsonWrapper.load(lheFile)
self.lheStep0CreateArgs = lheArgs["createRequest"]
self.lheStep0AssignArgs = lheArgs["assignRequest"]
self.lheStep0AssignArgs["AcquisitionEra"] = "test_aqc"

cmsswDoc = {"_id": "software"}
cmsswDoc[self.rerecoCreateArgs["ScramArch"]] = []
Expand Down

0 comments on commit e861874

Please sign in to comment.