github.com/moby/docker@v26.1.3+incompatible/client/image_history_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  	"strings"
    11  	"testing"
    12  
    13  	"github.com/docker/docker/api/types/image"
    14  	"github.com/docker/docker/errdefs"
    15  	"gotest.tools/v3/assert"
    16  	is "gotest.tools/v3/assert/cmp"
    17  )
    18  
    19  func TestImageHistoryError(t *testing.T) {
    20  	client := &Client{
    21  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    22  	}
    23  	_, err := client.ImageHistory(context.Background(), "nothing")
    24  	assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
    25  }
    26  
    27  func TestImageHistory(t *testing.T) {
    28  	expectedURL := "/images/image_id/history"
    29  	client := &Client{
    30  		client: newMockClient(func(r *http.Request) (*http.Response, error) {
    31  			if !strings.HasPrefix(r.URL.Path, expectedURL) {
    32  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
    33  			}
    34  			b, err := json.Marshal([]image.HistoryResponseItem{
    35  				{
    36  					ID:   "image_id1",
    37  					Tags: []string{"tag1", "tag2"},
    38  				},
    39  				{
    40  					ID:   "image_id2",
    41  					Tags: []string{"tag1", "tag2"},
    42  				},
    43  			})
    44  			if err != nil {
    45  				return nil, err
    46  			}
    47  
    48  			return &http.Response{
    49  				StatusCode: http.StatusOK,
    50  				Body:       io.NopCloser(bytes.NewReader(b)),
    51  			}, nil
    52  		}),
    53  	}
    54  	imageHistories, err := client.ImageHistory(context.Background(), "image_id")
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	if len(imageHistories) != 2 {
    59  		t.Fatalf("expected 2 containers, got %v", imageHistories)
    60  	}
    61  }