Skip to content

Commit 107ab9a

Browse files
authored
Merge pull request #158 from kzys/enable-race-detector
Enable Go's race detector
2 parents 701dc99 + b7b73b3 commit 107ab9a

File tree

4 files changed

+40
-24
lines changed

4 files changed

+40
-24
lines changed

.buildkite/pipeline.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ steps:
5252
- 'ln -s /var/lib/fc-ci/rootfs.ext4 testdata/root-drive.img'
5353
- 'ln -s /usr/local/bin/firecracker-v0.19.0 testdata/firecracker'
5454
- 'ln -s /usr/local/bin/jailer-v0.19.0 testdata/jailer'
55-
- "FC_TEST_TAP=fc-test-tap${BUILDKITE_BUILD_NUMBER} make test EXTRAGOARGS='-v -count=1' DISABLE_ROOT_TESTS=true"
55+
- "FC_TEST_TAP=fc-test-tap${BUILDKITE_BUILD_NUMBER} make test EXTRAGOARGS='-v -count=1 -race' DISABLE_ROOT_TESTS=true"
5656
agents:
5757
queue: "${BUILDKITE_AGENT_META_DATA_QUEUE:-default}"
5858

@@ -63,7 +63,7 @@ steps:
6363
- 'cp /usr/local/bin/firecracker-v0.19.0 testdata/firecracker'
6464
- 'cp /usr/local/bin/jailer-v0.19.0 testdata/jailer'
6565
- 'make -C cni install CNI_BIN_ROOT=$(pwd)/testdata/bin'
66-
- "sudo FC_TEST_TAP=fc-root-tap${BUILDKITE_BUILD_NUMBER} make test EXTRAGOARGS='-v -count=1' DISABLE_ROOT_TESTS="
66+
- "sudo FC_TEST_TAP=fc-root-tap${BUILDKITE_BUILD_NUMBER} make test EXTRAGOARGS='-v -count=1 -race' DISABLE_ROOT_TESTS="
6767
agents:
6868
queue: "${BUILDKITE_AGENT_META_DATA_QUEUE:-default}"
6969

machine.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,10 @@ func (m *Machine) setupLogging(ctx context.Context) error {
584584
}
585585

586586
func (m *Machine) captureFifoToFile(ctx context.Context, logger *log.Entry, fifoPath string, w io.Writer) error {
587+
return m.captureFifoToFileWithChannel(ctx, logger, fifoPath, w, make(chan error, 1))
588+
}
589+
590+
func (m *Machine) captureFifoToFileWithChannel(ctx context.Context, logger *log.Entry, fifoPath string, w io.Writer, done chan error) error {
587591
// open the fifo pipe which will be used
588592
// to write its contents to a file.
589593
fifoPipe, err := fifo.OpenFifo(ctx, fifoPath, syscall.O_RDONLY|syscall.O_NONBLOCK, 0600)
@@ -620,7 +624,10 @@ func (m *Machine) captureFifoToFile(ctx context.Context, logger *log.Entry, fifo
620624

621625
if _, err := io.Copy(w, fifoPipe); err != nil {
622626
logger.WithError(err).Warn("io.Copy failed to copy contents of fifo pipe")
627+
done <- err
623628
}
629+
630+
close(done)
624631
}()
625632

626633
return nil

machine_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,20 +1073,19 @@ func TestCaptureFifoToFile_leak(t *testing.T) {
10731073
logger := fctesting.NewLogEntry(t)
10741074
logger.Logger.Level = logrus.WarnLevel
10751075
logger.Logger.Out = loggerBuffer
1076-
err = m.captureFifoToFile(context.Background(), logger, fifoPath, buf)
1076+
1077+
done := make(chan error)
1078+
err = m.captureFifoToFileWithChannel(context.Background(), logger, fifoPath, buf, done)
10771079
assert.NoError(t, err, "failed to capture fifo to file")
1080+
1081+
// Stopping the machine will close the FIFO
10781082
close(m.exitCh)
10791083

1080-
// wait sometime for the logs to populate
1081-
time.Sleep(250 * time.Millisecond)
1082-
expectedClosedFifo := `reading from a closed fifo`
1083-
expectedClosedFile := `file already closed`
1084-
logs := loggerBuffer.String()
1085-
if !(strings.Contains(logs, expectedClosedFifo) ||
1086-
strings.Contains(logs, expectedClosedFile)) {
1084+
// Waiting the channel to make sure that the contents of the FIFO has been copied
1085+
copyErr := <-done
10871086

1088-
t.Errorf("logs did not container a closed fifo error or closed file")
1089-
}
1087+
assert.Contains(t, copyErr.Error(), `file already closed`, "error")
1088+
assert.Contains(t, loggerBuffer.String(), `file already closed`, "log")
10901089
}
10911090

10921091
func TestWaitWithKill(t *testing.T) {

network_test.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -295,19 +295,25 @@ func TestNetworkMachineCNI(t *testing.T) {
295295
var vmWg sync.WaitGroup
296296
for i := 0; i < numVMs; i++ {
297297
vmWg.Add(1)
298-
go func(vmID string) {
299-
defer vmWg.Done()
300298

301-
ctx, cancel := context.WithCancel(context.Background())
299+
vmID := fmt.Sprintf("%d-%s-%d", timestamp, networkName, i)
300+
301+
firecrackerSockPath := filepath.Join(testCNIDir, fmt.Sprintf("%s.sock", vmID))
302+
rootfsPath := filepath.Join(testCNIDir, fmt.Sprintf("%s.img", vmID))
303+
304+
ctx, cancel := context.WithCancel(context.Background())
305+
// NewMachine cannot be in the goroutine below, since go-openapi/runtime has a globally-shared mutable logger...
306+
// https://github.com/go-openapi/runtime/blob/553c9d1fb273d9550562d9f76949a413af265138/client/runtime.go#L463
307+
m := newCNIMachine(t, ctx, firecrackerSockPath, rootfsPath, cniConfDir, cniCacheDir, networkName, ifName, vmID, cniBinPath)
308+
309+
go func(ctx context.Context, cancel func(), m *Machine, vmID string) {
310+
defer vmWg.Done()
302311
defer cancel()
303312

304-
firecrackerSockPath := filepath.Join(testCNIDir, fmt.Sprintf("%s.sock", vmID))
305-
rootfsPath := filepath.Join(testCNIDir, fmt.Sprintf("%s.img", vmID))
306313
expectedCacheDirPath := filepath.Join(cniCacheDir, "results",
307314
fmt.Sprintf("%s-%s-%s", networkName, vmID, ifName))
308315

309-
m, vmIP := startCNIMachine(t, ctx,
310-
firecrackerSockPath, rootfsPath, cniConfDir, cniCacheDir, networkName, ifName, vmID, cniBinPath)
316+
vmIP := startCNIMachine(t, ctx, m)
311317
vmIPs <- vmIP
312318

313319
assert.FileExists(t, expectedCacheDirPath, "CNI cache dir doesn't exist after vm startup")
@@ -326,7 +332,7 @@ func TestNetworkMachineCNI(t *testing.T) {
326332
_, err := os.Stat(expectedCacheDirPath)
327333
assert.True(t, os.IsNotExist(err), "expected CNI cache dir to not exist after vm exit")
328334

329-
}(fmt.Sprintf("%d-%s-%d", timestamp, networkName, i))
335+
}(ctx, cancel, m, vmID)
330336
}
331337
vmWg.Wait()
332338
close(vmIPs)
@@ -341,7 +347,7 @@ func TestNetworkMachineCNI(t *testing.T) {
341347
}
342348
}
343349

344-
func startCNIMachine(t *testing.T,
350+
func newCNIMachine(t *testing.T,
345351
ctx context.Context,
346352
firecrackerSockPath,
347353
rootfsPath,
@@ -351,7 +357,7 @@ func startCNIMachine(t *testing.T,
351357
ifName,
352358
vmID string,
353359
cniBinPath []string,
354-
) (*Machine, string) {
360+
) *Machine {
355361
rootfsBytes, err := ioutil.ReadFile(testRootfs)
356362
require.NoError(t, err, "failed to read rootfs file")
357363
err = ioutil.WriteFile(rootfsPath, rootfsBytes, 0666)
@@ -391,7 +397,11 @@ func startCNIMachine(t *testing.T,
391397
}, WithProcessRunner(cmd))
392398
require.NoError(t, err, "failed to create machine with CNI network interface")
393399

394-
err = m.Start(context.Background())
400+
return m
401+
}
402+
403+
func startCNIMachine(t *testing.T, ctx context.Context, m *Machine) string {
404+
err := m.Start(ctx)
395405
require.NoError(t, err, "failed to start machine")
396406

397407
staticConfig := m.Cfg.NetworkInterfaces[0].StaticConfiguration
@@ -406,7 +416,7 @@ func startCNIMachine(t *testing.T,
406416
require.NotNil(t, ipConfig,
407417
"cni configuration should have updated network interface ip configuration")
408418

409-
return m, ipConfig.IPAddr.IP.String()
419+
return ipConfig.IPAddr.IP.String()
410420
}
411421

412422
func testPing(t *testing.T, ip string, count int, timeout time.Duration) {

0 commit comments

Comments
 (0)