This repository was archived by the owner on Oct 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Add mocks and UTs for docker model status
#20
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,38 @@ | ||
| package commands | ||
|
|
||
| import "github.com/spf13/cobra" | ||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/docker/docker/client" | ||
| "github.com/docker/model-cli/desktop" | ||
| "github.com/docker/pinata/common/pkg/engine" | ||
| "github.com/docker/pinata/common/pkg/paths" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func NewRootCmd() *cobra.Command { | ||
| rootCmd := &cobra.Command{ | ||
| Use: "model", | ||
| Short: "Docker Model Runner", | ||
| } | ||
| dockerClient, err := client.NewClientWithOpts( | ||
| // TODO: Make sure it works while running in Windows containers mode. | ||
| client.WithHost(paths.HostServiceSockets().DockerHost(engine.Linux)), | ||
| ) | ||
| if err != nil { | ||
| fmt.Println("Failed to create Docker client:", err) | ||
| os.Exit(1) | ||
| } | ||
| desktopClient := desktop.New(dockerClient.HTTPClient()) | ||
| rootCmd.AddCommand( | ||
| newVersionCmd(), | ||
| newStatusCmd(), | ||
| newPullCmd(), | ||
| newListCmd(), | ||
| newRunCmd(), | ||
| newRemoveCmd(), | ||
| newInspectCmd(), | ||
| newStatusCmd(desktopClient), | ||
| newPullCmd(desktopClient), | ||
| newListCmd(desktopClient), | ||
| newRunCmd(desktopClient), | ||
| newRemoveCmd(desktopClient), | ||
| newInspectCmd(desktopClient), | ||
| ) | ||
| return rootCmd | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,34 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/docker/cli/cli-plugins/hooks" | ||
| "github.com/docker/model-cli/desktop" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func newStatusCmd() *cobra.Command { | ||
| func newStatusCmd(desktopClient *desktop.Client) *cobra.Command { | ||
| c := &cobra.Command{ | ||
| Use: "status", | ||
| Short: "Check if the Docker Model Runner is running", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| client, err := desktop.New() | ||
| if err != nil { | ||
| return fmt.Errorf("Failed to create Docker client: %v\n", err) | ||
| } | ||
| status := client.Status() | ||
| status := desktopClient.Status() | ||
| if status.Error != nil { | ||
| return fmt.Errorf("Failed to get Docker Model Runner status: %v\n", err) | ||
| return handleClientError(status.Error, "Failed to get Docker Model Runner status") | ||
| } | ||
| if status.Running { | ||
| cmd.Println("Docker Model Runner is running") | ||
| } else { | ||
| cmd.Println("Docker Model Runner is not running") | ||
| hooks.PrintNextSteps(os.Stdout, []string{enableViaCLI, enableViaGUI}) | ||
| os.Exit(1) | ||
| hooks.PrintNextSteps(cmd.OutOrStdout(), []string{enableViaCLI, enableViaGUI}) | ||
| osExit(1) | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
| return c | ||
| } | ||
|
|
||
| var osExit = os.Exit | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/docker/cli/cli-plugins/hooks" | ||
| "github.com/docker/model-cli/desktop" | ||
| mockdesktop "github.com/docker/model-cli/mocks" | ||
| "github.com/docker/pinata/common/pkg/inference" | ||
| "github.com/stretchr/testify/require" | ||
| "go.uber.org/mock/gomock" | ||
| ) | ||
|
|
||
| func TestStatus(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
| defer ctrl.Finish() | ||
|
|
||
| mockBody := io.NopCloser(strings.NewReader("")) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| doResponse *http.Response | ||
| doErr error | ||
| expectExit bool | ||
| expectedErr error | ||
| expectedOutput string | ||
| }{ | ||
| { | ||
| name: "running", | ||
| doResponse: &http.Response{StatusCode: http.StatusOK, Body: mockBody}, | ||
| doErr: nil, | ||
| expectExit: false, | ||
| expectedErr: nil, | ||
| expectedOutput: "Docker Model Runner is running\n", | ||
| }, | ||
| { | ||
| name: "not running", | ||
| doResponse: &http.Response{StatusCode: http.StatusServiceUnavailable, Body: mockBody}, | ||
| doErr: nil, | ||
| expectExit: true, | ||
| expectedErr: nil, | ||
| expectedOutput: func() string { | ||
| buf := new(bytes.Buffer) | ||
| fmt.Fprintln(buf, "Docker Model Runner is not running") | ||
| hooks.PrintNextSteps(buf, []string{enableViaCLI, enableViaGUI}) | ||
| return buf.String() | ||
| }(), | ||
| }, | ||
| { | ||
| name: "request with error", | ||
| doResponse: &http.Response{StatusCode: http.StatusInternalServerError, Body: mockBody}, | ||
| doErr: nil, | ||
| expectExit: false, | ||
| expectedErr: handleClientError( | ||
| fmt.Errorf("unexpected status code: %d", http.StatusInternalServerError), | ||
| "Failed to get Docker Model Runner status", | ||
| ), | ||
| expectedOutput: "", | ||
| }, | ||
| { | ||
| name: "failed request", | ||
| doResponse: nil, | ||
| doErr: fmt.Errorf("failed to make request"), | ||
| expectExit: false, | ||
| expectedErr: handleClientError( | ||
| fmt.Errorf("error querying %s: %w", inference.ModelsPrefix, fmt.Errorf("failed to make request")), | ||
| "Failed to get Docker Model Runner status", | ||
| ), | ||
| expectedOutput: "", | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| client := mockdesktop.NewMockDockerHttpClient(ctrl) | ||
| req, err := http.NewRequest(http.MethodGet, desktop.URL(inference.ModelsPrefix), nil) | ||
| require.NoError(t, err) | ||
| client.EXPECT().Do(req).Return(test.doResponse, test.doErr) | ||
|
|
||
| originalOsExit := osExit | ||
| exitCalled := false | ||
| osExit = func(code int) { | ||
| exitCalled = true | ||
| require.Equal(t, 1, code, "Expected exit code to be 1") | ||
| } | ||
| defer func() { osExit = originalOsExit }() | ||
|
|
||
| cmd := newStatusCmd(desktop.New(client)) | ||
| buf := new(bytes.Buffer) | ||
| cmd.SetOut(buf) | ||
| cmd.SetErr(buf) | ||
|
|
||
| err = cmd.Execute() | ||
| if test.expectExit { | ||
| require.True(t, exitCalled, "Expected os.Exit to be called") | ||
| } else { | ||
| require.False(t, exitCalled, "Did not expect os.Exit to be called") | ||
| } | ||
| if test.expectedErr != nil { | ||
| require.Error(t, err) | ||
| require.EqualError(t, err, test.expectedErr.Error()) | ||
| } else { | ||
| require.NoError(t, err) | ||
| require.Equal(t, test.expectedOutput, buf.String()) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why
cmd.OutOrStdout()instead ofos.Stdout? Also why to extractos.Exitinto a variable?I assume it has to do with being able to assert the output in the test but I'm unsure of how it works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I can capture it in the UT where I overwrite
cmd's output.https://github.com/docker/model-cli/pull/20/files/d706eeb6e5433e561bc3e661066ef3a1cfe07bd4