Skip to content

Commit

Permalink
added unit tests and removed log entry
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Trabelsi <tobias.trabelsi@dbschenker.com>
  • Loading branch information
Tobias Trabelsi committed Jul 28, 2022
1 parent 18e4afa commit 5325d71
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
8 changes: 1 addition & 7 deletions pkg/oci/mutate/signatures.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package mutate

import (
"log"
"time"

v1 "github.com/google/go-containerregistry/pkg/v1"
Expand All @@ -42,13 +41,8 @@ func AppendSignatures(base oci.Signatures, sigs ...oci.Signature) (oci.Signature

img, err := mutate.Append(base, adds...)

imgCfg, _ := img.ConfigFile()

// Set the Created date to time of execution
if imgCfg.Created.Time.IsZero() {
log.Println("Signed Entry Date was Zero, Setting to current time")
img, _ = mutate.CreatedAt(img, v1.Time{Time: time.Now()})
}
img, _ = mutate.CreatedAt(img, v1.Time{Time: time.Now()})

if err != nil {
return nil, err
Expand Down
6 changes: 6 additions & 0 deletions pkg/oci/mutate/signatures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package mutate

import (
"testing"
"time"

"github.com/sigstore/cosign/pkg/oci/empty"
"github.com/sigstore/cosign/pkg/oci/static"
Expand Down Expand Up @@ -70,4 +71,9 @@ func TestAppendSignatures(t *testing.T) {
} else if got, want := len(sl), 3; got != want {
t.Errorf("len(Get()) = %d, wanted %d", got, want)
}

testCfg, _ := threeSig.ConfigFile()
if testCfg.Created.Time.IsZero() {
t.Errorf("Date of Signature was Zero. Should be %s", time.Now())
}
}
8 changes: 1 addition & 7 deletions pkg/oci/static/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package static

import (
"io"
"log"
"time"

v1 "github.com/google/go-containerregistry/pkg/v1"
Expand All @@ -44,13 +43,8 @@ func NewFile(payload []byte, opts ...Option) (oci.File, error) {
Layer: layer,
})

imgCfg, _ := img.ConfigFile()

// Set the Created date to time of execution
if imgCfg.Created.Time.IsZero() {
log.Println("Signed Entry Date was Zero, Setting to current time")
img, err = mutate.CreatedAt(img, v1.Time{Time: time.Now()})
}
img, err = mutate.CreatedAt(img, v1.Time{Time: time.Now()})

if err != nil {
return nil, err
Expand Down
10 changes: 10 additions & 0 deletions pkg/oci/static/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,14 @@ func TestNewFile(t *testing.T) {
t.Errorf("Payload() = %s, wanted %s", got, want)
}
})

t.Run("check date", func(t *testing.T) {
fileCfg, err := file.ConfigFile()
if err != nil {
t.Fatalf("FileCfg() = %v", err)
}
if fileCfg.Created.Time.IsZero() {
t.Errorf("Date of Signature was Zero")
}
})
}

0 comments on commit 5325d71

Please sign in to comment.