Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integration tests #274

Merged
merged 30 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
c391e61
Boiler plate
amishas157 Aug 5, 2024
687ff9d
Make the tests run
amishas157 Aug 6, 2024
8ea6e38
Truncate output file/dir before the export
amishas157 Aug 6, 2024
6b7921f
Make export transactions test run
amishas157 Aug 6, 2024
2b55e5b
Update the golden files
amishas157 Aug 6, 2024
b7c36b3
prettier
amishas157 Aug 6, 2024
b38fae5
omit long running tests
amishas157 Aug 6, 2024
9600e7b
Make all the tests work
amishas157 Aug 6, 2024
3e2df01
prettier
amishas157 Aug 6, 2024
8a956c5
update
amishas157 Aug 7, 2024
4211b9e
Make all the test run
amishas157 Aug 7, 2024
9b95998
Add workflow to run integration tests
amishas157 Aug 7, 2024
b2a3ae6
revert me
amishas157 Aug 7, 2024
00056df
Fix docker compose in workflow
amishas157 Aug 7, 2024
f08b276
use secret
amishas157 Aug 8, 2024
fcffd97
pass env directly in docker compose run
amishas157 Aug 8, 2024
1d39d47
Fail the test intentionally
amishas157 Aug 8, 2024
31fb156
Revert "Fail the test intentionally"
amishas157 Aug 8, 2024
77c0027
find test coverage
amishas157 Aug 8, 2024
b797bdf
test overrides
amishas157 Aug 8, 2024
eb0bfad
Revert "test overrides"
amishas157 Aug 8, 2024
6b4d818
add coverage report and readme
amishas157 Aug 8, 2024
9d1693c
lower our test coverage standard
amishas157 Aug 8, 2024
dae96ac
Remove duplicate functions
amishas157 Aug 8, 2024
dd3d1d4
Add make command for lint
amishas157 Aug 8, 2024
d41df7a
Cancel the previous runs if new commit is pushed
amishas157 Aug 8, 2024
6aac320
rename the job
amishas157 Aug 8, 2024
406f4dc
Update cmd/export_ledger_entry_changes_test.go
amishas157 Aug 9, 2024
b7442e1
Update cmd/export_ledgers_test.go
amishas157 Aug 9, 2024
b33d0d5
Rename contract event golden file
amishas157 Aug 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
docker-compose:
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}}
sydneynotthecity marked this conversation as resolved.
Show resolved Hide resolved

- name: Run tests
sydneynotthecity marked this conversation as resolved.
Show resolved Hide resolved
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: "diagnostic events from one ledger",
args: []string{"export_contract_events", "-s", "52271338", "-e", "52271350", "-o", gotTestDir(t, "one_ledger_txs.txt")},
sydneynotthecity marked this conversation as resolved.
Show resolved Hide resolved
golden: "one_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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we'll run into any issues using different ledger ranges to generate these files?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. There can be issues if code has bug for different kind of input. Since the ledger range can contain only certain variety of data. It may not cover for all test cases.

I had a thought to have custom fixtures along with ledger range test cases. But I think that's already part of our unit tests. so we should be good generally.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good enough start point and if we run into edge cases, we can always either adjust the ledger ranges or add custom fixtures as you suggested

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: "Trustllines from ledger entry",
amishas157 marked this conversation as resolved.
Show resolved Hide resolved
amishas157 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading