github.com/someshkoli/terratest@v0.41.1/modules/docker/images.go (about)

     1  package docker
     2  
     3  import (
     4  	"bufio"
     5  	"encoding/json"
     6  	"fmt"
     7  	"strings"
     8  
     9  	"github.com/gruntwork-io/terratest/modules/collections"
    10  	"github.com/gruntwork-io/terratest/modules/logger"
    11  	"github.com/gruntwork-io/terratest/modules/shell"
    12  	"github.com/gruntwork-io/terratest/modules/testing"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  // Image represents a docker image, and exports all the fields that the docker images command returns for the
    17  // image.
    18  type Image struct {
    19  	// ID is the image ID in docker, and can be used to identify the image in place of the repo and tag.
    20  	ID string
    21  
    22  	// Repository is the image repository.
    23  	Repository string
    24  
    25  	// Tag is the image tag wichin the repository.
    26  	Tag string
    27  
    28  	// CreatedAt represents a timestamp for when the image was created.
    29  	CreatedAt string
    30  
    31  	// CreatedSince is a diff between when the image was created to now.
    32  	CreatedSince string
    33  
    34  	// SharedSize is the amount of space that an image shares with another one (i.e. their common data).
    35  	SharedSize string
    36  
    37  	// UniqueSize is the amount of space that is only used by a given image.
    38  	UniqueSize string
    39  
    40  	// VirtualSize is the total size of the image, combining SharedSize and UniqueSize.
    41  	VirtualSize string
    42  
    43  	// Containers represents the list of containers that are using the image.
    44  	Containers string
    45  
    46  	// Digest is the hash digest of the image, if requested.
    47  	Digest string
    48  }
    49  
    50  func (image Image) String() string {
    51  	return fmt.Sprintf("%s:%s", image.Repository, image.Tag)
    52  }
    53  
    54  // DeleteImage removes a docker image using the Docker CLI. This will fail the test if there is an error.
    55  func DeleteImage(t testing.TestingT, img string, logger *logger.Logger) {
    56  	require.NoError(t, DeleteImageE(t, img, logger))
    57  }
    58  
    59  // DeleteImageE removes a docker image using the Docker CLI.
    60  func DeleteImageE(t testing.TestingT, img string, logger *logger.Logger) error {
    61  	cmd := shell.Command{
    62  		Command: "docker",
    63  		Args:    []string{"rmi", img},
    64  		Logger:  logger,
    65  	}
    66  	return shell.RunCommandE(t, cmd)
    67  }
    68  
    69  // ListImages calls docker images using the Docker CLI to list the available images on the local docker daemon.
    70  func ListImages(t testing.TestingT, logger *logger.Logger) []Image {
    71  	out, err := ListImagesE(t, logger)
    72  	require.NoError(t, err)
    73  	return out
    74  }
    75  
    76  // ListImagesE calls docker images using the Docker CLI to list the available images on the local docker daemon.
    77  func ListImagesE(t testing.TestingT, logger *logger.Logger) ([]Image, error) {
    78  	cmd := shell.Command{
    79  		Command: "docker",
    80  		Args:    []string{"images", "--format", "{{ json . }}"},
    81  		Logger:  logger,
    82  	}
    83  	out, err := shell.RunCommandAndGetOutputE(t, cmd)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  
    88  	// Parse and return the list of image objects.
    89  	images := []Image{}
    90  	scanner := bufio.NewScanner(strings.NewReader(out))
    91  	for scanner.Scan() {
    92  		line := scanner.Text()
    93  		var image Image
    94  		err := json.Unmarshal([]byte(line), &image)
    95  		if err != nil {
    96  			return nil, err
    97  		}
    98  		images = append(images, image)
    99  	}
   100  	return images, nil
   101  }
   102  
   103  // DoesImageExist lists the images in the docker daemon and returns true if the given image label (repo:tag) exists.
   104  // This will fail the test if there is an error.
   105  func DoesImageExist(t testing.TestingT, imgLabel string, logger *logger.Logger) bool {
   106  	images := ListImages(t, logger)
   107  	imageTags := []string{}
   108  	for _, image := range images {
   109  		imageTags = append(imageTags, image.String())
   110  	}
   111  	return collections.ListContains(imageTags, imgLabel)
   112  }