github.com/kinvolk/docker@v1.13.1/client/image_inspect_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 "reflect" 10 "strings" 11 "testing" 12 13 "github.com/docker/docker/api/types" 14 "golang.org/x/net/context" 15 ) 16 17 func TestImageInspectError(t *testing.T) { 18 client := &Client{ 19 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 20 } 21 22 _, _, err := client.ImageInspectWithRaw(context.Background(), "nothing") 23 if err == nil || err.Error() != "Error response from daemon: Server error" { 24 t.Fatalf("expected a Server Error, got %v", err) 25 } 26 } 27 28 func TestImageInspectImageNotFound(t *testing.T) { 29 client := &Client{ 30 client: newMockClient(errorMock(http.StatusNotFound, "Server error")), 31 } 32 33 _, _, err := client.ImageInspectWithRaw(context.Background(), "unknown") 34 if err == nil || !IsErrImageNotFound(err) { 35 t.Fatalf("expected an imageNotFound error, got %v", err) 36 } 37 } 38 39 func TestImageInspect(t *testing.T) { 40 expectedURL := "/images/image_id/json" 41 expectedTags := []string{"tag1", "tag2"} 42 client := &Client{ 43 client: newMockClient(func(req *http.Request) (*http.Response, error) { 44 if !strings.HasPrefix(req.URL.Path, expectedURL) { 45 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 46 } 47 content, err := json.Marshal(types.ImageInspect{ 48 ID: "image_id", 49 RepoTags: expectedTags, 50 }) 51 if err != nil { 52 return nil, err 53 } 54 return &http.Response{ 55 StatusCode: http.StatusOK, 56 Body: ioutil.NopCloser(bytes.NewReader(content)), 57 }, nil 58 }), 59 } 60 61 imageInspect, _, err := client.ImageInspectWithRaw(context.Background(), "image_id") 62 if err != nil { 63 t.Fatal(err) 64 } 65 if imageInspect.ID != "image_id" { 66 t.Fatalf("expected `image_id`, got %s", imageInspect.ID) 67 } 68 if !reflect.DeepEqual(imageInspect.RepoTags, expectedTags) { 69 t.Fatalf("expected `%v`, got %v", expectedTags, imageInspect.RepoTags) 70 } 71 }