Skip to content

Commit

Permalink
Add integration tests (#274)
Browse files Browse the repository at this point in the history
* Boiler plate

* Make the tests run

* Truncate output file/dir before the export

* Make export transactions test run

* Update the golden files

* prettier

* omit long running tests

* Make all the tests work

* prettier

* update

* Make all the test run

* Add workflow to run integration tests

* revert me

* Fix docker compose in workflow

* use secret

add secret as env variable in github workflow

remove env from docker compose

* pass env directly in docker compose run

mount file and pass env

remove extra env

temp

cat temp

empty

printf

rearrange

validate json

workaround

cleanup

verbose

* Fail the test intentionally

* Revert "Fail the test intentionally"

This reverts commit 1d39d47.

* find test coverage

* test overrides

* Revert "test overrides"

This reverts commit b797bdf.

* add coverage report and readme

Fix coverage report

coverage fix

* lower our test coverage standard

* Remove duplicate functions

* Add make command for lint

* Cancel the previous runs if new commit is pushed

* rename the job

* Update cmd/export_ledger_entry_changes_test.go

* Update cmd/export_ledgers_test.go

* Rename contract event golden file
  • Loading branch information
amishas157 authored Aug 9, 2024
1 parent ffb4b4a commit c7991a5
Show file tree
Hide file tree
Showing 46 changed files with 163,611 additions and 3,216 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ on:
schedule:
- cron: "42 15 * * 6"

concurrency:
group: ${{ github.workflow }}-${{ github.ref_protected == 'true' && github.sha || github.ref }}-{{ github.event_name }}
cancel-in-progress: true

jobs:
analyze:
name: Analyze (${{ matrix.language }})
Expand Down
67 changes: 67 additions & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Integration Tests

on:
push:
branches:
- master
pull_request:
branches:
- master

concurrency:
group: ${{ github.workflow }}-${{ github.ref_protected == 'true' && github.sha || github.ref }}-{{ github.event_name }}
cancel-in-progress: true

jobs:
integration-tests:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Docker Compose
run: |
sudo apt-get update
sudo apt-get install -y docker-compose
- name: Build Docker Image
run: |
docker-compose build
- name: Build and start services
run: |
docker-compose up -d
- name: Create GCP Credentials File
run: |
echo "$CREDS_TEST_HUBBLE" > ${{ runner.workspace }}/gcp-key.json
shell: bash
env:
CREDS_TEST_HUBBLE: ${{secrets.CREDS_TEST_HUBBLE}}

- name: Run tests
run: |
docker-compose run -v ${{ runner.workspace }}/gcp-key.json:/usr/credential.json:ro \
-v ${{ runner.workspace }}/coverage/:/usr/coverage/ \
-e GOOGLE_APPLICATION_CREDENTIALS=/usr/credential.json \
integration-tests \
go test -v -coverprofile=/usr/coverage/coverage.out ./cmd ./internal/transform -timeout 30m
- name: Generate Coverage Report
run: |
go tool cover -func=${{ runner.workspace }}/coverage/coverage.out
- name: Check Coverage
id: coverage
run: |
COVERAGE=$(go tool cover -func=${{ runner.workspace }}/coverage/coverage.out | grep total: | awk '{print $3}' | sed 's/%//')
echo "Coverage: $COVERAGE%"
if (( $(echo "$COVERAGE < 55" | bc -l) )); then
echo "Coverage is below the 55% threshold."
exit 1
fi
- name: Stop and remove containers
run: |
docker-compose down
4 changes: 4 additions & 0 deletions .github/workflows/internal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ on:
pull_request:
branches: [master]

concurrency:
group: ${{ github.workflow }}-${{ github.ref_protected == 'true' && github.sha || github.ref }}-{{ github.event_name }}
cancel-in-progress: true

jobs:
build:
runs-on: ubuntu-latest
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/lint-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ on:
branches:
- master

concurrency:
group: ${{ github.workflow }}-${{ github.ref_protected == 'true' && github.sha || github.ref }}-{{ github.event_name }}
cancel-in-progress: true

jobs:
pre-commit:
runs-on: ubuntu-latest
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ on:
pull_request:
types: [closed]

concurrency:
group: ${{ github.workflow }}-${{ github.ref_protected == 'true' && github.sha || github.ref }}-{{ github.event_name }}
cancel-in-progress: true

permissions:
contents: read

Expand Down
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,24 @@ docker-build:
docker-push:
$(SUDO) docker push $(ETLHASH)
$(SUDO) docker push stellar/stellar-etl:latest

int-test:
docker-compose build
docker-compose run \
-v $(HOME)/.config/gcloud/application_default_credentials.json:/usr/credential.json:ro \
-v $(PWD)/testdata:/usr/src/etl/testdata \
-e GOOGLE_APPLICATION_CREDENTIALS=/usr/credential.json \
integration-tests \
go test -v ./cmd -timeout 30m

int-test-update:
docker-compose build
docker-compose run \
-v $(HOME)/.config/gcloud/application_default_credentials.json:/usr/credential.json:ro \
-v $(PWD)/testdata:/usr/src/etl/testdata \
-e GOOGLE_APPLICATION_CREDENTIALS=/usr/credential.json \
integration-tests \
go test -v ./cmd -timeout 30m -args -update=true

lint:
pre-commit run --show-diff-on-failure --color=always --all-files
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,46 @@ Add following to docker run command to pass gcloud credentials to docker contain
<br>

## **Running Tests**

### Unit tests

```sh
# Running all unit tests
go test -v -cover ./internal/transform

# Running an individual test
go test -v -run ^TestTransformAsset$ ./internal/transform
```

### Integration tests

```sh
# Running all integration tests
make int-test

# Running all integration tests and update golden files
make int-test-update

# Above essentially runs following:
docker-compose build
docker-compose run \
-v $(HOME)/.config/gcloud/application_default_credentials.json:/usr/credential.json:ro \
-v $(PWD)/testdata:/usr/src/etl/testdata \
-e GOOGLE_APPLICATION_CREDENTIALS=/usr/credential.json \
integration-tests \
go test -v ./cmd -timeout 30m -args -update=true

# Running an individual test
docker-compose build
docker-compose run \
-v $(HOME)/.config/gcloud/application_default_credentials.json:/usr/credential.json:ro \
-v $(PWD)/testdata:/usr/src/etl/testdata \
-e GOOGLE_APPLICATION_CREDENTIALS=/usr/credential.json \
integration-tests \
go test -v -run ^TestExportAssets$ ./cmd -timeout 30m -args -update=true
```

---

# **Command Reference**
Expand Down
5 changes: 3 additions & 2 deletions cmd/command_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,14 @@ func exportParquetFilename(start, end uint32, dataType string) string {
return fmt.Sprintf("%d-%d-%s.parquet", start, end-1, dataType)
}

func deleteLocalFiles(path string) {
func deleteLocalFiles(path string) error {
err := os.RemoveAll(path)
if err != nil {
cmdLogger.Errorf("Unable to remove %s: %s", path, err)
return
return err
}
cmdLogger.Infof("Successfully deleted %s", path)
return nil
}

func maybeUpload(cloudCredentials, cloudStorageBucket, cloudProvider, path string) {
Expand Down
20 changes: 20 additions & 0 deletions cmd/export_contract_events_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"testing"
)

func TestExportContractEvents(t *testing.T) {
tests := []cliTest{
{
name: "contract events from multiple ledger",
args: []string{"export_contract_events", "-s", "52271338", "-e", "52271350", "-o", gotTestDir(t, "large_range_ledger_txs.txt")},
golden: "large_range_ledger_txs.golden",
wantErr: nil,
},
}

for _, test := range tests {
runCLITest(t, test, "testdata/contract_events/")
}
}
22 changes: 0 additions & 22 deletions cmd/export_diagnostic_events_test.go

This file was deleted.

74 changes: 66 additions & 8 deletions cmd/export_ledger_entry_changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,74 @@ func TestExportChanges(t *testing.T) {
wantErr: fmt.Errorf("batch-size (0) must be greater than 0"),
},
{
name: "changes from single ledger",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "140116", "-e", "140116", "-o", gotTestDir(t, "single/")},
golden: "single_ledger.golden",
wantErr: nil,
name: "All changes from ledger entry",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "49265302", "-e", "49265350", "-o", gotTestDir(t, "all/")},
golden: "all.golden",
wantErr: nil,
sortForComparison: true,
},
{
name: "changes from large range",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "100000", "-e", "164000", "-o", gotTestDir(t, "large_range_changes/")},
golden: "large_range_changes.golden",
wantErr: nil,
name: "Account changes from ledger entry",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "49265302", "-e", "49265400", "-o", gotTestDir(t, "accounts/"), "--export-accounts", "true"},
golden: "accounts.golden",
wantErr: nil,
sortForComparison: true,
},
{
name: "Claimable balance from ledger entry",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "49265302", "-e", "49265400", "-o", gotTestDir(t, "claimable_balances/"), "--export-balances", "true"},
golden: "claimable_balances.golden",
wantErr: nil,
sortForComparison: true,
},
{
name: "trustlines from ledger entry",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "49265302", "-e", "49265400", "-o", gotTestDir(t, "trustlines/"), "--export-trustlines", "true"},
golden: "trustlines.golden",
wantErr: nil,
sortForComparison: true,
},
{
name: "Offers from ledger entry",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "49265302", "-e", "49265400", "-o", gotTestDir(t, "offers/"), "--export-offers", "true"},
golden: "offers.golden",
wantErr: nil,
sortForComparison: true,
},
{
name: "Pools from ledger entry",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "49265302", "-e", "49265400", "-o", gotTestDir(t, "pools/"), "--export-pools", "true"},
golden: "pools.golden",
wantErr: nil,
sortForComparison: true,
},
{
name: "Contract code from ledger entry",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "50666990", "-e", "50666999", "-o", gotTestDir(t, "contract_code/"), "--export-contract-code", "true"},
golden: "contract_code.golden",
wantErr: nil,
sortForComparison: true,
},
{
name: "Contract data from ledger entry",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "51340657", "-e", "51340757", "-o", gotTestDir(t, "contract_data/"), "--export-contract-data", "true"},
golden: "contract_data.golden",
wantErr: nil,
sortForComparison: true,
},
{
name: "Config setting from ledger entry",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "50457424", "-e", "50457440", "-o", gotTestDir(t, "config_setting/"), "--export-config-settings", "true"},
golden: "config_setting.golden",
wantErr: nil,
sortForComparison: true,
},
{
name: "ttl from ledger entry",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "50603521", "-e", "50603621", "-o", gotTestDir(t, "ttl/"), "--export-ttl", "true"},
golden: "ttl.golden",
wantErr: nil,
sortForComparison: true,
},
}

Expand Down
20 changes: 20 additions & 0 deletions cmd/export_ledger_transaction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"testing"
)

func TestExportLedgerTransaction(t *testing.T) {
tests := []cliTest{
{
name: "Transactions from one ledger",
args: []string{"export_ledger_transaction", "-s", "30820015", "-e", "30820015", "-o", gotTestDir(t, "ledger_transactions.txt")},
golden: "ledger_transactions.golden",
wantErr: nil,
},
}

for _, test := range tests {
runCLITest(t, test, "testdata/ledger_transactions/")
}
}
Loading

0 comments on commit c7991a5

Please sign in to comment.