Skip to content

Commit

Permalink
test: add unit tests for config
Browse files Browse the repository at this point in the history
Signed-off-by: Darren Murray <darren.murray@lacework.net>
  • Loading branch information
dmurray-lacework committed Nov 23, 2023
1 parent defed1b commit 7da23a3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
13 changes: 13 additions & 0 deletions .github/workflows/config_test.yml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 12 additions & 1 deletion setup_lacework_agent.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -213,4 +221,7 @@ verify_valid_token() {
fi
}

main
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main
fi

39 changes: 39 additions & 0 deletions test_config.bats
Original file line number Diff line number Diff line change
@@ -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 ]
}

0 comments on commit 7da23a3

Please sign in to comment.