Skip to content

Commit

Permalink
Cache control
Browse files Browse the repository at this point in the history
  • Loading branch information
Blaize Kaye committed Apr 14, 2024
1 parent ba23bc3 commit 25f03ce
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
9 changes: 6 additions & 3 deletions internal/handler/eolfunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type NewEOLDataArgs struct {
func NewEOLData(args NewEOLDataArgs) (*EOLData, error) {

// basic assertions of logic
if args.ForceCacheRefresh && !args.PreventCacheRefresh {
return nil, fmt.Errorf("You cannot Force Cache Refresh AND Prevent Cache Refresh")
if args.ForceCacheRefresh && args.PreventCacheRefresh {
return nil, fmt.Errorf("you cannot Force Cache Refresh AND Prevent Cache Refresh")
}

packages := args.Packages
Expand All @@ -55,6 +55,9 @@ func NewEOLData(args NewEOLDataArgs) (*EOLData, error) {
return nil, err
}
} else if os.IsNotExist(err) {
if args.PreventCacheRefresh {
return nil, fmt.Errorf("cache not found and Prevent Cache Refresh enabled")
}
// Cache file does not exist, fetch data and write to file
endOfLifeInfo := GetEndOfLifeInfo(packages)
data.Packages = endOfLifeInfo
Expand Down Expand Up @@ -91,7 +94,7 @@ func GetEndOfLifeInfo(packageNames []string) map[string][]PackageInfo {

var data []PackageInfo
if err := json.Unmarshal(body, &data); err != nil {
fmt.Printf("Error parsing JSON for %s: %v\n", packageName, err)
fmt.Printf("error parsing JSON for %s: %v\n", packageName, err)
continue
}

Expand Down
34 changes: 34 additions & 0 deletions internal/handler/eolfunctions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ func TestNewEOLData(t *testing.T) {
},
},
},
{
name: "Test Assertion Failure",
wantErr: true,
args: args{
EolArgs: NewEOLDataArgs{
Packages: []string{
"alpine",
},
CacheLocation: "testnocache.json",
ForceCacheRefresh: true,
PreventCacheRefresh: true,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -82,6 +96,9 @@ func TestNewEOLData(t *testing.T) {
t.Errorf("NewEOLData() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err != nil { // We've got an error, and probably don't need to report it, 'cause it was expected
return
}
if len(got.Packages[tt.args.EolArgs.Packages[0]]) == 0 {
t.Errorf("Could not find any Packages for package '%v'\n", tt.args.EolArgs.Packages[0])
}
Expand Down Expand Up @@ -111,6 +128,20 @@ func TestNewEOLDataWithExistingCache(t *testing.T) {
},
},
},
{
name: "Test Nonexistent cache failure",
wantErr: true,
args: args{
EolArgs: NewEOLDataArgs{
Packages: []string{
"alpine",
},
CacheLocation: "doesntexist.json",
ForceCacheRefresh: false,
PreventCacheRefresh: true,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -120,6 +151,9 @@ func TestNewEOLDataWithExistingCache(t *testing.T) {
t.Errorf("NewEOLData() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err != nil {
return
}
if len(got.Packages[tt.args.EolArgs.Packages[0]]) == 0 {
t.Errorf("Could not find any Packages for package '%v'\n", tt.args.EolArgs.Packages[0])
}
Expand Down

0 comments on commit 25f03ce

Please sign in to comment.