From 620cc81f0c118cdb68b28492d65702df3d8bf460 Mon Sep 17 00:00:00 2001 From: davideschiera Date: Wed, 20 Feb 2019 15:19:44 -0800 Subject: [PATCH 1/6] Consolidate and simplify tests using Sysdig agent 1. Use a single command line to launch Sysdig agent container 2. Wait longer after the launch, to make data is made available via Sysdig API 3. Simplify test for policy events (wait longer instead of polling) --- test/test_monitor_apis.sh | 6 ++++-- test/test_secure_apis.sh | 27 ++++++++++++++------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/test/test_monitor_apis.sh b/test/test_monitor_apis.sh index a9b5587e..b42b7d89 100644 --- a/test/test_monitor_apis.sh +++ b/test/test_monitor_apis.sh @@ -7,9 +7,11 @@ SCRIPTDIR=$(dirname $SCRIPT) export SDC_URL=https://app-staging.sysdigcloud.com -docker run -d -it --rm --name sysdig-agent --privileged --net host --pid host -e COLLECTOR=collector-staging.sysdigcloud.com -e ACCESS_KEY=$PYTHON_SDC_TEST_ACCESS_KEY -v /var/run/docker.sock:/host/var/run/docker.sock -v /dev:/host/dev -v /proc:/host/proc:ro -v /boot:/host/boot:ro -v /lib/modules:/host/lib/modules:ro -v /usr:/host/usr:ro sysdig/agent +# Start an agent using the testing account API key to send some data +docker run -d -it --rm --name sysdig-agent --privileged --net host --pid host -e COLLECTOR=collector-staging.sysdigcloud.com -e ACCESS_KEY=$PYTHON_SDC_TEST_ACCESS_KEY -v /var/run/docker.sock:/host/var/run/docker.sock -v /dev:/host/dev -v /proc:/host/proc:ro -v /boot:/host/boot:ro -v /lib/modules:/host/lib/modules:ro -v /usr:/host/usr:ro --shm-size=350m sysdig/agent -sleep 20 +# make sure the agent starts sending data and the backend makes it available via API +sleep 60 AGENT_HOSTNAME=$(hostname -s) SESSION_UUID=$(head -c 32 /dev/urandom | tr -dc 'a-zA-Z0-9') diff --git a/test/test_secure_apis.sh b/test/test_secure_apis.sh index 203852b7..4408b5c3 100644 --- a/test/test_secure_apis.sh +++ b/test/test_secure_apis.sh @@ -131,26 +131,27 @@ fi echo $OUT -# Start an agent using this account's api key and trigger some events -docker run -d -it --rm --name sysdig-agent --privileged --net host --pid host -e COLLECTOR=collector-staging.sysdigcloud.com -e ACCESS_KEY=$PYTHON_SDC_TEST_ACCESS_KEY -v /var/run/docker.sock:/host/var/run/docker.sock -v /dev:/host/dev -v /proc:/host/proc:ro -v /boot:/host/boot:ro -v /lib/modules:/host/lib/modules:ro -v /usr:/host/usr:ro -e ADDITIONAL_CONF="security: {enabled: true}\ncommandlines_capture: {enabled: true}\nmemdump: {enabled: true}" --shm-size=350m sysdig/agent +# Start an agent using the testing account API key and trigger an event +docker run -d -it --rm --name sysdig-agent --privileged --net host --pid host -e COLLECTOR=collector-staging.sysdigcloud.com -e ACCESS_KEY=$PYTHON_SDC_TEST_ACCESS_KEY -v /var/run/docker.sock:/host/var/run/docker.sock -v /dev:/host/dev -v /proc:/host/proc:ro -v /boot:/host/boot:ro -v /lib/modules:/host/lib/modules:ro -v /usr:/host/usr:ro --shm-size=350m sysdig/agent -FOUND=0 +# make sure the agent starts sending data and the backend makes it available via API +sleep 60 -for i in $(seq 10); do - sleep 10 - sudo touch /bin/some-file.txt +sudo touch /bin/some-file.txt - EVTS=`$SCRIPTDIR/../examples/get_secure_policy_events.py $PYTHON_SDC_TEST_API_TOKEN 60` +# make sure the agent sends the policy event and the backend makes it available via API +sleep 60 + +EVTS=`$SCRIPTDIR/../examples/get_secure_policy_events.py $PYTHON_SDC_TEST_API_TOKEN 90` + +if [[ "$EVTS" != "" ]]; then + FOUND=1 +fi - if [[ "$EVTS" != "" ]]; then - FOUND=1 - break; - fi -done docker logs sysdig-agent docker stop sysdig-agent if [[ $FOUND == 0 ]]; then - echo "Did not find any policy events after 10 attempts..." + echo "Did not find any policy events after 60 seconds of wait" exit 1 fi From 201779b522e5face0961b78ed744a1456af9ac27 Mon Sep 17 00:00:00 2001 From: davideschiera Date: Wed, 20 Feb 2019 15:37:15 -0800 Subject: [PATCH 2/6] Start/stop agent separately from test scripts --- .travis.yml | 2 ++ test/start_agent.sh | 9 +++++++++ test/stop_agent.sh | 6 ++++++ test/test_monitor_apis.sh | 9 --------- test/test_secure_apis.sh | 7 +------ 5 files changed, 18 insertions(+), 15 deletions(-) create mode 100644 test/start_agent.sh create mode 100644 test/stop_agent.sh diff --git a/.travis.yml b/.travis.yml index f44b4936..7df44101 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,8 +7,10 @@ install: - sudo apt-get install linux-headers-$(uname -r) dkms gcc-multilib g++-multilib - pip install pyyaml requests script: +- bash test/start_agent.sh - bash test/test_monitor_apis.sh - bash test/test_secure_apis.sh +- bash test/stop_agent.sh notifications: slack: secure: GJ0H2wAaW67t3+x+cCjOVLw/b+YAZjH9rebAfluCFJklJS9u+V6/qqIyiNDAkMPIcgilFyhiyOQCZYXapgthQ1ieFbZZo//mrRtuGo2wuxCAwcOx2mXAZpZ4I5b3+Q/znVqSuqkwFRid1d138z4TW7sYSIburouDX3QUNoUOy+g7VJxCFQCcqlN8LYxGJHQYdOZa9zIGCtKrOtZ/B8C1TLgXmDMwAAVNO2WzL4GiBTLCGuMsQWMTLw2Qmv1ayZPztmeDWo1C9oa7HIH8Bg2YVjssR87el28X+EqEO533mgYjPmW2/hii30WVFOUE5hMdvKeQLBvy5N3/OCch1np0RQBd8eYEtaPv38rc5L2wAnUq9G5Zzr252z7vnwSLi6lap9jWU8tOerSTEPU+jG05PnuCnufVDXVNPyiPsH6BDP4qxHmLjooNpxfe63Df7NNyUi2I3QoroLj/UzI7zZVQjJEqsTrr5BbsH4z6NTGY91+ZqobBn62+hV3ESAam0ivQgC7s2AKko0qkKyIUGjj7ozU8ebo1UpagNvKC/J9szMqtdXJgKtG8BeonyLMeN6MEEyEvcMJbB4dCcfet+1Sb9AZWnGvYVdajhVLb1HE6OrbzZyhC3KqCe06J9O5BCY9ncy+l16i7MyIfcKTibHQlxPU+Id/VijD97JSRXxnd2i4= diff --git a/test/start_agent.sh b/test/start_agent.sh new file mode 100644 index 00000000..09cf19d5 --- /dev/null +++ b/test/start_agent.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +set -euxo pipefail + +# Start an agent using the testing account API key to send some data +docker run -d -it --rm --name sysdig-agent --privileged --net host --pid host -e COLLECTOR=collector-staging.sysdigcloud.com -e ACCESS_KEY=$PYTHON_SDC_TEST_ACCESS_KEY -v /var/run/docker.sock:/host/var/run/docker.sock -v /dev:/host/dev -v /proc:/host/proc:ro -v /boot:/host/boot:ro -v /lib/modules:/host/lib/modules:ro -v /usr:/host/usr:ro --shm-size=350m sysdig/agent + +# make sure the agent starts sending data and the backend makes it available via API +sleep 60 diff --git a/test/stop_agent.sh b/test/stop_agent.sh new file mode 100644 index 00000000..359435fe --- /dev/null +++ b/test/stop_agent.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +set -euxo pipefail + +docker logs sysdig-agent +docker stop sysdig-agent diff --git a/test/test_monitor_apis.sh b/test/test_monitor_apis.sh index b42b7d89..29257c9c 100644 --- a/test/test_monitor_apis.sh +++ b/test/test_monitor_apis.sh @@ -7,12 +7,6 @@ SCRIPTDIR=$(dirname $SCRIPT) export SDC_URL=https://app-staging.sysdigcloud.com -# Start an agent using the testing account API key to send some data -docker run -d -it --rm --name sysdig-agent --privileged --net host --pid host -e COLLECTOR=collector-staging.sysdigcloud.com -e ACCESS_KEY=$PYTHON_SDC_TEST_ACCESS_KEY -v /var/run/docker.sock:/host/var/run/docker.sock -v /dev:/host/dev -v /proc:/host/proc:ro -v /boot:/host/boot:ro -v /lib/modules:/host/lib/modules:ro -v /usr:/host/usr:ro --shm-size=350m sysdig/agent - -# make sure the agent starts sending data and the backend makes it available via API -sleep 60 - AGENT_HOSTNAME=$(hostname -s) SESSION_UUID=$(head -c 32 /dev/urandom | tr -dc 'a-zA-Z0-9') ALERT_NAME=python-test-alert-$SESSION_UUID @@ -51,6 +45,3 @@ date; $SCRIPTDIR/../examples/create_sysdig_capture.py $PYTHON_SDC_TEST_MONITOR_A date; $SCRIPTDIR/../examples/notification_channels.py -c $CHANNEL_NAME $PYTHON_SDC_TEST_MONITOR_API_TOKEN date; $SCRIPTDIR/../examples/user_team_mgmt.py $PYTHON_SDC_TEST_MONITOR_API_TOKEN $TEAM_NAME example-user@example-domain.com date; $SCRIPTDIR/../examples/user_team_mgmt_extended.py $PYTHON_SDC_TEST_MONITOR_API_TOKEN $TEAM_NAME example-user@example-domain.com - -docker stop sysdig-agent - diff --git a/test/test_secure_apis.sh b/test/test_secure_apis.sh index 4408b5c3..d32cfefd 100644 --- a/test/test_secure_apis.sh +++ b/test/test_secure_apis.sh @@ -131,12 +131,7 @@ fi echo $OUT -# Start an agent using the testing account API key and trigger an event -docker run -d -it --rm --name sysdig-agent --privileged --net host --pid host -e COLLECTOR=collector-staging.sysdigcloud.com -e ACCESS_KEY=$PYTHON_SDC_TEST_ACCESS_KEY -v /var/run/docker.sock:/host/var/run/docker.sock -v /dev:/host/dev -v /proc:/host/proc:ro -v /boot:/host/boot:ro -v /lib/modules:/host/lib/modules:ro -v /usr:/host/usr:ro --shm-size=350m sysdig/agent - -# make sure the agent starts sending data and the backend makes it available via API -sleep 60 - +# Trigger an event sudo touch /bin/some-file.txt # make sure the agent sends the policy event and the backend makes it available via API From 33be4a4dba7d2a44101f3481827cf70ada74053b Mon Sep 17 00:00:00 2001 From: davideschiera Date: Wed, 20 Feb 2019 15:37:50 -0800 Subject: [PATCH 3/6] Remove stop --- test/test_secure_apis.sh | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/test_secure_apis.sh b/test/test_secure_apis.sh index d32cfefd..e80de92a 100644 --- a/test/test_secure_apis.sh +++ b/test/test_secure_apis.sh @@ -143,9 +143,6 @@ if [[ "$EVTS" != "" ]]; then FOUND=1 fi -docker logs sysdig-agent -docker stop sysdig-agent - if [[ $FOUND == 0 ]]; then echo "Did not find any policy events after 60 seconds of wait" exit 1 From da5687251204e52a14a059b6dec79da24e30d788 Mon Sep 17 00:00:00 2001 From: davideschiera Date: Wed, 20 Feb 2019 15:57:08 -0800 Subject: [PATCH 4/6] Restore loop hoping to fix the test --- test/test_secure_apis.sh | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/test/test_secure_apis.sh b/test/test_secure_apis.sh index e80de92a..e09d653e 100644 --- a/test/test_secure_apis.sh +++ b/test/test_secure_apis.sh @@ -131,17 +131,18 @@ fi echo $OUT -# Trigger an event -sudo touch /bin/some-file.txt - -# make sure the agent sends the policy event and the backend makes it available via API -sleep 60 - -EVTS=`$SCRIPTDIR/../examples/get_secure_policy_events.py $PYTHON_SDC_TEST_API_TOKEN 90` - -if [[ "$EVTS" != "" ]]; then - FOUND=1 -fi +# Trigger some events +FOUND=0 + +for i in $(seq 10); do + sleep 10 + sudo touch /bin/some-file.txt + EVTS=`$SCRIPTDIR/../examples/get_secure_policy_events.py $PYTHON_SDC_TEST_API_TOKEN 60` + if [[ "$EVTS" != "" ]]; then + FOUND=1 + break; + fi +done if [[ $FOUND == 0 ]]; then echo "Did not find any policy events after 60 seconds of wait" From 8583474054feccd3ec05532769602b0efd92d89a Mon Sep 17 00:00:00 2001 From: davideschiera Date: Wed, 20 Feb 2019 15:57:49 -0800 Subject: [PATCH 5/6] Revert few more changes --- test/test_secure_apis.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/test_secure_apis.sh b/test/test_secure_apis.sh index e09d653e..1911a536 100644 --- a/test/test_secure_apis.sh +++ b/test/test_secure_apis.sh @@ -137,7 +137,9 @@ FOUND=0 for i in $(seq 10); do sleep 10 sudo touch /bin/some-file.txt + EVTS=`$SCRIPTDIR/../examples/get_secure_policy_events.py $PYTHON_SDC_TEST_API_TOKEN 60` + if [[ "$EVTS" != "" ]]; then FOUND=1 break; @@ -145,6 +147,6 @@ for i in $(seq 10); do done if [[ $FOUND == 0 ]]; then - echo "Did not find any policy events after 60 seconds of wait" + echo "Did not find any policy events after 10 attempts..." exit 1 fi From d67dfa52b2bff65a097bec4014be32b342740098 Mon Sep 17 00:00:00 2001 From: davideschiera Date: Wed, 20 Feb 2019 16:04:28 -0800 Subject: [PATCH 6/6] Touch first, sleep then --- test/test_secure_apis.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_secure_apis.sh b/test/test_secure_apis.sh index 1911a536..ba97d491 100644 --- a/test/test_secure_apis.sh +++ b/test/test_secure_apis.sh @@ -135,11 +135,11 @@ echo $OUT FOUND=0 for i in $(seq 10); do - sleep 10 sudo touch /bin/some-file.txt + sleep 10 EVTS=`$SCRIPTDIR/../examples/get_secure_policy_events.py $PYTHON_SDC_TEST_API_TOKEN 60` - + if [[ "$EVTS" != "" ]]; then FOUND=1 break;