aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/admin/prog/valgrind_test.go
blob: 5bc9c1625fe89c4bd5e735b9aa41857d907a08d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package prog

import (
	"fmt"
	"os"
	"runtime"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/vespa-engine/vespa/client/go/internal/admin/trace"
	"github.com/vespa-engine/vespa/client/go/internal/util"
)

var tmpBin string
var mockBinParent string

func useMock(prog, target string) {
	mock := fmt.Sprintf("%s/mockbin/%s", mockBinParent, prog)
	symlink := fmt.Sprintf("%s/%s", tmpBin, target)
	os.Remove(symlink)
	err := os.Symlink(mock, symlink)
	if err != nil {
		util.JustExitWith(err)
	}
}

func setupValgrind(t *testing.T, testFileName string) {
	trace.AdjustVerbosity(1)
	t.Setenv("VESPA_HOME", mockBinParent+"/mock_vespahome")
	mockBinParent = strings.TrimSuffix(testFileName, "/valgrind_test.go")
	tmpBin = t.TempDir() + "/mock.bin.valgrind_test"
	err := os.MkdirAll(tmpBin, 0755)
	assert.Nil(t, err)
	t.Setenv("PATH", fmt.Sprintf("%s:%s", tmpBin, os.Getenv("PATH")))
}

func TestValgrindDetection(t *testing.T) {
	if runtime.GOOS == "windows" {
		return
	}
	_, tfn, _, _ := runtime.Caller(0)
	setupValgrind(t, tfn)
	spec := NewSpec([]string{"/opt/vespa/bin/foobar"})
	var argv []string

	useMock("has-valgrind", VALGRIND_PROG)

	t.Setenv("VESPA_USE_VALGRIND", "")
	spec.ConfigureValgrind()
	assert.Equal(t, false, spec.shouldUseValgrind)
	assert.Equal(t, false, spec.shouldUseCallgrind)

	t.Setenv("VESPA_USE_VALGRIND", "all")
	spec.ConfigureValgrind()
	assert.Equal(t, true, spec.shouldUseValgrind)
	assert.Equal(t, false, spec.shouldUseCallgrind)

	t.Setenv("VESPA_USE_VALGRIND", "foo bar")
	spec.ConfigureValgrind()
	assert.Equal(t, false, spec.shouldUseValgrind)
	assert.Equal(t, false, spec.shouldUseCallgrind)

	t.Setenv("VESPA_USE_VALGRIND", "foobar")
	spec.ConfigureValgrind()
	assert.Equal(t, true, spec.shouldUseValgrind)
	assert.Equal(t, false, spec.shouldUseCallgrind)

	argv = spec.prependValgrind([]string{"/bin/myprog", "-c", "cfgid"})
	trace.Trace("argv:", argv)
	assert.Equal(t, 11, len(argv))
	assert.Equal(t, "valgrind", argv[0])
	assert.Equal(t, "/bin/myprog", argv[8])

	t.Setenv("VESPA_USE_VALGRIND", "another foobar yetmore")
	spec.ConfigureValgrind()
	assert.Equal(t, true, spec.shouldUseValgrind)
	assert.Equal(t, false, spec.shouldUseCallgrind)

	t.Setenv("VESPA_VALGRIND_OPT", "--tool=callgrind")
	spec.ConfigureValgrind()
	assert.Equal(t, true, spec.shouldUseValgrind)
	assert.Equal(t, true, spec.shouldUseCallgrind)

	argv = spec.prependValgrind([]string{"/bin/myprog", "-c", "cfgid"})
	trace.Trace("argv:", argv)
	assert.Equal(t, 6, len(argv))
	assert.Equal(t, "valgrind", argv[0])
	assert.Equal(t, "--tool=callgrind", argv[1])
	assert.Equal(t, "/bin/myprog", argv[3])

	useMock("no-valgrind", "valgrind")
	spec.ConfigureValgrind()
	assert.Equal(t, false, spec.shouldUseValgrind)
	assert.Equal(t, false, spec.shouldUseCallgrind)
}