Skip to content

Commit 2e9ad00

Browse files
ayushr2gvisor-bot
authored andcommitted
Deflake TestIPv6DisableAllSysctl.
Earlier we were using waitForFileNotEmpty() which could return before the container had completed running the "ip addr" program. Hence when processing the partial results, the test would fail. It is much easier to just use executeCombinedOutput() to pipe the output out. It waits for the exec'd process to complete. PiperOrigin-RevId: 770246387
1 parent 84c77aa commit 2e9ad00

File tree

1 file changed

+14
-34
lines changed

1 file changed

+14
-34
lines changed

runsc/container/container_test.go

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"bytes"
1919
"fmt"
2020
"io"
21-
"io/ioutil"
2221
"math"
2322
"math/rand"
2423
"os"
@@ -4135,24 +4134,7 @@ func TestIPv6DisableAllSysctl(t *testing.T) {
41354134
for name, conf := range configs(t, true /* noOverlay */) {
41364135
for _, test := range tests {
41374136
t.Run(test.name+name, func(t *testing.T) {
4138-
dir, err := os.MkdirTemp(testutil.TmpDir(), "ipv6-test")
4139-
if err != nil {
4140-
t.Fatalf("os.MkdirTemp failed: %v", err)
4141-
}
4142-
defer os.RemoveAll(dir)
4143-
if err := os.Chmod(dir, 0777); err != nil {
4144-
t.Fatalf("error chmoding file: %q, %v", dir, err)
4145-
}
4146-
4147-
outputPath := filepath.Join(dir, "output")
4148-
outputFile, err := createWriteableOutputFile(outputPath)
4149-
if err != nil {
4150-
t.Fatalf("error creating output file: %v", err)
4151-
}
4152-
defer outputFile.Close()
4153-
4154-
script := fmt.Sprintf("ip addr >> %q", outputPath)
4155-
spec := testutil.NewSpecWithArgs("bash", "-c", script)
4137+
spec := testutil.NewSpecWithArgs("sleep", "infinity")
41564138
conf.Network = config.NetworkSandbox
41574139
if test.ipv6Disabled {
41584140
spec.Linux = &specs.Linux{}
@@ -4175,33 +4157,31 @@ func TestIPv6DisableAllSysctl(t *testing.T) {
41754157
if err != nil {
41764158
t.Fatalf("error creating container: %v", err)
41774159
}
4160+
defer cont.Destroy()
41784161
if err := cont.Start(conf); err != nil {
41794162
t.Fatalf("error starting container: %v", err)
41804163
}
41814164

4182-
// Wait until application has ran.
4183-
if err := waitForFileNotEmpty(outputFile); err != nil {
4184-
// This can happen when the network does not
4185-
// have any network interfaces configured.
4186-
// We cannot test whether the sysctl works
4187-
// properly in this case. Log a warning and
4188-
// return.
4189-
t.Logf("No network interfaces are configured: %v", err)
4190-
return
4165+
out, err := executeCombinedOutput(conf, cont, nil, "/bin/sh", "-c", "ip addr")
4166+
if err != nil {
4167+
t.Fatalf("error executing 'ip addr' command: %v", err)
41914168
}
41924169

4193-
content, err := ioutil.ReadFile(outputPath)
4194-
if err != nil {
4195-
fmt.Println("Error reading file:", err)
4170+
if len(out) == 0 {
4171+
// This can happen when the network does not have any network
4172+
// interfaces configured. We cannot test whether the sysctl works
4173+
// properly in this case. Log a warning and return.
4174+
t.Logf("No output from 'ip addr' command, no network interfaces are configured")
41964175
return
41974176
}
4198-
res := strings.Contains(string(content), "inet6")
4177+
4178+
res := strings.Contains(string(out), "inet6")
41994179
if test.ipv6Disabled && res {
4200-
t.Fatalf("IPv6 address present when IPv6 is disabled on all interfaces")
4180+
t.Errorf("IPv6 address present when IPv6 is disabled on all interfaces, output: %v", string(out))
42014181
}
42024182

42034183
if !test.ipv6Disabled && !res {
4204-
t.Fatalf("IPv6 address not present when IPv6 is enabled on all interfaces")
4184+
t.Errorf("IPv6 address not present when IPv6 is enabled on all interfaces, output: %v", string(out))
42054185
}
42064186
})
42074187
}

0 commit comments

Comments
 (0)