forked from kubeflow/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_utils.go
More file actions
133 lines (113 loc) · 3.54 KB
/
test_utils.go
File metadata and controls
133 lines (113 loc) · 3.54 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
package api
import (
"bytes"
"context"
"encoding/json"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"github.com/kubeflow/model-registry/ui/bff/internal/config"
k8s "github.com/kubeflow/model-registry/ui/bff/internal/integrations/httpclient"
"github.com/kubeflow/model-registry/ui/bff/internal/integrations/kubernetes"
"github.com/kubeflow/model-registry/ui/bff/internal/constants"
"github.com/kubeflow/model-registry/ui/bff/internal/mocks"
"github.com/kubeflow/model-registry/ui/bff/internal/repositories"
)
func setupApiTest[T any](method string, url string, body interface{}, k8Factory kubernetes.KubernetesClientFactory, requestIdentity kubernetes.RequestIdentity, namespace string) (T, *http.Response, error) {
mockMRClient, err := mocks.NewModelRegistryClient(nil)
if err != nil {
return *new(T), nil, err
}
mockModelCatalogClient, err := mocks.NewModelCatalogClientMock(nil)
if err != nil {
return *new(T), nil, err
}
mockClient := new(mocks.MockHTTPClient)
cfg := config.EnvConfig{
AuthMethod: config.AuthMethodInternal,
}
//if token is set, use token auth
if requestIdentity.Token != "" {
cfg.AuthMethod = config.AuthMethodUser
}
testApp := App{
repositories: repositories.NewRepositories(mockMRClient, mockModelCatalogClient),
kubernetesClientFactory: k8Factory,
logger: slog.Default(),
config: cfg,
}
var req *http.Request
if body != nil {
r, err := json.Marshal(body)
if err != nil {
return *new(T), nil, err
}
bytes.NewReader(r)
req, err = http.NewRequest(method, url, bytes.NewReader(r))
if err != nil {
return *new(T), nil, err
}
} else {
req, err = http.NewRequest(method, url, nil)
if err != nil {
return *new(T), nil, err
}
}
// Set the kubeflow-userid header (middleware work)
if requestIdentity.UserID != "" {
req.Header.Set(constants.KubeflowUserIDHeader, requestIdentity.UserID)
}
ctx := mocks.NewMockSessionContext(req.Context())
ctx = context.WithValue(ctx, constants.ModelRegistryHttpClientKey, mockClient)
ctx = context.WithValue(ctx, constants.RequestIdentityKey, requestIdentity)
ctx = context.WithValue(ctx, constants.NamespaceHeaderParameterKey, namespace)
mrHttpClient := k8s.HTTPClient{}
modelCatalogHttpClient := k8s.HTTPClient{}
ctx = context.WithValue(ctx, constants.ModelRegistryHttpClientKey, mrHttpClient)
ctx = context.WithValue(ctx, constants.ModelCatalogHttpClientKey, modelCatalogHttpClient)
req = req.WithContext(ctx)
rr := httptest.NewRecorder()
testApp.Routes().ServeHTTP(rr, req)
rs := rr.Result()
defer rs.Body.Close()
respBody, err := io.ReadAll(rs.Body)
if err != nil {
return *new(T), nil, err
}
var entity T
err = json.Unmarshal(respBody, &entity)
if err != nil {
if err == io.EOF {
// There's no body to parse.
return *new(T), rs, nil
}
return *new(T), nil, err
}
return entity, rs, nil
}
func resolveStaticAssetsDirOnTests() string {
// Fall back to finding project root for testing
projectRoot, err := findProjectRootOnTests()
if err != nil {
panic("Failed to find project root: ")
}
return filepath.Join(projectRoot, "static")
}
// on tests findProjectRoot searches for the project root by locating go.mod
func findProjectRootOnTests() (string, error) {
currentDir, err := os.Getwd()
if err != nil {
return "", err
}
// Traverse up until go.mod is found
for currentDir != "/" {
if _, err := os.Stat(filepath.Join(currentDir, "go.mod")); err == nil {
return currentDir, nil
}
currentDir = filepath.Dir(currentDir)
}
return "", os.ErrNotExist
}