Skip to content

Commit

Permalink
Replace *core.Factory with CoreFactory interface
Browse files Browse the repository at this point in the history
Make this field an interface instead of pointer to allow mocking. Not sure why wrangler has a type that returns an interface instead of just making it an interface itself. Wrangler in general is hard to mock for testing.

Signed-off-by: Brad Davidson <[email protected]>
  • Loading branch information
brandond committed Dec 18, 2024
1 parent 0ca3cae commit b41f2c6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
10 changes: 7 additions & 3 deletions pkg/cluster/https.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,13 @@ func (c *Cluster) initClusterAndHTTPS(ctx context.Context) error {
func tlsStorage(ctx context.Context, dataDir string, runtime *config.ControlRuntime) dynamiclistener.TLSStorage {
fileStorage := file.New(filepath.Join(dataDir, "tls/dynamic-cert.json"))
cache := memory.NewBacked(fileStorage)
return kubernetes.New(ctx, func() *core.Factory {
return runtime.Core
}, metav1.NamespaceSystem, version.Program+"-serving", cache)
coreGetter := func() *core.Factory {
if coreFactory, ok := runtime.Core.(*core.Factory); ok {
return coreFactory
}
return nil
}
return kubernetes.New(ctx, coreGetter, metav1.NamespaceSystem, version.Program+"-serving", cache)
}

// wrapHandler wraps the dynamiclistener request handler, adding a User-Agent value to
Expand Down
9 changes: 8 additions & 1 deletion pkg/daemons/config/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"context"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -372,11 +373,17 @@ type ControlRuntime struct {

K8s kubernetes.Interface
K3s *k3s.Factory
Core *core.Factory
Core CoreFactory
Event record.EventRecorder
EtcdConfig endpoint.ETCDConfig
}

type CoreFactory interface {
Core() core.Interface
Sync(ctx context.Context) error
Start(ctx context.Context, defaultThreadiness int) error
}

func NewRuntime(containerRuntimeReady <-chan struct{}) *ControlRuntime {
return &ControlRuntime{
ContainerRuntimeReady: containerRuntimeReady,
Expand Down
28 changes: 28 additions & 0 deletions tests/mock/core.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package mock

import (
"context"

"github.com/golang/mock/gomock"
"github.com/k3s-io/k3s/pkg/daemons/config"
"github.com/rancher/wrangler/v3/pkg/generated/controllers/core"
corev1 "github.com/rancher/wrangler/v3/pkg/generated/controllers/core/v1"
"github.com/rancher/wrangler/v3/pkg/generic/fake"
Expand All @@ -16,6 +19,31 @@ import (
// Mocks so that we can call Runtime.Core.Core().V1() without a functioning apiserver
//

// explicit interface check for core factory mock
var _ config.CoreFactory = &CoreFactoryMock{}

type CoreFactoryMock struct {
CoreMock *CoreMock
}

func NewCoreFactory(c *gomock.Controller) *CoreFactoryMock {
return &CoreFactoryMock{
CoreMock: NewCore(c),
}
}

func (m *CoreFactoryMock) Core() core.Interface {
return m.CoreMock
}

func (m *CoreFactoryMock) Sync(ctx context.Context) error {
return nil
}

func (m *CoreFactoryMock) Start(ctx context.Context, defaultThreadiness int) error {
return nil
}

// explicit interface check for core mock
var _ core.Interface = &CoreMock{}

Expand Down

0 comments on commit b41f2c6

Please sign in to comment.