summaryrefslogtreecommitdiffstats
path: root/internal/legacy/copy_test.go
blob: b8ca799e85fd207b4bdaea951fcb9f041dd56c1b (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
// Copyright 2019 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 legacy

import (
	"encoding/json"
	"fmt"
	"net/http/httptest"
	"net/url"
	"strings"
	"testing"

	"github.com/google/go-containerregistry/pkg/name"
	"github.com/google/go-containerregistry/pkg/registry"
	v1 "github.com/google/go-containerregistry/pkg/v1"
	"github.com/google/go-containerregistry/pkg/v1/random"
	"github.com/google/go-containerregistry/pkg/v1/remote"
	"github.com/google/go-containerregistry/pkg/v1/types"
)

func TestCopySchema1(t *testing.T) {
	// Set up a fake registry.
	s := httptest.NewServer(registry.New())
	defer s.Close()
	u, err := url.Parse(s.URL)
	if err != nil {
		t.Fatal(err)
	}

	// We'll copy from src to dst.
	src := fmt.Sprintf("%s/schema1/src", u.Host)
	srcRef, err := name.ParseReference(src)
	if err != nil {
		t.Fatal(err)
	}
	dst := fmt.Sprintf("%s/schema1/dst", u.Host)
	dstRef, err := name.ParseReference(dst)
	if err != nil {
		t.Fatal(err)
	}

	// Create a random layer.
	layer, err := random.Layer(1024, types.DockerLayer)
	if err != nil {
		t.Fatal(err)
	}
	digest, err := layer.Digest()
	if err != nil {
		t.Fatal(err)
	}
	layerRef, err := name.NewDigest(fmt.Sprintf("%s@%s", src, digest))
	if err != nil {
		t.Fatal(err)
	}

	// Populate the registry with a layer and a schema 1 manifest referencing it.
	if err := remote.WriteLayer(layerRef.Context(), layer); err != nil {
		t.Fatal(err)
	}
	manifest := schema1{
		FSLayers: []fslayer{{
			BlobSum: digest.String(),
		}},
	}
	b, err := json.Marshal(manifest)
	if err != nil {
		t.Fatal(err)
	}
	desc := &remote.Descriptor{
		Manifest: b,
		Descriptor: v1.Descriptor{
			MediaType: types.DockerManifestSchema1,
			Digest: v1.Hash{Algorithm: "sha256",
				Hex: strings.Repeat("a", 64),
			},
		},
	}
	if err := remote.Put(dstRef, desc); err != nil {
		t.Fatal(err)
	}

	if err := CopySchema1(desc, srcRef, dstRef); err != nil {
		t.Errorf("failed to copy schema 1: %v", err)
	}
}