Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple tests for yamlprobe #1479

Merged
merged 11 commits into from
Feb 18, 2020
1 change: 1 addition & 0 deletions tests/probes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ add_subdirectory("systemdunitproperty")
add_subdirectory("textfilecontent54")
add_subdirectory("uname")
add_subdirectory("xinetd")
add_subdirectory("yamlfilecontent")
5 changes: 5 additions & 0 deletions tests/probes/yamlfilecontent/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if(ENABLE_PROBES_INDEPENDENT)
add_oscap_test("test_probes_yamlfilecontent_key.sh")
add_oscap_test("test_probes_yamlfilecontent_array.sh")
endif()

40 changes: 40 additions & 0 deletions tests/probes/yamlfilecontent/openshift-logging.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
apiVersion: "logging.openshift.io/v1alpha1"
kind: "LogForwarding"
metadata:
name: instance
namespace: openshift-logging
spec:
disableDefaultForwarding: true
outputs:
- type: "elasticsearch"
name: elasticsearch
endpoint: elasticsearch.openshift-logging.svc:9200
mode: 066
secret:
name: fluentd
- type: "elasticsearch"
name: elasticsearch-insecure
endpoint: elasticsearch-insecure.svc.messaging.cluster.local
mode: "066"
insecure: true
- type: "forward"
name: secureforward-offcluster
endpoint: https://secureforward.offcluster.com:9200
mode: "067"
secret:
name: secureforward
pipelines:
- inputSource: "hello"
- name: container-logs
inputSource: logs.app
outputRefs:
- elasticsearch
- secureforward-offcluster
- name: infra-logs
inputSource: logs.infra
outputRefs:
- elasticsearch-insecure
- name: audit-logs
inputSource: logs.audit
outputRefs:
- secureforward-offcluster
33 changes: 33 additions & 0 deletions tests/probes/yamlfilecontent/test_probes_yamlfilecontent_array.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash

. $builddir/tests/test_common.sh

function test_probes_yamlfilecontent_array {

probecheck "yamlfilecontent" || return 255

local ret_val=0
local DF="${srcdir}/test_probes_yamlfilecontent_array.xml"
local RF="results.xml"

[ -f $RF ] && rm -f $RF

cp "${srcdir}/openshift-logging.yaml" /tmp

local YAML_FILE="/tmp/openshift-logging.yaml"

$OSCAP oval eval --results $RF $DF

if [ -f $RF ]; then
verify_results "def" $DF $RF 2 && verify_results "tst" $DF $RF 3
ret_val=$?
else
ret_val=1
fi
yuumasato marked this conversation as resolved.
Show resolved Hide resolved

rm -f $YAML_FILE

return $ret_val
}

test_probes_yamlfilecontent_array
99 changes: 99 additions & 0 deletions tests/probes/yamlfilecontent/test_probes_yamlfilecontent_array.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0"?>
<oval_definitions xmlns:oval-def="http://oval.mitre.org/XMLSchema/oval-definitions-5" xmlns:oval="http://oval.mitre.org/XMLSchema/oval-common-5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ind-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent" xmlns:unix-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#unix" xmlns:lin-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#linux" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5" xsi:schemaLocation="http://oval.mitre.org/XMLSchema/oval-definitions-5#unix unix-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5#independent independent-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5#linux linux-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5 oval-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-common-5 oval-common-schema.xsd">

<generator>
<oval:product_name>yamlfilecontent</oval:product_name>
<oval:product_version>1.0</oval:product_version>
<oval:schema_version>5.11.3</oval:schema_version>
<oval:timestamp>2020-02-13T00:00:00-00:00</oval:timestamp>
</generator>

<definitions>

<definition class="compliance" version="1" id="oval:0:def:1"> <!-- comment="true" -->
<metadata>
<title></title>
<description></description>
</metadata>
<criteria operator="AND">
<criterion comment="get_indexed_array" test_ref="oval:0:tst:1"/>
<criterion comment="get_range_array" test_ref="oval:0:tst:2"/>
</criteria>
</definition>

<definition class="compliance" version="1" id="oval:0:def:2"> <!-- comment="error" -->
<metadata>
<title></title>
<description></description>
</metadata>
<criteria operator="AND">
<criterion comment="get_non_scalar_array" test_ref="oval:0:tst:3"/>
</criteria>
</definition>

</definitions>

<tests>

<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:1" check="all" comment="true">
<ind-def:object object_ref="oval:0:obj:1"/>
<ind-def:state state_ref="oval:0:ste:1"/>
</ind-def:yamlfilecontent_test>

<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:2" check="all" comment="true">
<ind-def:object object_ref="oval:0:obj:2"/>
<ind-def:state state_ref="oval:0:ste:2"/>
</ind-def:yamlfilecontent_test>

<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:3" check="all" comment="error">
<ind-def:object object_ref="oval:0:obj:3"/>
</ind-def:yamlfilecontent_test>

</tests>

<objects>

<ind-def:yamlfilecontent_object version="1" id="oval:0:obj:1">
<ind-def:path>/tmp</ind-def:path>
<ind-def:filename>openshift-logging.yaml</ind-def:filename>
<ind-def:yamlpath>.spec.outputs[2].name</ind-def:yamlpath>
</ind-def:yamlfilecontent_object>

<ind-def:yamlfilecontent_object version="1" id="oval:0:obj:2">
<ind-def:path>/tmp</ind-def:path>
<ind-def:filename>openshift-logging.yaml</ind-def:filename>
<ind-def:yamlpath>.spec.outputs[0:2].name</ind-def:yamlpath>
</ind-def:yamlfilecontent_object>

<ind-def:yamlfilecontent_object version="1" id="oval:0:obj:3">
<ind-def:path>/tmp</ind-def:path>
<ind-def:filename>openshift-logging.yaml</ind-def:filename>
<ind-def:yamlpath>.spec.outputs[0]</ind-def:yamlpath>
</ind-def:yamlfilecontent_object>

</objects>

<states>

<ind-def:yamlfilecontent_state version="1" id="oval:0:ste:1">
<ind-def:value_of datatype="string">secureforward-offcluster</ind-def:value_of>
</ind-def:yamlfilecontent_state>

<ind-def:yamlfilecontent_state version="1" id="oval:0:ste:2">
<ind-def:value_of datatype="string" var_ref="oval:0:var:2"/>
</ind-def:yamlfilecontent_state>

</states>

<variables>

<local_variable comment="variable with two values" datatype="string" version="1" id="oval:0:var:2">
<!-- The use of split should force creation of a local variable with two values
NOTE: Wildly untested! -->
<split delimiter="|">
<literal_component>elasticsearch|elasticsearch-insecure</literal_component>
</split>
</local_variable>

</variables>
</oval_definitions>
33 changes: 33 additions & 0 deletions tests/probes/yamlfilecontent/test_probes_yamlfilecontent_key.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash

. $builddir/tests/test_common.sh

function test_probes_yamlfilecontent_key {

probecheck "yamlfilecontent" || return 255

local ret_val=0
local DF="${srcdir}/test_probes_yamlfilecontent_key.xml"
local RF="results.xml"

[ -f $RF ] && rm -f $RF

cp "${srcdir}/openshift-logging.yaml" /tmp

local YAML_FILE="/tmp/openshift-logging.yaml"

$OSCAP oval eval --results $RF $DF

if [ -f $RF ]; then
verify_results "def" $DF $RF 3 && verify_results "tst" $DF $RF 4
ret_val=$?
else
ret_val=1
fi
yuumasato marked this conversation as resolved.
Show resolved Hide resolved

rm -f $YAML_FILE

return $ret_val
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do you test that the correct data was extracted from the YAML? Please add assertions on collected items.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests have states, if they pass doesn't it mean we have collected the correct data?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably yes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the that expected error result? And what about that non-existing scalar? The test could assume that no objects are collected.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per yamlpath description in https://github.com/OVAL-Community/OVAL/pull/90/files#diff-ae56180beddebc6013e4387e9ed9fe0eR2118, error is the expected result when the path leads to a non-scalar.

The xpath field in xmlfilecontent_object behaves the same way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, maybe @matejak is asking about how the test verifies that the result was error?
In verify_results() the result of evaluation is compared with the results the comments.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I was not able to understand just by looking at verify_results documentation. That doc needs a refresh.

}

test_probes_yamlfilecontent_key
108 changes: 108 additions & 0 deletions tests/probes/yamlfilecontent/test_probes_yamlfilecontent_key.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?xml version="1.0"?>
<oval_definitions xmlns:oval-def="http://oval.mitre.org/XMLSchema/oval-definitions-5" xmlns:oval="http://oval.mitre.org/XMLSchema/oval-common-5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ind-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent" xmlns:unix-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#unix" xmlns:lin-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#linux" xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5" xsi:schemaLocation="http://oval.mitre.org/XMLSchema/oval-definitions-5#unix unix-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5#independent independent-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5#linux linux-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5 oval-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-common-5 oval-common-schema.xsd">

<generator>
<oval:product_name>yamlfilecontent</oval:product_name>
<oval:product_version>1.0</oval:product_version>
<oval:schema_version>5.11.3</oval:schema_version>
<oval:timestamp>2020-02-13T00:00:00-00:00</oval:timestamp>
</generator>

<definitions>

<definition class="compliance" version="1" id="oval:0:def:1"> <!-- comment="true" -->
<metadata>
<title></title>
<description></description>
</metadata>
<criteria operator="AND">
<criterion comment="get_root_scalar" test_ref="oval:0:tst:1"/>
<criterion comment="get_nested_scalar" test_ref="oval:0:tst:2"/>
</criteria>
</definition>

<definition class="compliance" version="1" id="oval:0:def:2"> <!-- comment="error" -->
<metadata>
<title></title>
<description></description>
</metadata>
<criteria operator="AND">
<criterion comment="get_non_scalar" test_ref="oval:0:tst:3"/>
</criteria>
</definition>

<definition class="compliance" version="1" id="oval:0:def:3"> <!-- comment="false" -->
<metadata>
<title></title>
<description></description>
</metadata>
<criteria operator="AND">
<criterion comment="get_non_existing_scalar" test_ref="oval:0:tst:4"/>
</criteria>
</definition>

</definitions>

<tests>

<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:1" check="all" comment="true">
<ind-def:object object_ref="oval:0:obj:1"/>
<ind-def:state state_ref="oval:0:ste:1"/>
</ind-def:yamlfilecontent_test>

<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:2" check="all" comment="true">
<ind-def:object object_ref="oval:0:obj:2"/>
<ind-def:state state_ref="oval:0:ste:2"/>
</ind-def:yamlfilecontent_test>

<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:3" check="all" comment="error">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is error expected?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

<ind-def:object object_ref="oval:0:obj:3"/>
</ind-def:yamlfilecontent_test>

<ind-def:yamlfilecontent_test version="1" id="oval:0:tst:4" check="all" comment="false">
<ind-def:object object_ref="oval:0:obj:4"/>
</ind-def:yamlfilecontent_test>

</tests>

<objects>

<ind-def:yamlfilecontent_object version="1" id="oval:0:obj:1">
<ind-def:path>/tmp</ind-def:path>
<ind-def:filename>openshift-logging.yaml</ind-def:filename>
<ind-def:yamlpath>.kind</ind-def:yamlpath>
</ind-def:yamlfilecontent_object>

<ind-def:yamlfilecontent_object version="1" id="oval:0:obj:2">
<ind-def:path>/tmp</ind-def:path>
<ind-def:filename>openshift-logging.yaml</ind-def:filename>
<ind-def:yamlpath>.metadata.namespace</ind-def:yamlpath>
</ind-def:yamlfilecontent_object>

<ind-def:yamlfilecontent_object version="1" id="oval:0:obj:3">
<ind-def:path>/tmp</ind-def:path>
<ind-def:filename>openshift-logging.yaml</ind-def:filename>
<ind-def:yamlpath>.metadata</ind-def:yamlpath>
</ind-def:yamlfilecontent_object>

<ind-def:yamlfilecontent_object version="1" id="oval:0:obj:4">
<ind-def:path>/tmp</ind-def:path>
<ind-def:filename>openshift-logging.yaml</ind-def:filename>
<ind-def:yamlpath>.doesnt.exist</ind-def:yamlpath>
</ind-def:yamlfilecontent_object>

</objects>

<states>

<ind-def:yamlfilecontent_state version="1" id="oval:0:ste:1">
<ind-def:value_of datatype="string">LogForwarding</ind-def:value_of>
</ind-def:yamlfilecontent_state>

<ind-def:yamlfilecontent_state version="1" id="oval:0:ste:2">
<ind-def:value_of datatype="string">openshift-logging</ind-def:value_of>
</ind-def:yamlfilecontent_state>

</states>

</oval_definitions>