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

test: add ability to mock the Docker client #287

Merged
merged 3 commits into from
Nov 25, 2024

Conversation

rickstaa
Copy link
Member

This pull request adds the ability to mock the docker.client so that we can perform tests on the methods in the docker.go file.

This commit adds the ability to mock the docker.client so that we can
perform tests on the methods in the `docker.go` file.
@rickstaa rickstaa marked this pull request as draft November 22, 2024 20:54
Copy link
Member

@victorges victorges left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat!

worker/docker.go Outdated Show resolved Hide resolved
worker/docker.go Show resolved Hide resolved
mock.Mock
}

func (m *MockDockerClient) ImagePull(ctx context.Context, ref string, options image.PullOptions) (io.ReadCloser, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FTR to reduce the boilerplate here, we can also only add to the interface the exact functions that we need from the client. Then we can mock them on a need-by-need basiss

Copy link
Member Author

@rickstaa rickstaa Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @victorges,

I’ve ensured that all tests related to [this branch](https://github.com/livepeer/ai-worker/tree/auto_pull_containers) have been implemented based on your changes. Please take a look, and feel free to make any adjustments if you see room for improvement.

I also attempted to remove the [ContainerInspect](https://github.com/livepeer/ai-worker/blob/72bca183d51b4437914ec31e994039298a0204d2/worker/docker_test.go#L46) mock method since it’s not being used in the tests. However, doing so caused the following error:

cannot use mockDockerClient (variable of type *MockDockerClient) as DockerClient value in struct literal: *MockDockerClient does not implement DockerClient (missing method ContainerInspect)

Let me know if you have any suggestions on how to address this, or if there’s a better way to handle it.

Thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really curious that it would complain after removing the function from the interface. Let's leave it anyway since I'm using it in some of the latest changes I made to ai-worker for realtime containers.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea we use it already here. However, this function is not part of the Docker Manager but is separate. Ideally, it should be removed from the manager and mocked independently.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you already use it in the Manager for realtime let's keep it as is.

This commit renames the docker client interface so that it is more clear
what it does.
This commit ensures the docker.warm and docker.hasCapacity methods are
tested correctly. It also cleans up the earlier added tests.
@rickstaa rickstaa marked this pull request as ready for review November 25, 2024 21:43
Copy link
Member

@victorges victorges left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

mock.Mock
}

func (m *MockDockerClient) ImagePull(ctx context.Context, ref string, options image.PullOptions) (io.ReadCloser, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really curious that it would complain after removing the function from the interface. Let's leave it anyway since I'm using it in some of the latest changes I made to ai-worker for realtime containers.

@rickstaa rickstaa merged commit f68df27 into auto_pull_containers Nov 25, 2024
1 check passed
@rickstaa rickstaa deleted the auto_pull_containers_mock_tests branch November 25, 2024 21:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants