Skip to content

Commit

Permalink
Run harness tests against 2 previous versions and HEAD
Browse files Browse the repository at this point in the history
Signed-off-by: Priya Wadhwa <priya@chainguard.dev>
  • Loading branch information
priyawadhwa committed Sep 7, 2022
1 parent a6e58f7 commit 3f68ab0
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 23 deletions.
19 changes: 19 additions & 0 deletions cmd/rekor-cli/app/pflag_groups.go
Expand Up @@ -199,3 +199,22 @@ func ParseTypeFlag(typeStr string) (string, string, error) {
}
return "", "", errors.New("malformed type string")
}

func GetSupportedVersions(typeStr string) ([]string, error) {
typeStrings := strings.SplitN(typeStr, ":", 2)
tf, ok := types.TypeMap.Load(typeStrings[0])
if !ok {
return nil, fmt.Errorf("unknown type %v", typeStrings[0])
}
ti := tf.(func() types.TypeImpl)()
if ti == nil {
return nil, fmt.Errorf("type %v is not implemented", typeStrings[0])
}
switch len(typeStrings) {
case 1:
return ti.SupportedVersions(), nil
case 2:
return []string{typeStrings[1]}, nil
}
return nil, errors.New("malformed type string")
}
32 changes: 31 additions & 1 deletion cmd/rekor-cli/app/pflags_test.go
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/spf13/cobra"
"github.com/spf13/viper"

Expand Down Expand Up @@ -762,7 +763,7 @@ func TestParseTypeFlag(t *testing.T) {
{
caseDesc: "explicit intoto v0.0.1",
typeStr: "intoto:0.0.1",
expectSuccess: false,
expectSuccess: true,
},
{
caseDesc: "explicit intoto v0.0.2",
Expand Down Expand Up @@ -842,3 +843,32 @@ func TestParseTypeFlag(t *testing.T) {
}
}
}

func TestGetSupportedVersions(t *testing.T) {
tests := []struct {
description string
typeStr string
expectedVersions []string
}{
{
description: "intoto specified with version",
typeStr: "intoto:0.0.1",
expectedVersions: []string{"0.0.1"},
}, {
description: "intoto no version specified",
typeStr: "intoto",
expectedVersions: []string{"0.0.2", "0.0.1"},
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
got, err := GetSupportedVersions(test.typeStr)
if err != nil {
t.Fatal(err)
}
if d := cmp.Diff(test.expectedVersions, got); d != "" {
t.Fatalf("got unexpected versions: %s", d)
}
})
}
}
63 changes: 44 additions & 19 deletions cmd/rekor-cli/app/upload.go
Expand Up @@ -72,11 +72,12 @@ var uploadCmd = &cobra.Command{
if err != nil {
return nil, err
}
var entry models.ProposedEntry
params := entries.NewCreateLogEntryParams()
params.SetTimeout(viper.GetDuration("timeout"))

entryStr := viper.GetString("entry")
var resp *entries.CreateLogEntryCreated

if entryStr != "" {
var entryReader io.Reader
entryURL, err := url.Parse(entryStr)
Expand All @@ -94,36 +95,60 @@ var uploadCmd = &cobra.Command{
return nil, fmt.Errorf("error processing entry file: %w", err)
}
}
entry, err = models.UnmarshalProposedEntry(entryReader, runtime.JSONConsumer())
entry, err := models.UnmarshalProposedEntry(entryReader, runtime.JSONConsumer())
if err != nil {
return nil, fmt.Errorf("error parsing entry file: %w", err)
}
params.SetProposedEntry(entry)
r, err := rekorClient.Entries.CreateLogEntry(params)
if err != nil {
switch e := err.(type) {
case *entries.CreateLogEntryConflict:
return &uploadCmdOutput{
Location: e.Location.String(),
AlreadyExists: true,
}, nil
default:
return nil, err
}
}
resp = r
} else {
typeStr, versionStr, err := ParseTypeFlag(viper.GetString("type"))
// Start with the default entry and work your way down
typeStr, _, err := ParseTypeFlag(viper.GetString("type"))
if err != nil {
return nil, err
}
supportedVersions, err := GetSupportedVersions(viper.GetString("type"))
if err != nil {
return nil, err
}

props := CreatePropsFromPflags()

entry, err = types.NewProposedEntry(context.Background(), typeStr, versionStr, *props)
if err != nil {
return nil, fmt.Errorf("error: %w", err)
for _, sv := range supportedVersions {
log.CliLogger.Infof("Trying to upload entry of type %s at version %s", typeStr, sv)
entry, err := types.NewProposedEntry(context.Background(), typeStr, sv, *props)
if err != nil {
return nil, fmt.Errorf("error: %w", err)
}
params.SetProposedEntry(entry)
r, err := rekorClient.Entries.CreateLogEntry(params)
if err == nil {
resp = r
break
}
switch e := err.(type) {
case *entries.CreateLogEntryConflict:
return &uploadCmdOutput{
Location: e.Location.String(),
AlreadyExists: true,
}, nil
}
}
}
params.SetProposedEntry(entry)

resp, err := rekorClient.Entries.CreateLogEntry(params)
if err != nil {
switch e := err.(type) {
case *entries.CreateLogEntryConflict:
return &uploadCmdOutput{
Location: e.Location.String(),
AlreadyExists: true,
}, nil
default:
return nil, err
}
if resp == nil {
return nil, fmt.Errorf("no valid entry was created")
}

var newIndex int64
Expand Down
2 changes: 1 addition & 1 deletion pkg/types/intoto/intoto.go
Expand Up @@ -78,7 +78,7 @@ func (it BaseIntotoType) DefaultVersion() string {
// it deliberately omits 0.0.1 from the list of supported versions as that
// version did not persist signatures inside the log entry
func (it BaseIntotoType) SupportedVersions() []string {
return []string{"0.0.2"}
return []string{"0.0.2", "0.0.1"}
}

// IsSupportedVersion returns true if the version can be inserted into the log, and false if not
Expand Down
11 changes: 9 additions & 2 deletions tests/rekor-harness.sh
Expand Up @@ -87,10 +87,17 @@ function run_tests () {
fi
}

# Get last 3 server versions
# Get last 2 server versions
git fetch origin
NUM_VERSIONS_TO_TEST=3
NUM_VERSIONS_TO_TEST=2
VERSIONS=$(git tag --sort=-version:refname | head -n $NUM_VERSIONS_TO_TEST | tac)

# Also add the commit @ HEAD
HEAD=$(git log --pretty="%H" -n 1 )
echo "Also testing at HEAD at commit $HEAD"

VERSIONS="$VERSIONS $HEAD"

echo $VERSIONS

export REKOR_HARNESS_TMPDIR="$(mktemp -d -t rekor_test_harness.XXXXXX)"
Expand Down

0 comments on commit 3f68ab0

Please sign in to comment.