Skip to content

Commit

Permalink
Clean /run/ from final image
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Mars <[email protected]>
  • Loading branch information
upils committed May 2, 2024
1 parent 7df0807 commit 7a90854
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
25 changes: 25 additions & 0 deletions internal/statemachine/classic_states.go
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,20 @@ func (stateMachine *StateMachine) cleanRootfs() error {
return err
}

toDeleteDirFromPattern, err := listWithPatterns(stateMachine.tempDirs.chroot,
[]string{
filepath.Join("run", "*"),
})
fmt.Printf("toDeleteDirFromPattern: %s\n", toDeleteDirFromPattern)
if err != nil {
return err

Check warning on line 1372 in internal/statemachine/classic_states.go

View check run for this annotation

Codecov / codecov/patch

internal/statemachine/classic_states.go#L1372

Added line #L1372 was not covered by tests
}

err = doDeleteDirectories(toDeleteDirFromPattern)
if err != nil {
return err
}

toTruncateFromPattern, err := listWithPatterns(stateMachine.tempDirs.chroot,
[]string{
// udev persistent rules
Expand Down Expand Up @@ -1401,6 +1415,17 @@ func doDeleteFiles(toDelete []string) error {
return nil
}

// doDeleteDirectories deletes the given list of directories
func doDeleteDirectories(toDelete []string) error {
for _, f := range toDelete {
err := osRemoveAll(f)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("Error removing %s: %s", f, err.Error())
}
}
return nil
}

// doTruncateFiles truncates content in the given list of files
func doTruncateFiles(toTruncate []string) error {
for _, f := range toTruncate {
Expand Down
41 changes: 39 additions & 2 deletions internal/statemachine/classic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5348,18 +5348,20 @@ func TestClassicStateMachine_cleanRootfs_real_rootfs(t *testing.T) {
err = stateMachine.cleanRootfs()
asserter.AssertErrNil(err, true)

// Check cleaned files were removed
// Check cleaned files and dirs were removed
cleaned := []string{
filepath.Join(stateMachine.tempDirs.chroot, "var", "lib", "dbus", "machine-id"),
filepath.Join(stateMachine.tempDirs.chroot, "etc", "ssh", "ssh_host_rsa_key"),
filepath.Join(stateMachine.tempDirs.chroot, "etc", "ssh", "ssh_host_rsa_key.pub"),
filepath.Join(stateMachine.tempDirs.chroot, "etc", "ssh", "ssh_host_ecdsa_key"),
filepath.Join(stateMachine.tempDirs.chroot, "etc", "ssh", "ssh_host_ecdsa_key.pub"),
filepath.Join(stateMachine.tempDirs.chroot, "run", "lock"),
filepath.Join(stateMachine.tempDirs.chroot, "run", "mount"),
}
for _, file := range cleaned {
_, err := os.Stat(file)
if !os.IsNotExist(err) {
t.Errorf("File %s should not exist, but does", file)
t.Errorf("File/Directory %s should not exist, but does", file)
}
}

Expand Down Expand Up @@ -5398,6 +5400,8 @@ func TestClassicStateMachine_cleanRootfs(t *testing.T) {
filepath.Join("etc", "udev", "rules.d", "test2-persistent-net.rules"),
filepath.Join("var", "cache", "debconf", "test-old"),
filepath.Join("var", "lib", "dpkg", "testdpkg-old"),
filepath.Join("run", "lock", "test"),
filepath.Join("run", "mount", "test"),
},
wantRootfsContent: map[string]int64{
filepath.Join("etc", "machine-id"): 0,
Expand All @@ -5422,13 +5426,44 @@ func TestClassicStateMachine_cleanRootfs(t *testing.T) {
filepath.Join("etc", "udev", "rules.d", "test-persistent-net.rules"),
filepath.Join("var", "cache", "debconf", "test-old"),
filepath.Join("var", "lib", "dpkg", "testdpkg-old"),
filepath.Join("run", "lock", "test"),
filepath.Join("run", "mount", "test"),
},
wantRootfsContent: map[string]int64{
filepath.Join("etc", "machine-id"): sampleSize,
filepath.Join("var", "lib", "dbus", "machine-id"): sampleSize,
filepath.Join("etc", "udev", "rules.d", "test-persistent-net.rules"): sampleSize,
filepath.Join("var", "cache", "debconf", "test-old"): sampleSize,
filepath.Join("var", "lib", "dpkg", "testdpkg-old"): sampleSize,
filepath.Join("run", "lock", "test"): sampleSize,
filepath.Join("run", "mount", "test"): sampleSize,
},
},
{
name: "fail to delete dirs",
mockFuncs: func() func() {
mock := testhelper.NewOSMock(
&testhelper.OSMockConf{},
)

osRemoveAll = mock.RemoveAll
return func() { osRemoveAll = os.RemoveAll }
},
expectedErr: "Error removing",
initialRootfsContent: []string{
filepath.Join("etc", "machine-id"),
filepath.Join("var", "lib", "dbus", "machine-id"),
filepath.Join("etc", "udev", "rules.d", "test-persistent-net.rules"),
filepath.Join("var", "cache", "debconf", "test-old"),
filepath.Join("var", "lib", "dpkg", "testdpkg-old"),
filepath.Join("run", "lock", "test"),
filepath.Join("run", "mount", "test"),
},
wantRootfsContent: map[string]int64{
filepath.Join("etc", "machine-id"): sampleSize,
filepath.Join("etc", "udev", "rules.d", "test-persistent-net.rules"): sampleSize,
filepath.Join("run", "lock", "test"): sampleSize,
filepath.Join("run", "mount", "test"): sampleSize,
},
},
{
Expand All @@ -5446,6 +5481,8 @@ func TestClassicStateMachine_cleanRootfs(t *testing.T) {
filepath.Join("etc", "machine-id"),
filepath.Join("var", "lib", "dbus", "machine-id"),
filepath.Join("etc", "udev", "rules.d", "test-persistent-net.rules"),
filepath.Join("run", "lock", "test"),
filepath.Join("run", "mount", "test"),
},
wantRootfsContent: map[string]int64{
filepath.Join("etc", "machine-id"): sampleSize,
Expand Down
12 changes: 12 additions & 0 deletions internal/testhelper/testhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type OSMockConf struct {
OsutilCopySpecialFileThreshold uint
ReadDirThreshold uint
RemoveThreshold uint
RemoveAllThreshold uint
TruncateThreshold uint
OpenFileThreshold uint
MkdirAllThreshold uint
Expand All @@ -42,6 +43,7 @@ type osMock struct {
beforeOsutilCopySpecialFileFail uint
beforeReadDirFail uint
beforeRemoveFail uint
beforeRemoveAllFail uint
beforeTruncateFail uint
beforeOpenFileFail uint
beforeMkdirAllFail uint
Expand Down Expand Up @@ -79,6 +81,16 @@ func (o *osMock) Remove(name string) error {
return nil
}

// RemoveAll mocks os.RemoveAll
func (o *osMock) RemoveAll(name string) error {
if o.beforeRemoveAllFail >= o.conf.RemoveAllThreshold {
return fmt.Errorf("RemoveAll fail")
}
o.beforeRemoveAllFail++

Check warning on line 89 in internal/testhelper/testhelper.go

View check run for this annotation

Codecov / codecov/patch

internal/testhelper/testhelper.go#L89

Added line #L89 was not covered by tests

return nil

Check warning on line 91 in internal/testhelper/testhelper.go

View check run for this annotation

Codecov / codecov/patch

internal/testhelper/testhelper.go#L91

Added line #L91 was not covered by tests
}

// Truncate mocks osTruncate
func (o *osMock) Truncate(name string, size int64) error {
if o.beforeTruncateFail >= o.conf.TruncateThreshold {
Expand Down

0 comments on commit 7a90854

Please sign in to comment.