Skip to content

Commit

Permalink
Fix lint - funlen - thelper - perfsprint - godox - goconst (#72)
Browse files Browse the repository at this point in the history
* Helper

* perfsprint

* godox

* goconst

* int-config-uri
  • Loading branch information
the1bit authored Feb 1, 2024
1 parent aec50fa commit 7f37300
Show file tree
Hide file tree
Showing 15 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ jobs:
uses: kyma-project/eventing-tools/.github/workflows/lint-go-reusable.yml@main
with:
go-version: '1.21'
lint-config-uri: https://raw.githubusercontent.com/kyma-project/eventing-tools/cf7f3aebaad2d323730a9ab2a76f6cb78f459ee4/config/lint/.golangci.yaml
lint-config-uri: https://raw.githubusercontent.com/kyma-project/eventing-tools/a9cc2c5524838736f3f8fd084021a0116675476d/config/lint/.golangci.yaml
2 changes: 1 addition & 1 deletion pkg/commander/eventmesh/eventmesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/kyma-project/eventing-manager/pkg/backend/cleaner"
"github.com/kyma-project/eventing-manager/pkg/logger"

_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // TODO: remove as this is only used in a development setup
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // IMORTANT: remove as this is only used in a development setup
)

const (
Expand Down
2 changes: 1 addition & 1 deletion pkg/commander/nats/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/kyma-project/eventing-manager/pkg/backend/cleaner"
"github.com/kyma-project/eventing-manager/pkg/logger"

_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // TODO: remove as this is only required in a dev setup
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // IMPORTANT: remove as this is only required in a dev setup
)

const (
Expand Down
1 change: 1 addition & 0 deletions pkg/handler/handler_v1alpha1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ func TestHandler_sendEventAndRecordMetrics_TracingAndDefaults(t *testing.T) {
}

func CreateCloudEvent(t *testing.T) *ceevent.Event {
t.Helper()
builder := epptestingutils.NewCloudEventBuilder(
epptestingutils.WithCloudEventType(epptestingutils.CloudEventTypeWithPrefix),
)
Expand Down
2 changes: 2 additions & 0 deletions pkg/handler/health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func TestChecker(t *testing.T) {
}

func assertResponseLivenessStatusCode(t *testing.T, endpoint string, checker *ConfigurableChecker, statusCode int) {
t.Helper()
writer := httptest.NewRecorder()
request := httptest.NewRequest(http.MethodGet, endpoint, nil)

Expand All @@ -107,6 +108,7 @@ func assertResponseLivenessStatusCode(t *testing.T, endpoint string, checker *Co
}

func assertResponseReadinessStatusCode(t *testing.T, endpoint string, checker *ConfigurableChecker, statusCode int) {
t.Helper()
writer := httptest.NewRecorder()
request := httptest.NewRequest(http.MethodGet, endpoint, nil)

Expand Down
1 change: 1 addition & 0 deletions pkg/legacy/legacytest/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func ValidLegacyRequest(version, appname, eventType string) (*http.Request, erro
}

func InvalidLegacyRequestOrDie(t *testing.T, version, appname, eventType string) *http.Request {
t.Helper()
r, err := InvalidLegacyRequest(version, appname, eventType)
assert.NoError(t, err)
return r
Expand Down
6 changes: 3 additions & 3 deletions pkg/metrics/collector.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package metrics

import (
"fmt"
"net/http"
"strconv"
"time"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -143,7 +143,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {

// RecordLatency records a backendLatencyHelp metric.
func (c *Collector) RecordBackendLatency(duration time.Duration, statusCode int, destSvc string) {
c.backendLatency.WithLabelValues(fmt.Sprint(statusCode), destSvc).Observe(float64(duration.Milliseconds()))
c.backendLatency.WithLabelValues(strconv.Itoa(statusCode), destSvc).Observe(float64(duration.Milliseconds()))
}

// SetHealthStatus updates the health metric.
Expand All @@ -157,7 +157,7 @@ func (c *Collector) SetHealthStatus(healthy bool) {

// RecordEventType records an eventType metric.
func (c *Collector) RecordEventType(eventType, eventSource string, statusCode int) {
c.eventType.WithLabelValues(eventType, eventSource, fmt.Sprint(statusCode)).Inc()
c.eventType.WithLabelValues(eventType, eventSource, strconv.Itoa(statusCode)).Inc()
}

// MetricsMiddleware returns a http.Handler that can be used as middleware in gorilla.mux to track
Expand Down
4 changes: 4 additions & 0 deletions pkg/metrics/metricstest/metricstest.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ import (

// EnsureMetricLatency ensures metric eventing_epp_backend_duration_seconds exists.
func EnsureMetricLatency(t *testing.T, collector metrics.PublishingMetricsCollector, count int) {
t.Helper()
ensureMetricCount(t, collector, metrics.BackendLatencyKey, count)
}

// EnsureMetricEventTypePublished ensures metric eventing_epp_event_type_published_total exists.
func EnsureMetricEventTypePublished(t *testing.T, collector metrics.PublishingMetricsCollector, count int) {
t.Helper()
ensureMetricCount(t, collector, metrics.EventTypePublishedMetricKey, count)
}

func ensureMetricCount(t *testing.T, collector metrics.PublishingMetricsCollector, metric string, expectedCount int) {
t.Helper()
if count := testutil.CollectAndCount(collector, metric); count != expectedCount {
t.Fatalf("invalid count for metric:%s, want:%d, got:%d", metric, expectedCount, count)
}
Expand All @@ -34,6 +37,7 @@ func ensureMetricCount(t *testing.T, collector metrics.PublishingMetricsCollecto
func EnsureMetricMatchesTextExpositionFormat(t *testing.T, collector metrics.PublishingMetricsCollector,
tef string, metricNames ...string,
) {
t.Helper()
if err := testutil.CollectAndCompare(collector, strings.NewReader(tef), metricNames...); err != nil {
t.Fatalf("%v", err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/receiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestStartListener(t *testing.T) {
defer close(start)
wg.Add(1)
go func(t *testing.T) {
t.Helper()
defer wg.Done()
start <- true
t.Log("starting receiver in goroutine")
Expand Down
4 changes: 4 additions & 0 deletions pkg/sender/jetstream/jetstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ type TestEnvironment struct {

// setupTestEnvironment sets up the resources and mocks required for testing.
func setupTestEnvironment(t *testing.T) *TestEnvironment {
t.Helper()
natsServer := epptestingutils.StartNATSServer()
require.NotNil(t, natsServer)

Expand Down Expand Up @@ -141,6 +142,7 @@ func setupTestEnvironment(t *testing.T) *TestEnvironment {

// createCloudEvent build a cloud event.
func createCloudEvent(t *testing.T) *event.Event {
t.Helper()
jsType := fmt.Sprintf("%s.%s", epptestingutils.StreamName, epptestingutils.CloudEventTypeWithPrefix)
builder := epptestingutils.NewCloudEventBuilder(
epptestingutils.WithCloudEventType(jsType),
Expand Down Expand Up @@ -177,6 +179,7 @@ func getConsumerConfig() *natsgo.ConsumerConfig {

// addStream creates a stream for the test events.
func addStream(t *testing.T, connection *natsgo.Conn, config *natsgo.StreamConfig) {
t.Helper()
js, err := connection.JetStream()
assert.NoError(t, err)
info, err := js.AddStream(config)
Expand All @@ -185,6 +188,7 @@ func addStream(t *testing.T, connection *natsgo.Conn, config *natsgo.StreamConfi
}

func addConsumer(t *testing.T, connection *natsgo.Conn, sc *natsgo.StreamConfig, config *natsgo.ConsumerConfig) {
t.Helper()
js, err := connection.JetStream()
assert.NoError(t, err)
info, err := js.AddConsumer(sc.Name, config)
Expand Down
2 changes: 1 addition & 1 deletion pkg/subscribed/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func FilterEventTypeVersionsV1alpha1(eventTypePrefix, bebNs, appName string,
continue
}

// TODO revisit the filtration logic as part of https://github.com/kyma-project/kyma/issues/10761
// IMPORTANT revisit the filtration logic as part of https://github.com/kyma-project/kyma/issues/10761

// filter by event-source if exists
if len(strings.TrimSpace(filter.EventSource.Value)) > 0 && !strings.EqualFold(filter.EventSource.Value, bebNs) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/subscribed/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,16 +352,16 @@ func NewEventMeshFilters(opts ...EventMeshFilterOption) *emeventingv1alpha1.BEBF
}

func WithOneEventMeshFilter(filters *emeventingv1alpha1.BEBFilters) {
evSource := "/default/foo.kyma/kt1"
evType := "foo.prefix.custom.foovarkes.order.created.v1"
const evSource = "/default/foo.kyma/kt1"
const evType = "foo.prefix.custom.foovarkes.order.created.v1"
filters.Filters = []*emeventingv1alpha1.EventMeshFilter{
NewEventMeshFilter(evSource, evType),
}
}

func WithMultipleEventMeshFiltersFromSameSource(filters *emeventingv1alpha1.BEBFilters) {
evSource := "/default/foo.kyma/kt1"
evType := "foo.prefix.custom.foovarkes.order.created.v1"
const evSource = "/default/foo.kyma/kt1"
const evType = "foo.prefix.custom.foovarkes.order.created.v1"
filters.Filters = []*emeventingv1alpha1.EventMeshFilter{
NewEventMeshFilter(evSource, evType),
NewEventMeshFilter(evSource, evType),
Expand All @@ -374,7 +374,7 @@ func WithMultipleEventMeshFiltersFromDiffSource(filters *emeventingv1alpha1.BEBF
evSource2 := "/default/foo.different/kt1"
evSource3 := "/default/foo.different2/kt1"
evSource4 := ""
evType := "foo.prefix.custom.foovarkes.order.created.v1"
const evType = "foo.prefix.custom.foovarkes.order.created.v1"
filters.Filters = []*emeventingv1alpha1.EventMeshFilter{
NewEventMeshFilter(evSource1, evType),
NewEventMeshFilter(evSource2, evType),
Expand Down
3 changes: 1 addition & 2 deletions pkg/tracing/helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tracing

import (
"fmt"
"net/http"

ceevent "github.com/cloudevents/sdk-go/v2/event"
Expand All @@ -27,7 +26,7 @@ func AddTracingContextToCEExtensions(reqHeaders http.Header, event *ceevent.Even
traceParent := reqHeaders.Get(traceParentKey)
if len(traceParent) > 0 {
st := extensions.DistributedTracingExtension{
TraceParent: fmt.Sprintf("%v", traceParent),
TraceParent: traceParent,
}
st.AddTracingAttributes(event)
}
Expand Down
1 change: 1 addition & 0 deletions testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func (b *CloudEventBuilder) BuildStructured() (string, http.Header) {
}

func (b *CloudEventBuilder) Build(t *testing.T) *ceevent.Event {
t.Helper()
e := ceevent.New(b.specVersion)
assert.NoError(t, e.Context.SetID(b.id))
assert.NoError(t, e.Context.SetType(b.eventType))
Expand Down
1 change: 1 addition & 0 deletions testing/mock_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func WithValidator(validator Validator) MockServerOption {
}

func (m *MockServer) Start(t *testing.T, tokenEndpoint, eventsEndpoint, eventsWithHTTP400 string) {
t.Helper()
m.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(m.responseTime)

Expand Down

0 comments on commit 7f37300

Please sign in to comment.