Automated Testing
Setting Up Test Environment
How to Set Up Your Local Test Environment
-
Prerequisites:
# Install required tools docker compose go 1.21 or higher make
-
Initial setup:
# Clone and build git clone [repository] cd teranode docker compose build
-
Verify setup:
# Run smoke tests make smoketests
How to Configure Test Settings
-
Create local settings:
touch settings_local.conf
-
Add required settings for your specific testing activity in there. Recommended to use the docker.ci.tc1.run context.
Running Tests
How to Run Test Suites
-
Run all tests in a suite:
# Format cd /teranode/test/<suite-name> go test -v -tags test_<suite-code> # Examples # Run TNA tests cd /teranode/test/tna go test -v -tags test_tna # Run TNC tests cd /teranode/test/tnc go test -v -tags test_tnc
-
Run specific test:
# Format go test -v -run "^<TestSuiteName>$/<TestName>$" # Example: Run specific coinbase test go test -v -run "^TestTNC1TestSuite$/TestCoinbaseTXAmount$" -tags test_tnc
-
Run with different options:
# Format / Skip Build go test -v -tags test_<suite-code> -timeout <duration> make smoketests no-build=1 # Kill docker containers after test make smoketests no-build=1 kill-docker=1 # Keep data directory make smoketests no-build=1 kill-docker=1 no-reset=1
How to Debug Failed Tests
-
Enable verbose logging:
# Set log level to DEBUG export LOG_LEVEL=DEBUG
-
Check container logs:
# View logs for specific node docker logs teranode-1 docker logs teranode-2 docker logs teranode-3
-
Inspect test state:
# Check node health curl http://localhost:10090/health curl http://localhost:12090/health curl http://localhost:14090/health
Adding New Tests
How to Add a New Test Case
-
Create test file:
// test/tna/tna_new_test.go package tna import ( "testing" "github.com/bitcoin-sv/teranode/test/setup" ) type TNANewTestSuite struct { setup.BitcoinTestSuite } func (suite *TNANewTestSuite) TestNewFeature() { t := suite.T() framework := suite.Framework // Your test logic here } func TestTNANewTestSuite(t *testing.T) { suite.Run(t, new(TNANewTestSuite)) }
-
Common test patterns:
// Health check pattern blockchainHealth, err := framework.Nodes[1].BlockchainClient.Health(framework.Context) if err != nil { t.Errorf("Failed to get health: %v", err) } if !blockchainHealth.Ok { t.Errorf("Expected health to be OK") } // Node restart pattern err := framework.RestartNodes(settingsMap) if err != nil { t.Errorf("Failed to restart nodes: %v", err) }
How to Add Custom Test Configuration
-
Create override file:
# docker-compose.custom.override.yml services: teranode-1: environment: CUSTOM_SETTING: "value"
-
Use in test:
func (suite *CustomTestSuite) SetupTest() { composeFiles := []string{ "../../docker-compose.e2etest.yml", "../../docker-compose.custom.override.yml", } suite.SetupTestWithCustomComposeAndSettings( suite.DefaultSettingsMap(), composeFiles, ) }
Working with Multiple Nodes
How to Test Node Communication
-
Setup multiple nodes:
// Ensure all nodes are running for _, node := range framework.Nodes { blockchainHealth, err := node.BlockchainClient.Health(framework.Context) if err != nil { t.Errorf("Node health check failed: %v", err) } }
-
Test node interaction:
// Send transaction to node 1 tx, err := framework.Nodes[0].BlockchainClient.SubmitTransaction(...) // Verify propagation to node 2 time.Sleep(2 * time.Second) // Allow propagation verified, err := framework.Nodes[1].BlockchainClient.VerifyTransaction(...)
How to Test Node Failure Scenarios
-
Stop specific node:
err := framework.StopNode("teranode-2") if err != nil { t.Errorf("Failed to stop node: %v", err) }
-
Restart node:
err := framework.StartNode("teranode-2") if err != nil { t.Errorf("Failed to start node: %v", err) }
Cleanup and Maintenance
How to Clean Test Environment
-
Remove test data:
# Clean data directory rm -rf data/test/*
-
Remove containers:
# Remove all test containers docker ps -a -q --filter label=com.docker.compose.project=e2e | xargs docker rm -f
-
Full cleanup:
# Stop framework and clean up framework.StopNodesWithRmVolume() framework.Cancel()
How to Handle Common Issues
-
Port conflicts:
# Check port usage lsof -i :8084 lsof -i :8085 lsof -i :8087
-
Resource cleanup:
# Force remove hanging containers docker rm -f $(docker ps -a -q --filter label=com.docker.compose.project=e2e)
-
Data persistence issues:
# Reset all data sudo rm -rf ../../data/test