Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return just the latest version of the packages if all=true is not set when proxy mode is enabled #1055

Merged
merged 4 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Bugfixes

* Update Go runtime to 1.20.6. [#1053](https://github.com/elastic/package-registry/pull/1053)
* Return all packages when using proxy mode and "all" query parameter is not set [#1055](https://github.com/elastic/package-registry/pull/1055)

### Added

Expand Down
4 changes: 3 additions & 1 deletion search.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ func searchHandlerWithProxyMode(logger *zap.Logger, indexer Indexer, proxyMode *
return
}
packages = packages.Join(proxiedPackages)
packages = latestPackagesVersion(packages)
if !opts.Filter.AllVersions {
packages = latestPackagesVersion(packages)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have tests covered by this? I would be interested on seeing that we don't duplicate packages, but packages.Join should take care of this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were no tests for this proxy mode in search endpoint.

I've added a test here 54d32e7 using proxy mode using all=true and not using it.

}
}

data, err := getPackageOutput(r.Context(), packages)
Expand Down
127 changes: 127 additions & 0 deletions search_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)
})
}
}
Loading