Mohammed Naser | 4c8e0cb | 2024-02-21 11:51:34 -0500 | [diff] [blame] | 1 | package defaults |
| 2 | |
| 3 | import ( |
| 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 | |
| 14 | func 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 Naser | 91e2fa0 | 2024-02-23 01:46:39 -0500 | [diff] [blame] | 30 | nameWithTagSplit := strings.Split(image, "@") |
| 31 | require.Len(t, nameWithTagSplit, 2) |
| 32 | nameWithTag := nameWithTagSplit[0] |
Mohammed Naser | 4c8e0cb | 2024-02-21 11:51:34 -0500 | [diff] [blame] | 33 | name := strings.Split(nameWithTag, ":")[0] |
| 34 | digest := strings.Split(image, "@")[1] |
| 35 | image := fmt.Sprintf("%s@%s", name, digest) |
| 36 | |
| 37 | t.Run(image, func(t *testing.T) { |
| 38 | t.Parallel() |
| 39 | |
| 40 | ref, err := docker.ParseReference(fmt.Sprintf("//%s", image)) |
| 41 | require.NoError(t, err) |
| 42 | |
| 43 | ctx := context.Background() |
| 44 | img, err := ref.NewImage(ctx, nil) |
| 45 | require.NoError(t, err) |
| 46 | defer img.Close() |
| 47 | |
| 48 | _, _, err = img.Manifest(ctx) |
| 49 | require.NoError(t, err) |
| 50 | }) |
| 51 | } |
| 52 | } |