Skip to content

Commit

Permalink
src/commands: support "schema test" (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
datdao authored Jun 26, 2024
1 parent aa2ef73 commit d22101e
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 8 deletions.
122 changes: 114 additions & 8 deletions .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,41 @@ jobs:
grep -qe "INPUT_CONFIG=file://atlas.hcl" /tmp/env.out
# check vars should be set in the environment
grep -qe "INPUT_VARS=foo=bar baz=qux quux=corge" /tmp/env.out
integration-test:
command-test-schema-test:
executor: atlas-orb/default
steps:
- checkout
- run:
name: Mock atlas with echo.sh
command: |
sudo cp ./src/scripts/echo.sh /bin/atlas
sudo cp ./src/scripts/echo.sh /bin/atlasaction
- atlas-orb/schema_test:
working_directory: testdata
url: "file://schema.hcl"
run: "example"
dev_url: postgres://postgres:pass@localhost:5432/test?sslmode=disable
env: "circleci-test"
config: "file://atlas.hcl"
vars: |
foo=bar baz=qux quux=corge
- run:
name: Check echo.out for expected output
command: |
grep -qe "--action schema/test" /tmp/echo.out
# check url should be set in the environment
grep -qe "INPUT_URL=file://schema.hcl" /tmp/env.out
# check dev_url should be set in the environment
grep -qe "INPUT_DEV_URL=postgres://postgres:pass@localhost:5432/test?sslmode=disable" /tmp/env.out
# check env should be set in the environment
grep -qe "INPUT_ENV=circleci-test" /tmp/env.out
# check run should be set in the environment
grep -qe "INPUT_RUN=example" /tmp/env.out
# check config should be set in the environment
grep -qe "INPUT_CONFIG=file://atlas.hcl" /tmp/env.out
# check vars should be set in the environment
grep -qe "INPUT_VARS=foo=bar baz=qux quux=corge" /tmp/env.out
integration-test-versioned:
docker:
- image: cimg/base:current
- image: cimg/postgres:16.2
Expand Down Expand Up @@ -264,7 +298,32 @@ jobs:
dir: "file://migrations"
dev_url: postgres://postgres:pass@localhost:5432/dev?sslmode=disable
amount: 1
integration-test-gh:
integration-test-declarative:
docker:
- image: cimg/base:current
- image: cimg/postgres:16.2
environment:
POSTGRES_USER: postgres
POSTGRES_DB: test
POSTGRES_PASSWORD: pass
steps:
- checkout
- run:
name: Wait for Postgres
command: dockerize -wait tcp://localhost:5432 -timeout 60s
- run:
name: Create "dev" database
command: |
psql postgres://postgres:pass@localhost:5432 -c "CREATE DATABASE dev;"
- atlas-orb/setup:
version: "latest"
cloud_token_env: "ATLAS_TOKEN"
- atlas-orb/schema_test:
working_directory: testdata
run: "expected_success"
url: "file://schema.hcl"
dev_url: postgres://postgres:pass@localhost:5432/test?search_path=public&sslmode=disable
integration-test-versioned-gh:
docker:
- image: cimg/base:current
- image: cimg/postgres:16.2
Expand Down Expand Up @@ -309,7 +368,7 @@ jobs:
dir: "file://migrations"
dev_url: postgres://postgres:pass@localhost:5432/dev?sslmode=disable
amount: 1
integration-test-gh-lint:
integration-test-versioned-gh-lint:
docker:
- image: cimg/base:current
environment:
Expand Down Expand Up @@ -339,7 +398,7 @@ jobs:
dir_name: test-dir-sqlite
dir: "file://sqlite-wrong-sum"
dev_url: "sqlite://file.db?mode=memory"
integration-test-gh-test:
integration-test-versioned-gh-test:
docker:
- image: cimg/base:current
- image: cimg/postgres:16.2
Expand Down Expand Up @@ -371,6 +430,38 @@ jobs:
run: "expected_failure"
dir: atlas://my-cool-project
dev_url: postgres://postgres:pass@localhost:5432/test?sslmode=disable
integration-test-declarative-gh-test:
docker:
- image: cimg/base:current
- image: cimg/postgres:16.2
environment:
POSTGRES_USER: postgres
POSTGRES_DB: test
POSTGRES_PASSWORD: pass
environment:
GITHUB_REPOSITORY: "ariga/atlas-orb"
steps:
- checkout
- run:
name: Wait for Postgres
command: dockerize -wait tcp://localhost:5432 -timeout 60s
- run:
name: Create "dev" database
command: |
psql postgres://postgres:pass@localhost:5432 -c "CREATE DATABASE dev;"
- atlas-orb/setup:
version: "latest"
cloud_token_env: "ATLAS_TOKEN"
- run:
name: Run atlasaction with schema-test-fail.sh
command: |
sudo cp /bin/atlasaction /bin/atlasaction-origin
sudo cp ./src/scripts/schema-test-fail.sh /bin/atlasaction
- atlas-orb/schema_test:
working_directory: testdata
run: "expected_failure"
url: file://schema.hcl
dev_url: postgres://postgres:pass@localhost:5432/test?sslmode=disable
workflows:
test-deploy:
jobs:
Expand All @@ -387,7 +478,9 @@ workflows:
filters: *filters
- command-test-migrate-down:
filters: *filters
- integration-test:
- command-test-schema-test:
filters: *filters
- integration-test-versioned:
context: ariga-atlas
filters: *filters
requires:
Expand All @@ -397,7 +490,7 @@ workflows:
- command-test-migrate-lint
- command-test-migrate-apply
- command-test-migrate-down
- integration-test-gh:
- integration-test-versioned-gh:
context: ariga-atlas-gh
filters: *filters
requires:
Expand All @@ -407,18 +500,30 @@ workflows:
- command-test-migrate-lint
- command-test-migrate-apply
- command-test-migrate-down
- integration-test-gh-lint:
- integration-test-versioned-gh-lint:
context: ariga-atlas-gh
filters: *filters
requires:
- command-test-setup
- command-test-migrate-lint
- integration-test-gh-test:
- integration-test-versioned-gh-test:
context: ariga-atlas-gh
filters: *filters
requires:
- command-test-setup
- command-test-migrate-test
- integration-test-declarative:
context: ariga-atlas
filters: *filters
requires:
- command-test-setup
- command-test-schema-test
- integration-test-declarative-gh-test:
context: ariga-atlas-gh
filters: *filters
requires:
- command-test-setup
- command-test-schema-test
# The orb must be re-packed for publishing, and saved to the workspace.
- orb-tools/pack:
filters: *release-filters
Expand All @@ -435,5 +540,6 @@ workflows:
- command-test-migrate-lint
- command-test-migrate-apply
- command-test-migrate-down
- command-test-schema-test
context: orb-publisher
filters: *release-filters
53 changes: 53 additions & 0 deletions src/commands/schema_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
description: >
This command test schema on a database.
parameters:
dev_url:
type: string
default: ''
description: |
The URL of the dev-database to use for analysis. For example: mysql://root:pass@localhost:3306/dev.
Read more about [dev-databases](https://atlasgo.io/concepts/dev-database).
url:
type: string
default: ''
description: |
The desired schema URL(s) to test. For Example: `file://schema.hcl`
run:
type: string
default: ''
description: |
Filter tests to run by regexp. For example, `^test_.*` will only run tests that start with `test_`. Default is to run all tests.
config:
type: string
default: ''
description: |
The path to the Atlas configuration file. By default, Atlas will look for a file named `atlas.hcl` in the current directory.
For example, `file://config/atlas.hcl`. Learn more about [Atlas configuration files](https://atlasgo.io/atlas-schema/projects).
vars:
type: string
default: ''
description: |
Extra variables to pass to the Atlas configuration file. For example, `key=value`.
working_directory:
type: string
default: "."
description: |
The working directory to run from. Defaults to project root.
env:
type: string
default: ''
description: |
The environment to use from the Atlas configuration file. For example, `dev`.
steps:
- run:
name: Test schema on a database
working_directory: <<parameters.working_directory>>
command: atlasaction --action schema/test
environment:
INPUT_DEV_URL: <<parameters.dev_url>>
INPUT_RUN: <<parameters.run>>
INPUT_URL: <<parameters.url>>
INPUT_CONFIG: <<parameters.config>>
INPUT_ENV: <<parameters.env>>
INPUT_VARS: <<parameters.vars>>
12 changes: 12 additions & 0 deletions src/scripts/schema-test-fail.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# execute the command and check the exit code is 1
# testing purpose only, use for integration tests
atlasaction-origin --action schema/test
if [ $? -ne 1 ]; then
echo "Expected exit code 1, got $?"
exit 1
else
echo "Exit code 1 as expected"
exit 0
fi
8 changes: 8 additions & 0 deletions testdata/schema.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
table "t1" {
schema = schema.public
column "c1" {
type = int
}
}

schema "public" {}
13 changes: 13 additions & 0 deletions testdata/schema.test.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
test "schema" "expected_success" {
exec {
sql = "select * from t1"
output = ""
}
}

test "schema" "expected_failure" {
exec {
sql = "select * from t1"
output = "0"
}
}

0 comments on commit d22101e

Please sign in to comment.