summaryrefslogtreecommitdiffstats
path: root/pkg/v1/tarball/image_test.go
blob: 3a46400e40b2b6415a05126e5279f30ae321ce30 (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
// Copyright 2018 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tarball

import (
	"io"
	"testing"

	"github.com/google/go-containerregistry/pkg/name"
	v1 "github.com/google/go-containerregistry/pkg/v1"
	"github.com/google/go-containerregistry/pkg/v1/validate"
)

func TestManifestAndConfig(t *testing.T) {
	img, err := ImageFromPath("testdata/test_image_1.tar", nil)
	if err != nil {
		t.Fatalf("Error loading image: %v", err)
	}
	manifest, err := img.Manifest()
	if err != nil {
		t.Fatalf("Error loading manifest: %v", err)
	}
	if len(manifest.Layers) != 1 {
		t.Fatalf("layers should be 1, got %d", len(manifest.Layers))
	}

	config, err := img.ConfigFile()
	if err != nil {
		t.Fatalf("Error loading config file: %v", err)
	}
	if len(config.History) != 1 {
		t.Fatalf("history length should be 1, got %d", len(config.History))
	}

	if err := validate.Image(img); err != nil {
		t.Errorf("Validate() = %v", err)
	}
}

func TestNullManifest(t *testing.T) {
	img, err := ImageFromPath("testdata/null_manifest.tar", nil)
	if err == nil {
		t.Fatalf("Error expected loading null image: %v", img)
	}
}

func TestNoManifest(t *testing.T) {
	img, err := ImageFromPath("testdata/no_manifest.tar", nil)
	if err == nil {
		t.Fatalf("Error expected loading image: %v", img)
	}
}

func TestBundleSingle(t *testing.T) {
	img, err := ImageFromPath("testdata/test_bundle.tar", nil)
	if err == nil {
		t.Fatalf("Error expected loading image: %v", img)
	}
}

func TestBundleMultiple(t *testing.T) {
	for _, imgName := range []string{
		"test_image_1",
		"test_image_2",
		"test_image_1:latest",
		"test_image_2:latest",
		"index.docker.io/library/test_image_1:latest",
	} {
		t.Run(imgName, func(t *testing.T) {
			tag, err := name.NewTag(imgName, name.WeakValidation)
			if err != nil {
				t.Fatalf("Error creating tag: %v", err)
			}
			img, err := ImageFromPath("testdata/test_bundle.tar", &tag)
			if err != nil {
				t.Fatalf("Error loading image: %v", err)
			}
			if _, err := img.Manifest(); err != nil {
				t.Fatalf("Unexpected error loading manifest: %v", err)
			}

			if err := validate.Image(img); err != nil {
				t.Errorf("Validate() = %v", err)
			}
		})
	}
}

func TestLayerLink(t *testing.T) {
	tag, err := name.NewTag("bazel/v1/tarball:test_image_3", name.WeakValidation)
	if err != nil {
		t.Fatalf("Error creating tag: %v", err)
	}
	img, err := ImageFromPath("testdata/test_link.tar", &tag)
	if err != nil {
		t.Fatalf("Error loading image: %v", img)
	}
	hash := v1.Hash{
		Algorithm: "sha256",
		Hex:       "8897395fd26dc44ad0e2a834335b33198cb41ac4d98dfddf58eced3853fa7b17",
	}
	layer, err := img.LayerByDiffID(hash)
	if err != nil {
		t.Fatalf("Error getting layer by diff ID: %v, %v", hash, err)
	}
	rc, err := layer.Uncompressed()
	if err != nil {
		t.Fatal(err)
	}
	bs, err := io.ReadAll(rc)
	if err != nil {
		t.Fatal(err)
	}
	if len(bs) == 0 {
		t.Errorf("layer.Uncompressed() returned a link file")
	}
}

func TestLoadManifest(t *testing.T) {
	manifest, err := LoadManifest(pathOpener("testdata/test_load_manifest.tar"))
	if err != nil {
		t.Fatalf("Error load manifest: %v", err)
	}
	if len(manifest) == 0 {
		t.Fatalf("get nothing")
	}
}