Skip to content

Commit

Permalink
chore: small usability fixes
Browse files Browse the repository at this point in the history
* Replace logging.Wrap(log.Writer()) with zaptest.NewLogger(suite.T()) where possible.
* Replace reflect.DeepEqual with =|slices.Equal|bytes.Equal where possible.

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
  • Loading branch information
DmitriyMV committed Jun 10, 2024
1 parent 26cf566 commit aca475c
Show file tree
Hide file tree
Showing 64 changed files with 157 additions and 223 deletions.
4 changes: 2 additions & 2 deletions internal/app/apid/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"fmt"
"log"
"os/signal"
"reflect"
"regexp"
"slices"
"syscall"
"time"

Expand Down Expand Up @@ -260,7 +260,7 @@ func verifyExtKeyUsage(rawCerts [][]byte, verifiedChains [][]*x509.Certificate)
continue
}

if !reflect.DeepEqual(cert.ExtKeyUsage, []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}) {
if !slices.Equal(cert.ExtKeyUsage, []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}) {
return fmt.Errorf("certificate %q is missing the client auth extended key usage", cert.Subject)
}
}
Expand Down
8 changes: 2 additions & 6 deletions internal/app/machined/pkg/controllers/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cluster_test

import (
"context"
"log"
"sync"
"time"

Expand All @@ -17,8 +16,7 @@ import (
"github.com/cosi-project/runtime/pkg/state/impl/namespaced"
"github.com/siderolabs/go-retry/retry"
"github.com/stretchr/testify/suite"

"github.com/siderolabs/talos/pkg/logging"
"go.uber.org/zap/zaptest"
)

type ClusterSuite struct {
Expand All @@ -40,9 +38,7 @@ func (suite *ClusterSuite) SetupTest() {

var err error

logger := logging.Wrap(log.Writer())

suite.runtime, err = runtime.NewRuntime(suite.state, logger)
suite.runtime, err = runtime.NewRuntime(suite.state, zaptest.NewLogger(suite.T()))
suite.Require().NoError(err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"crypto/rand"
"encoding/base64"
"io"
"log"
"net/netip"
"net/url"
"testing"
Expand All @@ -22,11 +21,11 @@ import (
"github.com/siderolabs/go-retry/retry"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"go.uber.org/zap/zaptest"

clusteradapter "github.com/siderolabs/talos/internal/app/machined/pkg/adapters/cluster"
clusterctrl "github.com/siderolabs/talos/internal/app/machined/pkg/controllers/cluster"
"github.com/siderolabs/talos/internal/app/machined/pkg/controllers/ctest"
"github.com/siderolabs/talos/pkg/logging"
"github.com/siderolabs/talos/pkg/machinery/config/machine"
"github.com/siderolabs/talos/pkg/machinery/constants"
"github.com/siderolabs/talos/pkg/machinery/proto"
Expand Down Expand Up @@ -113,7 +112,7 @@ func (suite *DiscoveryServiceSuite) TestReconcile() {
defer cliCtxCancel()

go func() {
errCh <- cli.Run(cliCtx, logging.Wrap(log.Writer()), notifyCh)
errCh <- cli.Run(cliCtx, zaptest.NewLogger(suite.T()), notifyCh)
}()

suite.Assert().NoError(retry.Constant(3*time.Second, retry.WithUnits(100*time.Millisecond)).Retry(
Expand Down Expand Up @@ -328,7 +327,7 @@ func (suite *DiscoveryServiceSuite) TestDisable() {
defer cliCtxCancel()

go func() {
errCh <- cli.Run(cliCtx, logging.Wrap(log.Writer()), notifyCh)
errCh <- cli.Run(cliCtx, zaptest.NewLogger(suite.T()), notifyCh)
}()

// inject some affiliate via our client, controller should publish it as an affiliate
Expand Down
4 changes: 2 additions & 2 deletions internal/app/machined/pkg/controllers/cluster/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"context"
"fmt"
"net/netip"
"reflect"
"slices"
"sort"

"github.com/cosi-project/runtime/pkg/controller"
Expand Down Expand Up @@ -82,7 +82,7 @@ func (ctrl *EndpointController) Run(ctx context.Context, r controller.Runtime, l
r,
k8s.NewEndpoint(k8s.ControlPlaneNamespaceName, k8s.ControlPlaneDiscoveredEndpointsID),
func(r *k8s.Endpoint) error {
if !reflect.DeepEqual(r.TypedSpec().Addresses, endpoints) {
if !slices.Equal(r.TypedSpec().Addresses, endpoints) {
logger.Debug("updated controlplane endpoints", zap.Any("endpoints", endpoints))
}

Expand Down
4 changes: 2 additions & 2 deletions internal/app/machined/pkg/controllers/etcd/advertised_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"errors"
"fmt"
"net/netip"
"reflect"
"slices"
"time"

"github.com/cosi-project/runtime/pkg/controller"
Expand Down Expand Up @@ -153,7 +153,7 @@ func (ctrl *AdvertisedPeerController) updateAdvertisedPeers(ctx context.Context,
})
currentPeerURLs := localMember.PeerURLs

if reflect.DeepEqual(newPeerURLs, currentPeerURLs) {
if slices.Equal(newPeerURLs, currentPeerURLs) {
return nil
}

Expand Down
5 changes: 2 additions & 3 deletions internal/app/machined/pkg/controllers/files/etcfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package files_test

import (
"context"
"log"
"os"
"path/filepath"
"strconv"
Expand All @@ -22,9 +21,9 @@ import (
"github.com/cosi-project/runtime/pkg/state/impl/namespaced"
"github.com/siderolabs/go-retry/retry"
"github.com/stretchr/testify/suite"
"go.uber.org/zap/zaptest"

filesctrl "github.com/siderolabs/talos/internal/app/machined/pkg/controllers/files"
"github.com/siderolabs/talos/pkg/logging"
"github.com/siderolabs/talos/pkg/machinery/resources/files"
)

Expand All @@ -50,7 +49,7 @@ func (suite *EtcFileSuite) SetupTest() {

var err error

suite.runtime, err = runtime.NewRuntime(suite.state, logging.Wrap(log.Writer()))
suite.runtime, err = runtime.NewRuntime(suite.state, zaptest.NewLogger(suite.T()))
suite.Require().NoError(err)

suite.startRuntime()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package hardware_test

import (
"context"
"log"
"sync"
"time"

Expand All @@ -17,8 +16,7 @@ import (
"github.com/cosi-project/runtime/pkg/state/impl/namespaced"
"github.com/siderolabs/go-retry/retry"
"github.com/stretchr/testify/suite"

"github.com/siderolabs/talos/pkg/logging"
"go.uber.org/zap/zaptest"
)

type HardwareSuite struct {
Expand All @@ -40,9 +38,7 @@ func (suite *HardwareSuite) SetupTest() {

var err error

logger := logging.Wrap(log.Writer())

suite.runtime, err = runtime.NewRuntime(suite.state, logger)
suite.runtime, err = runtime.NewRuntime(suite.state, zaptest.NewLogger(suite.T()))
suite.Require().NoError(err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package k8s_test
import (
"context"
"fmt"
"log"
"net/url"
"sync"
"testing"
Expand All @@ -21,9 +20,9 @@ import (
"github.com/cosi-project/runtime/pkg/state/impl/namespaced"
"github.com/siderolabs/go-retry/retry"
"github.com/stretchr/testify/suite"
"go.uber.org/zap/zaptest"

k8sctrl "github.com/siderolabs/talos/internal/app/machined/pkg/controllers/k8s"
"github.com/siderolabs/talos/pkg/logging"
"github.com/siderolabs/talos/pkg/machinery/config/container"
"github.com/siderolabs/talos/pkg/machinery/config/types/v1alpha1"
"github.com/siderolabs/talos/pkg/machinery/resources/config"
Expand Down Expand Up @@ -51,7 +50,7 @@ func (suite *K8sAddressFilterSuite) SetupTest() {

var err error

suite.runtime, err = runtime.NewRuntime(suite.state, logging.Wrap(log.Writer()))
suite.runtime, err = runtime.NewRuntime(suite.state, zaptest.NewLogger(suite.T()))
suite.Require().NoError(err)

suite.Require().NoError(suite.runtime.RegisterController(&k8sctrl.AddressFilterController{}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ package k8s_test
import (
"context"
"fmt"
"log"
"reflect"
"slices"
"strconv"
"strings"
"sync"
Expand All @@ -23,13 +22,13 @@ import (
"github.com/siderolabs/gen/xslices"
"github.com/siderolabs/go-retry/retry"
"github.com/stretchr/testify/suite"
"go.uber.org/zap/zaptest"
v1 "k8s.io/api/core/v1"
apiresource "k8s.io/apimachinery/pkg/api/resource"

k8sadapter "github.com/siderolabs/talos/internal/app/machined/pkg/adapters/k8s"
"github.com/siderolabs/talos/internal/app/machined/pkg/controllers/ctest"
k8sctrl "github.com/siderolabs/talos/internal/app/machined/pkg/controllers/k8s"
"github.com/siderolabs/talos/pkg/logging"
"github.com/siderolabs/talos/pkg/machinery/constants"
"github.com/siderolabs/talos/pkg/machinery/resources/k8s"
"github.com/siderolabs/talos/pkg/machinery/resources/v1alpha1"
Expand All @@ -54,7 +53,7 @@ func (suite *ControlPlaneStaticPodSuite) SetupTest() {

var err error

suite.runtime, err = runtime.NewRuntime(suite.state, logging.Wrap(log.Writer()))
suite.runtime, err = runtime.NewRuntime(suite.state, zaptest.NewLogger(suite.T()))
suite.Require().NoError(err)

suite.Require().NoError(suite.runtime.RegisterController(&k8sctrl.ControlPlaneStaticPodController{}))
Expand Down Expand Up @@ -93,7 +92,7 @@ func (suite *ControlPlaneStaticPodSuite) assertControlPlaneStaticPods(manifests

ids := xslices.Map(resources.Items, func(r resource.Resource) string { return r.Metadata().ID() })

if !reflect.DeepEqual(manifests, ids) {
if !slices.Equal(manifests, ids) {
return retry.ExpectedErrorf("expected %q, got %q", manifests, ids)
}

Expand Down
3 changes: 1 addition & 2 deletions internal/app/machined/pkg/controllers/k8s/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"fmt"
"net/netip"
"reflect"
"slices"
"time"

Expand Down Expand Up @@ -213,7 +212,7 @@ func (ctrl *EndpointController) updateEndpointsResource(ctx context.Context, r c
r,
k8s.NewEndpoint(k8s.ControlPlaneNamespaceName, k8s.ControlPlaneAPIServerEndpointsID),
func(r *k8s.Endpoint) error {
if !reflect.DeepEqual(r.TypedSpec().Addresses, addrs) {
if !slices.Equal(r.TypedSpec().Addresses, addrs) {
logger.Debug("updated controlplane endpoints", zap.Any("endpoints", addrs))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ package k8s_test

import (
"context"
"log"
"reflect"
"slices"
"strings"
"sync"
"testing"
Expand All @@ -22,10 +21,10 @@ import (
"github.com/siderolabs/gen/xslices"
"github.com/siderolabs/go-retry/retry"
"github.com/stretchr/testify/suite"
"go.uber.org/zap/zaptest"

k8sadapter "github.com/siderolabs/talos/internal/app/machined/pkg/adapters/k8s"
k8sctrl "github.com/siderolabs/talos/internal/app/machined/pkg/controllers/k8s"
"github.com/siderolabs/talos/pkg/logging"
"github.com/siderolabs/talos/pkg/machinery/resources/k8s"
"github.com/siderolabs/talos/pkg/machinery/resources/network"
)
Expand All @@ -49,7 +48,7 @@ func (suite *ExtraManifestSuite) SetupTest() {

var err error

suite.runtime, err = runtime.NewRuntime(suite.state, logging.Wrap(log.Writer()))
suite.runtime, err = runtime.NewRuntime(suite.state, zaptest.NewLogger(suite.T()))
suite.Require().NoError(err)

suite.Require().NoError(suite.runtime.RegisterController(&k8sctrl.ExtraManifestController{}))
Expand Down Expand Up @@ -79,7 +78,7 @@ func (suite *ExtraManifestSuite) assertExtraManifests(manifests []string) error

ids := xslices.Map(resources.Items, func(r resource.Resource) string { return r.Metadata().ID() })

if !reflect.DeepEqual(manifests, ids) {
if !slices.Equal(manifests, ids) {
return retry.ExpectedErrorf("expected %q, got %q", manifests, ids)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package k8s_test

import (
"context"
"log"
"net/url"
"sync"
"testing"
Expand All @@ -22,9 +21,9 @@ import (
"github.com/siderolabs/go-pointer"
"github.com/siderolabs/go-retry/retry"
"github.com/stretchr/testify/suite"
"go.uber.org/zap/zaptest"

k8sctrl "github.com/siderolabs/talos/internal/app/machined/pkg/controllers/k8s"
"github.com/siderolabs/talos/pkg/logging"
"github.com/siderolabs/talos/pkg/machinery/config/container"
"github.com/siderolabs/talos/pkg/machinery/config/types/v1alpha1"
"github.com/siderolabs/talos/pkg/machinery/constants"
Expand All @@ -51,7 +50,7 @@ func (suite *KubeletConfigSuite) SetupTest() {

var err error

suite.runtime, err = runtime.NewRuntime(suite.state, logging.Wrap(log.Writer()))
suite.runtime, err = runtime.NewRuntime(suite.state, zaptest.NewLogger(suite.T()))
suite.Require().NoError(err)

suite.Require().NoError(suite.runtime.RegisterController(k8sctrl.NewKubeletConfigController()))
Expand Down
9 changes: 4 additions & 5 deletions internal/app/machined/pkg/controllers/k8s/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ package k8s_test
import (
"context"
"fmt"
"log"
"reflect"
"slices"
"sync"
"testing"
"time"
Expand All @@ -22,11 +21,11 @@ import (
"github.com/siderolabs/gen/xslices"
"github.com/siderolabs/go-retry/retry"
"github.com/stretchr/testify/suite"
"go.uber.org/zap/zaptest"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

k8sadapter "github.com/siderolabs/talos/internal/app/machined/pkg/adapters/k8s"
k8sctrl "github.com/siderolabs/talos/internal/app/machined/pkg/controllers/k8s"
"github.com/siderolabs/talos/pkg/logging"
"github.com/siderolabs/talos/pkg/machinery/constants"
"github.com/siderolabs/talos/pkg/machinery/resources/k8s"
"github.com/siderolabs/talos/pkg/machinery/resources/secrets"
Expand All @@ -51,7 +50,7 @@ func (suite *ManifestSuite) SetupTest() {

var err error

suite.runtime, err = runtime.NewRuntime(suite.state, logging.Wrap(log.Writer()))
suite.runtime, err = runtime.NewRuntime(suite.state, zaptest.NewLogger(suite.T()))
suite.Require().NoError(err)

suite.Require().NoError(suite.runtime.RegisterController(&k8sctrl.ManifestController{}))
Expand Down Expand Up @@ -81,7 +80,7 @@ func (suite *ManifestSuite) assertManifests(manifests []string) error {

ids := xslices.Map(resources.Items, func(r resource.Resource) string { return r.Metadata().ID() })

if !reflect.DeepEqual(manifests, ids) {
if !slices.Equal(manifests, ids) {
return retry.ExpectedErrorf("expected %q, got %q", manifests, ids)
}

Expand Down
Loading

0 comments on commit aca475c

Please sign in to comment.