-
Notifications
You must be signed in to change notification settings - Fork 473
/
Copy pathtesting_utils.go
74 lines (65 loc) · 2.12 KB
/
testing_utils.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
package maintainedapps
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path"
"path/filepath"
"runtime"
"testing"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/go-kit/log"
"github.com/stretchr/testify/require"
)
// IngestMaintainedApps ingests the maintained apps from the testdata
// directory, to fill the library of maintained apps with valid data for tests.
// It returns the expected results of the ingestion as a slice of
// fleet.MaintainedApps with only a few fields filled - the result of
// unmarshaling the testdata/expected_apps.json file.
func IngestMaintainedApps(t *testing.T, ds fleet.Datastore) []fleet.MaintainedApp {
_, filename, _, _ := runtime.Caller(0)
base := filepath.Dir(filename)
testdataDir := filepath.Join(base, "testdata")
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token := path.Base(r.URL.Path)
b, err := os.ReadFile(filepath.Join(testdataDir, token))
if err != nil {
if os.IsNotExist(err) {
w.WriteHeader(http.StatusNotFound)
return
}
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}
_, _ = w.Write(b)
}))
defer srv.Close()
// not using t.Setenv because we want the env var to be unset on return of
// this call
os.Setenv("FLEET_DEV_BREW_API_URL", srv.URL)
defer os.Unsetenv("FLEET_DEV_BREW_API_URL")
err := Refresh(context.Background(), ds, log.NewNopLogger())
require.NoError(t, err)
var expected []fleet.MaintainedApp
b, err := os.ReadFile(filepath.Join(testdataDir, "expected_apps.json"))
require.NoError(t, err)
err = json.Unmarshal(b, &expected)
require.NoError(t, err)
return expected
}
// ExpectedAppTokens returns the list of app tokens (unique identifier) that are
// expected to be in the maintained apps library after ingestion. The tokens are
// taken from the apps.json list.
func ExpectedAppTokens(t *testing.T) []string {
var apps []maintainedApp
err := json.Unmarshal(appsJSON, &apps)
require.NoError(t, err)
tokens := make([]string, len(apps))
for i, app := range apps {
tokens[i] = app.Identifier
}
return tokens
}