aports/testing/opa/tests-go1.22-compat.patch

275 lines
9.2 KiB
Diff

Patch-Source: https://github.com/open-policy-agent/opa/commit/963f5eaada9afef608a649550eb7f50d89a06181.patch
--
From 963f5eaada9afef608a649550eb7f50d89a06181 Mon Sep 17 00:00:00 2001
From: Stephan Renatus <stephan@styra.com>
Date: Tue, 20 Feb 2024 12:46:17 +0100
Subject: [PATCH] build(go): bump golang, build with 1.22, support 1.20+
(#6595)
https://go.dev/doc/go1.22
Signed-off-by: Stephan Renatus <stephan@styra.com>
---
.github/workflows/pull-request.yaml | 2 +-
.go-version | 2 +-
go.mod | 2 +-
internal/prometheus/prometheus_go1.19_test.go | 145 ------------------
internal/prometheus/prometheus_test.go | 20 ++-
topdown/tokens_test.go | 6 -
6 files changed, 18 insertions(+), 159 deletions(-)
delete mode 100644 internal/prometheus/prometheus_go1.19_test.go
diff --git a/.github/workflows/pull-request.yaml b/.github/workflows/pull-request.yaml
index 10f073f7f9..52ddd670cf 100644
--- a/.github/workflows/pull-request.yaml
+++ b/.github/workflows/pull-request.yaml
@@ -292,7 +292,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-22.04, macos-latest]
- version: ["1.19"]
+ version: ["1.20"]
steps:
- uses: actions/checkout@v4
- name: Download generated artifacts
diff --git a/.go-version b/.go-version
index ce2dd53570..71f7f51df9 100644
--- a/.go-version
+++ b/.go-version
@@ -1 +1 @@
-1.21.5
+1.22
diff --git a/go.mod b/go.mod
index e0d860b6a0..7abc6d4cfd 100644
--- a/go.mod
+++ b/go.mod
@@ -1,6 +1,6 @@
module github.com/open-policy-agent/opa
-go 1.19
+go 1.20
require (
github.com/OneOfOne/xxhash v1.2.8
diff --git a/internal/prometheus/prometheus_go1.19_test.go b/internal/prometheus/prometheus_go1.19_test.go
deleted file mode 100644
index 4b253d8ada..0000000000
--- a/internal/prometheus/prometheus_go1.19_test.go
+++ /dev/null
@@ -1,145 +0,0 @@
-// Copyright 2022 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.
-
-// NOTE(sr): Different go runtime metrics on 1.19.
-// This can be removed when we drop support for go 1.19.
-//go:build go1.19 && !go1.20
-// +build go1.19,!go1.20
-
-package prometheus
-
-import (
- "encoding/json"
- "testing"
-
- "github.com/open-policy-agent/opa/logging"
- "github.com/open-policy-agent/opa/metrics"
-)
-
-func TestJSONSerialization(t *testing.T) {
- inner := metrics.New()
- logger := func(logger logging.Logger) loggerFunc {
- return func(attrs map[string]interface{}, f string, a ...interface{}) {
- logger.WithFields(attrs).Error(f, a...)
- }
- }(logging.NewNoOpLogger())
-
- prom := New(inner, logger, []float64{1e-6, 5e-6, 1e-5, 5e-5, 1e-4, 5e-4, 1e-3, 0.01, 0.1, 1})
-
- m := prom.All()
- bs, err := json.Marshal(m)
- if err != nil {
- t.Fatal(err)
- }
-
- act := make(map[string]map[string]interface{}, len(m))
- err = json.Unmarshal(bs, &act)
- if err != nil {
- t.Fatal(err)
- }
-
- // NOTE(sr): "http_request_duration_seconds" only shows up after there has been a request
- exp := map[string][]string{
- "GAUGE": {
- "go_gc_heap_goal_bytes",
- "go_gc_heap_objects_objects",
- "go_gc_stack_starting_size_bytes",
- "go_gc_limiter_last_enabled_gc_cycle",
- "go_goroutines",
- "go_info",
- "go_memory_classes_heap_free_bytes",
- "go_memory_classes_heap_objects_bytes",
- "go_memory_classes_heap_released_bytes",
- "go_memory_classes_heap_stacks_bytes",
- "go_memory_classes_heap_unused_bytes",
- "go_memory_classes_metadata_mcache_free_bytes",
- "go_memory_classes_metadata_mcache_inuse_bytes",
- "go_memory_classes_metadata_mspan_free_bytes",
- "go_memory_classes_metadata_mspan_inuse_bytes",
- "go_memory_classes_metadata_other_bytes",
- "go_memory_classes_os_stacks_bytes",
- "go_memory_classes_other_bytes",
- "go_memory_classes_profiling_buckets_bytes",
- "go_memory_classes_total_bytes",
- "go_memstats_alloc_bytes",
- "go_memstats_buck_hash_sys_bytes",
- // "go_memstats_gc_cpu_fraction", // removed: https://github.com/prometheus/client_golang/issues/842#issuecomment-861812034
- "go_memstats_gc_sys_bytes",
- "go_memstats_heap_alloc_bytes",
- "go_memstats_heap_idle_bytes",
- "go_memstats_heap_inuse_bytes",
- "go_memstats_heap_objects",
- "go_memstats_heap_released_bytes",
- "go_memstats_heap_sys_bytes",
- "go_memstats_last_gc_time_seconds",
- "go_memstats_mcache_inuse_bytes",
- "go_memstats_mcache_sys_bytes",
- "go_memstats_mspan_inuse_bytes",
- "go_memstats_mspan_sys_bytes",
- "go_memstats_next_gc_bytes",
- "go_memstats_other_sys_bytes",
- "go_memstats_stack_inuse_bytes",
- "go_memstats_stack_sys_bytes",
- "go_memstats_sys_bytes",
- "go_sched_goroutines_goroutines",
- "go_sched_gomaxprocs_threads",
- "go_threads",
- },
- "COUNTER": {
- "go_gc_cycles_automatic_gc_cycles_total",
- "go_gc_cycles_forced_gc_cycles_total",
- "go_gc_cycles_total_gc_cycles_total",
- "go_gc_heap_allocs_bytes_total",
- "go_gc_heap_allocs_objects_total",
- "go_gc_heap_tiny_allocs_objects_total",
- "go_gc_heap_frees_bytes_total",
- "go_gc_heap_frees_objects_total",
- "go_cgo_go_to_c_calls_calls_total",
- "go_memstats_alloc_bytes_total",
- "go_memstats_lookups_total",
- "go_memstats_mallocs_total",
- "go_memstats_frees_total",
- },
- "SUMMARY": {
- "go_gc_duration_seconds",
- },
- "HISTOGRAM": {
- "go_gc_pauses_seconds", // was: "go_gc_pauses_seconds_total"
- "go_gc_heap_allocs_by_size_bytes", // was: "go_gc_heap_allocs_by_size_bytes_total"
- "go_gc_heap_frees_by_size_bytes", // was: "go_gc_heap_frees_by_size_bytes_total"
- "go_sched_latencies_seconds",
- },
- }
- found := 0
- for typ, es := range exp {
- for _, e := range es {
- a, ok := act[e]
- if !ok {
- t.Errorf("%v: metric missing", e)
- continue
- }
- if act, ok := a["type"].(string); !ok || act != typ {
- t.Errorf("%v: unexpected type: %v (expected %v)", e, act, typ)
- continue
- }
- found++
- }
- }
- if len(act) != found {
- t.Errorf("unexpected extra metrics, expected %d, got %d", found, len(act))
- for a, ty := range act {
- found := false
- for _, es := range exp {
- for _, e := range es {
- if a == e {
- found = true
- }
- }
- }
- if !found {
- t.Errorf("unexpected metric: %v (type: %v)", a, ty)
- }
- }
- }
-}
diff --git a/internal/prometheus/prometheus_test.go b/internal/prometheus/prometheus_test.go
index 7f2c73f441..4f09f866bb 100644
--- a/internal/prometheus/prometheus_test.go
+++ b/internal/prometheus/prometheus_test.go
@@ -1,11 +1,10 @@
// Copyright 2022 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.
-
-// NOTE(an): Different go runtime metrics on 1.20.
-// This can be removed when we drop support for go 1.19.
-//go:build go1.20
-// +build go1.20
+//
+// NOTE(sr): Different go runtime metrics on 1.20 vs 1.22.
+// Let's only test these on 1.22.
+//go:build go1.22
package prometheus
@@ -137,6 +136,13 @@ func TestJSONSerialization(t *testing.T) {
"go_godebug_non_default_behavior_x509usefallbackroots_events_total",
"go_godebug_non_default_behavior_zipinsecurepath_events_total",
"go_godebug_non_default_behavior_tlsmaxrsasize_events_total",
+ "go_godebug_non_default_behavior_gotypesalias_events_total", // BEGIN added in 1.22
+ "go_godebug_non_default_behavior_tlsunsafeekm_events_total",
+ "go_godebug_non_default_behavior_httplaxcontentlength_events_total",
+ "go_godebug_non_default_behavior_x509usepolicies_events_total",
+ "go_godebug_non_default_behavior_tls10server_events_total",
+ "go_godebug_non_default_behavior_httpmuxgo121_events_total",
+ "go_godebug_non_default_behavior_tlsrsakex_events_total",
},
"SUMMARY": {
"go_gc_duration_seconds",
@@ -146,6 +152,10 @@ func TestJSONSerialization(t *testing.T) {
"go_gc_heap_allocs_by_size_bytes", // was: "go_gc_heap_allocs_by_size_bytes_total"
"go_gc_heap_frees_by_size_bytes", // was: "go_gc_heap_frees_by_size_bytes_total"
"go_sched_latencies_seconds",
+ "go_sched_pauses_stopping_other_seconds", // BEGIN added in 1.22
+ "go_sched_pauses_stopping_gc_seconds",
+ "go_sched_pauses_total_gc_seconds",
+ "go_sched_pauses_total_other_seconds",
},
}
found := 0
diff --git a/topdown/tokens_test.go b/topdown/tokens_test.go
index 975a78461c..d3f482cb00 100644
--- a/topdown/tokens_test.go
+++ b/topdown/tokens_test.go
@@ -10,7 +10,6 @@ import (
"encoding/json"
"fmt"
"os"
- "runtime"
"strings"
"testing"
"time"
@@ -472,12 +471,7 @@ func TestTopdownJWTEncodeSignECWithSeedReturnsSameSignature(t *testing.T) {
"d":"jpsQnnGQmL-YBIffH1136cspYG6-0iY7X1fCE9-E9LI"
}, x)`
- // go1.20 and beyond
encodedSigned := "eyJhbGciOiAiRVMyNTYifQ.eyJwYXkiOiAibG9hZCJ9.GRp6wIqDZuYnvQH50hnIy559LdrjUux76v1ynxX6lH0XtlgwreyR16x2JMnuElo79X3zUbqlWrZITICv86arew"
- if strings.HasPrefix(runtime.Version(), "go1.19") { // we don't use go1.18 anymore
- encodedSigned = "eyJhbGciOiAiRVMyNTYifQ.eyJwYXkiOiAibG9hZCJ9.05wmHY3NomU1jr7yvusBvKwhthRklPuJhUPOkoeIn5e5n_GXvE25EfRs9AJK2wOy6NoY2ljhj07M9BMtV0dfyA"
- }
-
for i := 0; i < 10; i++ {
q := NewQuery(ast.MustParseBody(query)).
WithSeed(&cng{}).