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

chore: cleanup test mocks #253

Merged
merged 2 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 6 additions & 1 deletion .github/workflows/ci-go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ jobs:
CGO_ENABLED: 0
- name: mock atlas
run: |
cp atlasaction/mock-atlas-down.sh $HOME/atlas
cp atlasaction/mock-atlas.sh $HOME/atlas
echo "$HOME" >> $GITHUB_PATH
- id: pending-approval
continue-on-error: true
Expand All @@ -220,6 +220,7 @@ jobs:
dir: file://atlasaction/testdata/down
dev-url: sqlite://dev?mode=memory
env:
TEST_ARGS: "migrate down --format {{ json . }} --dev-url sqlite://dev?mode=memory --context {\"triggerType\":\"GITHUB_ACTION\",\"triggerVersion\":\"v0.0.0\"} --dir file://atlasaction/testdata/down"
TEST_STDOUT: "{\"URL\":\"PENDING_URL\",\"Status\":\"PENDING_USER\"}"
TEST_EXIT_CODE: 1
- name: check-exit-code
Expand All @@ -236,6 +237,7 @@ jobs:
dev-url: sqlite://dev?mode=memory
wait-timeout: 5s
env:
TEST_ARGS: "migrate down --format {{ json . }} --dev-url sqlite://dev?mode=memory --context {\"triggerType\":\"GITHUB_ACTION\",\"triggerVersion\":\"v0.0.0\"} --dir file://atlasaction/testdata/down"
TEST_STDOUT: "{\"URL\":\"PENDING_URL\",\"Status\":\"PENDING_USER\"}"
TEST_EXIT_CODE: 1
- name: check-exit-code-wait
Expand All @@ -251,6 +253,7 @@ jobs:
dir: file://atlasaction/testdata/down
dev-url: sqlite://dev?mode=memory
env:
TEST_ARGS: "migrate down --format {{ json . }} --dev-url sqlite://dev?mode=memory --context {\"triggerType\":\"GITHUB_ACTION\",\"triggerVersion\":\"v0.0.0\"} --dir file://atlasaction/testdata/down"
TEST_STDOUT: "{\"URL\":\"ABORTED_URL\",\"Status\":\"ABORTED\"}"
TEST_EXIT_CODE: 1
- name: check-exit-code
Expand All @@ -265,13 +268,15 @@ jobs:
dir: file://atlasaction/testdata/down
dev-url: sqlite://dev?mode=memory
env:
TEST_ARGS: "migrate down --format {{ json . }} --dev-url sqlite://dev?mode=memory --context {\"triggerType\":\"GITHUB_ACTION\",\"triggerVersion\":\"v0.0.0\"} --dir file://atlasaction/testdata/down"
TEST_STDOUT: "{\"Planned\":[{}],\"Reverted\":[{}],\"Status\":\"APPROVED\"}"
- id: applied
uses: ./migrate/down
with:
dir: file://atlasaction/testdata/down
dev-url: sqlite://dev?mode=memory
env:
TEST_ARGS: "migrate down --format {{ json . }} --dev-url sqlite://dev?mode=memory --context {\"triggerType\":\"GITHUB_ACTION\",\"triggerVersion\":\"v0.0.0\"} --dir file://atlasaction/testdata/down"
TEST_STDOUT: "{\"Planned\":[{}],\"Reverted\":[{}],\"Status\":\"APPLIED\"}"
- name: verify output
run: |
Expand Down
111 changes: 51 additions & 60 deletions atlasaction/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,13 @@ func TestMigrateDown(t *testing.T) {

t.Run("down approval pending", func(t *testing.T) {
tt := setup(t)
tt.cli = must(atlasexec.NewClient("", "./mock-atlas-down.sh"))
tt.cli = must(atlasexec.NewClient("", "./mock-atlas.sh"))
tt.setupConfigWithLogin(t, "", "")
st := must(json.Marshal(atlasexec.MigrateDown{
URL: "URL",
Status: "PENDING_USER",
}))
t.Setenv("TEST_ARGS", fmt.Sprintf(`migrate down --format {{ json . }} --env test --config %s --dev-url sqlite://dev?mode=memory --context {"triggerType":"GITHUB_ACTION"} --url sqlite://%s --dir file://testdata/down/`, tt.configUrl, tt.db))
t.Setenv("TEST_STDOUT", string(st))
tt.setInput("env", "test")
require.EqualError(t, (&atlasaction.Actions{Action: tt.act, Atlas: tt.cli}).MigrateDown(context.Background()), "plan approval pending, review here: URL")
Expand All @@ -221,12 +222,13 @@ func TestMigrateDown(t *testing.T) {

t.Run("aborted", func(t *testing.T) {
tt := setup(t)
tt.cli = must(atlasexec.NewClient("", "./mock-atlas-down.sh"))
tt.cli = must(atlasexec.NewClient("", "./mock-atlas.sh"))
tt.setupConfigWithLogin(t, "", "")
st := must(json.Marshal(atlasexec.MigrateDown{
URL: "URL",
Status: "ABORTED",
}))
t.Setenv("TEST_ARGS", fmt.Sprintf(`migrate down --format {{ json . }} --env test --config %s --dev-url sqlite://dev?mode=memory --context {"triggerType":"GITHUB_ACTION"} --url sqlite://%s --dir file://testdata/down/`, tt.configUrl, tt.db))
t.Setenv("TEST_STDOUT", string(st))
t.Setenv("TEST_EXIT_CODE", "1")
tt.setInput("env", "test")
Expand Down Expand Up @@ -410,48 +412,55 @@ func TestMigratePushWithCloud(t *testing.T) {
require.ErrorContains(t, err, `tag must be lowercase alphanumeric`)
})
t.Run("tag", func(t *testing.T) {
c, err := atlasexec.NewClient("", "./mock-atlas.sh")
require.NoError(t, err)
tt := newT(t, nil)
tt.cli, _ = atlasexec.NewClient("", "./mock-push.sh")
os.Remove("push-out.txt")
t.Cleanup(func() {
os.Remove("push-out.txt")
})
tt.cli = c
tt.setupConfigWithLogin(t, srv.URL, token)

dir := t.TempDir()
require.NoError(t, c.SetEnv(map[string]string{"TEST_BATCH": dir}))
require.NoError(t, os.MkdirAll(filepath.Join(dir, "1"), 0755))
require.NoError(t, os.WriteFile(filepath.Join(dir, "1", "args"), []byte(fmt.Sprintf(`migrate push --dev-url sqlite://file?mode=memory --dir file://testdata/migrations --context {"path":"file://testdata/migrations","scmType":"GITHUB"} --config %s test-dir`, tt.configUrl)), 0644))
require.NoError(t, os.WriteFile(filepath.Join(dir, "1", "stdout"), []byte("LINK1"), 0644))
require.NoError(t, os.MkdirAll(filepath.Join(dir, "2"), 0755))
require.NoError(t, os.WriteFile(filepath.Join(dir, "2", "args"), []byte(fmt.Sprintf(`migrate push --dev-url sqlite://file?mode=memory --dir file://testdata/migrations --context {"path":"file://testdata/migrations","scmType":"GITHUB"} --config %s test-dir:valid-tag-123`, tt.configUrl)), 0644))
require.NoError(t, os.WriteFile(filepath.Join(dir, "2", "stdout"), []byte("LINK2"), 0644))

tt.setInput("dir", "file://testdata/migrations")
tt.setInput("dir-name", "test-dir")
tt.setInput("dev-url", "sqlite://file?mode=memory")
tt.setInput("tag", "valid-tag-123")
tt.setInput("latest", "true")
err := (&atlasaction.Actions{Action: tt.act, Atlas: tt.cli}).MigratePush(context.Background())
require.NoError(t, err)
out, err := os.ReadFile("push-out.txt")
require.NoError(t, (&atlasaction.Actions{Action: tt.act, Atlas: tt.cli}).MigratePush(context.Background()))

b, err := os.ReadFile(filepath.Join(dir, "counter"))
require.NoError(t, err)
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
require.Len(t, lines, 2)
require.Contains(t, lines[0], "test-dir")
require.NotContains(t, lines[0], "valid-tag-123")
require.Contains(t, lines[1], "test-dir:valid-tag-123")
require.Equal(t, "2", string(b))
})
t.Run("no latest", func(t *testing.T) {
c, err := atlasexec.NewClient("", "./mock-atlas.sh")
require.NoError(t, err)
tt := newT(t, nil)
tt.cli, _ = atlasexec.NewClient("", "./mock-push.sh")
os.Remove("push-out.txt")
t.Cleanup(func() {
os.Remove("push-out.txt")
})
tt.cli = c
tt.setupConfigWithLogin(t, srv.URL, token)

dir := t.TempDir()
require.NoError(t, c.SetEnv(map[string]string{"TEST_BATCH": dir}))
require.NoError(t, os.MkdirAll(filepath.Join(dir, "1"), 0755))
require.NoError(t, os.WriteFile(filepath.Join(dir, "1", "args"), []byte(fmt.Sprintf(`migrate push --dev-url sqlite://file?mode=memory --dir file://testdata/migrations --context {"path":"file://testdata/migrations","scmType":"GITHUB"} --config %s test-dir:valid-tag-123`, tt.configUrl)), 0644))
require.NoError(t, os.WriteFile(filepath.Join(dir, "1", "stdout"), []byte("LINK2"), 0644))

tt.setInput("dir", "file://testdata/migrations")
tt.setInput("dir-name", "test-dir")
tt.setInput("dev-url", "sqlite://file?mode=memory")
tt.setInput("tag", "valid-tag-123")
tt.setInput("latest", "false")
err := (&atlasaction.Actions{Action: tt.act, Atlas: tt.cli}).MigratePush(context.Background())
require.NoError(t, err)
out, err := os.ReadFile("push-out.txt")
require.NoError(t, (&atlasaction.Actions{Action: tt.act, Atlas: tt.cli}).MigratePush(context.Background()))

b, err := os.ReadFile(filepath.Join(dir, "counter"))
require.NoError(t, err)
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
require.Len(t, lines, 1)
require.Contains(t, lines[0], "test-dir:valid-tag-123")
require.Equal(t, "1", string(b))
})
t.Run("config", func(t *testing.T) {
tt := newT(t, nil)
Expand All @@ -476,59 +485,41 @@ func TestMigratePushWithCloud(t *testing.T) {

func TestMigrateTest(t *testing.T) {
t.Run("all inputs", func(t *testing.T) {
c, err := atlasexec.NewClient("", "./mock-atlas.sh")
require.NoError(t, err)
require.NoError(t, c.SetEnv(map[string]string{
"TEST_ARGS": "migrate test --env test --config file://testdata/config/atlas.hcl --dir file://testdata/migrations --dev-url sqlite://file?mode=memory --run example --var var1=value1 --var var2=value2",
"TEST_STDOUT": `No errors found`,
}))
tt := newT(t, nil)
tt.cli, _ = atlasexec.NewClient("", "./mock-atlas-test.sh")
os.Remove("test-out.txt")
t.Cleanup(func() {
os.Remove("test-out.txt")
})
tt.cli = c
tt.setInput("dir", "file://testdata/migrations")
tt.setInput("dev-url", "sqlite://file?mode=memory")
tt.setInput("run", "example")
tt.setInput("config", "file://testdata/config/atlas.hcl")
tt.setInput("env", "test")
tt.setInput("vars", `{"var1": "value1", "var2": "value2"}`)
err := (&atlasaction.Actions{Action: tt.act, Atlas: tt.cli}).MigrateTest(context.Background())
require.NoError(t, err)
out, err := os.ReadFile("test-out.txt")
require.NoError(t, err)
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
require.Len(t, lines, 1)
require.Contains(t, lines[0], "--env test")
require.Contains(t, lines[0], "--run example")
require.Contains(t, lines[0], "--var var1=value1")
require.Contains(t, lines[0], "--var var2=value2")
require.Contains(t, lines[0], "--dir file://testdata/migrations")
require.Contains(t, lines[0], "--dev-url sqlite://file?mode=memory")
require.NoError(t, (&atlasaction.Actions{Action: tt.act, Atlas: tt.cli}).MigrateTest(context.Background()))
})
}

func TestSchemaTest(t *testing.T) {
t.Run("all inputs", func(t *testing.T) {
c, err := atlasexec.NewClient("", "./mock-atlas.sh")
require.NoError(t, err)
require.NoError(t, c.SetEnv(map[string]string{
"TEST_ARGS": "schema test --env test --config file://testdata/config/atlas.hcl --url file://schema.hcl --dev-url sqlite://file?mode=memory --run example --var var1=value1 --var var2=value2",
"TEST_STDOUT": `No errors found`,
}))
tt := newT(t, nil)
tt.cli, _ = atlasexec.NewClient("", "./mock-atlas-test.sh")
os.Remove("test-out.txt")
t.Cleanup(func() {
os.Remove("test-out.txt")
})
tt.cli = c
tt.setInput("url", "file://schema.hcl")
tt.setInput("dev-url", "sqlite://file?mode=memory")
tt.setInput("run", "example")
tt.setInput("config", "file://testdata/config/atlas.hcl")
tt.setInput("env", "test")
tt.setInput("vars", `{"var1": "value1", "var2": "value2"}`)
err := (&atlasaction.Actions{Action: tt.act, Atlas: tt.cli}).SchemaTest(context.Background())
require.NoError(t, err)
out, err := os.ReadFile("test-out.txt")
require.NoError(t, err)
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
require.Len(t, lines, 1)
require.Contains(t, lines[0], "--env test")
require.Contains(t, lines[0], "--run example")
require.Contains(t, lines[0], "--var var1=value1")
require.Contains(t, lines[0], "--var var2=value2")
require.Contains(t, lines[0], "--url file://schema.hcl")
require.Contains(t, lines[0], "--dev-url sqlite://file?mode=memory")
require.NoError(t, (&atlasaction.Actions{Action: tt.act, Atlas: tt.cli}).SchemaTest(context.Background()))
})
}

Expand Down
4 changes: 0 additions & 4 deletions atlasaction/mock-atlas-down.sh

This file was deleted.

2 changes: 0 additions & 2 deletions atlasaction/mock-atlas-test.sh

This file was deleted.

4 changes: 3 additions & 1 deletion atlasaction/mock-atlas.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ fi
if [[ "$TEST_STDOUT" != "" ]]; then
echo -n $TEST_STDOUT
if [[ "$TEST_STDERR" == "" ]]; then
exit 0 # No stderr
# `migrate down` and `migrate lint` commands print result to stdout
# but the error code is set to 1.
exit ${TEST_EXIT_CODE:-0} # No stderr
fi
# In some cases, Atlas will write the error in stderr
# when if the command is partially successful.
Expand Down
2 changes: 0 additions & 2 deletions atlasaction/mock-push.sh

This file was deleted.

Loading