Skip to content

Commit

Permalink
feat: add setMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
wobito committed Sep 15, 2023
1 parent e9e4c73 commit f50e8cd
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 0 deletions.
39 changes: 39 additions & 0 deletions pkg/sif/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package sif

import (
"encoding"
"encoding/binary"
"errors"
"fmt"
Expand Down Expand Up @@ -676,3 +677,41 @@ func (f *FileImage) SetPrimPart(id uint32, opts ...SetOpt) error {

return nil
}

// SetMetadata writes the mutated descriptors and returns an error if any.
func (f *FileImage) SetMetadata(id uint32, md encoding.BinaryMarshaler, opts ...SetOpt) error {
so := setOpts{}

if !f.isDeterministic() {
so.t = time.Now()
}

for _, opt := range opts {
if err := opt(&so); err != nil {
return fmt.Errorf("%w", err)
}
}

rd, err := f.getDescriptor(WithID(id))
if err != nil {
return fmt.Errorf("%w", err)
}

if err := rd.setExtra(md); err != nil {
return fmt.Errorf("%w", err)
}

rd.ModifiedAt = so.t.Unix()

if err := f.writeDescriptors(); err != nil {
return fmt.Errorf("%w", err)
}

f.h.ModifiedAt = so.t.Unix()

if err := f.writeHeader(); err != nil {
return fmt.Errorf("%w", err)
}

return nil
}
81 changes: 81 additions & 0 deletions pkg/sif/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,3 +649,84 @@ func TestSetPrimPart(t *testing.T) {
})
}
}

func TestSetMetadata(t *testing.T) {
tests := []struct {
name string
createOpts []CreateOpt
id uint32
opts []SetOpt
wantErr error
}{
{
name: "Deterministic",
createOpts: []CreateOpt{
OptCreateWithID("de170c43-36ab-44a8-bca9-1ea1a070a274"),
OptCreateWithDescriptors(
getDescriptorInput(t, DataOCIBlob, []byte{0xfa, 0xce}),
),
OptCreateWithTime(time.Unix(946702800, 0)),
},
id: 1,
opts: []SetOpt{
OptSetDeterministic(),
},
},
{
name: "WithTime",
createOpts: []CreateOpt{
OptCreateDeterministic(),
OptCreateWithDescriptors(
getDescriptorInput(t, DataOCIBlob, []byte{0xfa, 0xce}),
),
},
id: 1,
opts: []SetOpt{
OptSetWithTime(time.Unix(946702800, 0)),
},
},
{
name: "One",
createOpts: []CreateOpt{
OptCreateDeterministic(),
OptCreateWithDescriptors(
getDescriptorInput(t, DataOCIBlob, []byte{0xfa, 0xce}),
),
},
id: 1,
},
{
name: "Two",
createOpts: []CreateOpt{
OptCreateDeterministic(),
OptCreateWithDescriptors(
getDescriptorInput(t, DataOCIBlob, []byte{0xfa, 0xce}),
getDescriptorInput(t, DataOCIBlob, []byte{0xfe, 0xed}),
),
},
id: 2,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var b Buffer

f, err := CreateContainer(&b, tt.createOpts...)
if err != nil {
t.Fatal(err)
}

if got, want := f.SetMetadata(tt.id, newOCIBlobDigest(), tt.opts...), tt.wantErr; !errors.Is(got, want) {
t.Errorf("got error %v, want %v", got, want)
}

if err := f.UnloadContainer(); err != nil {
t.Error(err)
}

g := goldie.New(t, goldie.WithTestNameForDir(true))
g.Assert(t, tt.name, b.Bytes())
})
}
}
6 changes: 6 additions & 0 deletions pkg/sif/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ func (d rawDescriptor) isPartitionOfType(pt PartType) bool {
return t == pt
}

// isDeterministic returns true if the timestamps in d are set to
// deterministic values.
func (d rawDescriptor) isDeterministic() bool {
return (time.Unix(d.CreatedAt, 0).IsZero() && time.Unix(d.ModifiedAt, 0).IsZero())
}

// Descriptor represents the SIF descriptor type.
type Descriptor struct {
r io.ReaderAt // Backing storage.
Expand Down
Binary file not shown.
Binary file not shown.
Binary file added pkg/sif/testdata/TestSetMetadata/One.golden
Binary file not shown.
Binary file added pkg/sif/testdata/TestSetMetadata/Two.golden
Binary file not shown.
Binary file added pkg/sif/testdata/TestSetMetadata/WithTime.golden
Binary file not shown.

0 comments on commit f50e8cd

Please sign in to comment.