Skip to content

Commit

Permalink
refactor test
Browse files Browse the repository at this point in the history
Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
  • Loading branch information
somtochiama committed Jan 25, 2024
1 parent 8687514 commit f155227
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
13 changes: 7 additions & 6 deletions oci/client/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,34 +107,35 @@ func (c *Client) Pull(ctx context.Context, url, outPath string, opts ...PullOpti
return nil, fmt.Errorf("index '%d' out of bound for '%d' layers in artifact", o.layerIndex, len(layers))
}

err = extractLayer(layers[o.layerIndex], outPath, o)
err = extractLayer(layers[o.layerIndex], outPath, o.layerType)
if err != nil {
return nil, err
}
return meta, nil
}

// extractLayer extracts the Layer to the path
func extractLayer(layer gcrv1.Layer, path string, opts *PullOptions) error {
func extractLayer(layer gcrv1.Layer, path string, layerType LayerType) error {
var blob io.Reader
blob, err := layer.Compressed()
if err != nil {
return fmt.Errorf("extracting layer failed: %w", err)
}

if opts.layerType == "" {
actualLayerType := layerType
if actualLayerType == "" {
bufReader := bufio.NewReader(blob)
if ok, _ := isGzipBlob(bufReader); ok {
opts.layerType = LayerTypeTarball
actualLayerType = LayerTypeTarball
} else {
opts.layerType = LayerTypeStatic
actualLayerType = LayerTypeStatic
}
// the bufio.Reader has read the bytes from the io.Reader
// and should be used instead
blob = bufReader
}

return extractLayerType(path, blob, opts.layerType)
return extractLayerType(path, blob, actualLayerType)
}

// extractLayerType extracts the contents of a io.Reader to the given path.
Expand Down
40 changes: 21 additions & 19 deletions oci/client/push_pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,32 @@ func Test_Push_Pull(t *testing.T) {
pushOpts []PushOption
pullOpts []PullOption
pushFn func(url string, path string) error
testLayerType LayerType
testLayerIndex int
expectedNumLayers int
expectPullErr bool
expectPushErr bool
expectedPullErr bool
expectedPushErr bool
expectedMediaType types.MediaType
}{
{
name: "push directory (default layer type)",
tag: "v0.0.1",
sourcePath: "testdata/artifact",
testLayerType: LayerTypeTarball,
expectedMediaType: oci.CanonicalContentMediaType,
},
{
name: "push directory (specify layer type)",
tag: "v0.0.1",
sourcePath: "testdata/artifact",
expectedMediaType: oci.CanonicalContentMediaType,
name: "push directory (specify layer type)",
tag: "v0.0.1",
sourcePath: "testdata/artifact",
pushOpts: []PushOption{
WithPushLayerType(LayerTypeTarball),
},
pullOpts: []PullOption{
WithPullLayerType(LayerTypeTarball),
},
testLayerType: LayerTypeTarball,
expectedMediaType: oci.CanonicalContentMediaType,
},
{
name: "push static file",
Expand All @@ -89,6 +92,7 @@ func Test_Push_Pull(t *testing.T) {
pullOpts: []PullOption{
WithPullLayerType(LayerTypeStatic),
},
testLayerType: LayerTypeTarball,
expectedMediaType: getLayerMediaType("ml"),
},
{
Expand All @@ -97,7 +101,7 @@ func Test_Push_Pull(t *testing.T) {
pushOpts: []PushOption{
WithPushLayerType(LayerTypeStatic),
},
expectPushErr: true,
expectedPushErr: true,
},
{
name: "push static file without media type extension",
Expand All @@ -106,6 +110,7 @@ func Test_Push_Pull(t *testing.T) {
pushOpts: []PushOption{
WithPushLayerType(LayerTypeStatic),
},
testLayerType: LayerTypeStatic,
expectedMediaType: oci.CanonicalMediaTypePrefix,
},
{
Expand All @@ -115,6 +120,7 @@ func Test_Push_Pull(t *testing.T) {
pullOpts: []PullOption{
WithPullLayerType(LayerTypeStatic),
},
testLayerType: LayerTypeStatic,
expectedMediaType: oci.CanonicalContentMediaType,
},
{
Expand All @@ -123,7 +129,7 @@ func Test_Push_Pull(t *testing.T) {
sourcePath: "testdata/artifact/deployment.yaml",
pullOpts: []PullOption{WithPullLayerType(LayerTypeTarball)},
pushOpts: []PushOption{WithPushLayerType(LayerTypeStatic)},
expectPullErr: true,
expectedPullErr: true,
expectedMediaType: oci.CanonicalMediaTypePrefix,
},
{
Expand Down Expand Up @@ -160,6 +166,7 @@ func Test_Push_Pull(t *testing.T) {
return err
},
testLayerIndex: 1,
testLayerType: LayerTypeTarball,
expectedNumLayers: 2,
expectedMediaType: oci.CanonicalContentMediaType,
},
Expand Down Expand Up @@ -196,7 +203,7 @@ func Test_Push_Pull(t *testing.T) {
return err
},
expectedMediaType: oci.CanonicalContentMediaType,
expectPullErr: true,
expectedPullErr: true,
},
}

Expand All @@ -222,7 +229,7 @@ func Test_Push_Pull(t *testing.T) {
} else {
err = tt.pushFn(url, tt.sourcePath)
}
if tt.expectPushErr {
if tt.expectedPushErr {
g.Expect(err).To(HaveOccurred())
return
}
Expand Down Expand Up @@ -268,23 +275,16 @@ func Test_Push_Pull(t *testing.T) {
g.Expect(meta.Annotations["org.opencontainers.image.licenses"]).To(BeEquivalentTo("Apache-2.0"))
}

po := &PullOptions{
layerType: LayerTypeTarball,
}
for _, opt := range tt.pullOpts {
opt(po)
}

// Pull the artifact from registry and extract its contents to tmp
tmpPath := filepath.Join(t.TempDir(), "artifact")
_, err = c.Pull(ctx, url, tmpPath, tt.pullOpts...)
if tt.expectPullErr {
if tt.expectedPullErr {
g.Expect(err).To(HaveOccurred())
return
}
g.Expect(err).ToNot(HaveOccurred())

switch po.layerType {
switch tt.testLayerType {
case LayerTypeTarball:
// Walk the test directory and check that all files exist in the pulled artifact
fsErr := filepath.Walk(tt.sourcePath, func(path string, info fs.FileInfo, err error) error {
Expand Down Expand Up @@ -312,6 +312,8 @@ func Test_Push_Pull(t *testing.T) {
g.Expect(err).ToNot(HaveOccurred())

g.Expect(expected).To(Equal(got))
default:
t.Errorf("no layer type specified for test")
}
})
}
Expand Down

0 comments on commit f155227

Please sign in to comment.