File: load_test.go

package info (click to toggle)
golang-github-containers-common 0.64.2%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,528 kB
  • sloc: makefile: 130; sh: 102
file content (155 lines) | stat: -rw-r--r-- 6,046 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//go:build !remote

package libimage

import (
	"context"
	"os"
	"testing"

	"github.com/containers/image/v5/transports/alltransports"
	"github.com/stretchr/testify/require"
)

func TestLoadByPath(t *testing.T) {
	// Make sure that loading images does not leave any artifacts in TMPDIR
	// behind (containers/podman/issues/14287).
	tmpdir := t.TempDir()
	t.Setenv("TMPDIR", tmpdir)
	defer func() {
		dir, err := os.ReadDir(tmpdir)
		require.NoError(t, err)
		require.Len(t, dir, 0)
	}()

	runtime := testNewRuntime(t)
	ctx := context.Background()
	loadOptions := &LoadOptions{}
	loadOptions.Writer = os.Stdout

	for _, test := range []struct {
		input       string
		expectError bool
		numImages   int
		names       []string
	}{
		// DOCKER ARCHIVE
		{"testdata/docker-name-only.tar.xz", false, 1, []string{"localhost/pretty-empty:latest"}},
		{"testdata/docker-registry-name.tar.xz", false, 1, []string{"example.com/empty:latest"}},
		{"testdata/docker-two-names.tar.xz", false, 1, []string{"localhost/pretty-empty:latest", "example.com/empty:latest"}},
		{"testdata/docker-two-images.tar.xz", false, 2, []string{"example.com/empty:latest", "example.com/empty/but:different"}},
		{"testdata/docker-unnamed.tar.xz", false, 1, []string{"sha256:ec9293436c2e66da44edb9efb8d41f6b13baf62283ebe846468bc992d76d7951"}},
		{"testdata/buildkit-docker.tar", false, 1, []string{"github.com/buildkit/archive:docker"}},

		// OCI ARCHIVE
		{"testdata/oci-name-only.tar.gz", false, 1, []string{"localhost/pretty-empty:latest"}},
		{"testdata/oci-non-docker-name.tar.gz", true, 0, nil},
		{"testdata/oci-registry-name.tar.gz", false, 1, []string{"example.com/empty:latest"}},
		{"testdata/oci-unnamed.tar.gz", false, 1, []string{"sha256:5c8aca8137ac47e84c69ae93ce650ce967917cc001ba7aad5494073fac75b8b6"}},
		{"testdata/buildkit-oci.tar", false, 1, []string{"github.com/buildkit/archive:oci"}},
	} {
		loadedImages, err := runtime.Load(ctx, test.input, loadOptions)
		if test.expectError {
			require.Error(t, err, test.input)
			continue
		}
		require.NoError(t, err, test.input)
		require.Equal(t, test.names, loadedImages, test.input)

		// Make sure that all returned names exist as images in the
		// local containers storage.
		ids := []string{} // later used for image removal
		names := [][]string{}
		for _, name := range loadedImages {
			image, resolvedName, err := runtime.LookupImage(name, nil)
			require.NoError(t, err, test.input)
			require.Equal(t, name, resolvedName, test.input)
			ids = append(ids, image.ID())
			names = append(names, image.Names())
		}

		// Now remove the image.
		rmReports, rmErrors := runtime.RemoveImages(ctx, ids, &RemoveImagesOptions{Force: true})
		require.Len(t, rmErrors, 0)
		require.Len(t, rmReports, test.numImages)

		// Now inspect the removal reports.
		for i, report := range rmReports {
			require.Equal(t, ids[i], report.ID, test.input)
			require.Equal(t, names[i], report.Untagged, test.input)
		}
	}
}

func TestLoadReference(t *testing.T) {
	// Make sure that loading images does not leave any artifacts in TMPDIR
	// behind (containers/podman/issues/14287).
	tmpdir := t.TempDir()
	t.Setenv("TMPDIR", tmpdir)

	runtime := testNewRuntime(t)
	ctx := context.Background()
	loadOptions := &LoadOptions{}
	loadOptions.Writer = os.Stdout

	for _, test := range []struct {
		input       string
		expectError bool
		numImages   int
		names       []string
	}{
		// DOCKER ARCHIVE
		{"docker-archive:testdata/docker-name-only.tar.xz", false, 1, []string{"localhost/pretty-empty:latest"}},
		{"docker-archive:testdata/docker-registry-name.tar.xz", false, 1, []string{"example.com/empty:latest"}},
		{"docker-archive:testdata/docker-two-names.tar.xz", false, 1, []string{"localhost/pretty-empty:latest", "example.com/empty:latest"}},
		{"docker-archive:testdata/docker-two-images.tar.xz", false, 2, []string{"example.com/empty:latest", "example.com/empty/but:different"}},
		{"docker-archive:testdata/docker-unnamed.tar.xz", false, 1, []string{"sha256:ec9293436c2e66da44edb9efb8d41f6b13baf62283ebe846468bc992d76d7951"}},
		{"docker-archive:testdata/buildkit-docker.tar", false, 1, []string{"github.com/buildkit/archive:docker"}},

		// OCI ARCHIVE
		{"oci-archive:testdata/oci-name-only.tar.gz", false, 1, []string{"localhost/pretty-empty:latest"}},
		{"oci-archive:testdata/oci-non-docker-name.tar.gz", true, 0, nil},
		{"oci-archive:testdata/oci-registry-name.tar.gz", false, 1, []string{"example.com/empty:latest"}},
		{"oci-archive:testdata/oci-unnamed.tar.gz", false, 1, []string{"sha256:5c8aca8137ac47e84c69ae93ce650ce967917cc001ba7aad5494073fac75b8b6"}},
		{"oci-archive:testdata/buildkit-oci.tar", false, 1, []string{"github.com/buildkit/archive:oci"}},

		{"docker://quay.io/libpod/alpine_nginx", false, 1, []string{"quay.io/libpod/alpine_nginx:latest"}},
	} {
		ref, err := alltransports.ParseImageName(test.input)
		require.NoError(t, err, test.input)
		loadedImages, err := runtime.LoadReference(ctx, ref, loadOptions)
		if test.expectError {
			require.Error(t, err, test.input)
			continue
		}
		require.NoError(t, err, test.input)
		require.Equal(t, test.names, loadedImages, test.input)

		// Make sure that all returned names exist as images in the
		// local containers storage.
		ids := []string{} // later used for image removal
		names := [][]string{}
		for _, name := range loadedImages {
			image, resolvedName, err := runtime.LookupImage(name, nil)
			require.NoError(t, err, test.input)
			require.Equal(t, name, resolvedName, test.input)
			ids = append(ids, image.ID())
			names = append(names, image.Names())
		}

		// Now remove the image.
		rmReports, rmErrors := runtime.RemoveImages(ctx, ids, &RemoveImagesOptions{Force: true})
		require.Len(t, rmErrors, 0)
		require.Len(t, rmReports, test.numImages)

		// Now inspect the removal reports.
		for i, report := range rmReports {
			require.Equal(t, ids[i], report.ID, test.input)
			require.Equal(t, names[i], report.Untagged, test.input)
		}
	}

	dir, err := os.ReadDir(tmpdir)
	require.NoError(t, err)
	require.Len(t, dir, 0)
}