Skip to content

Commit

Permalink
Fix lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
belimawr committed Sep 30, 2024
1 parent b3813b3 commit b7b49b6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 25 deletions.
19 changes: 2 additions & 17 deletions libbeat/licenser/elastic_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"encoding/json"
"errors"
"fmt"
"math/rand"
"net/http"

"github.com/elastic/beats/v7/libbeat/esleg/eslegclient"
Expand Down Expand Up @@ -99,6 +98,7 @@ func (f *ElasticFetcher) parseJSON(b []byte) (License, error) {

// esClientMux is taking care of round robin request over an array of elasticsearch client, note that
// calling request is not threadsafe.
// nolint: unused // it's used on Linux
type esClientMux struct {
clients []eslegclient.Connection
idx int
Expand All @@ -108,6 +108,7 @@ type esClientMux struct {
// at the end of the function call, if an error occur we return the error and will pick up the next client on the
// next call. Not that we just round robin between hosts, any backoff strategy should be handled by
// the consumer of this type.
// nolint: unused // it's used on Linux
func (mux *esClientMux) Request(
method, path string,
pipeline string,
Expand All @@ -130,19 +131,3 @@ func (mux *esClientMux) Request(
}
return status, response, err
}

// newESClientMux takes a list of clients and randomize where we start and the list of host we are
// querying.
func newESClientMux(clients []eslegclient.Connection) *esClientMux {
// randomize where we start
idx := rand.Intn(len(clients))

// randomize the list of round robin hosts.
tmp := make([]eslegclient.Connection, len(clients))
copy(tmp, clients)
rand.Shuffle(len(tmp), func(i, j int) {
tmp[i], tmp[j] = tmp[j], tmp[i]
})

return &esClientMux{idx: idx, clients: tmp}
}
17 changes: 9 additions & 8 deletions libbeat/licenser/elastic_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package licenser

import (
"context"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand All @@ -33,7 +32,7 @@ import (

func newServerClientPair(t *testing.T, handler http.HandlerFunc) (*httptest.Server, *eslegclient.Connection) {
mux := http.NewServeMux()
mux.Handle("/_license/", http.HandlerFunc(handler))
mux.Handle("/_license/", handler)

server := httptest.NewServer(mux)

Expand All @@ -46,15 +45,17 @@ func newServerClientPair(t *testing.T, handler http.HandlerFunc) (*httptest.Serv

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
client.Connect(ctx)
if err := client.Connect(ctx); err != nil {
t.Fatalf("cannot connect to ES: %s", err)
}

return server, client
}

func TestParseJSON(t *testing.T) {
t.Run("OSS release of Elasticsearch (Code: 405)", func(t *testing.T) {
h := func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Method Not Allowed", 405)
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
}
s, c := newServerClientPair(t, h)
defer s.Close()
Expand All @@ -80,7 +81,7 @@ func TestParseJSON(t *testing.T) {

t.Run("malformed JSON", func(t *testing.T) {
h := func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello bad JSON"))
_, _ = w.Write([]byte("hello bad JSON"))
}
s, c := newServerClientPair(t, h)
defer s.Close()
Expand All @@ -93,7 +94,7 @@ func TestParseJSON(t *testing.T) {

t.Run("401 response", func(t *testing.T) {
h := func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Unauthorized", 401)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
}
s, c := newServerClientPair(t, h)
defer s.Close()
Expand All @@ -118,14 +119,14 @@ func TestParseJSON(t *testing.T) {
})

t.Run("200 response", func(t *testing.T) {
filepath.Walk("testdata/", func(path string, i os.FileInfo, err error) error {
_ = filepath.Walk("testdata/", func(path string, i os.FileInfo, err error) error {
if i.IsDir() {
return nil
}

t.Run(path, func(t *testing.T) {
h := func(w http.ResponseWriter, r *http.Request) {
json, err := ioutil.ReadFile(path)
json, err := os.ReadFile(path)
if err != nil {
t.Fatal("could not read JSON")
}
Expand Down

0 comments on commit b7b49b6

Please sign in to comment.