Commit 93dd57ab authored by Brad Davidson's avatar Brad Davidson Committed by Brad Davidson

Rework mock executor using gomock for call validation

Generate the mock executor with mockgen and convert existing uses of the mock executor to set it up properly. Signed-off-by: 's avatarBrad Davidson <brad.davidson@rancher.com> (cherry picked from commit 0eeac6a6) Signed-off-by: 's avatarBrad Davidson <brad.davidson@rancher.com>
parent a524556b
...@@ -19,6 +19,7 @@ import ( ...@@ -19,6 +19,7 @@ import (
"github.com/k3s-io/k3s/pkg/daemons/config" "github.com/k3s-io/k3s/pkg/daemons/config"
"github.com/k3s-io/k3s/pkg/etcd/s3" "github.com/k3s-io/k3s/pkg/etcd/s3"
testutil "github.com/k3s-io/k3s/tests" testutil "github.com/k3s-io/k3s/tests"
"github.com/k3s-io/k3s/tests/mock"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"go.etcd.io/etcd/api/v3/etcdserverpb" "go.etcd.io/etcd/api/v3/etcdserverpb"
...@@ -137,7 +138,9 @@ func Test_UnitETCD_IsInitialized(t *testing.T) { ...@@ -137,7 +138,9 @@ func Test_UnitETCD_IsInitialized(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
mock.NewExecutorWithEmbeddedETCD(t)
e := NewETCD() e := NewETCD()
defer tt.teardown(tt.args.config) defer tt.teardown(tt.args.config)
if err := tt.setup(tt.args.config); err != nil { if err := tt.setup(tt.args.config); err != nil {
t.Errorf("Prep for ETCD.IsInitialized() failed = %v", err) t.Errorf("Prep for ETCD.IsInitialized() failed = %v", err)
...@@ -215,6 +218,7 @@ func Test_UnitETCD_Register(t *testing.T) { ...@@ -215,6 +218,7 @@ func Test_UnitETCD_Register(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
mock.NewExecutorWithEmbeddedETCD(t)
e := NewETCD() e := NewETCD()
defer tt.teardown(tt.args.config) defer tt.teardown(tt.args.config)
...@@ -389,6 +393,7 @@ func Test_UnitETCD_Start(t *testing.T) { ...@@ -389,6 +393,7 @@ func Test_UnitETCD_Start(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
mock.NewExecutorWithEmbeddedETCD(t)
e := &ETCD{ e := &ETCD{
client: tt.fields.client, client: tt.fields.client,
config: tt.fields.config, config: tt.fields.config,
...@@ -628,6 +633,7 @@ func Test_UnitETCD_Test(t *testing.T) { ...@@ -628,6 +633,7 @@ func Test_UnitETCD_Test(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
mock.NewExecutorWithEmbeddedETCD(t)
e := &ETCD{ e := &ETCD{
client: tt.fields.client, client: tt.fields.client,
config: tt.fields.config, config: tt.fields.config,
......
package mock
import (
"testing"
executor "github.com/k3s-io/k3s/pkg/daemons/executor"
"go.uber.org/mock/gomock"
)
// NewExecutorWithEmbeddedETCD creates a new mock executor, and sets it as the current executor.
// The executor exepects calls to ETCD(), and wraps the embedded executor method of the same name.
// The various ready channels are also mocked with immediate channel closure.
func NewExecutorWithEmbeddedETCD(t *testing.T) {
mockController := gomock.NewController(t)
mockExecutor := NewExecutor(mockController)
embed := &executor.Embedded{}
initialOptions := func() (executor.InitialOptions, error) {
return executor.InitialOptions{}, nil
}
closedChannel := func() <-chan struct{} {
c := make(chan struct{})
close(c)
return c
}
mockExecutor.EXPECT().ETCD(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn(embed.ETCD)
mockExecutor.EXPECT().CurrentETCDOptions().AnyTimes().DoAndReturn(initialOptions)
mockExecutor.EXPECT().CRIReadyChan().AnyTimes().DoAndReturn(closedChannel)
mockExecutor.EXPECT().ETCDReadyChan().AnyTimes().DoAndReturn(closedChannel)
executor.Set(mockExecutor)
}
...@@ -7,8 +7,6 @@ import ( ...@@ -7,8 +7,6 @@ import (
"github.com/k3s-io/k3s/pkg/daemons/config" "github.com/k3s-io/k3s/pkg/daemons/config"
"github.com/k3s-io/k3s/pkg/daemons/control/deps" "github.com/k3s-io/k3s/pkg/daemons/control/deps"
"github.com/k3s-io/k3s/pkg/daemons/executor"
"github.com/k3s-io/k3s/tests/mock"
) )
// GenerateDataDir creates a temporary directory at "/tmp/k3s/<RANDOM_STRING>/". // GenerateDataDir creates a temporary directory at "/tmp/k3s/<RANDOM_STRING>/".
...@@ -45,9 +43,6 @@ func CleanupDataDir(cnf *config.Control) { ...@@ -45,9 +43,6 @@ func CleanupDataDir(cnf *config.Control) {
// GenerateRuntime creates a temporary data dir and configures // GenerateRuntime creates a temporary data dir and configures
// config.ControlRuntime with all the appropriate certificate keys. // config.ControlRuntime with all the appropriate certificate keys.
func GenerateRuntime(cnf *config.Control) error { func GenerateRuntime(cnf *config.Control) error {
// use mock executor that does not actually start things
executor.Set(&mock.Executor{})
// reuse ready channel from existing runtime if set // reuse ready channel from existing runtime if set
cnf.Runtime = config.NewRuntime() cnf.Runtime = config.NewRuntime()
if err := GenerateDataDir(cnf); err != nil { if err := GenerateDataDir(cnf); err != nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment