-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathclient_methods_test.go
More file actions
224 lines (191 loc) · 6.24 KB
/
Copy pathclient_methods_test.go
File metadata and controls
224 lines (191 loc) · 6.24 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright 2026 Columnar Technologies Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dbc_test
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"github.com/columnar-tech/dbc"
"github.com/columnar-tech/dbc/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func newTestClientForServer(t *testing.T, serverURL string) *dbc.Client {
t.Helper()
c, err := dbc.NewClient(
dbc.WithHTTPClient(&http.Client{}),
dbc.WithBaseURL(serverURL),
)
require.NoError(t, err)
return c
}
func newInstallTestServer(t *testing.T) *httptest.Server {
t.Helper()
indexData, err := os.ReadFile(filepath.Join("cmd", "dbc", "testdata", "test_index.yaml"))
require.NoError(t, err)
tarballData, err := os.ReadFile(filepath.Join("cmd", "dbc", "testdata", "test-driver-1.tar.gz"))
require.NoError(t, err)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.URL.Path == "/index.yaml":
w.Header().Set("Content-Type", "application/yaml")
w.Write(indexData)
case strings.HasSuffix(r.URL.Path, ".tar.gz"):
w.Header().Set("Content-Type", "application/gzip")
w.Header().Set("Content-Length", fmt.Sprint(len(tarballData)))
w.Write(tarballData)
default:
http.NotFound(w, r)
}
}))
t.Cleanup(srv.Close)
return srv
}
func TestClientSearch(t *testing.T) {
srv := newInstallTestServer(t)
c, err := dbc.NewClient(
dbc.WithHTTPClient(&http.Client{}),
dbc.WithBaseURL(srv.URL),
)
require.NoError(t, err)
t.Run("empty pattern returns all drivers", func(t *testing.T) {
drivers, err := c.Search(t.Context(), "")
require.NoError(t, err)
assert.NotEmpty(t, drivers)
var paths []string
for _, d := range drivers {
paths = append(paths, d.Path)
}
assert.Contains(t, paths, "test-driver-1")
assert.Contains(t, paths, "test-driver-2")
})
t.Run("pattern matches by path", func(t *testing.T) {
drivers, err := c.Search(t.Context(), "test-driver-1")
require.NoError(t, err)
require.NotEmpty(t, drivers)
var paths []string
for _, d := range drivers {
paths = append(paths, d.Path)
}
assert.Contains(t, paths, "test-driver-1")
assert.NotContains(t, paths, "test-driver-2")
})
t.Run("nonexistent pattern returns empty list", func(t *testing.T) {
drivers, err := c.Search(t.Context(), "nonexistent-driver-xyz")
require.NoError(t, err)
assert.Empty(t, drivers)
})
}
func TestClientInstall(t *testing.T) {
srv := newInstallTestServer(t)
c := newTestClientForServer(t, srv.URL)
tmpDir := t.TempDir()
cfg := config.Config{
Level: config.ConfigEnv,
Location: tmpDir,
}
t.Run("installs driver successfully", func(t *testing.T) {
manifest, err := c.Install(t.Context(), cfg, "test-driver-1")
require.NoError(t, err)
require.NotNil(t, manifest)
assert.Equal(t, "test-driver-1", manifest.DriverInfo.ID)
assert.NotNil(t, manifest.DriverInfo.Version)
})
t.Run("returns error for nonexistent driver", func(t *testing.T) {
_, err := c.Install(t.Context(), cfg, "nonexistent-driver")
assert.Error(t, err)
assert.Contains(t, err.Error(), "nonexistent-driver")
})
}
func TestClientUninstall(t *testing.T) {
srv := newInstallTestServer(t)
c := newTestClientForServer(t, srv.URL)
t.Run("uninstalls a previously installed driver", func(t *testing.T) {
tmpDir := t.TempDir()
cfg := config.Config{
Level: config.ConfigEnv,
Location: tmpDir,
}
_, err := c.Install(t.Context(), cfg, "test-driver-1")
require.NoError(t, err)
manifestPath := filepath.Join(tmpDir, "test-driver-1.toml")
_, err = os.Stat(manifestPath)
require.NoError(t, err, "manifest TOML should exist after install")
err = c.Uninstall(cfg, "test-driver-1")
require.NoError(t, err)
_, err = os.Stat(manifestPath)
assert.True(t, os.IsNotExist(err), "manifest TOML should be removed after uninstall")
})
t.Run("returns error for driver not installed", func(t *testing.T) {
tmpDir := t.TempDir()
cfg := config.Config{
Level: config.ConfigEnv,
Location: tmpDir,
}
err := c.Uninstall(cfg, "not-installed-driver")
assert.Error(t, err)
assert.Contains(t, err.Error(), "not-installed-driver")
})
}
func TestClientDownload(t *testing.T) {
srv := newInstallTestServer(t)
c := newTestClientForServer(t, srv.URL)
t.Run("successful download returns readable body", func(t *testing.T) {
pkgURL, err := url.Parse(srv.URL + "/test-driver-1.tar.gz")
require.NoError(t, err)
pkg := dbc.PkgInfo{
Driver: dbc.Driver{Title: "test-driver-1"},
Path: pkgURL,
}
body, err := c.Download(t.Context(), pkg)
require.NoError(t, err)
defer body.Close()
data, err := io.ReadAll(body)
require.NoError(t, err)
assert.NotEmpty(t, data, "download body should not be empty")
})
t.Run("nil path returns error", func(t *testing.T) {
pkg := dbc.PkgInfo{
Driver: dbc.Driver{Title: "nil-path-driver"},
}
_, err := c.Download(t.Context(), pkg)
require.Error(t, err)
assert.Contains(t, err.Error(), "no url set")
assert.Contains(t, err.Error(), "nil-path-driver")
})
t.Run("non-200 status returns error with body", func(t *testing.T) {
errSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, "package not found in storage backend")
}))
t.Cleanup(errSrv.Close)
errClient := newTestClientForServer(t, errSrv.URL)
pkgURL, err := url.Parse(errSrv.URL + "/some-driver.tar.gz")
require.NoError(t, err)
pkg := dbc.PkgInfo{
Driver: dbc.Driver{Title: "error-driver"},
Path: pkgURL,
}
_, err = errClient.Download(t.Context(), pkg)
require.Error(t, err)
assert.Contains(t, err.Error(), "500")
assert.Contains(t, err.Error(), "package not found in storage backend")
})
}