-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeodata_service_test.go
More file actions
205 lines (182 loc) · 7.29 KB
/
Copy pathgeodata_service_test.go
File metadata and controls
205 lines (182 loc) · 7.29 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
package main
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"github.com/rubiojr/whereami/internal/admingeo"
"github.com/rubiojr/whereami/internal/geodata"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type serviceTestResolver struct {
version admingeo.DatasetVersion
}
func (r *serviceTestResolver) Resolve(context.Context, admingeo.Coordinate) (admingeo.AdminPath, error) {
return admingeo.AdminPath{}, nil
}
func (r *serviceTestResolver) Version() admingeo.DatasetVersion { return r.version }
func (r *serviceTestResolver) Close() error { return nil }
func TestEmbeddedGeodataManifestIsValid(t *testing.T) {
manifest, err := geodata.ParseManifest(embeddedGeodataManifest, maxGeodataArtifactBytes)
require.NoError(t, err)
for _, generation := range manifest.Generations {
roles := make(map[geodata.ArtifactRole]bool)
for _, artifact := range generation.Artifacts {
roles[artifact.Role] = true
}
assert.Equal(t, map[geodata.ArtifactRole]bool{geodataIndexRole: true, geodataSlabRole: true}, roles)
}
}
func TestOpenGeodataServiceFileAcceptsLocalManifest(t *testing.T) {
manifestPath := filepath.Join(t.TempDir(), "manifest.json")
require.NoError(t, os.WriteFile(manifestPath, embeddedGeodataManifest, 0o600))
service, err := openGeodataServiceFile(filepath.Join(t.TempDir(), "geodata"), manifestPath)
require.NoError(t, err)
service.Close()
}
func TestOpenGeodataServiceFileRejectsOversizedManifest(t *testing.T) {
manifestPath := filepath.Join(t.TempDir(), "manifest.json")
require.NoError(t, os.WriteFile(manifestPath, bytes.Repeat([]byte{'x'}, int(maxGeodataManifestBytes)+1), 0o600))
service, err := openGeodataServiceFile(filepath.Join(t.TempDir(), "geodata"), manifestPath)
assert.Nil(t, service)
assert.ErrorContains(t, err, "exceeds")
}
func TestOpenXiangshanResolverRequiresExactArtifactRoles(t *testing.T) {
_, err := openXiangshanResolver(context.Background(), geodata.Generation{DatasetVersion: "test"}, map[geodata.ArtifactRole]string{
geodataIndexRole: "index",
})
assert.ErrorContains(t, err, "exactly index and polygons")
_, err = openXiangshanResolver(context.Background(), geodata.Generation{DatasetVersion: "test"}, map[geodata.ArtifactRole]string{
geodataIndexRole: "index",
geodataSlabRole: "slab",
"extra": "extra",
})
assert.ErrorContains(t, err, "exactly index and polygons")
}
func TestGeodataAPIStatusAndValidation(t *testing.T) {
root := t.TempDir()
cleanupGeodataTestRoot(t, root)
service, err := newGeodataService(root, geodata.Manifest{FormatVersion: geodata.ManifestFormatVersion}, nil, serviceTestFactory)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, service.Close()) })
mux := http.NewServeMux()
RegisterGeodataAPI(mux, service)
response := httptest.NewRecorder()
mux.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/api/geodata", nil))
require.Equal(t, http.StatusOK, response.Code)
assert.NotContains(t, response.Body.String(), "http")
var status geodataAPIStatus
require.NoError(t, json.Unmarshal(response.Body.Bytes(), &status))
assert.Empty(t, status.Available)
assert.Equal(t, "idle", status.Install.State)
response = httptest.NewRecorder()
request := httptest.NewRequest(http.MethodPost, "/api/geodata/install", bytes.NewBufferString(`{"generation_id":"unknown"}`))
mux.ServeHTTP(response, request)
assert.Equal(t, http.StatusBadRequest, response.Code)
response = httptest.NewRecorder()
request = httptest.NewRequest(http.MethodPost, "/api/geodata/install", bytes.NewBufferString(`{"generation_id":"unknown"} {}`))
mux.ServeHTTP(response, request)
assert.Equal(t, http.StatusBadRequest, response.Code)
}
func TestGeodataAPIInstallsInBackground(t *testing.T) {
body := []byte("geodata")
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write(body)
}))
t.Cleanup(server.Close)
manifest := serviceTestManifest(server.URL+"/artifact", body)
root := t.TempDir()
cleanupGeodataTestRoot(t, root)
service, err := newGeodataService(root, manifest, server.Client(), serviceTestFactory)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, service.Close()) })
activated := make(chan struct{}, 1)
service.SetActivationCallback(func() { activated <- struct{}{} })
mux := http.NewServeMux()
RegisterGeodataAPI(mux, service)
response := httptest.NewRecorder()
request := httptest.NewRequest(http.MethodPost, "/api/geodata/install", bytes.NewBufferString(`{"generation_id":"test-v1"}`))
mux.ServeHTTP(response, request)
assert.Equal(t, http.StatusAccepted, response.Code)
require.Eventually(t, func() bool {
return service.Status().Install.State == "installed"
}, time.Second, 10*time.Millisecond)
status := service.Status()
assert.Equal(t, "test-v1", status.Current.GenerationID)
assert.True(t, status.Current.Valid)
select {
case <-activated:
case <-time.After(time.Second):
t.Fatal("activation callback was not called")
}
}
func TestGeodataAPICancelsInstall(t *testing.T) {
started := make(chan struct{})
server := httptest.NewTLSServer(http.HandlerFunc(func(_ http.ResponseWriter, request *http.Request) {
close(started)
<-request.Context().Done()
}))
t.Cleanup(server.Close)
body := []byte("geodata")
root := t.TempDir()
cleanupGeodataTestRoot(t, root)
service, err := newGeodataService(root, serviceTestManifest(server.URL+"/artifact", body), server.Client(), serviceTestFactory)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, service.Close()) })
mux := http.NewServeMux()
RegisterGeodataAPI(mux, service)
response := httptest.NewRecorder()
request := httptest.NewRequest(http.MethodPost, "/api/geodata/install", bytes.NewBufferString(`{"generation_id":"test-v1"}`))
mux.ServeHTTP(response, request)
require.Equal(t, http.StatusAccepted, response.Code)
<-started
response = httptest.NewRecorder()
mux.ServeHTTP(response, httptest.NewRequest(http.MethodDelete, "/api/geodata/install", nil))
assert.Equal(t, http.StatusAccepted, response.Code)
require.Eventually(t, func() bool {
return service.Status().Install.State == "cancelled"
}, time.Second, 10*time.Millisecond)
}
func serviceTestFactory(_ context.Context, generation geodata.Generation, _ map[geodata.ArtifactRole]string) (admingeo.Resolver, error) {
return &serviceTestResolver{version: generation.DatasetVersion}, nil
}
func cleanupGeodataTestRoot(t *testing.T, root string) {
t.Helper()
t.Cleanup(func() {
_ = filepath.WalkDir(root, func(path string, entry os.DirEntry, _ error) error {
if entry != nil && entry.IsDir() {
_ = os.Chmod(path, 0o700)
}
return nil
})
})
}
func serviceTestManifest(downloadURL string, body []byte) geodata.Manifest {
hash := sha256.Sum256(body)
return geodata.Manifest{
FormatVersion: geodata.ManifestFormatVersion,
Generations: []geodata.Generation{{
ID: "test-v1",
DatasetVersion: "dataset-v1",
SourceVersion: "source-v1",
Attribution: "Test contributors",
License: "Test license",
Artifacts: []geodata.Artifact{{
Role: geodataIndexRole,
Filename: "test.index",
URL: downloadURL,
Bytes: int64(len(body)),
SHA256: hex.EncodeToString(hash[:]),
}},
Probes: []geodata.Probe{{Latitude: 0, Longitude: 0}},
}},
}
}