blob: 44e022dd4530804555e516896d052069463f594e [file] [log] [blame]
Mohammed Naser4c8e0cb2024-02-21 11:51:34 -05001package defaults
2
3import (
4 "context"
5 "fmt"
6 "slices"
7 "strings"
8 "testing"
9
10 "github.com/containers/image/v5/docker"
11 "github.com/stretchr/testify/require"
12)
13
14func TestImageExist(t *testing.T) {
15 images, err := GetImages()
16 require.NoError(t, err)
17
18 var uniqueImages []string
19 for _, image := range images {
20 if slices.Contains(uniqueImages, image) {
21 continue
22 }
23
24 uniqueImages = append(uniqueImages, image)
25 }
26
27 for _, image := range uniqueImages {
28 // NOTE(mnaser): ParseReference does not allow both tag & digest,
29 // so we strip the tags from the image name.
Mohammed Naser91e2fa02024-02-23 01:46:39 -050030 nameWithTagSplit := strings.Split(image, "@")
Oleksandr K.574ce3c2024-10-31 19:14:58 +010031 // NOTE(okozachenko1203): We'll enable this again when use image digest.
32 // require.Len(t, nameWithTagSplit, 2)
Mohammed Naser91e2fa02024-02-23 01:46:39 -050033 nameWithTag := nameWithTagSplit[0]
Oleksandr K.574ce3c2024-10-31 19:14:58 +010034 var imageRef string
35 if len(nameWithTagSplit) == 2 {
36 name := strings.Split(nameWithTag, ":")[0]
37 digest := strings.Split(image, "@")[1]
38 imageRef = fmt.Sprintf("%s@%s", name, digest)
39 } else {
40 imageRef = nameWithTag
41 }
Mohammed Naser4c8e0cb2024-02-21 11:51:34 -050042
Oleksandr K.574ce3c2024-10-31 19:14:58 +010043 t.Run(imageRef, func(t *testing.T) {
Mohammed Naser4c8e0cb2024-02-21 11:51:34 -050044 t.Parallel()
45
Oleksandr K.574ce3c2024-10-31 19:14:58 +010046 ref, err := docker.ParseReference(fmt.Sprintf("//%s", imageRef))
Mohammed Naser4c8e0cb2024-02-21 11:51:34 -050047 require.NoError(t, err)
48
49 ctx := context.Background()
50 img, err := ref.NewImage(ctx, nil)
51 require.NoError(t, err)
52 defer img.Close()
53
54 _, _, err = img.Manifest(ctx)
55 require.NoError(t, err)
56 })
57 }
58}