Skip to content

Commit f4a7fc3

Browse files
committed
Run harness tests against 2 previous versions and HEAD
Signed-off-by: Priya Wadhwa <priya@chainguard.dev>
1 parent a6e58f7 commit f4a7fc3

File tree

5 files changed

+89
-21
lines changed

5 files changed

+89
-21
lines changed

cmd/rekor-cli/app/pflag_groups.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,22 @@ func ParseTypeFlag(typeStr string) (string, string, error) {
199199
}
200200
return "", "", errors.New("malformed type string")
201201
}
202+
203+
func GetSupportedVersions(typeStr string) ([]string, error) {
204+
typeStrings := strings.SplitN(typeStr, ":", 2)
205+
tf, ok := types.TypeMap.Load(typeStrings[0])
206+
if !ok {
207+
return nil, fmt.Errorf("unknown type %v", typeStrings[0])
208+
}
209+
ti := tf.(func() types.TypeImpl)()
210+
if ti == nil {
211+
return nil, fmt.Errorf("type %v is not implemented", typeStrings[0])
212+
}
213+
switch len(typeStrings) {
214+
case 1:
215+
return ti.SupportedVersions(), nil
216+
case 2:
217+
return []string{typeStrings[1]}, nil
218+
}
219+
return nil, errors.New("malformed type string")
220+
}

cmd/rekor-cli/app/pflags_test.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"os"
2424
"testing"
2525

26+
"github.com/google/go-cmp/cmp"
2627
"github.com/spf13/cobra"
2728
"github.com/spf13/viper"
2829

@@ -762,7 +763,7 @@ func TestParseTypeFlag(t *testing.T) {
762763
{
763764
caseDesc: "explicit intoto v0.0.1",
764765
typeStr: "intoto:0.0.1",
765-
expectSuccess: false,
766+
expectSuccess: true,
766767
},
767768
{
768769
caseDesc: "explicit intoto v0.0.2",
@@ -842,3 +843,32 @@ func TestParseTypeFlag(t *testing.T) {
842843
}
843844
}
844845
}
846+
847+
func TestGetSupportedVersions(t *testing.T) {
848+
tests := []struct {
849+
description string
850+
typeStr string
851+
expectedVersions []string
852+
}{
853+
{
854+
description: "intoto specified with version",
855+
typeStr: "intoto:0.0.1",
856+
expectedVersions: []string{"0.0.1"},
857+
}, {
858+
description: "intoto no version specified",
859+
typeStr: "intoto",
860+
expectedVersions: []string{"0.0.2", "0.0.1"},
861+
},
862+
}
863+
for _, test := range tests {
864+
t.Run(test.description, func(t *testing.T) {
865+
got, err := GetSupportedVersions(test.typeStr)
866+
if err != nil {
867+
t.Fatal(err)
868+
}
869+
if d := cmp.Diff(test.expectedVersions, got); d != "" {
870+
t.Fatalf("got unexpected versions: %s", d)
871+
}
872+
})
873+
}
874+
}

cmd/rekor-cli/app/upload.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ var uploadCmd = &cobra.Command{
7777
params.SetTimeout(viper.GetDuration("timeout"))
7878

7979
entryStr := viper.GetString("entry")
80+
var resp *entries.CreateLogEntryCreated
81+
8082
if entryStr != "" {
8183
var entryReader io.Reader
8284
entryURL, err := url.Parse(entryStr)
@@ -99,31 +101,41 @@ var uploadCmd = &cobra.Command{
99101
return nil, fmt.Errorf("error parsing entry file: %w", err)
100102
}
101103
} else {
102-
typeStr, versionStr, err := ParseTypeFlag(viper.GetString("type"))
104+
// Start with the default entry and work your way down
105+
typeStr, _, err := ParseTypeFlag(viper.GetString("type"))
106+
if err != nil {
107+
return nil, err
108+
}
109+
supportedVersions, err := GetSupportedVersions(viper.GetString("type"))
103110
if err != nil {
104111
return nil, err
105112
}
106-
107113
props := CreatePropsFromPflags()
108114

109-
entry, err = types.NewProposedEntry(context.Background(), typeStr, versionStr, *props)
110-
if err != nil {
111-
return nil, fmt.Errorf("error: %w", err)
115+
for _, sv := range supportedVersions {
116+
log.CliLogger.Infof("Trying to upload entry of type %s at version %s", typeStr, sv)
117+
entry, err = types.NewProposedEntry(context.Background(), typeStr, sv, *props)
118+
if err != nil {
119+
return nil, fmt.Errorf("error: %w", err)
120+
}
121+
params.SetProposedEntry(entry)
122+
r, err := rekorClient.Entries.CreateLogEntry(params)
123+
if err == nil {
124+
resp = r
125+
break
126+
}
127+
switch e := err.(type) {
128+
case *entries.CreateLogEntryConflict:
129+
return &uploadCmdOutput{
130+
Location: e.Location.String(),
131+
AlreadyExists: true,
132+
}, nil
133+
}
112134
}
113135
}
114-
params.SetProposedEntry(entry)
115136

116-
resp, err := rekorClient.Entries.CreateLogEntry(params)
117-
if err != nil {
118-
switch e := err.(type) {
119-
case *entries.CreateLogEntryConflict:
120-
return &uploadCmdOutput{
121-
Location: e.Location.String(),
122-
AlreadyExists: true,
123-
}, nil
124-
default:
125-
return nil, err
126-
}
137+
if resp == nil {
138+
return nil, fmt.Errorf("no valid entry was created")
127139
}
128140

129141
var newIndex int64

pkg/types/intoto/intoto.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (it BaseIntotoType) DefaultVersion() string {
7878
// it deliberately omits 0.0.1 from the list of supported versions as that
7979
// version did not persist signatures inside the log entry
8080
func (it BaseIntotoType) SupportedVersions() []string {
81-
return []string{"0.0.2"}
81+
return []string{"0.0.2", "0.0.1"}
8282
}
8383

8484
// IsSupportedVersion returns true if the version can be inserted into the log, and false if not

tests/rekor-harness.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,17 @@ function run_tests () {
8787
fi
8888
}
8989

90-
# Get last 3 server versions
90+
# Get last 2 server versions
9191
git fetch origin
92-
NUM_VERSIONS_TO_TEST=3
92+
NUM_VERSIONS_TO_TEST=2
9393
VERSIONS=$(git tag --sort=-version:refname | head -n $NUM_VERSIONS_TO_TEST | tac)
94+
95+
# Also add the commit @ HEAD
96+
HEAD=$(git log --pretty="%H" -n 1 )
97+
echo "Also testing at HEAD at commit $HEAD"
98+
99+
VERSIONS="$VERSIONS $HEAD"
100+
94101
echo $VERSIONS
95102

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

0 commit comments

Comments
 (0)