github.com/cockroachdb/tools@v0.0.0-20230222021103-a6d27438930d/cmd/signature-fuzzer/fuzz-driver/drv_test.go (about)

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"os"
     9  	"os/exec"
    10  	"path/filepath"
    11  	"runtime"
    12  	"testing"
    13  
    14  	"golang.org/x/tools/internal/testenv"
    15  )
    16  
    17  // buildDriver builds the fuzz-driver executable, returning its path.
    18  func buildDriver(t *testing.T) string {
    19  	t.Helper()
    20  	if runtime.GOOS == "android" {
    21  		t.Skipf("the dependencies are not available on android")
    22  		return ""
    23  	}
    24  	bindir := filepath.Join(t.TempDir(), "bin")
    25  	err := os.Mkdir(bindir, os.ModePerm)
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	binary := filepath.Join(bindir, "driver")
    30  	if runtime.GOOS == "windows" {
    31  		binary += ".exe"
    32  	}
    33  	cmd := exec.Command("go", "build", "-o", binary)
    34  	if err := cmd.Run(); err != nil {
    35  		t.Fatalf("Building fuzz-driver: %v", err)
    36  	}
    37  	return binary
    38  }
    39  
    40  func TestEndToEndIntegration(t *testing.T) {
    41  	testenv.NeedsTool(t, "go")
    42  	td := t.TempDir()
    43  
    44  	// Build the fuzz-driver binary.
    45  	// Note: if more tests are added to this package, move this to single setup fcn, so
    46  	// that we don't have to redo the build each time.
    47  	binary := buildDriver(t)
    48  
    49  	// Kick off a run.
    50  	gendir := filepath.Join(td, "gen")
    51  	args := []string{"-numfcns", "3", "-numpkgs", "1", "-seed", "101", "-outdir", gendir}
    52  	c := exec.Command(binary, args...)
    53  	b, err := c.CombinedOutput()
    54  	if err != nil {
    55  		t.Fatalf("error invoking fuzz-driver: %v\n%s", err, b)
    56  	}
    57  
    58  	found := ""
    59  	walker := func(path string, info os.FileInfo, err error) error {
    60  		found = found + ":" + info.Name()
    61  		return nil
    62  	}
    63  
    64  	// Make sure it emits something.
    65  	err2 := filepath.Walk(gendir, walker)
    66  	if err2 != nil {
    67  		t.Fatalf("error from filepath.Walk: %v", err2)
    68  	}
    69  	const expected = ":gen:genCaller0:genCaller0.go:genChecker0:genChecker0.go:genMain.go:genUtils:genUtils.go:go.mod"
    70  	if found != expected {
    71  		t.Errorf("walk of generated code: got %s want %s", found, expected)
    72  	}
    73  }