From 7da23a3a39af2084afe413e5095fb41eee4ed9dc Mon Sep 17 00:00:00 2001 From: Darren Murray Date: Wed, 22 Nov 2023 16:21:47 +0000 Subject: [PATCH] test: add unit tests for config Signed-off-by: Darren Murray --- .github/workflows/config_test.yml | 13 +++++++++++ setup_lacework_agent.sh | 13 ++++++++++- test_config.bats | 39 +++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/config_test.yml mode change 100644 => 100755 setup_lacework_agent.sh create mode 100644 test_config.bats diff --git a/.github/workflows/config_test.yml b/.github/workflows/config_test.yml new file mode 100644 index 0000000..78de846 --- /dev/null +++ b/.github/workflows/config_test.yml @@ -0,0 +1,13 @@ +name: "Config Test" +on: [pull_request] +jobs: + check-bats-version: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v3 + with: + node-version: '14' + - run: npm install -g bats + - run: npm install -g jsonlint + - run: bats test_config.bats diff --git a/setup_lacework_agent.sh b/setup_lacework_agent.sh old mode 100644 new mode 100755 index a8c743b..6602180 --- a/setup_lacework_agent.sh +++ b/setup_lacework_agent.sh @@ -50,6 +50,13 @@ notify_use_docker() { } render_agent_config() { + if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then + LACEWORK_INSTALL_PATH=$1 + ADDITIONAL_CONFIG=$2 + SERVER_URL=$3 + TAGS=$4 + fi + local _config_json local _token_json local _server_url_json @@ -84,6 +91,7 @@ render_agent_config() { ${_tags_json} }""" + echo "Updating the Lacework agent config.json file..." if [ ! -d "$LACEWORK_INSTALL_PATH/config" ]; then mkdir "$LACEWORK_INSTALL_PATH/config" @@ -213,4 +221,7 @@ verify_valid_token() { fi } -main +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + main +fi + diff --git a/test_config.bats b/test_config.bats new file mode 100644 index 0000000..007b5d7 --- /dev/null +++ b/test_config.bats @@ -0,0 +1,39 @@ +#!/usr/bin/env bats + +@test "Validate json with no args" { + local path=$(pwd) + result=$(bash -c "source setup_lacework_agent.sh && render_agent_config $path") + + local config="${path}/config/config.json" + validateJson=$(jsonlint $config) + echo $validateJson + + status=$? + [ $status -eq 0 ] +} + +@test "Validate json with additonal config" { + local path=$(pwd) + local addCfg='"{\"codeaware\": {\"enable\":\"all\" }}"' + result=$(bash -c "source setup_lacework_agent.sh && render_agent_config $path $addCfg") + local config="${path}/config/config.json" + validateJson=$(jsonlint $config) + echo $validateJson + + status=$? + [ $status -eq 0 ] +} + +@test "Validate json with all config" { + local path=$(pwd) + local addCfg='"{\"codeaware\": {\"enable\":\"all\" }}"' + local serverUrl="https://agent.lacework.net" + local tags='"{\"env\": \"dev\"}"' + result=$(bash -c "source setup_lacework_agent.sh && render_agent_config $path $addCfg $serverUrl, $tags") + local config="${path}/config/config.json" + validateJson=$(jsonlint $config) + echo $validateJson + + status=$? + [ $status -eq 0 ] +} \ No newline at end of file