Skip to content

Commit

Permalink
add cli tests
Browse files Browse the repository at this point in the history
  • Loading branch information
balanza committed Jan 8, 2025
1 parent 162b64a commit dfaf398
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 14 deletions.
24 changes: 11 additions & 13 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ jobs:
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: goveralls -coverprofile=covprofile -service=github

test-plugins:
test-build:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4
Expand All @@ -102,22 +102,20 @@ jobs:
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: build dummy plugin
run: |
mkdir /tmp/plugins
go build -o /tmp/plugins/dummy ./plugin_examples/dummy.go
- name: build plugins
run: make build-plugin-examples
- name: build trento binary
run: make build
- name: run facts list command
run: |
./trento-agent facts list --plugins-folder /tmp/plugins 2>&1 | grep -q "dummy"
- name: run facts gather command
run: |
./trento-agent facts gather --gatherer dummy --argument 1 --plugins-folder /tmp/plugins 2>&1 | grep -q 'Name: 1'
- name: Setup BATS
uses: mig4/setup-bats@v1
with:
bats-version: 1.11.1
- name: run tests
run: make test-build

build-static-binary:
runs-on: ubuntu-20.04
needs: [static-analysis, test, test-plugins]
needs: [static-analysis, test, test-build]
steps:
- uses: actions/checkout@v4
with:
Expand Down Expand Up @@ -183,7 +181,7 @@ jobs:
repo-token: ${{ secrets.GITHUB_TOKEN }}

obs-commit:
needs: [static-analysis, test, test-plugins]
needs: [static-analysis, test, test-build]
runs-on: ubuntu-20.04
if: github.ref == 'refs/heads/main' || github.event_name == 'release'
container:
Expand Down
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
golang 1.22.5
mockery 2.32.3
golangci-lint 1.59.1
bats 1.11.1
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ LDFLAGS = -X github.com/trento-project/agent/version.Version="$(VERSION)"
LDFLAGS := $(LDFLAGS) -X github.com/trento-project/agent/version.InstallationSource="$(INSTALLATIONSOURCE)"
ARCHS ?= amd64 arm64 ppc64le s390x
DEBUG ?= 0
BUILD_DIR ?= ./build

ifeq ($(DEBUG), 0)
LDFLAGS += -s -w
Expand All @@ -18,7 +19,11 @@ default: clean mod-tidy fmt vet-check test build
.PHONY: build
build: agent
agent:
$(GO_BUILD) -o trento-agent
$(GO_BUILD) -o $(BUILD_DIR)/trento-agent

.PHONY: build-plugin-examples
build-plugin-examples:
$(GO_BUILD) -o $(BUILD_DIR)/plugin_examples/dummy ./plugin_examples/dummy.go

.PHONY: cross-compiled $(ARCHS)
cross-compiled: $(ARCHS)
Expand Down Expand Up @@ -76,3 +81,7 @@ test-short:
.PHONY: test-coverage
test-coverage:
go test -v -p 1 -race -covermode atomic -coverprofile=covprofile ./...

.PHONY: test-build
test-build:
bats -r ./test
21 changes: 21 additions & 0 deletions test/cli/agent.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
setup() {
# Set the test root as the project root
DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )/../.." >/dev/null 2>&1 && pwd )"

# Add the build folder to the PATH
PATH="$DIR/build:$PATH"
}

@test "it should show help" {
run trento-agent --help

[ "$status" -eq 0 ]
echo "$output" | grep -q "Usage:"
}

@test "it should show agent id" {
run trento-agent id

[ "$status" -eq 0 ]
[ -n "$output" ]
}
33 changes: 33 additions & 0 deletions test/cli/plugins.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
setup() {
# Set the test root as the project root
DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )/../.." >/dev/null 2>&1 && pwd )"

# Add the build folder to the PATH
PATH="$DIR/build:$PATH"
}


@test "it should include the dummy plugin into list" {
run trento-agent facts list --plugins-folder ./build/plugin_examples

[ "$status" -eq 0 ]
echo "$output" | grep -q "dummy"
}

@test "it should should execute the dummy plugin" {
run trento-agent facts gather \
--plugins-folder ./build/plugin_examples \
--gatherer dummy --argument 1

[ "$status" -eq 0 ]
echo $output | grep -q "Name: 1"
}

@test "it should execute the dummy plugin with a different argument" {
run trento-agent facts gather \
--plugins-folder ./build/plugin_examples \
--gatherer dummy --argument 2

[ "$status" -eq 0 ]
echo $output | grep -q "Name: 2"
}

0 comments on commit dfaf398

Please sign in to comment.