github.com/rish1988/moby@v25.0.2+incompatible/client/image_inspect_test.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"fmt"
     8  	"io"
     9  	"net/http"
    10  	"reflect"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/docker/docker/api/types"
    15  	"github.com/docker/docker/errdefs"
    16  	"github.com/pkg/errors"
    17  	"gotest.tools/v3/assert"
    18  	is "gotest.tools/v3/assert/cmp"
    19  )
    20  
    21  func TestImageInspectError(t *testing.T) {
    22  	client := &Client{
    23  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    24  	}
    25  
    26  	_, _, err := client.ImageInspectWithRaw(context.Background(), "nothing")
    27  	assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
    28  }
    29  
    30  func TestImageInspectImageNotFound(t *testing.T) {
    31  	client := &Client{
    32  		client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
    33  	}
    34  
    35  	_, _, err := client.ImageInspectWithRaw(context.Background(), "unknown")
    36  	assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
    37  }
    38  
    39  func TestImageInspectWithEmptyID(t *testing.T) {
    40  	client := &Client{
    41  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    42  			return nil, errors.New("should not make request")
    43  		}),
    44  	}
    45  	_, _, err := client.ImageInspectWithRaw(context.Background(), "")
    46  	assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
    47  }
    48  
    49  func TestImageInspect(t *testing.T) {
    50  	expectedURL := "/images/image_id/json"
    51  	expectedTags := []string{"tag1", "tag2"}
    52  	client := &Client{
    53  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    54  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    55  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    56  			}
    57  			content, err := json.Marshal(types.ImageInspect{
    58  				ID:       "image_id",
    59  				RepoTags: expectedTags,
    60  			})
    61  			if err != nil {
    62  				return nil, err
    63  			}
    64  			return &http.Response{
    65  				StatusCode: http.StatusOK,
    66  				Body:       io.NopCloser(bytes.NewReader(content)),
    67  			}, nil
    68  		}),
    69  	}
    70  
    71  	imageInspect, _, err := client.ImageInspectWithRaw(context.Background(), "image_id")
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  	if imageInspect.ID != "image_id" {
    76  		t.Fatalf("expected `image_id`, got %s", imageInspect.ID)
    77  	}
    78  	if !reflect.DeepEqual(imageInspect.RepoTags, expectedTags) {
    79  		t.Fatalf("expected `%v`, got %v", expectedTags, imageInspect.RepoTags)
    80  	}
    81  }