Mohammed Naser | 4c8e0cb | 2024-02-21 11:51:34 -0500 | [diff] [blame] | 1 | package defaults |
| 2 | |
| 3 | import ( |
Jason Hall | e547ce9 | 2024-02-19 12:56:23 -0600 | [diff] [blame] | 4 | "bytes" |
Mohammed Naser | 4c8e0cb | 2024-02-21 11:51:34 -0500 | [diff] [blame] | 5 | "context" |
| 6 | "fmt" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/containers/image/v5/docker" |
Jason Hall | e547ce9 | 2024-02-19 12:56:23 -0600 | [diff] [blame] | 12 | "github.com/goccy/go-yaml" |
| 13 | "github.com/stretchr/testify/assert" |
Mohammed Naser | 4c8e0cb | 2024-02-21 11:51:34 -0500 | [diff] [blame] | 14 | "github.com/stretchr/testify/require" |
| 15 | ) |
| 16 | |
Jason Hall | e547ce9 | 2024-02-19 12:56:23 -0600 | [diff] [blame] | 17 | func TestImageHasPrefix(t *testing.T) { |
| 18 | path, err := yaml.PathString("$._atmosphere_images") |
| 19 | require.NoError(t, err) |
| 20 | |
| 21 | var images map[string]string |
| 22 | err = path.Read(bytes.NewReader(varsFile), &images) |
| 23 | require.NoError(t, err) |
| 24 | |
| 25 | prefix := "{{ atmosphere_image_prefix }}" |
| 26 | |
| 27 | for _, image := range images { |
| 28 | testName := strings.ReplaceAll(image, prefix, "") |
| 29 | |
| 30 | t.Run(testName, func(t *testing.T) { |
| 31 | assert.True(t, strings.HasPrefix(image, prefix)) |
| 32 | }) |
| 33 | } |
| 34 | } |
| 35 | |
Mohammed Naser | 4c8e0cb | 2024-02-21 11:51:34 -0500 | [diff] [blame] | 36 | func TestImageExist(t *testing.T) { |
| 37 | images, err := GetImages() |
| 38 | require.NoError(t, err) |
| 39 | |
| 40 | var uniqueImages []string |
| 41 | for _, image := range images { |
| 42 | if slices.Contains(uniqueImages, image) { |
| 43 | continue |
| 44 | } |
| 45 | |
| 46 | uniqueImages = append(uniqueImages, image) |
| 47 | } |
| 48 | |
| 49 | for _, image := range uniqueImages { |
| 50 | // NOTE(mnaser): ParseReference does not allow both tag & digest, |
| 51 | // so we strip the tags from the image name. |
Mohammed Naser | 91e2fa0 | 2024-02-23 01:46:39 -0500 | [diff] [blame] | 52 | nameWithTagSplit := strings.Split(image, "@") |
Oleksandr K. | 574ce3c | 2024-10-31 19:14:58 +0100 | [diff] [blame] | 53 | // NOTE(okozachenko1203): We'll enable this again when use image digest. |
| 54 | // require.Len(t, nameWithTagSplit, 2) |
Mohammed Naser | 91e2fa0 | 2024-02-23 01:46:39 -0500 | [diff] [blame] | 55 | nameWithTag := nameWithTagSplit[0] |
Oleksandr K. | 574ce3c | 2024-10-31 19:14:58 +0100 | [diff] [blame] | 56 | var imageRef string |
| 57 | if len(nameWithTagSplit) == 2 { |
| 58 | name := strings.Split(nameWithTag, ":")[0] |
| 59 | digest := strings.Split(image, "@")[1] |
| 60 | imageRef = fmt.Sprintf("%s@%s", name, digest) |
| 61 | } else { |
| 62 | imageRef = nameWithTag |
| 63 | } |
Mohammed Naser | 4c8e0cb | 2024-02-21 11:51:34 -0500 | [diff] [blame] | 64 | |
Oleksandr K. | 574ce3c | 2024-10-31 19:14:58 +0100 | [diff] [blame] | 65 | t.Run(imageRef, func(t *testing.T) { |
Mohammed Naser | 4c8e0cb | 2024-02-21 11:51:34 -0500 | [diff] [blame] | 66 | t.Parallel() |
| 67 | |
Oleksandr K. | 574ce3c | 2024-10-31 19:14:58 +0100 | [diff] [blame] | 68 | ref, err := docker.ParseReference(fmt.Sprintf("//%s", imageRef)) |
Mohammed Naser | 4c8e0cb | 2024-02-21 11:51:34 -0500 | [diff] [blame] | 69 | require.NoError(t, err) |
| 70 | |
| 71 | ctx := context.Background() |
| 72 | img, err := ref.NewImage(ctx, nil) |
| 73 | require.NoError(t, err) |
| 74 | defer img.Close() |
| 75 | |
| 76 | _, _, err = img.Manifest(ctx) |
| 77 | require.NoError(t, err) |
| 78 | }) |
| 79 | } |
| 80 | } |