Fix test --race violation

SetupContainerInRoot was setting Config.RootDir unnecessarily
and causing a --race violation in TestMultiContainerDestroyStarting.

PiperOrigin-RevId: 220580073
Change-Id: Ie0b28c19846106c7458a92681b708ae70f87d25a
This commit is contained in:
Fabricio Voznika 2018-11-07 21:30:11 -08:00 committed by Shentubot
parent 13b48f2e6a
commit d12a0dd6b8
3 changed files with 32 additions and 23 deletions

View File

@ -1287,24 +1287,25 @@ func TestReadonlyMount(t *testing.T) {
// TestAbbreviatedIDs checks that runsc supports using abbreviated container
// IDs in place of full IDs.
func TestAbbreviatedIDs(t *testing.T) {
rootDir, err := testutil.SetupRootDir()
if err != nil {
t.Fatalf("error creating root dir: %v", err)
}
defer os.RemoveAll(rootDir)
conf := testutil.TestConfigWithRoot(rootDir)
cids := []string{
"foo-" + testutil.UniqueContainerID(),
"bar-" + testutil.UniqueContainerID(),
"baz-" + testutil.UniqueContainerID(),
}
rootDir, err := testutil.SetupRootDir()
if err != nil {
t.Fatalf("error creating root dir: %v", err)
}
for _, cid := range cids {
spec := testutil.NewSpecWithArgs("sleep", "100")
conf := testutil.TestConfig()
bundleDir, err := testutil.SetupContainerInRoot(rootDir, spec, conf)
bundleDir, err := testutil.SetupBundleDir(spec)
if err != nil {
t.Fatalf("error setting up container: %v", err)
}
defer os.RemoveAll(rootDir)
defer os.RemoveAll(bundleDir)
// Create and start the container.

View File

@ -63,6 +63,7 @@ func startContainers(conf *boot.Config, specs []*specs.Spec, ids []string) ([]*C
if err != nil {
return nil, nil, fmt.Errorf("error creating root dir: %v", err)
}
conf.RootDir = rootDir
var containers []*Container
var bundles []string
@ -76,7 +77,7 @@ func startContainers(conf *boot.Config, specs []*specs.Spec, ids []string) ([]*C
os.RemoveAll(rootDir)
}
for i, spec := range specs {
bundleDir, err := testutil.SetupContainerInRoot(rootDir, spec, conf)
bundleDir, err := testutil.SetupBundleDir(spec)
if err != nil {
cleanup()
return nil, nil, fmt.Errorf("error setting up container: %v", err)
@ -617,16 +618,16 @@ func TestMultiContainerDestroyNotStarted(t *testing.T) {
specs, ids := createSpecs(
[]string{"/bin/sleep", "100"},
[]string{"/bin/sleep", "100"})
conf := testutil.TestConfig()
rootDir, err := testutil.SetupRootDir()
if err != nil {
t.Fatalf("error creating root dir: %v", err)
}
defer os.RemoveAll(rootDir)
conf := testutil.TestConfigWithRoot(rootDir)
// Create and start root container.
rootBundleDir, err := testutil.SetupContainerInRoot(rootDir, specs[0], conf)
rootBundleDir, err := testutil.SetupBundleDir(specs[0])
if err != nil {
t.Fatalf("error setting up container: %v", err)
}
@ -642,7 +643,7 @@ func TestMultiContainerDestroyNotStarted(t *testing.T) {
}
// Create and destroy sub-container.
bundleDir, err := testutil.SetupContainerInRoot(rootDir, specs[1], conf)
bundleDir, err := testutil.SetupBundleDir(specs[1])
if err != nil {
t.Fatalf("error setting up container: %v", err)
}
@ -667,7 +668,6 @@ func TestMultiContainerDestroyStarting(t *testing.T) {
cmds[i] = []string{"/bin/sleep", "100"}
}
specs, ids := createSpecs(cmds...)
conf := testutil.TestConfig()
rootDir, err := testutil.SetupRootDir()
if err != nil {
@ -675,8 +675,10 @@ func TestMultiContainerDestroyStarting(t *testing.T) {
}
defer os.RemoveAll(rootDir)
conf := testutil.TestConfigWithRoot(rootDir)
// Create and start root container.
rootBundleDir, err := testutil.SetupContainerInRoot(rootDir, specs[0], conf)
rootBundleDir, err := testutil.SetupBundleDir(specs[0])
if err != nil {
t.Fatalf("error setting up container: %v", err)
}
@ -697,7 +699,7 @@ func TestMultiContainerDestroyStarting(t *testing.T) {
continue // skip root container
}
bundleDir, err := testutil.SetupContainerInRoot(rootDir, specs[i], conf)
bundleDir, err := testutil.SetupBundleDir(specs[i])
if err != nil {
t.Fatalf("error setting up container: %v", err)
}

View File

@ -104,7 +104,8 @@ func FindFile(path string) (string, error) {
return matches[0], nil
}
// TestConfig return the default configuration to use in tests.
// TestConfig returns the default configuration to use in tests. Note that
// 'RootDir' must be set by caller if required.
func TestConfig() *boot.Config {
return &boot.Config{
Debug: true,
@ -117,6 +118,13 @@ func TestConfig() *boot.Config {
}
}
// TestConfigWithRoot returns the default configuration to use in tests.
func TestConfigWithRoot(rootDir string) *boot.Config {
conf := TestConfig()
conf.RootDir = rootDir
return conf
}
// NewSpecWithArgs creates a simple spec with the given args suitable for use
// in tests.
func NewSpecWithArgs(args ...string) *specs.Spec {
@ -162,13 +170,13 @@ func SetupContainer(spec *specs.Spec, conf *boot.Config) (rootDir, bundleDir str
if err != nil {
return "", "", err
}
bundleDir, err = SetupContainerInRoot(rootDir, spec, conf)
conf.RootDir = rootDir
bundleDir, err = SetupBundleDir(spec)
return rootDir, bundleDir, err
}
// SetupContainerInRoot creates a bundle for the container, generates a test
// config, and writes the spec to config.json in the bundle dir.
func SetupContainerInRoot(rootDir string, spec *specs.Spec, conf *boot.Config) (bundleDir string, err error) {
// SetupBundleDir creates a bundle dir and writes the spec to config.json.
func SetupBundleDir(spec *specs.Spec) (bundleDir string, err error) {
bundleDir, err = ioutil.TempDir(TmpDir(), "bundle")
if err != nil {
return "", fmt.Errorf("error creating bundle dir: %v", err)
@ -177,8 +185,6 @@ func SetupContainerInRoot(rootDir string, spec *specs.Spec, conf *boot.Config) (
if err = writeSpec(bundleDir, spec); err != nil {
return "", fmt.Errorf("error writing spec: %v", err)
}
conf.RootDir = rootDir
return bundleDir, nil
}