chore: bump go to 1.20 for golangci-lint-action (#12614)

This commit is contained in:
Paweł Żak 2023-02-03 22:35:29 +01:00 committed by GitHub
parent 17c77df228
commit 257083dba8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 60 additions and 22 deletions

View File

@ -8,7 +8,7 @@ on:
- master - master
schedule: schedule:
# Trigger every day at 16:00 UTC # Trigger every day at 16:00 UTC
- cron: '0 16 * * *' - cron: '0 16 * * *'
permissions: permissions:
contents: read # to fetch code (actions/checkout) contents: read # to fetch code (actions/checkout)
pull-requests: read # to fetch pull requests (golangci/golangci-lint-action) pull-requests: read # to fetch pull requests (golangci/golangci-lint-action)
@ -20,7 +20,7 @@ jobs:
steps: steps:
- uses: actions/setup-go@v3 - uses: actions/setup-go@v3
with: with:
go-version: 1.19 go-version: '1.20'
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- name: golangci-lint - name: golangci-lint
uses: golangci/golangci-lint-action@v3 uses: golangci/golangci-lint-action@v3

View File

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"compress/gzip" "compress/gzip"
"context" "context"
cryptoRand "crypto/rand"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -87,13 +88,16 @@ func ReadLines(filename string) ([]string, error) {
} }
// RandomString returns a random string of alphanumeric characters // RandomString returns a random string of alphanumeric characters
func RandomString(n int) string { func RandomString(n int) (string, error) {
var bytes = make([]byte, n) var bytes = make([]byte, n)
rand.Read(bytes) //nolint:revive // from math/rand/rand.go: "It always returns len(p) and a nil error." _, err := cryptoRand.Read(bytes)
if err != nil {
return "", err
}
for i, b := range bytes { for i, b := range bytes {
bytes[i] = alphanum[b%byte(len(alphanum))] bytes[i] = alphanum[b%byte(len(alphanum))]
} }
return string(bytes) return string(bytes), nil
} }
// SnakeCase converts the given string to snake case following the Golang format: // SnakeCase converts the given string to snake case following the Golang format:

View File

@ -15,8 +15,6 @@ import (
var sampleConfig string var sampleConfig string
type Mock struct { type Mock struct {
counter int64
MetricName string `toml:"metric_name"` MetricName string `toml:"metric_name"`
Tags map[string]string `toml:"tags"` Tags map[string]string `toml:"tags"`
@ -25,6 +23,9 @@ type Mock struct {
Step []*step `toml:"step"` Step []*step `toml:"step"`
Stock []*stock `toml:"stock"` Stock []*stock `toml:"stock"`
SineWave []*sineWave `toml:"sine_wave"` SineWave []*sineWave `toml:"sine_wave"`
counter int64
rand *rand.Rand
} }
type constant struct { type constant struct {
@ -65,7 +66,7 @@ func (*Mock) SampleConfig() string {
} }
func (m *Mock) Init() error { func (m *Mock) Init() error {
rand.Seed(time.Now().UnixNano()) m.rand = rand.New(rand.NewSource(time.Now().UnixNano()))
return nil return nil
} }
@ -92,17 +93,17 @@ func (m *Mock) Gather(acc telegraf.Accumulator) error {
return nil return nil
} }
// Generate random value between min and max, inclusivly // Generate random value between min and max, inclusively
func (m *Mock) generateRandomFloat64(fields map[string]interface{}) { func (m *Mock) generateRandomFloat64(fields map[string]interface{}) {
for _, random := range m.Random { for _, random := range m.Random {
fields[random.Name] = random.Min + rand.Float64()*(random.Max-random.Min) fields[random.Name] = random.Min + m.rand.Float64()*(random.Max-random.Min)
} }
} }
// Create sine waves // Create sine waves
func (m *Mock) generateSineWave(fields map[string]interface{}) { func (m *Mock) generateSineWave(fields map[string]interface{}) {
for _, field := range m.SineWave { for _, field := range m.SineWave {
fields[field.Name] = math.Sin((float64(m.counter) * field.Period * math.Pi)) * field.Amplitude fields[field.Name] = math.Sin(float64(m.counter)*field.Period*math.Pi) * field.Amplitude
} }
} }
@ -125,7 +126,7 @@ func (m *Mock) generateStockPrice(fields map[string]interface{}) {
if stock.latest == 0.0 { if stock.latest == 0.0 {
stock.latest = stock.Price stock.latest = stock.Price
} else { } else {
noise := 2 * (rand.Float64() - 0.5) noise := 2 * (m.rand.Float64() - 0.5)
stock.latest = stock.latest + (stock.latest * stock.Volatility * noise) stock.latest = stock.latest + (stock.latest * stock.Volatility * noise)
// avoid going below zero // avoid going below zero

View File

@ -3,8 +3,9 @@ package mock
import ( import (
"testing" "testing"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/influxdata/telegraf/testutil"
) )
func TestGather(t *testing.T) { func TestGather(t *testing.T) {
@ -62,6 +63,7 @@ func TestGather(t *testing.T) {
} }
var acc testutil.Accumulator var acc testutil.Accumulator
require.NoError(t, m.Init())
require.NoError(t, m.Gather(&acc)) require.NoError(t, m.Gather(&acc))
require.Len(t, acc.Metrics, 1) require.Len(t, acc.Metrics, 1)
@ -100,6 +102,7 @@ func TestGatherEmpty(t *testing.T) {
} }
var acc testutil.Accumulator var acc testutil.Accumulator
require.NoError(t, m.Init())
require.NoError(t, m.Gather(&acc)) require.NoError(t, m.Gather(&acc))
acc.AssertDoesNotContainMeasurement(t, "test_empty") acc.AssertDoesNotContainMeasurement(t, "test_empty")

View File

@ -320,7 +320,11 @@ func (m *MQTTConsumer) createOpts() (*mqtt.ClientOptions, error) {
opts := mqtt.NewClientOptions() opts := mqtt.NewClientOptions()
opts.ConnectTimeout = time.Duration(m.ConnectionTimeout) opts.ConnectTimeout = time.Duration(m.ConnectionTimeout)
if m.ClientID == "" { if m.ClientID == "" {
opts.SetClientID("Telegraf-Consumer-" + internal.RandomString(5)) randomString, err := internal.RandomString(5)
if err != nil {
return nil, fmt.Errorf("generating random string for client ID failed: %w", err)
}
opts.SetClientID("Telegraf-Consumer-" + randomString)
} else { } else {
opts.SetClientID(m.ClientID) opts.SetClientID(m.ClientID)
} }

View File

@ -435,7 +435,11 @@ func (s *Statsd) tcpListen(listener *net.TCPListener) error {
// not over connection limit, handle the connection properly. // not over connection limit, handle the connection properly.
s.wg.Add(1) s.wg.Add(1)
// generate a random id for this TCPConn // generate a random id for this TCPConn
id := internal.RandomString(6) id, err := internal.RandomString(6)
if err != nil {
return err
}
s.remember(id, conn) s.remember(id, conn)
go s.handler(conn, id) go s.handler(conn, id)
default: default:

View File

@ -170,10 +170,16 @@ func (t *TCPListener) tcpListen() {
select { select {
case <-t.accept: case <-t.accept:
// generate a random id for this TCPConn
id, err := internal.RandomString(6)
if err != nil {
t.Log.Errorf("generating a random id for TCP connection failed: %v", err)
return
}
// not over connection limit, handle the connection properly. // not over connection limit, handle the connection properly.
t.wg.Add(1) t.wg.Add(1)
// generate a random id for this TCPConn
id := internal.RandomString(6)
t.remember(id, conn) t.remember(id, conn)
go t.handler(conn, id) go t.handler(conn, id)
default: default:

View File

@ -185,7 +185,9 @@ func createFile(t *testing.T) *os.File {
} }
func tmpFile(t *testing.T) string { func tmpFile(t *testing.T) string {
return t.TempDir() + internal.RandomString(10) randomString, err := internal.RandomString(10)
require.NoError(t, err)
return t.TempDir() + randomString
} }
func validateFile(t *testing.T, fileName, expS string) { func validateFile(t *testing.T, fileName, expS string) {

View File

@ -4,8 +4,8 @@ import (
"fmt" "fmt"
"time" "time"
// Library that supports v3.1.1 mqttv3 "github.com/eclipse/paho.mqtt.golang" // Library that supports v3.1.1
mqttv3 "github.com/eclipse/paho.mqtt.golang"
"github.com/influxdata/telegraf/config" "github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/internal"
) )
@ -31,7 +31,11 @@ func (m *mqttv311Client) Connect() error {
if m.ClientID != "" { if m.ClientID != "" {
opts.SetClientID(m.ClientID) opts.SetClientID(m.ClientID)
} else { } else {
opts.SetClientID("Telegraf-Output-" + internal.RandomString(5)) randomString, err := internal.RandomString(5)
if err != nil {
return fmt.Errorf("generating random string for client ID failed: %w", err)
}
opts.SetClientID("Telegraf-Output-" + randomString)
} }
tlsCfg, err := m.ClientConfig.TLSConfig() tlsCfg, err := m.ClientConfig.TLSConfig()

View File

@ -8,6 +8,7 @@ import (
mqttv5auto "github.com/eclipse/paho.golang/autopaho" mqttv5auto "github.com/eclipse/paho.golang/autopaho"
mqttv5 "github.com/eclipse/paho.golang/paho" mqttv5 "github.com/eclipse/paho.golang/paho"
"github.com/influxdata/telegraf/config" "github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/internal"
) )
@ -26,7 +27,11 @@ func (m *mqttv5Client) Connect() error {
if m.ClientID != "" { if m.ClientID != "" {
opts.ClientID = m.ClientID opts.ClientID = m.ClientID
} else { } else {
opts.ClientID = "Telegraf-Output-" + internal.RandomString(5) randomString, err := internal.RandomString(5)
if err != nil {
return fmt.Errorf("generating random string for client ID failed: %w", err)
}
opts.ClientID = "Telegraf-Output-" + randomString
} }
user, err := m.Username.Get() user, err := m.Username.Get()

View File

@ -143,6 +143,11 @@ func main() {
Regex: `(quay\.io\/influxdb\/telegraf-ci):(\d.\d*.\d)`, Regex: `(quay\.io\/influxdb\/telegraf-ci):(\d.\d*.\d)`,
Replace: fmt.Sprintf("$1:%s", version), Replace: fmt.Sprintf("$1:%s", version),
}, },
{
FileName: ".github/workflows/golangci-lint.yml",
Regex: `(go-version).*`,
Replace: fmt.Sprintf("$1: '%s'", zeroPatchVersion),
},
{ {
FileName: "Makefile", FileName: "Makefile",
Regex: `(quay\.io\/influxdb\/telegraf-ci):(\d.\d*.\d)`, Regex: `(quay\.io\/influxdb\/telegraf-ci):(\d.\d*.\d)`,