summaryrefslogtreecommitdiff
path: root/libpod/image/image_test.go
blob: 4b5b782b162c6b8ba6706bf84cade488a552ec62 (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package image

import (
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"testing"

	"github.com/containers/storage"
	"github.com/containers/storage/pkg/reexec"
	"github.com/pkg/errors"
	"github.com/projectatomic/libpod/libpod"
	"github.com/stretchr/testify/assert"
)

var (
	bbNames      = []string{"docker.io/library/busybox:latest", "docker.io/library/busybox", "docker.io/busybox:latest", "docker.io/busybox", "busybox:latest", "busybox"}
	bbGlibcNames = []string{"docker.io/library/busybox:glibc", "docker.io/busybox:glibc", "busybox:glibc"}
	fedoraNames  = []string{"registry.fedoraproject.org/fedora-minimal:latest", "registry.fedoraproject.org/fedora-minimal", "fedora-minimal:latest", "fedora-minimal"}
)

// setup a runtime for the tests in an alternative location on the filesystem
func setupRuntime(workdir string) (*libpod.Runtime, error) {
	if reexec.Init() {
		return nil, errors.Errorf("dude")
	}
	sc := libpod.WithStorageConfig(storage.StoreOptions{
		GraphRoot: workdir,
		RunRoot:   workdir,
	})
	sd := libpod.WithStaticDir(path.Join(workdir, "libpod_tmp"))
	td := libpod.WithTmpDir(path.Join(workdir, "tmpdir"))

	options := []libpod.RuntimeOption{sc, sd, td}
	return libpod.NewRuntime(options...)
}

// getImage is only used to build a test matrix for testing local images
func getImage(r *libpod.Runtime, fqImageName string) (*storage.Image, error) {
	img, err := NewFromLocal(fqImageName, r)
	if err != nil {
		return nil, err
	}
	return img.image, nil
}

func tagImage(r *libpod.Runtime, fqImageName, tagName string) error {
	img, err := NewFromLocal(fqImageName, r)
	if err != nil {
		return err
	}
	r.TagImage(img.image, tagName)
	return nil
}

type localImageTest struct {
	fqname, taggedName string
	img                *storage.Image
	names              []string
}

// make a temporary directory for the runtime
func mkWorkDir() (string, error) {
	return ioutil.TempDir("", "podman-test")
}

// shutdown the runtime and clean behind it
func cleanup(r *libpod.Runtime, workdir string) {
	r.Shutdown(true)
	err := os.RemoveAll(workdir)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

func makeLocalMatrix(r *libpod.Runtime) ([]localImageTest, error) {
	var l []localImageTest
	// busybox
	busybox := localImageTest{
		fqname:     "docker.io/library/busybox:latest",
		taggedName: "bb:latest",
	}
	b, err := getImage(r, busybox.fqname)
	if err != nil {
		return nil, err
	}
	busybox.img = b
	busybox.names = bbNames
	busybox.names = append(busybox.names, []string{"bb:latest", "bb", b.ID, b.ID[0:7], fmt.Sprintf("busybox@%s", b.Digest.String())}...)

	//fedora
	fedora := localImageTest{
		fqname:     "registry.fedoraproject.org/fedora-minimal:latest",
		taggedName: "f27:latest",
	}
	f, err := getImage(r, fedora.fqname)
	if err != nil {
		return nil, err
	}
	fedora.img = f
	fedora.names = fedoraNames

	// busybox-glibc
	busyboxGlibc := localImageTest{
		fqname:     "docker.io/library/busybox:glibc",
		taggedName: "bb:glibc",
	}
	bg, err := getImage(r, busyboxGlibc.fqname)
	if err != nil {
		return nil, err
	}
	busyboxGlibc.img = bg
	busyboxGlibc.names = bbGlibcNames

	l = append(l, busybox, fedora)
	return l, nil

}

// TestImage_NewFromLocal tests finding the image locally by various names,
// tags, and aliases
func TestImage_NewFromLocal(t *testing.T) {
	workdir, err := mkWorkDir()
	assert.NoError(t, err)
	runtime, err := setupRuntime(workdir)
	assert.NoError(t, err)

	// Need images to be present for this test
	_, err = runtime.PullImage("docker.io/library/busybox:latest", libpod.CopyOptions{})
	assert.NoError(t, err)
	_, err = runtime.PullImage("docker.io/library/busybox:glibc", libpod.CopyOptions{})
	assert.NoError(t, err)
	_, err = runtime.PullImage("registry.fedoraproject.org/fedora-minimal:latest", libpod.CopyOptions{})
	assert.NoError(t, err)

	tm, err := makeLocalMatrix(runtime)
	assert.NoError(t, err)
	for _, image := range tm {
		// tag our images
		err = tagImage(runtime, image.fqname, image.taggedName)
		assert.NoError(t, err)
		for _, name := range image.names {
			newImage, err := NewFromLocal(name, runtime)
			assert.NoError(t, err)
			assert.Equal(t, newImage.ID(), image.img.ID)
		}
	}

	// Shutdown the runtime and remove the temporary storage
	cleanup(runtime, workdir)
}

// TestImage_New tests pulling the image by various names, tags, and from
// different registries
func TestImage_New(t *testing.T) {
	var names []string
	workdir, err := mkWorkDir()
	assert.NoError(t, err)
	runtime, err := setupRuntime(workdir)
	assert.NoError(t, err)

	// Build the list of pull names
	names = append(names, bbNames...)
	names = append(names, fedoraNames...)

	// Iterate over the names and delete the image
	// after the pull
	for _, img := range names {
		_, err := runtime.GetImage(img)
		if err == nil {
			os.Exit(1)
		}
		newImage, err := New(img, runtime)
		assert.NoError(t, err)
		assert.NotEqual(t, newImage.ID(), "")
		err = newImage.Remove(false)
		assert.NoError(t, err)
	}

	// Shutdown the runtime and remove the temporary storage
	cleanup(runtime, workdir)
}