-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #134 from canonical/resolv-conf-fixes
Fix handling of resolv.conf in the chroot
- Loading branch information
Showing
7 changed files
with
185 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
ubuntu-image (3.0+23.04ubuntu2) UNRELEASED; urgency=medium | ||
ubuntu-image (3.0+23.04ubuntu1) UNRELEASED; urgency=medium | ||
|
||
[ William 'jawn-smith' Wilson ] | ||
* Redesign classic image builds to use image definition file. | ||
|
@@ -16,7 +16,11 @@ ubuntu-image (3.0+23.04ubuntu2) UNRELEASED; urgency=medium | |
[ Alfonso Sanchez-Beato ] | ||
* Run patchelf so we get dynamic linker and libs from the core22 base. | ||
|
||
-- mjdonis <[email protected]> Thu, 11 May 2023 12:29:08 +0200 | ||
[ Łukasz 'sil2100' Zemczak ] | ||
* Fix handling of resolv.conf in chroot, making sure we don't leak the build | ||
environment by accident. | ||
|
||
-- Łukasz 'sil2100' Zemczak <[email protected]> Wed, 12 Jul 2023 12:55:52 +0200 | ||
|
||
ubuntu-image (2.2+22.10ubuntu1) kinetic; urgency=medium | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package helper | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/google/uuid" | ||
"github.com/snapcore/snapd/osutil" | ||
) | ||
|
||
// define some mocked versions of go package functions | ||
func mockRemove(string) error { | ||
return fmt.Errorf("Test Error") | ||
} | ||
func mockRename(string, string) error { | ||
return fmt.Errorf("Test Error") | ||
} | ||
|
||
// TestRestoreResolvConf tests if resolv.conf is restored correctly | ||
func TestRestoreResolvConf(t *testing.T) { | ||
t.Run("test_restore_resolv_conf", func(t *testing.T) { | ||
asserter := Asserter{T: t} | ||
// Prepare temporary directory | ||
workDir := filepath.Join("/tmp", "ubuntu-image-"+uuid.NewString()) | ||
err := os.Mkdir(workDir, 0755) | ||
asserter.AssertErrNil(err, true) | ||
defer os.RemoveAll(workDir) | ||
|
||
// Create test objects for a regular backup | ||
err = os.MkdirAll(filepath.Join(workDir, "etc"), 0755) | ||
asserter.AssertErrNil(err, true) | ||
mainConfPath := filepath.Join(workDir, "etc", "resolv.conf") | ||
mainConf, err := os.Create(mainConfPath) | ||
asserter.AssertErrNil(err, true) | ||
testData := []byte("Main") | ||
_, err = mainConf.Write(testData) | ||
asserter.AssertErrNil(err, true) | ||
mainConf.Close() | ||
backupConfPath := filepath.Join(workDir, "etc", "resolv.conf.tmp") | ||
backupConf, err := os.Create(backupConfPath) | ||
asserter.AssertErrNil(err, true) | ||
testData = []byte("Backup") | ||
_, err = backupConf.Write(testData) | ||
asserter.AssertErrNil(err, true) | ||
backupConf.Close() | ||
|
||
err = RestoreResolvConf(workDir) | ||
asserter.AssertErrNil(err, true) | ||
if osutil.FileExists(backupConfPath) { | ||
t.Errorf("Backup resolv.conf.tmp has not been removed") | ||
} | ||
checkData, err := os.ReadFile(mainConfPath) | ||
asserter.AssertErrNil(err, true) | ||
if string(checkData) != "Backup" { | ||
t.Errorf("Main resolv.conf has not been restored") | ||
} | ||
|
||
// Now check if the symlink case also works | ||
_, err = os.Create(backupConfPath) | ||
asserter.AssertErrNil(err, true) | ||
err = os.Remove(mainConfPath) | ||
asserter.AssertErrNil(err, true) | ||
err = os.Symlink("resolv.conf.tmp", mainConfPath) | ||
asserter.AssertErrNil(err, true) | ||
|
||
err = RestoreResolvConf(workDir) | ||
asserter.AssertErrNil(err, true) | ||
if osutil.FileExists(backupConfPath) { | ||
t.Errorf("Backup resolv.conf.tmp has not been removed when dealing with as symlink") | ||
} | ||
if !osutil.IsSymlink(mainConfPath) { | ||
t.Errorf("Main resolv.conf should have remained a symlink, but it is not") | ||
} | ||
}) | ||
} | ||
|
||
// TestFailedRestoreResolvConf tests all resolv.conf error cases | ||
func TestFailedRestoreResolvConf(t *testing.T) { | ||
t.Run("test_failed_restore_resolv_conf", func(t *testing.T) { | ||
asserter := Asserter{T: t} | ||
// Prepare temporary directory | ||
workDir := filepath.Join("/tmp", "ubuntu-image-"+uuid.NewString()) | ||
err := os.Mkdir(workDir, 0755) | ||
asserter.AssertErrNil(err, true) | ||
defer os.RemoveAll(workDir) | ||
|
||
// Create test environment | ||
err = os.MkdirAll(filepath.Join(workDir, "etc"), 0755) | ||
asserter.AssertErrNil(err, true) | ||
mainConfPath := filepath.Join(workDir, "etc", "resolv.conf") | ||
_, err = os.Create(mainConfPath) | ||
asserter.AssertErrNil(err, true) | ||
backupConfPath := filepath.Join(workDir, "etc", "resolv.conf.tmp") | ||
_, err = os.Create(backupConfPath) | ||
asserter.AssertErrNil(err, true) | ||
|
||
// Mock the os.Rename failure | ||
osRename = mockRename | ||
defer func() { | ||
osRename = os.Rename | ||
}() | ||
err = RestoreResolvConf(workDir) | ||
asserter.AssertErrContains(err, "Error moving file") | ||
|
||
// Mock the os.Remove failure | ||
err = os.Remove(mainConfPath) | ||
asserter.AssertErrNil(err, true) | ||
err = os.Symlink("resolv.conf.tmp", mainConfPath) | ||
asserter.AssertErrNil(err, true) | ||
osRemove = mockRemove | ||
defer func() { | ||
osRemove = os.Remove | ||
}() | ||
err = RestoreResolvConf(workDir) | ||
asserter.AssertErrContains(err, "Error removing file") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters