blob: 3a8d2315cd1742b2b1b34c1d651c5cbb84b65663 [file] [log] [blame]
Mohammed Naser4c8e0cb2024-02-21 11:51:34 -05001package defaults
2
3import (
Jason Halle547ce92024-02-19 12:56:23 -06004 "bytes"
Mohammed Naser4c8e0cb2024-02-21 11:51:34 -05005 "context"
6 "fmt"
7 "slices"
8 "strings"
9 "testing"
10
11 "github.com/containers/image/v5/docker"
Jason Halle547ce92024-02-19 12:56:23 -060012 "github.com/goccy/go-yaml"
13 "github.com/stretchr/testify/assert"
Mohammed Naser4c8e0cb2024-02-21 11:51:34 -050014 "github.com/stretchr/testify/require"
15)
16
Jason Halle547ce92024-02-19 12:56:23 -060017func 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 Naser4c8e0cb2024-02-21 11:51:34 -050036func 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 Naser91e2fa02024-02-23 01:46:39 -050052 nameWithTagSplit := strings.Split(image, "@")
Oleksandr K.574ce3c2024-10-31 19:14:58 +010053 // NOTE(okozachenko1203): We'll enable this again when use image digest.
54 // require.Len(t, nameWithTagSplit, 2)
Mohammed Naser91e2fa02024-02-23 01:46:39 -050055 nameWithTag := nameWithTagSplit[0]
Oleksandr K.574ce3c2024-10-31 19:14:58 +010056 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 Naser4c8e0cb2024-02-21 11:51:34 -050064
Oleksandr K.574ce3c2024-10-31 19:14:58 +010065 t.Run(imageRef, func(t *testing.T) {
Mohammed Naser4c8e0cb2024-02-21 11:51:34 -050066 t.Parallel()
67
Oleksandr K.574ce3c2024-10-31 19:14:58 +010068 ref, err := docker.ParseReference(fmt.Sprintf("//%s", imageRef))
Mohammed Naser4c8e0cb2024-02-21 11:51:34 -050069 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}