Skip to content

Commit

Permalink
layer: add filesystem MediaType
Browse files Browse the repository at this point in the history
To enable mounting of node filesystems, a new MediaType,
'application/x-filesystem' is introduced.
As the fileystem does not provide a ReaderAt, the Reader function has
been updated with a check to return an error if the layer
does not have a ReaderAt.

Signed-off-by: Matthias Meidinger <[email protected]>
Signed-off-by: Piotr Rygielski <[email protected]>
  • Loading branch information
dependabot[bot] authored and RTann committed Jun 14, 2024
1 parent 50f9052 commit a883ddf
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
10 changes: 10 additions & 0 deletions layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ func (l *Layer) Init(ctx context.Context, desc *LayerDescription, r io.ReaderAt)
return fmt.Errorf("claircore: layer %v: unable to create fs.FS: %w", desc.Digest, err)
}
l.sys = sys
case `application/vnd.claircore.filesystem`:
if desc.URI == "" {
return fmt.Errorf("claircore: layer %v: unable to create fs.FS: no URI provided", desc.Digest)
}
sys := os.DirFS(desc.URI)
l.sys = sys
l.rd = nil // The reader cannot be used for a filesystem
default:
return fmt.Errorf("claircore: layer %v: unknown MediaType %q", desc.Digest, desc.MediaType)
}
Expand Down Expand Up @@ -196,6 +203,9 @@ func (l *Layer) Reader() (ReadAtCloser, error) {
if !l.init {
return nil, errors.New("claircore: unable to return Reader: uninitialized Layer")
}
if l.rd == nil {
return nil, errors.New("claircore: unable to return Reader: ReaderAt is nil")
}
// Some hacks for making the returned ReadAtCloser implements as many
// interfaces as possible.
switch rd := l.rd.(type) {
Expand Down
59 changes: 59 additions & 0 deletions layer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ func TestLayer(t *testing.T) {
return &l
}

goodFilesystem := func(t *testing.T) *claircore.Layer {
t.Helper()
var l claircore.Layer
desc := claircore.LayerDescription{
Digest: "sha256:" + strings.Repeat("00c0ffee", 8),
MediaType: `application/vnd.claircore.filesystem`,
URI: t.TempDir(),
}

if err := l.Init(ctx, &desc, nil); err != nil {
t.Fatalf("unexpected error: %v", err)
}
return &l
}

t.Run("Init", func(t *testing.T) {
t.Run("Checksum", func(t *testing.T) {
var l claircore.Layer
Expand Down Expand Up @@ -94,6 +109,25 @@ func TestLayer(t *testing.T) {
t.Error("unexpected success")
}
})
t.Run("Filesystem", func(t *testing.T) {
l := goodFilesystem(t)
if err := l.Close(); err != nil {
t.Errorf("close error: %v", err)
}
})
t.Run("FilesystemNoURI", func(t *testing.T) {
var l claircore.Layer
desc := claircore.LayerDescription{
Digest: "sha256:" + strings.Repeat("00c0ffee", 8),
MediaType: `application/vnd.claircore.filesystem`,
URI: "",
}

if err := l.Init(ctx, &desc, nil); err == nil {
t.Error("unexpected success")
}
})

})
t.Run("Close", func(t *testing.T) {
t.Run("Success", func(t *testing.T) {
Expand Down Expand Up @@ -177,6 +211,19 @@ func TestLayer(t *testing.T) {
t.Error("unexpected error")
}
})
t.Run("FilesystemSuccess", func(t *testing.T) {
l := goodFilesystem(t)
t.Cleanup(func() {
if err := l.Close(); err != nil {
t.Errorf("close error: %v", err)
}
})
_, err := l.FS()
t.Logf("error: %v", err)
if err != nil {
t.Error("unexpected error")
}
})
})
t.Run("Reader", func(t *testing.T) {
t.Run("Fail", func(t *testing.T) {
Expand Down Expand Up @@ -208,5 +255,17 @@ func TestLayer(t *testing.T) {
t.Errorf("unexpected error: read %d bytes, got error: %v", n, err)
}
})
t.Run("Filesystem", func(t *testing.T) {
l := goodFilesystem(t)
t.Cleanup(func() {
if err := l.Close(); err != nil {
t.Errorf("close error: %v", err)
}
})
_, err := l.Reader()
if err == nil {
t.Error("unexpected success")
}
})
})
}

0 comments on commit a883ddf

Please sign in to comment.