1// Copyright 2020 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5package main 6 7import ( 8 "context" 9 "io/ioutil" 10 "os" 11 "os/exec" 12 "path/filepath" 13 "strings" 14 "testing" 15 16 "go.skia.org/infra/go/testutils/unittest" 17 18 "github.com/stretchr/testify/assert" 19 "github.com/stretchr/testify/require" 20 "go.skia.org/infra/task_driver/go/td" 21) 22 23func TestSetupCIFuzzRepoAndDocker_Success(t *testing.T) { 24 unittest.LinuxOnlyTest(t) 25 base := t.TempDir() 26 fakeWorkDir := filepath.Join(base, "work") 27 gb, err := exec.Command("which", "git").Output() 28 require.NoError(t, err) 29 gitExe := strings.TrimSpace(string(gb)) 30 res := td.RunTestSteps(t, false, func(ctx context.Context) error { 31 return setupCIFuzzRepoAndDocker(ctx, fakeWorkDir, gitExe) 32 }) 33 require.Empty(t, res.Errors) 34 require.Empty(t, res.Exceptions) 35 require.Len(t, res.Steps, 1) 36 step := res.Steps[0] 37 assert.Equal(t, td.StepResultSuccess, step.Result) 38 assert.Empty(t, step.Errors) 39 assert.Empty(t, step.Exceptions) 40 // Make sure the output directory is created 41 assert.DirExists(t, filepath.Join(fakeWorkDir, "out")) 42} 43 44func TestExtractOutput_OnlyCopyFuzzOutputs(t *testing.T) { 45 base := t.TempDir() 46 fakeWorkDir := filepath.Join(base, "work") 47 fakeCIFuzzOut := filepath.Join(fakeWorkDir, "out") 48 fakeOutput := filepath.Join(base, "swarming_out") 49 50 err := os.MkdirAll(fakeCIFuzzOut, 0777) 51 require.NoError(t, err) 52 53 touch(t, filepath.Join(fakeCIFuzzOut, "crash-1234")) 54 touch(t, filepath.Join(fakeCIFuzzOut, "oom-1234")) 55 touch(t, filepath.Join(fakeCIFuzzOut, "timeout-1234")) 56 touch(t, filepath.Join(fakeCIFuzzOut, "a_fuzz_executable_that_should_not_be_copied")) 57 58 _, err = os.Stat(fakeOutput) 59 require.Error(t, err) 60 61 res := td.RunTestSteps(t, false, func(ctx context.Context) error { 62 return extractOutput(ctx, fakeWorkDir, fakeOutput) 63 }) 64 require.Empty(t, res.Errors) 65 require.Empty(t, res.Exceptions) 66 require.Len(t, res.Steps, 1) 67 step := res.Steps[0] 68 assert.Equal(t, td.StepResultSuccess, step.Result) 69 assert.Empty(t, step.Errors) 70 assert.Empty(t, step.Exceptions) 71 72 copiedFiles, err := ioutil.ReadDir(fakeOutput) 73 require.NoError(t, err) 74 fileNames := make([]string, 0, len(copiedFiles)) 75 for _, f := range copiedFiles { 76 fileNames = append(fileNames, f.Name()) 77 } 78 assert.ElementsMatch(t, []string{"crash-1234", "oom-1234", "timeout-1234"}, fileNames) 79} 80 81func touch(t *testing.T, file string) { 82 err := ioutil.WriteFile(file, []byte("Whatever"), 0666) 83 require.NoError(t, err) 84} 85