-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
207 lines (197 loc) · 4.58 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"context"
"database/sql"
"encoding/base64"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"ariga.io/atlas/sql/migrate"
"github.com/sethvargo/go-githubactions"
"github.com/stretchr/testify/require"
_ "github.com/mattn/go-sqlite3"
)
func TestLoad(t *testing.T) {
tests := []struct {
name string
env map[string]string
expect *Input
hasErr bool
}{
{
name: "Missing URL",
env: map[string]string{},
hasErr: true,
},
{
name: "Valid Inputs",
env: map[string]string{
"INPUT_URL": "sqlite://file.db",
"INPUT_AMOUNT": "1",
"INPUT_TX-MODE": "all",
"INPUT_BASELINE": "1234",
"INPUT_ALLOW-DIRTY": "true",
},
expect: &Input{
URL: "sqlite://file.db",
Amount: 1,
TxMode: "all",
Baseline: "1234",
AllowDirty: true,
},
hasErr: false,
},
{
name: "Illegal TxMode",
env: map[string]string{
"INPUT_URL": "sqlite://file.db",
"INPUT_COUNT": "1",
"INPUT_TX-MODE": "invalid",
"INPUT_BASELINE": "1234",
"INPUT_ALLOW-DIRTY": "true",
},
expect: nil,
hasErr: true,
},
{
name: "Invalid Dirty",
env: map[string]string{
"INPUT_URL": "sqlite://file.db",
"INPUT_ALLOW-DIRTY": "notABool",
},
expect: nil,
hasErr: true,
},
{
name: "Invalid Amount",
env: map[string]string{
"INPUT_URL": "sqlite://file.db",
"INPUT_AMOUNT": "notAnInt",
},
expect: nil,
hasErr: true,
},
{
name: "Dir and CloudDir Exclusion",
env: map[string]string{
"INPUT_URL": "sqlite://file.db",
"INPUT_DIR": "dir",
"INPUT_CLOUD-DIR": "cloud-dir",
},
expect: nil,
hasErr: true,
},
{
name: "Dir",
env: map[string]string{
"INPUT_URL": "sqlite://file.db",
"INPUT_DIR": "dir",
},
expect: &Input{
URL: "sqlite://file.db",
Dir: "dir",
},
},
{
name: "CloudDir no Token",
env: map[string]string{
"INPUT_URL": "sqlite://file.db",
"INPUT_CLOUD-DIR": "dir",
},
hasErr: true,
},
{
name: "CloudDir Token",
env: map[string]string{
"INPUT_URL": "sqlite://file.db",
"INPUT_CLOUD-DIR": "dir",
"INPUT_CLOUD-TOKEN": "token",
"INPUT_CLOUD-TAG": "tag",
},
expect: &Input{
URL: "sqlite://file.db",
Cloud: Cloud{
Token: "token",
Dir: "dir",
Tag: "tag",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
act := githubactions.New(githubactions.WithGetenv(func(key string) string {
return tt.env[key]
}))
input, err := Load(act)
if tt.hasErr {
require.Error(t, err)
} else {
require.NoError(t, err)
require.EqualValues(t, tt.expect, input)
}
})
}
}
func TestRun(t *testing.T) {
dbpath := sqlitedb(t)
dburl := fmt.Sprintf("sqlite://%s", dbpath)
run, err := Run(context.Background(), &Input{
Dir: "./internal/testdata/migrations",
URL: dburl,
})
require.NoError(t, err)
require.Equal(t, 2, len(run.Applied))
}
func TestCloud(t *testing.T) {
srv := fakeCloud(t)
defer srv.Close()
dbpath := sqlitedb(t)
dburl := fmt.Sprintf("sqlite://%s", dbpath)
run, err := Run(context.Background(), &Input{
Cloud: Cloud{
URL: srv.URL,
Dir: "dir",
Token: "token",
},
URL: dburl,
})
require.NoError(t, err)
require.Equal(t, 2, len(run.Applied))
}
// fakeCloud returns a httptest.Server that mocks the cloud endpoint.
func fakeCloud(t *testing.T) *httptest.Server {
dir := testDir(t, "./internal/testdata/migrations")
ad, err := migrate.ArchiveDir(&dir)
require.NoError(t, err)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "Bearer token", r.Header.Get("Authorization"))
// nolint:errcheck
fmt.Fprintf(w, `{"data":{"dirState":{"content":%q}}}`, base64.StdEncoding.EncodeToString(ad))
}))
return srv
}
// testDir returns a migrate.MemDir from the given path.
func testDir(t *testing.T, path string) (d migrate.MemDir) {
rd, err := os.ReadDir(path)
require.NoError(t, err)
for _, f := range rd {
fp := filepath.Join(path, f.Name())
b, err := os.ReadFile(fp)
require.NoError(t, err)
require.NoError(t, d.WriteFile(f.Name(), b))
}
return d
}
// sqlitedb returns a path to an initialized sqlite database file. The file is
// created in a temporary directory and will be deleted when the test finishes.
func sqlitedb(t *testing.T) string {
td := t.TempDir()
dbpath := filepath.Join(td, "file.db")
_, err := sql.Open("sqlite3", fmt.Sprintf("file:%s?cache=shared&_fk=1", dbpath))
require.NoError(t, err)
return dbpath
}