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, "@") |
Oleksandr K. | 574ce3c | 2024-10-31 19:14:58 +0100 | [diff] [blame] | 31 | // NOTE(okozachenko1203): We'll enable this again when use image digest. |
| 32 | // require.Len(t, nameWithTagSplit, 2) |
Mohammed Naser | 91e2fa0 | 2024-02-23 01:46:39 -0500 | [diff] [blame] | 33 | nameWithTag := nameWithTagSplit[0] |
Oleksandr K. | 574ce3c | 2024-10-31 19:14:58 +0100 | [diff] [blame] | 34 | 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 Naser | 4c8e0cb | 2024-02-21 11:51:34 -0500 | [diff] [blame] | 42 | |
Oleksandr K. | 574ce3c | 2024-10-31 19:14:58 +0100 | [diff] [blame] | 43 | t.Run(imageRef, func(t *testing.T) { |
Mohammed Naser | 4c8e0cb | 2024-02-21 11:51:34 -0500 | [diff] [blame] | 44 | t.Parallel() |
| 45 | |
Oleksandr K. | 574ce3c | 2024-10-31 19:14:58 +0100 | [diff] [blame] | 46 | ref, err := docker.ParseReference(fmt.Sprintf("//%s", imageRef)) |
Mohammed Naser | 4c8e0cb | 2024-02-21 11:51:34 -0500 | [diff] [blame] | 47 | 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 | } |