Skip to content

Lightweight server monitoring with historical data, docker stats, and alerts.

fix(agent): refresh dnf5 metadata copy before each update check (#2790)

crispy3392dcc6f0c9
diff --git a/agent/package_updates.go b/agent/package_updates.go
index 4e81c37e4..d9bd20552 100644
--- a/agent/package_updates.go
+++ b/agent/package_updates.go
@@ -224,16 +224,27 @@ func checkApt(ctx context.Context) (packageUpdatesResult, error) {
// newDnfCheck points dnf5's per-user state (log) and cache dirs into dataDir.
// They default to $HOME/.local/state and $HOME/.cache, and the agent user's home
-// is usually /nonexistent. With -C, dnf5 still reads the system metadata cache.
+// is usually /nonexistent. As non-root, dnf5 reads metadata from its own cache
+// and copies the system cache there only when it has none, and with -C any copy
+// counts as usable however old it is. So the copy is removed before each check
+// to pick up the system metadata that dnf-makecache refreshes.
func newDnfCheck(dataDir string) packageUpdatesCheck {
var env []string
+ var userCacheDir string
if dataDir != "" {
+ cacheHome := filepath.Join(dataDir, "cache")
env = []string{
"XDG_STATE_HOME=" + filepath.Join(dataDir, "state"),
- "XDG_CACHE_HOME=" + filepath.Join(dataDir, "cache"),
+ "XDG_CACHE_HOME=" + cacheHome,
}
+ userCacheDir = filepath.Join(cacheHome, "libdnf5")
}
return func(ctx context.Context) (packageUpdatesResult, error) {
+ if userCacheDir != "" {
+ if err := os.RemoveAll(userCacheDir); err != nil {
+ slog.Debug("Package updates: remove dnf5 cache", "err", err)
+ }
+ }
return checkDnf(ctx, env)
}
}
diff --git a/agent/package_updates_test.go b/agent/package_updates_test.go
index 7bc1e368c..277e193a1 100644
--- a/agent/package_updates_test.go
+++ b/agent/package_updates_test.go
@@ -327,6 +327,25 @@ exit 1`,
want := filepath.Join(dataDir, "state") + " " + filepath.Join(dataDir, "cache") + "\n"
assert.Equal(t, want+want, string(env))
})
+
+ t.Run("stale dnf5 metadata copy removed before check", func(t *testing.T) {
+ dataDir := t.TempDir()
+ repomd := filepath.Join(dataDir, "cache", "libdnf5", "fedora", "repomd.xml")
+ staleFile := filepath.Join(t.TempDir(), "stale")
+ // fake dnf5 copies the system cache only when its own copy is missing
+ fakeCommands(t, map[string]string{
+ "dnf": `case "$*" in *--security*) exit 0 ;; esac
+if [ -e "` + repomd + `" ]; then echo stale >> "` + staleFile + `"; fi
+mkdir -p "$(dirname "` + repomd + `")" && touch "` + repomd + `"`,
+ })
+ check := newDnfCheck(dataDir)
+ _, err := check(context.Background())
+ require.NoError(t, err)
+ require.FileExists(t, repomd)
+ _, err = check(context.Background())
+ require.NoError(t, err)
+ assert.NoFileExists(t, staleFile)
+ })
}
func TestDetectPackageManager(t *testing.T) {