From d22101e06cf9fb94d33261b1ed75fe7e6170efc0 Mon Sep 17 00:00:00 2001 From: "Dat. Ba Dao" Date: Wed, 26 Jun 2024 14:22:24 +0700 Subject: [PATCH] src/commands: support "schema test" (#20) --- .circleci/test-deploy.yml | 122 +++++++++++++++++++++++++++++--- src/commands/schema_test.yml | 53 ++++++++++++++ src/scripts/schema-test-fail.sh | 12 ++++ testdata/schema.hcl | 8 +++ testdata/schema.test.hcl | 13 ++++ 5 files changed, 200 insertions(+), 8 deletions(-) create mode 100644 src/commands/schema_test.yml create mode 100644 src/scripts/schema-test-fail.sh create mode 100644 testdata/schema.hcl create mode 100644 testdata/schema.test.hcl diff --git a/.circleci/test-deploy.yml b/.circleci/test-deploy.yml index 1c382b0..5c0422e 100644 --- a/.circleci/test-deploy.yml +++ b/.circleci/test-deploy.yml @@ -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 @@ -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 @@ -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: @@ -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 @@ -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: @@ -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: @@ -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: @@ -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 @@ -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 diff --git a/src/commands/schema_test.yml b/src/commands/schema_test.yml new file mode 100644 index 0000000..6f157ec --- /dev/null +++ b/src/commands/schema_test.yml @@ -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: <> + command: atlasaction --action schema/test + environment: + INPUT_DEV_URL: <> + INPUT_RUN: <> + INPUT_URL: <> + INPUT_CONFIG: <> + INPUT_ENV: <> + INPUT_VARS: <> diff --git a/src/scripts/schema-test-fail.sh b/src/scripts/schema-test-fail.sh new file mode 100644 index 0000000..f0bc4b4 --- /dev/null +++ b/src/scripts/schema-test-fail.sh @@ -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 \ No newline at end of file diff --git a/testdata/schema.hcl b/testdata/schema.hcl new file mode 100644 index 0000000..0c3617b --- /dev/null +++ b/testdata/schema.hcl @@ -0,0 +1,8 @@ +table "t1" { + schema = schema.public + column "c1" { + type = int + } +} + +schema "public" {} \ No newline at end of file diff --git a/testdata/schema.test.hcl b/testdata/schema.test.hcl new file mode 100644 index 0000000..f98b093 --- /dev/null +++ b/testdata/schema.test.hcl @@ -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" + } +} \ No newline at end of file