Skip to content

Commit

Permalink
Change /api/resources default results to include all nodes. update te…
Browse files Browse the repository at this point in the history
…sts. factor common node filter param parsing into utility method
  • Loading branch information
gschueler committed Feb 14, 2011
1 parent b6bca57 commit dbf0063
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 67 deletions.
39 changes: 32 additions & 7 deletions rundeckapp/grails-app/controllers/FrameworkController.groovy
Expand Up @@ -439,6 +439,9 @@ class FrameworkController {

}
}
/**
* Render project info result using a builder
*/
def renderApiProject={ pject, delegate ->
delegate.project{
name(pject.name)
Expand All @@ -450,6 +453,29 @@ class FrameworkController {
}
}
}

/**
* Convert input node filter parameters into specific property names used by
* domain objects
*/
public static Map extractApiNodeFilterParams(Map params){
def result=[:]

//convert api parameters to node filter parameters
BaseNodeFilters.filterKeys.each{k,v->
if(params[k]){
result["nodeInclude${v}"]=params[k]
}
if(params["exclude-"+k]){
result["nodeExclude${v}"]=params["exclude-"+k]
}
}
if(params.'exclude-precedence'){
result.nodeExcludePrecedence=params['exclude-precedence']=='true'
}
return result
}

/**
* API: /api/project/NAME, version 1.2
*/
Expand Down Expand Up @@ -517,17 +543,16 @@ class FrameworkController {
}

//convert api parameters to node filter parameters
BaseNodeFilters.filterKeys.each{k,v->
if(params[k]){
query["nodeInclude${v}"]=params.remove(k)
}
if(params["exclude-"+k]){
query["nodeExclude${v}"]=params.remove("exclude-"+k)
def filters=extractApiNodeFilterParams(params)
if(filters){
filters.each{k,v->
query[k]=v
}
}

if(query.nodeFilterIsEmpty()){
query.nodeIncludeName = framework.getFrameworkNodeName()
//return all results
query.nodeInclude=".*"
}
def pject=frameworkService.getFrameworkProject(params.project,framework)
final Collection nodes = pject.getNodes().filterNodes(ExecutionService.filtersAsNodeSet(query))
Expand Down
54 changes: 5 additions & 49 deletions test/api/test-resource.sh
Expand Up @@ -32,61 +32,17 @@ file=$DIR/curl.out
XMLSTARLET=xml

###
# Setup: acquire local node name
# Setup: acquire local node name from RDECK_BASE/etc/framework.properties#node.name
####
localnode=$(grep 'framework.node.name' $RDECK_BASE/etc/framework.properties | sed 's/framework.node.name = //')

runurl="${apiurl}/resources"

project="test"
params="project=${project}"

# get listing
$CURL --header "$VERSHEADER" ${runurl}?${params} > ${file}
if [ 0 != $? ] ; then
errorMsg "ERROR: failed query request"
exit 2
fi
#test curl.out for valid xml
$XMLSTARLET val -w ${file} > /dev/null 2>&1
validxml=$?
if [ 0 == $validxml ] ; then
#test for for possible result error message
$XMLSTARLET el ${file} | grep -e '^result' -q
if [ 0 == $? ] ; then
#test for error message
#If <result error="true"> then an error occured.
waserror=$($XMLSTARLET sel -T -t -v "/result/@error" ${file})
errmsg=$($XMLSTARLET sel -T -t -v "/result/error/message" ${file})
if [ "" != "$waserror" -a "true" == $waserror ] ; then
errorMsg "ERROR: expected resource.xml content but received error result: $errmsg"
exit 2
fi
fi
fi

#test curl.out for valid xml
if [ 0 != $validxml ] ; then
errorMsg "ERROR: Response was not valid xml"
exit 2
fi

#test for expected /joblist element
$XMLSTARLET el ${file} | grep -e '^project' -q
if [ 0 != $? ] ; then
errorMsg "ERROR: Response did not contain expected result"
if [ -z "${localnode}" ] ; then
errorMsg "FAIL: Unable to determine framework.node.name from $RDECK_BASE/etc/framework.properties"
exit 2
fi

#Check projects list
itemcount=$($XMLSTARLET sel -T -t -v "count(/project/node)" ${file})
if [ "1" != "$itemcount" ] ; then
errorMsg "FAIL: expected single /project/node element"
exit 2
fi

localnode=$($XMLSTARLET sel -T -t -v "/project/node/@name" ${file})

runurl="${apiurl}/resource/$localnode"
project="test"
params="project=${project}"

echo "TEST: /api/resource/$localnode"
Expand Down
126 changes: 115 additions & 11 deletions test/api/test-resources.sh
Expand Up @@ -31,10 +31,20 @@ file=$DIR/curl.out

XMLSTARLET=xml

###
# Setup: acquire local node name from RDECK_BASE/etc/framework.properties#node.name
####
localnode=$(grep 'framework.node.name' $RDECK_BASE/etc/framework.properties | sed 's/framework.node.name = //')

if [ -z "${localnode}" ] ; then
errorMsg "FAIL: Unable to determine framework.node.name from $RDECK_BASE/etc/framework.properties"
exit 2
fi

# now submit req
runurl="${apiurl}/resources"

echo "TEST: /api/resources, basic XML response with no query: 1 result"
echo "TEST: /api/resources, basic XML response with all nodes: >0 result"

project="test"
params="project=${project}"
Expand All @@ -45,6 +55,7 @@ if [ 0 != $? ] ; then
errorMsg "ERROR: failed query request"
exit 2
fi

#test curl.out for valid xml
$XMLSTARLET val -w ${file} > /dev/null 2>&1
validxml=$?
Expand Down Expand Up @@ -76,11 +87,11 @@ if [ 0 != $? ] ; then
exit 2
fi

#Check projects list
#Check results list
itemcount=$($XMLSTARLET sel -T -t -v "count(/project/node)" ${file})
echo "$itemcount Nodes"
if [ "1" != "$itemcount" ] ; then
errorMsg "FAIL: expected single /project/node element"
if [ "0" == "$itemcount" ] ; then
errorMsg "FAIL: expected multiple /project/node element"
exit 2
fi

Expand All @@ -89,7 +100,7 @@ echo "OK"
#test yaml output
params="project=${project}&format=yaml"

echo "TEST: /api/resources, basic YAML response with no query: 1 result"
echo "TEST: /api/resources, basic YAML response with no query: >0 result"

# get listing
$CURL --header "$VERSHEADER" ${runurl}?${params} > ${file}
Expand All @@ -100,7 +111,7 @@ fi
#test curl.out for valid xml
$XMLSTARLET val -w ${file} > /dev/null 2>&1
validxml=$?
if [ 0 == $validxml ] ; then
if [ 0 == $validxml ] ; then
#test for error message
#If <result error="true"> then an error occured.
waserror=$($XMLSTARLET sel -T -t -v "/result/@error" ${file})
Expand All @@ -111,8 +122,8 @@ fi

itemcount=$(grep 'hostname: ' ${file} | wc -l | sed 's/^[ ]*//g')

if [ "1" != "$itemcount" ] ; then
errorMsg "FAIL: Expected single yaml result, was \"${itemcount}\""
if [ "0" == "$itemcount" ] ; then
errorMsg "FAIL: Expected multiple yaml result, was \"${itemcount}\""
exit 2
fi

Expand Down Expand Up @@ -154,7 +165,7 @@ if [ 0 != $? ] ; then
exit 2
fi

#Check projects list
#Check results list
itemcount=$($XMLSTARLET sel -T -t -v "count(/project/node)" ${file})
if [ "1" != "$itemcount" ] ; then
errorMsg "FAIL: expected single /project/node element"
Expand Down Expand Up @@ -185,7 +196,7 @@ if [ 0 != $? ] ; then
exit 2
fi

#Check projects list
#Check results list
itemcount=$($XMLSTARLET sel -T -t -v "count(/project/node)" ${file})
if [ "1" != "$itemcount" ] ; then
errorMsg "FAIL: expected single /project/node element"
Expand Down Expand Up @@ -216,7 +227,7 @@ if [ 0 != $? ] ; then
exit 2
fi

#Check projects list
#Check results list
itemcount=$($XMLSTARLET sel -T -t -v "count(/project/node)" ${file})
assert "2" $itemcount "Expected two /project/node results"
itemname=$($XMLSTARLET sel -T -t -v "/project/node[@name='test1']/@name" ${file})
Expand All @@ -226,5 +237,98 @@ assert "test2" "$itemname" "Query for second item"

echo "OK"

####
#exclude both nodes node
####

query="exclude-tags=testboth"
params="project=${project}&format=xml&${query}"

echo "TEST: query result for /etc/resources&$query"

$CURL --header "$VERSHEADER" ${runurl}?${params} > ${file}
if [ 0 != $? ] ; then
errorMsg "ERROR: failed query request"
exit 2
fi
$XMLSTARLET val -w ${file} > /dev/null 2>&1
if [ 0 != $? ] ; then
errorMsg "FAIL: result was not valid xml"
exit 2
fi

#Check results list
itemcount=$($XMLSTARLET sel -T -t -v "count(/project/node)" ${file})
assert "1" $itemcount "Expected two /project/node results"
itemname=$($XMLSTARLET sel -T -t -v "/project/node[@name='test1']/@name" ${file})
assert "" "$itemname" "Query for first item"
itemname=$($XMLSTARLET sel -T -t -v "/project/node[@name='test2']/@name" ${file})
assert "" "$itemname" "Query for second item"

echo "OK"


####
#query mixed filters
####

query="tags=testboth&exclude-name=test2"
params="project=${project}&format=xml&${query}"

echo "TEST: query result for /etc/resources, using mixed include/exclude filters"

$CURL --header "$VERSHEADER" ${runurl}?${params} > ${file}
if [ 0 != $? ] ; then
errorMsg "ERROR: failed query request"
exit 2
fi
$XMLSTARLET val -w ${file} > /dev/null 2>&1
if [ 0 != $? ] ; then
errorMsg "FAIL: result was not valid xml"
exit 2
fi

#Check results list
itemcount=$($XMLSTARLET sel -T -t -v "count(/project/node)" ${file})
if [ "1" != "$itemcount" ] ; then
errorMsg "FAIL: expected single /project/node element"
exit 2
fi
itemname=$($XMLSTARLET sel -T -t -v "/project/node/@name" ${file})
assert "test1" $itemname "Query result"

echo "OK"

####
#query mixed filters, change exclude-precedence=false
####

query="tags=test1&exclude-tags=testboth&exclude-precedence=false"
params="project=${project}&format=xml&${query}"

echo "TEST: /etc/resources, using mixed include/exclude filters, exclude-precedence=false"

$CURL --header "$VERSHEADER" ${runurl}?${params} > ${file}
if [ 0 != $? ] ; then
errorMsg "ERROR: failed query request"
exit 2
fi
$XMLSTARLET val -w ${file} > /dev/null 2>&1
if [ 0 != $? ] ; then
errorMsg "FAIL: result was not valid xml"
exit 2
fi


#Check results list
itemcount=$($XMLSTARLET sel -T -t -v "count(/project/node)" ${file})
assert "2" $itemcount "Expected two /project/node results"
itemname=$($XMLSTARLET sel -T -t -v "/project/node[@name='test1']/@name" ${file})
assert "test1" "$itemname" "Query for first item"
itemname=$($XMLSTARLET sel -T -t -v "/project/node[@name='${localnode}']/@name" ${file})
assert "$localnode" "$itemname" "Query for local item"

echo "OK"

rm ${file}
mv $RDECK_BASE/projects/test/etc/resources.xml.backup $RDECK_BASE/projects/test/etc/resources.xml

0 comments on commit dbf0063

Please sign in to comment.