-
Notifications
You must be signed in to change notification settings - Fork 68
/
search_test.go
127 lines (116 loc) · 3.34 KB
/
search_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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package main
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
"github.com/elastic/package-registry/packages"
"github.com/elastic/package-registry/proxymode"
)
func TestSearchWithProxyMode(t *testing.T) {
// nginx 1.15.0 is not included as part of the local packages
// datasources 1.0.0 is included as part of the local packages
webServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := `
[
{
"name": "nginx",
"title": "Nginx",
"version": "1.15.0",
"release": "ga",
"description": "Collect logs and metrics from Nginx HTTP servers with Elastic Agent.",
"type": "integration",
"download": "/epr/nginx/nginx-1.15.0.zip",
"path": "/package/nginx/1.15.0",
"icons": [
{
"src": "/img/logo_nginx.svg",
"path": "/package/nginx/1.15.0/img/logo_nginx.svg",
"title": "logo nginx",
"size": "32x32",
"type": "image/svg+xml"
}
],
"policy_templates": [
{
"name": "nginx",
"title": "Nginx logs and metrics",
"description": "Collect logs and metrics from Nginx instances"
}
],
"conditions": {
"kibana": {
"version": "^8.8.0"
}
},
"owner": {
"github": "elastic/obs-infraobs-integrations"
},
"categories": [
"web",
"observability"
],
"signature_path": "/epr/nginx/nginx-1.15.0.zip.sig"
},
{
"name": "datasources",
"title": "Default datasource Integration",
"version": "1.0.0",
"release": "beta",
"description": "Package with data sources",
"type": "integration",
"download": "/epr/datasources/datasources-1.0.0.zip",
"path": "/package/datasources/1.0.0",
"policy_templates": [
{
"name": "nginx",
"title": "Datasource title",
"description": "Details about the data source."
}
],
"categories": [
"custom"
]
}
]
`
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, response)
}))
defer webServer.Close()
packagesBasePaths := []string{"./testdata/second_package_path", "./testdata/package"}
indexer := NewCombinedIndexer(
packages.NewZipFileSystemIndexer(testLogger, "./testdata/local-storage"),
packages.NewFileSystemIndexer(testLogger, packagesBasePaths...),
)
err := indexer.Init(context.Background())
require.NoError(t, err)
proxyMode, err := proxymode.NewProxyMode(
testLogger,
proxymode.ProxyOptions{
Enabled: true,
ProxyTo: webServer.URL,
},
)
require.NoError(t, err)
searchWithProxyHandler := searchHandlerWithProxyMode(testLogger, indexer, proxyMode, testCacheTime)
tests := []struct {
endpoint string
path string
file string
handler func(w http.ResponseWriter, r *http.Request)
}{
{"/search?all=true", "/search", "search-all-proxy.json", searchWithProxyHandler},
{"/search", "/search", "search-just-latest-proxy.json", searchWithProxyHandler},
}
for _, test := range tests {
t.Run(test.endpoint, func(t *testing.T) {
runEndpoint(t, test.endpoint, test.path, test.file, test.handler)
})
}
}