Skip to content

Commit cecf390

Browse files
authored
Split tarball progress test from write_test.go (#756)
This example test reaches out to the internet, which isn't ideal. For now, I'm just going to split it off into a separate file so it's easier to exclude. Also, fix some whitespace nits.
1 parent 6d4814a commit cecf390

File tree

4 files changed

+91
-65
lines changed

4 files changed

+91
-65
lines changed

Diff for: pkg/v1/partial/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ There are some properties of a [`Descriptor`](https://github.com/opencontainers/
5151

5252
For example, in a `tarball.Image`, there is a `LayerSources` field that contains
5353
an entire layer descriptor with `URLs` information for foreign layers. This
54-
information can be passed through to callers by implementing this optional
54+
information can be passed through to callers by implementing this optional
5555
`Descriptor` method.
5656

5757
See [`#654`](https://github.com/google/go-containerregistry/pull/654).

Diff for: pkg/v1/remote/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ per the [OCI distribution spec](https://github.com/opencontainers/distribution-s
88
It leans heavily on the lower level [`transport`](/pkg/v1/remote/transport) package, which handles the
99
authentication handshake and structured errors.
1010

11-
## Usage
11+
## Usage
1212

1313
```go
1414
package main

Diff for: pkg/v1/tarball/progress_test.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2018 Google LLC All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package tarball_test
16+
17+
import (
18+
"fmt"
19+
"io"
20+
"io/ioutil"
21+
"os"
22+
23+
"github.com/google/go-containerregistry/pkg/name"
24+
v1 "github.com/google/go-containerregistry/pkg/v1"
25+
"github.com/google/go-containerregistry/pkg/v1/remote"
26+
"github.com/google/go-containerregistry/pkg/v1/tarball"
27+
)
28+
29+
func ExampleWithProgress() {
30+
/* calculations for this test:
31+
The image we are using is docker.io/library/alpine:3.10
32+
its size on disk is 2800640
33+
The filesizes inside are:
34+
-rw-r--r-- 0 0 0 1509 Jan 1 1970 sha256:be4e4bea2c2e15b403bb321562e78ea84b501fb41497472e91ecb41504e8a27c
35+
-rw-r--r-- 0 0 0 2795580 Jan 1 1970 21c83c5242199776c232920ddb58cfa2a46b17e42ed831ca9001c8dbc532d22d.tar.gz
36+
-rw-r--r-- 0 0 0 216 Jan 1 1970 manifest.json
37+
when rounding each to a 512-byte block, plus the header, we get:
38+
1509 -> 1536 + 512 = 2048
39+
2795580 -> 2796032 + 512 = 2796544
40+
216 -> 512 + 512 = 1024
41+
add in 2 blocks of all 0x00 to indicate end of archive
42+
= 1024
43+
-------
44+
Total: 2800640
45+
*/
46+
// buffered channel to make the example test easier
47+
c := make(chan v1.Update, 200)
48+
// Make a tempfile for tarball writes.
49+
fp, err := ioutil.TempFile("", "")
50+
if err != nil {
51+
fmt.Printf("error creating temp file: %v\n", err)
52+
return
53+
}
54+
defer fp.Close()
55+
defer os.Remove(fp.Name())
56+
57+
tag, err := name.NewDigest("docker.io/library/alpine@sha256:f0e9534a598e501320957059cb2a23774b4d4072e37c7b2cf7e95b241f019e35", name.StrictValidation)
58+
if err != nil {
59+
fmt.Printf("error creating test tag: %v\n", err)
60+
return
61+
}
62+
desc, err := remote.Get(tag)
63+
if err != nil {
64+
fmt.Printf("error getting manifest: %v", err)
65+
return
66+
}
67+
img, err := desc.Image()
68+
if err != nil {
69+
fmt.Printf("error image: %v", err)
70+
return
71+
}
72+
go func() {
73+
_ = tarball.WriteToFile(fp.Name(), tag, img, tarball.WithProgress(c))
74+
}()
75+
for update := range c {
76+
switch {
77+
case update.Error != nil && update.Error == io.EOF:
78+
fmt.Fprintf(os.Stderr, "receive error message: %v\n", err)
79+
fmt.Printf("%d/%d", update.Complete, update.Total)
80+
// Output: 2800640/2800640
81+
return
82+
case update.Error != nil:
83+
fmt.Printf("error writing tarball: %v\n", update.Error)
84+
return
85+
default:
86+
fmt.Fprintf(os.Stderr, "receive update: %#v\n", update)
87+
}
88+
}
89+
}

Diff for: pkg/v1/tarball/write_test.go

-63
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
v1 "github.com/google/go-containerregistry/pkg/v1"
3131
"github.com/google/go-containerregistry/pkg/v1/mutate"
3232
"github.com/google/go-containerregistry/pkg/v1/random"
33-
"github.com/google/go-containerregistry/pkg/v1/remote"
3433
"github.com/google/go-containerregistry/pkg/v1/tarball"
3534
"github.com/google/go-containerregistry/pkg/v1/types"
3635
"github.com/google/go-containerregistry/pkg/v1/validate"
@@ -485,65 +484,3 @@ func getLayersFilenames(hashes []string) []string {
485484
}
486485
return filenames
487486
}
488-
489-
func ExampleWithProgress() {
490-
/* calculations for this test:
491-
The image we are using is docker.io/library/alpine:3.10
492-
its size on disk is 2800640
493-
The filesizes inside are:
494-
-rw-r--r-- 0 0 0 1509 Jan 1 1970 sha256:be4e4bea2c2e15b403bb321562e78ea84b501fb41497472e91ecb41504e8a27c
495-
-rw-r--r-- 0 0 0 2795580 Jan 1 1970 21c83c5242199776c232920ddb58cfa2a46b17e42ed831ca9001c8dbc532d22d.tar.gz
496-
-rw-r--r-- 0 0 0 216 Jan 1 1970 manifest.json
497-
when rounding each to a 512-byte block, plus the header, we get:
498-
1509 -> 1536 + 512 = 2048
499-
2795580 -> 2796032 + 512 = 2796544
500-
216 -> 512 + 512 = 1024
501-
add in 2 blocks of all 0x00 to indicate end of archive
502-
= 1024
503-
-------
504-
Total: 2800640
505-
*/
506-
// buffered channel to make the example test easier
507-
c := make(chan v1.Update, 200)
508-
// Make a tempfile for tarball writes.
509-
fp, err := ioutil.TempFile("", "")
510-
if err != nil {
511-
fmt.Printf("error creating temp file: %v\n", err)
512-
return
513-
}
514-
defer fp.Close()
515-
defer os.Remove(fp.Name())
516-
517-
tag, err := name.NewDigest("docker.io/library/alpine@sha256:f0e9534a598e501320957059cb2a23774b4d4072e37c7b2cf7e95b241f019e35", name.StrictValidation)
518-
if err != nil {
519-
fmt.Printf("error creating test tag: %v\n", err)
520-
return
521-
}
522-
desc, err := remote.Get(tag)
523-
if err != nil {
524-
fmt.Printf("error getting manifest: %v", err)
525-
return
526-
}
527-
img, err := desc.Image()
528-
if err != nil {
529-
fmt.Printf("error image: %v", err)
530-
return
531-
}
532-
go func() {
533-
_ = tarball.WriteToFile(fp.Name(), tag, img, tarball.WithProgress(c))
534-
}()
535-
for update := range c {
536-
switch {
537-
case update.Error != nil && update.Error == io.EOF:
538-
fmt.Fprintf(os.Stderr, "receive error message: %v\n", err)
539-
fmt.Printf("%d/%d", update.Complete, update.Total)
540-
// Output: 2800640/2800640
541-
return
542-
case update.Error != nil:
543-
fmt.Printf("error writing tarball: %v\n", update.Error)
544-
return
545-
default:
546-
fmt.Fprintf(os.Stderr, "receive update: %#v\n", update)
547-
}
548-
}
549-
}

0 commit comments

Comments
 (0)