github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/client/plugin_inspect_test.go (about) 1 package client // import "github.com/Prakhar-Agarwal-byte/moby/client" 2 3 import ( 4 "bytes" 5 "context" 6 "encoding/json" 7 "fmt" 8 "io" 9 "net/http" 10 "strings" 11 "testing" 12 13 "github.com/Prakhar-Agarwal-byte/moby/api/types" 14 "github.com/Prakhar-Agarwal-byte/moby/errdefs" 15 "github.com/pkg/errors" 16 "gotest.tools/v3/assert" 17 is "gotest.tools/v3/assert/cmp" 18 ) 19 20 func TestPluginInspectError(t *testing.T) { 21 client := &Client{ 22 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 23 } 24 25 _, _, err := client.PluginInspectWithRaw(context.Background(), "nothing") 26 assert.Check(t, is.ErrorType(err, errdefs.IsSystem)) 27 } 28 29 func TestPluginInspectWithEmptyID(t *testing.T) { 30 client := &Client{ 31 client: newMockClient(func(req *http.Request) (*http.Response, error) { 32 return nil, errors.New("should not make request") 33 }), 34 } 35 _, _, err := client.PluginInspectWithRaw(context.Background(), "") 36 assert.Check(t, is.ErrorType(err, errdefs.IsNotFound)) 37 } 38 39 func TestPluginInspect(t *testing.T) { 40 expectedURL := "/plugins/plugin_name" 41 client := &Client{ 42 client: newMockClient(func(req *http.Request) (*http.Response, error) { 43 if !strings.HasPrefix(req.URL.Path, expectedURL) { 44 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 45 } 46 content, err := json.Marshal(types.Plugin{ 47 ID: "plugin_id", 48 }) 49 if err != nil { 50 return nil, err 51 } 52 return &http.Response{ 53 StatusCode: http.StatusOK, 54 Body: io.NopCloser(bytes.NewReader(content)), 55 }, nil 56 }), 57 } 58 59 pluginInspect, _, err := client.PluginInspectWithRaw(context.Background(), "plugin_name") 60 if err != nil { 61 t.Fatal(err) 62 } 63 if pluginInspect.ID != "plugin_id" { 64 t.Fatalf("expected `plugin_id`, got %s", pluginInspect.ID) 65 } 66 }