Skip to content

Commit

Permalink
Fix EOL package marking. (#226)
Browse files Browse the repository at this point in the history
Previously we always assumed we were not EOL if we weren't in the EOL
window, but this was incorrect. Fixes the behavior to correctly set the
EOL value for package versions.
  • Loading branch information
wlynch authored Dec 30, 2024
1 parent 1d9dec2 commit ca217eb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 25 deletions.
19 changes: 12 additions & 7 deletions internal/provider/data_source_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func calculate(ctx context.Context, client registry.RegistryClient, pkg string,
continue
}

insideEOLGracePeriodWindow, err := checkEOLGracePeriodWindow(pv.EolDate, vproto.GracePeriodMonths)
isEOL, insideEOLGracePeriodWindow, err := checkEOLGracePeriodWindow(pv.EolDate, vproto.GracePeriodMonths)
if err != nil {
return nil, nil, nil, []diag.Diagnostic{errorToDiagnostic(err, "failed to calculate EOL grace period")}
}
Expand All @@ -425,7 +425,7 @@ func calculate(ctx context.Context, client registry.RegistryClient, pkg string,

vname := key + "-" + pv.Version
model := versionsDataSourceVersionMapModel{
Eol: true,
Eol: isEOL,
EolDate: pv.EolDate,
Exists: pv.Exists,
Fips: pv.Fips,
Expand All @@ -451,15 +451,20 @@ func calculate(ctx context.Context, client registry.RegistryClient, pkg string,
return vproto, vmap, orderedKeys, diags
}

func checkEOLGracePeriodWindow(eolDate string, gracePeriodMonths int64) (bool, error) {
// returns whether we are eol, whether we are in the grace period window, and any error.
func checkEOLGracePeriodWindow(eolDate string, gracePeriodMonths int64) (bool, bool, error) {
t, err := time.Parse(time.DateOnly, eolDate)
if err != nil {
return false, err
return false, false, err
}
// Take the parsed EOL date, fast forward it to X months in the future
// and ensure that it is greater than or equal to right now
t = t.AddDate(0, int(gracePeriodMonths), 0)
return t.Compare(time.Now().UTC()) >= 0, nil
// and ensure that it is greater than or equal to right now.
eol := t.AddDate(0, int(gracePeriodMonths), 0)
now := time.Now().UTC()

// We are EOL if the EOL date is before the current time.
// We are in the grace period window if the EOL grace period date is after the current time.
return t.Before(now), eol.After(now), nil
}

// Variant validates the string value is a valid variant.
Expand Down
73 changes: 55 additions & 18 deletions internal/provider/data_source_versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"errors"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"google.golang.org/grpc/codes"
Expand All @@ -20,6 +21,7 @@ import (
)

func Test_calculate(t *testing.T) {
eolDate := time.Now().Add(-30 * 24 * time.Hour).Format(time.DateOnly)
clients := &platformtest.MockPlatformClients{
RegistryClient: registrytest.MockRegistryClients{
RegistryClient: registrytest.MockRegistryClient{
Expand Down Expand Up @@ -84,6 +86,21 @@ func Test_calculate(t *testing.T) {
},
},
},
{
Given: &registry.PackageVersionMetadataRequest{
Package: "eol",
},
Get: &registry.PackageVersionMetadata{
GracePeriodMonths: 6,
EolVersions: []*registry.PackageVersion{
{
EolDate: eolDate,
Exists: true,
Version: "3.8",
},
},
},
},
},
},
},
Expand Down Expand Up @@ -157,7 +174,7 @@ func Test_calculate(t *testing.T) {
IsLatest: false,
Main: "found-3.8",
Version: "3.8",
Eol: true,
Eol: false,
EolDate: "2924-10-07",
},
"found-3.9": {
Expand Down Expand Up @@ -198,7 +215,7 @@ func Test_calculate(t *testing.T) {
IsLatest: false,
Main: "found-fips-3.8",
Version: "3.8",
Eol: true,
Eol: false,
EolDate: "2924-10-07",
},
"found-fips-3.9": {
Expand Down Expand Up @@ -235,7 +252,7 @@ func Test_calculate(t *testing.T) {
IsLatest: false,
Main: "found-3.8",
Version: "3.8",
Eol: true,
Eol: false,
EolDate: "2924-10-07",
},
"found-3.13": {
Expand Down Expand Up @@ -277,26 +294,46 @@ func Test_calculate(t *testing.T) {
expectedOrderedKeys: []string{},
expectedVersionsMap: map[string]versionsDataSourceVersionMapModel{},
},
{
name: "eol",
pkg: "eol",
expectedOrderedKeys: []string{
"eol-3.8",
},
allow: map[string]struct{}{
"eol-3.8": {},
},
expectedVersionsMap: map[string]versionsDataSourceVersionMapModel{
"eol-3.8": {
Exists: true,
Eol: true,
EolDate: eolDate,
IsLatest: true, // ensure new latest is also identified
Main: "eol-3.8",
Version: "3.8",
},
},
},
}

ctx := context.Background()
testClient := clients.Registry().Registry()

for _, test := range tests {
_, versionsMap, orderedKeys, diagnostic := calculate(ctx, testClient, test.pkg, test.variant, test.allow)
if !diagnostic.HasError() && test.wantError {
t.Errorf("%s: wanted error/diag returned but was nil", test.name)
continue
}
if diagnostic.HasError() && !test.wantError {
t.Errorf("%s: error/diag returned but expected nil: %s", test.name, diagnostic.Errors())
continue
}
if diff := cmp.Diff(test.expectedOrderedKeys, orderedKeys); diff != "" {
t.Errorf("%s: orderedKeys did not match: %s", test.name, diff)
}
if diff := cmp.Diff(test.expectedVersionsMap, versionsMap); diff != "" {
t.Errorf("%s: versionsMap did not match: %s", test.name, diff)
}
t.Run(test.name, func(t *testing.T) {
_, versionsMap, orderedKeys, diagnostic := calculate(ctx, testClient, test.pkg, test.variant, test.allow)
if !diagnostic.HasError() && test.wantError {
t.Fatalf("%s: wanted error/diag returned but was nil", test.name)
}
if diagnostic.HasError() && !test.wantError {
t.Fatalf("%s: error/diag returned but expected nil: %s", test.name, diagnostic.Errors())
}
if diff := cmp.Diff(test.expectedOrderedKeys, orderedKeys); diff != "" {
t.Errorf("%s: orderedKeys did not match: %s", test.name, diff)
}
if diff := cmp.Diff(test.expectedVersionsMap, versionsMap); diff != "" {
t.Errorf("%s: versionsMap did not match: %s", test.name, diff)
}
})
}
}

0 comments on commit ca217eb

Please sign in to comment.