Skip to content

Commit

Permalink
Merge pull request #324 from wobito/set-metadata
Browse files Browse the repository at this point in the history
feat: add setMetadata
  • Loading branch information
tri-adam committed Sep 18, 2023
2 parents e9e4c73 + fcd29f3 commit 3e01e9f
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 0 deletions.
43 changes: 43 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,45 @@ func (f *FileImage) SetPrimPart(id uint32, opts ...SetOpt) error {

return nil
}

// SetMetadata the metadata of the data object with id to md, according to opts.
//
// By default, the image/object modification times are set to the current time for
// non-deterministic images, and unset otherwise. To override this, consider using
// OptSetDeterministic or OptSetWithTime.
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())
})
}
}
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 3e01e9f

Please sign in to comment.