Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add setMetadata #324

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.