From 983c3bc9305f0ef7dc0889a81e33db5c9a50a21f Mon Sep 17 00:00:00 2001 From: rubenruizdegauna Date: Thu, 9 May 2024 11:31:51 +0200 Subject: [PATCH] chore: bump Go version to 1.21 (#494) * chore: bump Go version to 1.21 * test: fix flaky * test: use httptest --------- Co-authored-by: Guillermo Sanchez Gavier --- .github/workflows/push_pr.yml | 4 +- .github/workflows/release.yml | 2 +- Makefile | 2 +- build/Dockerfile | 2 +- docker-compose.yml | 2 +- go.mod | 2 +- .../configs/windows/windows-http-test.yml | 4 +- .../configs/windows/windows-https-test.yml | 2 +- integration-test/http_windows_test.go | 60 ++++++++++++++----- scripts/docker-compose-build.yml | 2 +- tools/go.mod | 7 ++- tools/go.sum | 4 ++ 12 files changed, 63 insertions(+), 30 deletions(-) diff --git a/.github/workflows/push_pr.yml b/.github/workflows/push_pr.yml index 32da6d75..2a1a2591 100644 --- a/.github/workflows/push_pr.yml +++ b/.github/workflows/push_pr.yml @@ -74,8 +74,8 @@ jobs: test-windows: strategy: matrix: - go: [ '1.20' ] - os: [ windows-2019 ] + go: [ '1.21' ] + os: [ windows-2022 ] name: Run unit and integration tests in ${{matrix.os}} with go ${{matrix.go}} runs-on: ${{matrix.os}} steps: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 96b598ad..92d5ea88 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -71,7 +71,7 @@ jobs: test-windows: strategy: matrix: - go: [ '1.20' ] + go: [ '1.21' ] os: [ windows-2019 ] name: Run unit and integration tests in ${{matrix.os}} with go ${{matrix.go}} runs-on: ${{matrix.os}} diff --git a/Makefile b/Makefile index f803e550..e56af4cc 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ SRCDIR ?= . BUILD_DIR ?= $(CURDIR)/bin COVERAGE_FILE ?= coverage.out -GO_VERSION ?= 1.20 +GO_VERSION ?= 1.21 GO_CMD ?= go GODOC ?= godocdown diff --git a/build/Dockerfile b/build/Dockerfile index 8dda0903..26deea5a 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -1,4 +1,4 @@ -ARG GO_VERSION=1.20 +ARG GO_VERSION=1.21 FROM golang:$GO_VERSION diff --git a/docker-compose.yml b/docker-compose.yml index 9d4ed04e..b0b080a9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,7 @@ version: "3" services: golang: - image: golang:1.20.13-bullseye + image: golang:1.21-bullseye volumes: - .:/go/src/github.com/newrelic/nri-flex working_dir: /go/src/github.com/newrelic/nri-flex diff --git a/go.mod b/go.mod index b1fdd423..30c7cf96 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/newrelic/nri-flex -go 1.20 +go 1.21 require ( github.com/Knetic/govaluate v3.0.0+incompatible diff --git a/integration-test/configs/windows/windows-http-test.yml b/integration-test/configs/windows/windows-http-test.yml index 9dca7178..5802e70d 100644 --- a/integration-test/configs/windows/windows-http-test.yml +++ b/integration-test/configs/windows/windows-http-test.yml @@ -1,7 +1,7 @@ name: httpTest apis: - name: httpTest - url: http://localhost:8080/json + url: http://localhost:$$TEST_HTTP_SERVER_PORT/json event_type: WindowsHttpSample start_key: - - metrics \ No newline at end of file + - metrics diff --git a/integration-test/configs/windows/windows-https-test.yml b/integration-test/configs/windows/windows-https-test.yml index 7c6e10c0..61938e02 100644 --- a/integration-test/configs/windows/windows-https-test.yml +++ b/integration-test/configs/windows/windows-https-test.yml @@ -1,7 +1,7 @@ name: httpsTest apis: - name: httpsTest - url: https://localhost:8043/json + url: https://localhost:$$TEST_HTTPS_SERVER_PORT/json event_type: WindowsHttpSample start_key: - metrics diff --git a/integration-test/http_windows_test.go b/integration-test/http_windows_test.go index afbcb3ab..dca9b5e7 100644 --- a/integration-test/http_windows_test.go +++ b/integration-test/http_windows_test.go @@ -1,23 +1,26 @@ -// +build integration -// +build windows +//go:build integration && windows +// +build integration,windows package integration_test import ( - "fmt" + "net/http" + "net/http/httptest" + "net/url" + "os" "path/filepath" "testing" "github.com/newrelic/infra-integrations-sdk/data/metric" sdk "github.com/newrelic/infra-integrations-sdk/integration" - "github.com/newrelic/nri-flex/integration-test/gofile" "github.com/newrelic/nri-flex/internal/load" "github.com/newrelic/nri-flex/internal/runtime" "github.com/stretchr/testify/require" ) func Test_WindowsHttp_ReturnsData(t *testing.T) { - go startServer(false) + err := os.Setenv("TEST_HTTP_SERVER_PORT", startServer(t, false)) + require.NoError(t, err) load.Refresh() i, _ := sdk.New(load.IntegrationName, load.IntegrationVersion) @@ -29,7 +32,7 @@ func Test_WindowsHttp_ReturnsData(t *testing.T) { // when r := runtime.GetDefaultRuntime() - err := runtime.RunFlex(r) + err = runtime.RunFlex(r) require.NoError(t, err) //then @@ -38,7 +41,8 @@ func Test_WindowsHttp_ReturnsData(t *testing.T) { } func Test_WindowsHttps_ReturnsData(t *testing.T) { - go startServer(true) + err := os.Setenv("TEST_HTTPS_SERVER_PORT", startServer(t, true)) + require.NoError(t, err) load.Refresh() i, _ := sdk.New(load.IntegrationName, load.IntegrationVersion) @@ -50,7 +54,7 @@ func Test_WindowsHttps_ReturnsData(t *testing.T) { // when r := runtime.GetDefaultRuntime() - err := runtime.RunFlex(r) + err = runtime.RunFlex(r) require.NoError(t, err) //then @@ -59,8 +63,10 @@ func Test_WindowsHttps_ReturnsData(t *testing.T) { } func Test_WindowsHttps_ConfigFolder_ReturnsData(t *testing.T) { - go startServer(false) - go startServer(true) + err := os.Setenv("TEST_HTTP_SERVER_PORT", startServer(t, false)) + require.NoError(t, err) + err = os.Setenv("TEST_HTTPS_SERVER_PORT", startServer(t, true)) + require.NoError(t, err) load.Refresh() i, _ := sdk.New(load.IntegrationName, load.IntegrationVersion) @@ -73,7 +79,7 @@ func Test_WindowsHttps_ConfigFolder_ReturnsData(t *testing.T) { // when r := runtime.GetDefaultRuntime() - err := runtime.RunFlex(r) + err = runtime.RunFlex(r) require.NoError(t, err) //then @@ -99,10 +105,32 @@ func checkOutput(t *testing.T, metrics []*metric.Set, expectedCount int) { require.Equal(t, expectedCount, actualCount) } -func startServer(tls bool) { - serverFile, _ := filepath.Abs(filepath.Join("https-server", "server.go")) - _, err := gofile.Run(serverFile, fmt.Sprint(tls)) - if err != nil { - fmt.Println(err) +func startServer(t *testing.T, tls bool) (port string) { + t.Helper() + + srv := &httptest.Server{} + if tls { + srv = httptest.NewTLSServer(http.HandlerFunc(serveJSON)) + } else { + srv = httptest.NewServer(http.HandlerFunc(serveJSON)) + } + + url, err := url.Parse(srv.URL) + require.NoError(t, err) + return url.Port() +} + +func serveJSON(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-type", "application/json") + _, _ = w.Write([]byte(` + { + "metrics": [ + { + "cpu": 10.0, + "memory": 3500, + "disk": 500 + } + ] } + `)) } diff --git a/scripts/docker-compose-build.yml b/scripts/docker-compose-build.yml index 0a45031b..a4ae2801 100644 --- a/scripts/docker-compose-build.yml +++ b/scripts/docker-compose-build.yml @@ -1,7 +1,7 @@ version: "3" services: golang: - image: golang:1.20.13-bullseye + image: golang:1.21-bullseye volumes: - ../:/go/src/github.com/newrelic/nri-flex working_dir: /go/src/github.com/newrelic/nri-flex diff --git a/tools/go.mod b/tools/go.mod index d69f9538..e202e00a 100644 --- a/tools/go.mod +++ b/tools/go.mod @@ -1,11 +1,12 @@ module github.com/newrelic/nri-flex/tools -go 1.20 +go 1.21 require ( github.com/AlekSi/gocov-xml v1.1.0 - github.com/axw/gocov v1.0.0 + github.com/axw/gocov v1.1.0 github.com/jandelgado/gcov2lcov v1.0.4 github.com/robertkrimen/godocdown v0.0.0-20130622164427-0bfa04905481 - gopkg.in/yaml.v2 v2.2.8 // indirect ) + +require gopkg.in/yaml.v2 v2.2.8 // indirect diff --git a/tools/go.sum b/tools/go.sum index 5db3046f..dbfefbef 100644 --- a/tools/go.sum +++ b/tools/go.sum @@ -1,7 +1,9 @@ github.com/AlekSi/gocov-xml v0.0.0-20190121064608-3a14fb1c4737 h1:JZHBkt0GhM+ARQykshqpI49yaWCHQbJonH3XpDTwMZQ= github.com/AlekSi/gocov-xml v0.0.0-20190121064608-3a14fb1c4737/go.mod h1:w1KSuh2JgIL3nyRiZijboSUwbbxOrTzWwyWVFUHtXBQ= +github.com/AlekSi/gocov-xml v1.1.0/go.mod h1:g1dRVOCHjKkMtlPfW6BokJ/qxoeZ1uPNAK7A/ii3CUo= github.com/axw/gocov v1.0.0 h1:YsqYR66hUmilVr23tu8USgnJIJvnwh3n7j5zRn7x4LU= github.com/axw/gocov v1.0.0/go.mod h1:LvQpEYiwwIb2nYkXY2fDWhg9/AsYqkhmrCshjlUJECE= +github.com/axw/gocov v1.1.0/go.mod h1:H9G4tivgdN3pYSSVrTFBr6kGDCmAkgbJhtxFzAvgcdw= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/jandelgado/gcov2lcov v1.0.4 h1:ADwQPyNsxguqzznIbfQTENwY9FU88JdXEvpdHR9c48A= @@ -13,6 +15,7 @@ github.com/robertkrimen/godocdown v0.0.0-20130622164427-0bfa04905481/go.mod h1:C github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -26,3 +29,4 @@ gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=