Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(k8s): move test logic to MockKubernetesRuntime interface #395

Merged
merged 3 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion runtime/kubernetes/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ func TestKubernetes_StreamBuild(t *testing.T) {
cancel()
} else if test.doReady {
// simulate AssembleBuild
close(_engine.PodTracker.Ready)
_engine.MarkPodTrackerReady()
}
}()

Expand Down
5 changes: 1 addition & 4 deletions runtime/kubernetes/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,8 @@ func TestKubernetes_WaitContainer(t *testing.T) {
}

go func() {
oldPod := test.oldPod.DeepCopy()
oldPod.SetResourceVersion("older")

// simulate a re-sync/PodUpdate event
_engine.PodTracker.HandlePodUpdate(oldPod, _engine.Pod)
_engine.SimulateResync(test.oldPod)
}()

err = _engine.WaitContainer(context.Background(), test.container)
Expand Down
31 changes: 31 additions & 0 deletions runtime/kubernetes/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,34 @@ func NewMock(_pod *v1.Pod, opts ...ClientOpt) (*client, error) {

return c, nil
}

// MockKubernetesRuntime makes it possible to use the client mocks in other packages.
//
// This interface is intended for running tests only.
type MockKubernetesRuntime interface {
MarkPodTrackerReady()
SimulateResync(*v1.Pod)
}

// MarkPodTrackerReady signals that PodTracker has been setup with ContainerTrackers.
//
// This function is intended for running tests only.
func (c *client) MarkPodTrackerReady() {
close(c.PodTracker.Ready)
}

// SimulateResync simulates an resync where the PodTracker refreshes its cache.
// This resync is from oldPod to runtime.Pod. If nil, oldPod defaults to runtime.Pod.
//
// This function is intended for running tests only.
func (c *client) SimulateResync(oldPod *v1.Pod) {
if oldPod == nil {
oldPod = c.Pod
}

oldPod = oldPod.DeepCopy()
oldPod.SetResourceVersion("older")

// simulate a re-sync/PodUpdate event
c.PodTracker.HandlePodUpdate(oldPod, c.Pod)
}