github.com/nektos/act@v0.2.63/pkg/container/docker_images_test.go (about)

     1  package container
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"testing"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	"github.com/docker/docker/client"
    10  	log "github.com/sirupsen/logrus"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func init() {
    15  	log.SetLevel(log.DebugLevel)
    16  }
    17  
    18  func TestImageExistsLocally(t *testing.T) {
    19  	if testing.Short() {
    20  		t.Skip("skipping integration test")
    21  	}
    22  	ctx := context.Background()
    23  	// to help make this test reliable and not flaky, we need to have
    24  	// an image that will exist, and onew that won't exist
    25  
    26  	// Test if image exists with specific tag
    27  	invalidImageTag, err := ImageExistsLocally(ctx, "library/alpine:this-random-tag-will-never-exist", "linux/amd64")
    28  	assert.Nil(t, err)
    29  	assert.Equal(t, false, invalidImageTag)
    30  
    31  	// Test if image exists with specific architecture (image platform)
    32  	invalidImagePlatform, err := ImageExistsLocally(ctx, "alpine:latest", "windows/amd64")
    33  	assert.Nil(t, err)
    34  	assert.Equal(t, false, invalidImagePlatform)
    35  
    36  	// pull an image
    37  	cli, err := client.NewClientWithOpts(client.FromEnv)
    38  	assert.Nil(t, err)
    39  	cli.NegotiateAPIVersion(context.Background())
    40  
    41  	// Chose alpine latest because it's so small
    42  	// maybe we should build an image instead so that tests aren't reliable on dockerhub
    43  	readerDefault, err := cli.ImagePull(ctx, "node:16-buster-slim", types.ImagePullOptions{
    44  		Platform: "linux/amd64",
    45  	})
    46  	assert.Nil(t, err)
    47  	defer readerDefault.Close()
    48  	_, err = io.ReadAll(readerDefault)
    49  	assert.Nil(t, err)
    50  
    51  	imageDefaultArchExists, err := ImageExistsLocally(ctx, "node:16-buster-slim", "linux/amd64")
    52  	assert.Nil(t, err)
    53  	assert.Equal(t, true, imageDefaultArchExists)
    54  
    55  	// Validate if another architecture platform can be pulled
    56  	readerArm64, err := cli.ImagePull(ctx, "node:16-buster-slim", types.ImagePullOptions{
    57  		Platform: "linux/arm64",
    58  	})
    59  	assert.Nil(t, err)
    60  	defer readerArm64.Close()
    61  	_, err = io.ReadAll(readerArm64)
    62  	assert.Nil(t, err)
    63  
    64  	imageArm64Exists, err := ImageExistsLocally(ctx, "node:16-buster-slim", "linux/arm64")
    65  	assert.Nil(t, err)
    66  	assert.Equal(t, true, imageArm64Exists)
    67  }