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

Ensure that invalid versions do not cause a nil panic #1781

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions pkg/releases/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,29 @@ func CheckAndLogRunE() cobrautil.CobraRunFunc {
return nil

case UpdateAvailable:
if release == nil {
log.Ctx(ctx).Warn().Msg("unable to check for or load the new SpiceDB version")
return nil
}

log.Ctx(ctx).Warn().Str("this-version", currentVersion).Str("latest-released-version", release.Version).Msgf("this version of SpiceDB is out of date. See: %s", release.ViewURL)
return nil

case UpToDate:
if release == nil {
log.Ctx(ctx).Warn().Msg("unable to check for or load the new SpiceDB version")
return nil
}

log.Ctx(ctx).Info().Str("latest-released-version", release.Version).Msg("this is the latest released version of SpiceDB")
return nil

case Unknown:
if release == nil {
log.Ctx(ctx).Warn().Msg("unable to check for or load the new SpiceDB version")
return nil
}

log.Ctx(ctx).Warn().Str("unknown-released-version", release.Version).Msg("unable to check for a new SpiceDB version")
return nil

Expand Down
2 changes: 2 additions & 0 deletions pkg/releases/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"runtime/debug"

"github.com/jzelinskie/cobrautil/v2"
"github.com/rs/zerolog/log"
"golang.org/x/mod/semver"
)

Expand Down Expand Up @@ -59,6 +60,7 @@ func CheckIsLatestVersion(
}

if !semver.IsValid(release.Version) {
log.Warn().Str("version", release.Version).Msg("invalid version")
return Unknown, currentVersion, nil, err
}

Expand Down
29 changes: 16 additions & 13 deletions pkg/releases/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,38 @@ import (
)

type testCase struct {
name string
version string
releaseVersion string
expectedState SoftwareUpdateState
name string
version string
releaseVersion string
expectedState SoftwareUpdateState
expectReleaseNil bool
}

func TestCheckIsLatestVersion(t *testing.T) {
testCases := []testCase{
{"up to date", "v1.5.6", "v1.5.6", UpToDate},
{"ahead of version", "v1.7.0", "v1.5.6", UpToDate},
{"new version", "v1.5.6", "v1.5.7", UpdateAvailable},
{"new minor version", "v1.5.6", "v1.6.0", UpdateAvailable},
{"new major version", "v1.5.6", "v2.0.0", UpdateAvailable},
{"invalid version", "abcdef", "v1.6.0", UnreleasedVersion},
{"empty version", "", "v1.6.0", UnreleasedVersion},
{"invalid release version", "v1.5.6", "abderf", Unknown},
{"up to date", "v1.5.6", "v1.5.6", UpToDate, false},
{"ahead of version", "v1.7.0", "v1.5.6", UpToDate, false},
{"new version", "v1.5.6", "v1.5.7", UpdateAvailable, false},
{"new minor version", "v1.5.6", "v1.6.0", UpdateAvailable, false},
{"new major version", "v1.5.6", "v2.0.0", UpdateAvailable, false},
{"invalid version", "abcdef", "v1.6.0", UnreleasedVersion, true},
{"empty version", "", "v1.6.0", UnreleasedVersion, true},
{"invalid release version", "v1.5.6", "abderf", Unknown, true},
{"invalid release version string", "v1.5.6", "1.7.8", Unknown, true},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
state, _, _, _ := CheckIsLatestVersion(context.Background(), func() (string, error) {
state, _, release, _ := CheckIsLatestVersion(context.Background(), func() (string, error) {
return tc.version, nil
}, func(ctx context.Context) (*Release, error) {
return &Release{
Version: tc.releaseVersion,
}, nil
})
require.Equal(t, tc.expectedState, state)
require.Equal(t, tc.expectReleaseNil, release == nil)
})
}
}
Loading