mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
6601188c64
* runtime: Correct naming of version checking code Rename telemetry functionality to version checking to accurately reflect current behavior following https://github.com/open-policy-agent/opa/pull/7756. The system only checks GitHub releases for version updates without sending any data about the OPA instance and so the privacy docs have been updated too. Signed-off-by: Charlie Egan <charlie_egan@apple.com> * Make WithTelemetryGatherers a no-op Deprecate WithTelemetryGatherers since telemetry gathering has been removed. The function now returns a no-op to maintain API compatibility without breaking existing code that might uses it. Signed-off-by: Charlie Egan <charlie_egan@apple.com> --------- Signed-off-by: Charlie Egan <charlie_egan@apple.com>
134 lines
3.0 KiB
Go
134 lines
3.0 KiB
Go
// Copyright 2020 The OPA Authors. All rights reserved.
|
|
// Use of this source code is governed by an Apache2
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"runtime"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/open-policy-agent/opa/internal/versioncheck"
|
|
)
|
|
|
|
func TestGenerateCmdOutputDisableCheckFlag(t *testing.T) {
|
|
var stdout bytes.Buffer
|
|
|
|
if err := generateCmdOutput(&stdout, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
expectOutputKeys(t, stdout.String(), []string{
|
|
"Version",
|
|
"Build Commit",
|
|
"Build Timestamp",
|
|
"Build Hostname",
|
|
"Go Version",
|
|
"Platform",
|
|
"WebAssembly",
|
|
"Rego Version",
|
|
})
|
|
}
|
|
|
|
func TestGenerateCmdOutputWithCheckFlagNoError(t *testing.T) {
|
|
// to support testing on all supported platforms
|
|
downloadLink := fmt.Sprintf("https://openpolicyagent.org/downloads/v100.0.0/opa_%v_%v",
|
|
runtime.GOOS, runtime.GOARCH)
|
|
|
|
if runtime.GOARCH == "arm64" {
|
|
downloadLink = fmt.Sprintf("%v_static", downloadLink)
|
|
}
|
|
|
|
if strings.HasPrefix(runtime.GOOS, "win") {
|
|
downloadLink = fmt.Sprintf("%v.exe", downloadLink)
|
|
}
|
|
|
|
resp := &versioncheck.GitHubRelease{
|
|
TagName: "v100.0.0",
|
|
Download: downloadLink,
|
|
ReleaseNotes: "https://github.com/open-policy-agent/opa/releases/tag/v100.0.0",
|
|
}
|
|
|
|
// test server
|
|
baseURL, teardown := getTestServer(resp, http.StatusOK)
|
|
defer teardown()
|
|
|
|
t.Setenv("OPA_VERSION_CHECK_SERVICE_URL", baseURL)
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
if err := generateCmdOutput(&stdout, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
expectOutputKeys(t, stdout.String(), []string{
|
|
"Version",
|
|
"Build Commit",
|
|
"Build Timestamp",
|
|
"Build Hostname",
|
|
"Go Version",
|
|
"Platform",
|
|
"WebAssembly",
|
|
"Latest Upstream Version",
|
|
"Release Notes",
|
|
"Download",
|
|
"Rego Version",
|
|
})
|
|
}
|
|
|
|
func TestCheckOPAUpdateBadURL(t *testing.T) {
|
|
url := "http://foo:8112"
|
|
t.Setenv("OPA_VERSION_CHECK_SERVICE_URL", url)
|
|
|
|
var stdout bytes.Buffer
|
|
err := checkOPAUpdate(context.Background(), &stdout)
|
|
if err == nil {
|
|
t.Fatal("Expected error but got nil")
|
|
}
|
|
}
|
|
|
|
func expectOutputKeys(t *testing.T, stdout string, expectedKeys []string) {
|
|
t.Helper()
|
|
|
|
lines := strings.Split(strings.Trim(stdout, "\n"), "\n")
|
|
gotKeys := make([]string, 0, len(lines))
|
|
|
|
for _, line := range lines {
|
|
gotKeys = append(gotKeys, strings.Split(line, ":")[0])
|
|
}
|
|
|
|
sort.Strings(expectedKeys)
|
|
sort.Strings(gotKeys)
|
|
|
|
if len(expectedKeys) != len(gotKeys) {
|
|
t.Fatalf("expected %v but got %v", expectedKeys, gotKeys)
|
|
}
|
|
|
|
for i, got := range gotKeys {
|
|
if expectedKeys[i] != got {
|
|
t.Fatalf("expected %v but got %v", expectedKeys, gotKeys)
|
|
}
|
|
}
|
|
}
|
|
|
|
func getTestServer(update any, statusCode int) (baseURL string, teardownFn func()) {
|
|
mux := http.NewServeMux()
|
|
ts := httptest.NewServer(mux)
|
|
|
|
mux.HandleFunc("/repos/open-policy-agent/opa/releases/latest", func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(statusCode)
|
|
bs, _ := json.Marshal(update)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write(bs)
|
|
})
|
|
return ts.URL, ts.Close
|
|
}
|