Testing¶
Overview¶
The gafkalo test suite includes unit tests for core functionality and validation logic.
Test Structure¶
Unit Tests¶
Located in various *_test.go files:
auth_test.go- Authentication and security tests (mTLS, SCRAM, TLS)cli_test.go- CLI argument parsing testsconfig_test.go- Configuration parsing testslint_topic_test.go- Topic linting testsreport_test.go- Reporting functionality testsresult_test.go- Result handling teststopics_test.go- Topic creation validation tests
Running Tests¶
All Tests¶
make test
# or
go test -v -cover
Specific Test¶
go test -v -run TestFunctionName
Topic Tests¶
# Run all topic-related tests
go test -v -run TestTopic
# Run specific test
go test -v -run TestTopicCreateValidation
Coverage Report¶
make coverage
This generates an HTML coverage report.
Test Coverage¶
Topic Creation Tests¶
TestTopicCreateValidation
Purpose: Tests parameter validation for topic creation
Verifies: - Valid partitions and replication factor pass validation - Zero partitions are rejected - Zero replication factor is rejected - Negative partitions are rejected
Duration: < 1 second
Type: Unit test (no Kafka required)
TestTopicListItemSorting
Purpose: Tests topic list sorting behavior
Verifies: Topics can be properly structured and sorted
Duration: < 1 second
Type: Unit test (no Kafka required)
Testing Best Practices¶
Writing New Tests¶
Use table-driven tests for parameter validation
Use meaningful test names that describe what is being tested
Add assertions for all critical behaviors
Keep tests isolated (no shared state)
Prefer unit tests over integration tests for speed
Test Organization¶
func TestFeatureName(t *testing.T) {
// Arrange: Setup test data
input := setupTestData()
// Act: Perform the action
result := functionUnderTest(input)
// Assert: Verify results
assert.Equal(t, expected, result)
}
Table-Driven Tests¶
func TestValidation(t *testing.T) {
tests := []struct {
name string
input string
expectedError bool
}{
{"valid input", "valid", false},
{"invalid input", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validate(tt.input)
if tt.expectedError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
Integration Testing¶
For end-to-end testing with real Kafka clusters:
Using Docker Compose¶
Test environments are available in testdata/compose/:
# Start Kafka cluster
cd testdata/compose/allinone
docker-compose up -d
# Run CLI commands
./gafkalo topic create -n test --config test-config.yaml
# Verify
./gafkalo topic list --config test-config.yaml
Manual Testing¶
Start a local Kafka cluster (via Docker, Confluent Platform, etc.)
Create a configuration file pointing to your cluster
Run CLI commands to test functionality
Verify results using describe/list commands
CI/CD Integration¶
GitHub Actions¶
The project uses GitHub Actions for CI. See .github/workflows/go.yml.
Workflow includes:
Go version: 1.24+
Test execution:
make testLint check:
make staticcheckBuild verification:
make build
Local Development¶
Prerequisites:
Go 1.24 or higher
Make (for Makefile targets)
Running tests locally:
# Install dependencies
go mod download
# Run all tests
make test
# Run with coverage
make coverage
# Run linter
make staticcheck
# Format check
gofmt -l .
Troubleshooting¶
Test Failures¶
If tests fail:
Check Go version:
go version(requires 1.24+)Update dependencies:
go mod downloadCheck for compilation errors:
go buildRun specific failing test:
go test -v -run TestNameCheck test output for specific error messages
Build Issues¶
# Clean and rebuild
go clean
go build
# Check for module issues
go mod tidy
go mod verify
Coverage Goals¶
Unit test coverage: Target > 80%
Critical paths: 100% coverage
Edge cases: Comprehensive coverage
Error handling: All error paths tested
Future Enhancements¶
Potential test improvements:
Add benchmark tests for performance-critical operations
Expand table-driven tests for more parameter combinations
Add integration test suite with testcontainers (when networking issues resolved)
Add performance regression tests
Add property-based testing for complex logic