Skip to content

Commit f7fafba

Browse files
committed
Merge branch 'sh-fix-gocloud-cache-modtime' into 'main'
Fix cache extractor redownloading up-to-date caches for Go Cloud URLs See merge request https://gitlab.com/gitlab-org/gitlab-runner/-/merge_requests/5394 Merged-by: Stan Hu <[email protected]> Approved-by: Axel von Bertoldi <[email protected]> Reviewed-by: Axel von Bertoldi <[email protected]>
2 parents 0234420 + daa6993 commit f7fafba

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

commands/helpers/cache_extractor.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ func (c *CacheExtractorCommand) getClient() *CacheClient {
5050
}
5151

5252
func checkIfUpToDate(path string, resp *http.Response) (bool, time.Time) {
53-
fi, _ := os.Lstat(path)
5453
date, _ := time.Parse(http.TimeFormat, resp.Header.Get("Last-Modified"))
55-
return fi != nil && !date.After(fi.ModTime()), date
54+
return isLocalCacheFileUpToDate(path, date), date
55+
}
56+
57+
func isLocalCacheFileUpToDate(path string, date time.Time) bool {
58+
fi, _ := os.Lstat(path)
59+
return fi != nil && !date.After(fi.ModTime())
5660
}
5761

5862
func getRemoteCacheSize(resp *http.Response) int64 {
@@ -151,6 +155,11 @@ func (c *CacheExtractorCommand) handleGoCloudURL() error {
151155
return err
152156
}
153157

158+
if isLocalCacheFileUpToDate(c.File, attrs.ModTime) {
159+
logrus.Infoln(filepath.Base(c.File), "is up to date")
160+
return nil
161+
}
162+
154163
reader, err := b.NewReader(ctx, objectName, nil)
155164
if err != nil {
156165
return err

commands/helpers/cache_extractor_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,25 @@ func TestCacheExtractorRemoteServerFailOnInvalidServer(t *testing.T) {
288288
_, err := os.Stat(cacheExtractorTestArchivedFile)
289289
assert.Error(t, err)
290290
}
291+
292+
func TestIsLocalCacheFileUpToDate(t *testing.T) {
293+
tmpDir := t.TempDir()
294+
cacheFile := path.Join(tmpDir, "cache-file")
295+
296+
// Create cache file
297+
err := os.WriteFile(cacheFile, []byte("test content"), 0644)
298+
require.NoError(t, err)
299+
300+
// Set a specific modification time
301+
modTime := time.Now()
302+
err = os.Chtimes(cacheFile, modTime, modTime)
303+
require.NoError(t, err)
304+
305+
// Test when remote file is older (cache is up to date)
306+
result := isLocalCacheFileUpToDate(cacheFile, modTime.Add(-1*time.Hour))
307+
require.True(t, result, "Cache should be up to date when remote file is older")
308+
309+
// Test when remote file is newer (cache is outdated)
310+
result = isLocalCacheFileUpToDate(cacheFile, modTime.Add(1*time.Hour))
311+
require.False(t, result, "Cache should be outdated when remote file is newer")
312+
}

0 commit comments

Comments
 (0)