Skip to content

Commit a24edad

Browse files
authored
Fix image history file. (#356)
The logic to detect if an image is on a read-only mount is broken. This change fixes it. Also, add some tests to ensure the image history file is being added to the image.
1 parent 6c67e1e commit a24edad

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/testrpms/

toolkit/tools/pkg/imagecustomizerlib/customizepackages_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ func TestCustomizeImagePackagesAddOfflineDir(t *testing.T) {
6262
"/usr/bin/jq",
6363
)
6464

65+
verifyImageHistoryFile(t, 1, config, imageConnection.Chroot().RootDir())
66+
6567
err = imageConnection.CleanClose()
6668
if !assert.NoError(t, err) {
6769
return
@@ -105,6 +107,8 @@ func TestCustomizeImagePackagesAddOfflineDir(t *testing.T) {
105107
"/usr/bin/jq",
106108
"/usr/bin/go",
107109
)
110+
111+
verifyImageHistoryFile(t, 2, config, imageConnection.Chroot().RootDir())
108112
}
109113

110114
func copyRpms(sourceDir string, targetDir string, excludePrefixes []string) error {
@@ -356,6 +360,8 @@ func TestCustomizeImagePackagesSnapshotTime(t *testing.T) {
356360
"snapshotTime %s should install jq version %s, but got: %s", snapshotTime, expectedVersion, jqVersionOutput)
357361

358362
ensureFilesNotExist(t, imageConnection, customTdnfConfRelPath)
363+
364+
verifyImageHistoryFile(t, 1, config, imageConnection.Chroot().RootDir())
359365
}
360366

361367
func TestCustomizeImagePackagesCliSnapshotTimeOverridesConfigFile(t *testing.T) {
@@ -405,6 +411,8 @@ func TestCustomizeImagePackagesCliSnapshotTimeOverridesConfigFile(t *testing.T)
405411
"snapshotTime %s should install jq version %s, but got: %s", snapshotTimeCLI, expectedVersion, jqVersionOutput)
406412

407413
ensureFilesNotExist(t, imageConnection, customTdnfConfRelPath)
414+
415+
verifyImageHistoryFile(t, 1, config, imageConnection.Chroot().RootDir())
408416
}
409417

410418
func TestCustomizeImagePackagesSnapshotTimeWithoutPreviewFlagFails(t *testing.T) {

toolkit/tools/pkg/imagecustomizerlib/imagehistory.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ const (
3333
func addImageHistory(ctx context.Context, imageChroot *safechroot.Chroot, imageUuid string,
3434
baseConfigPath string, toolVersion string, buildTime string, config *imagecustomizerapi.Config,
3535
) error {
36-
canWriteHistoryFile := isPathOnReadOnlyMount(customizerLoggingDir, imageChroot)
37-
if !canWriteHistoryFile {
36+
cannotWriteHistoryFile := isPathOnReadOnlyMount(customizerLoggingDir, imageChroot)
37+
if cannotWriteHistoryFile {
3838
return nil
3939
}
4040

toolkit/tools/pkg/imagecustomizerlib/imagehistory_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,15 @@ func TestAddImageHistory(t *testing.T) {
6161
assert.NotEqual(t, allHistory[0].ImageUuid, allHistory[1].ImageUuid, "imageUuid should be different for each entry")
6262
}
6363

64-
func verifyHistoryFile(t *testing.T, expectedEntries int, expectedUuid string, expectedVersion string, expectedDate string, config imagecustomizerapi.Config, historyFilePath string) (allHistory []ImageHistory) {
64+
func verifyImageHistoryFile(t *testing.T, expectedEntries int, config imagecustomizerapi.Config, rootPath string,
65+
) (allHistory []ImageHistory) {
66+
return verifyHistoryFile(t, expectedEntries, "", ToolVersion, "", config,
67+
filepath.Join(rootPath, customizerLoggingDir, historyFileName))
68+
}
69+
70+
func verifyHistoryFile(t *testing.T, expectedEntries int, expectedUuid string, expectedVersion string,
71+
expectedDate string, config imagecustomizerapi.Config, historyFilePath string,
72+
) (allHistory []ImageHistory) {
6573
exists, err := file.PathExists(historyFilePath)
6674
assert.NoError(t, err, "error checking history file existence")
6775
assert.True(t, exists, "history file should exist")
@@ -75,9 +83,13 @@ func verifyHistoryFile(t *testing.T, expectedEntries int, expectedUuid string, e
7583

7684
// Verify the last entry content
7785
entry := allHistory[expectedEntries-1]
78-
assert.Equal(t, expectedUuid, entry.ImageUuid, "imageUuid should match")
86+
if expectedUuid != "" {
87+
assert.Equal(t, expectedUuid, entry.ImageUuid, "imageUuid should match")
88+
}
7989
assert.Equal(t, expectedVersion, entry.ToolVersion, "toolVersion should match")
80-
assert.Equal(t, expectedDate, entry.BuildTime, "buildTime should match")
90+
if expectedDate != "" {
91+
assert.Equal(t, expectedDate, entry.BuildTime, "buildTime should match")
92+
}
8193
// Since the config is modified its entirety won't be an exact match; picking one consistent field to verify
8294
assert.Equal(t, config.OS.BootLoader.ResetType, entry.Config.OS.BootLoader.ResetType, "config bootloader reset type should match")
8395

toolkit/tools/pkg/imagecustomizerlib/readonlymounts.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package imagecustomizerlib
55

66
import (
77
"path/filepath"
8+
"strings"
89

910
"github.com/microsoft/azurelinux/toolkit/tools/internal/safechroot"
1011
"golang.org/x/sys/unix"
@@ -15,8 +16,8 @@ func isPathOnReadOnlyMount(chrootPath string, imageChroot *safechroot.Chroot) bo
1516

1617
mounts := imageChroot.GetMountPoints()
1718
for _, mount := range mounts {
18-
_, err := filepath.Rel(mount.GetTarget(), chrootPath)
19-
if err != nil {
19+
relativePath, err := filepath.Rel(mount.GetTarget(), chrootPath)
20+
if err != nil || strings.HasPrefix(relativePath, "../") {
2021
// Path is not relative to the mount.
2122
continue
2223
}

0 commit comments

Comments
 (0)