|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +//go:build !windows |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | + |
| 29 | + alib "go.opentelemetry.io/contrib/instrgen/lib" |
| 30 | +) |
| 31 | + |
| 32 | +var testcases = map[string]string{ |
| 33 | + "./testdata/basic": "./testdata/expected/basic", |
| 34 | + "./testdata/selector": "./testdata/expected/selector", |
| 35 | + "./testdata/interface": "./testdata/expected/interface", |
| 36 | +} |
| 37 | + |
| 38 | +var failures []string |
| 39 | + |
| 40 | +func inject(t *testing.T, root string, packagePattern string) { |
| 41 | + err := executeCommand("--inject-dump-ir", root, packagePattern) |
| 42 | + require.NoError(t, err) |
| 43 | +} |
| 44 | + |
| 45 | +func Test(t *testing.T) { |
| 46 | + for k, v := range testcases { |
| 47 | + inject(t, k, "./...") |
| 48 | + files := alib.SearchFiles(k, ".go_pass_tracing") |
| 49 | + expectedFiles := alib.SearchFiles(v, ".go") |
| 50 | + numOfFiles := len(expectedFiles) |
| 51 | + fmt.Println("Go Files:", len(files)) |
| 52 | + fmt.Println("Expected Go Files:", len(expectedFiles)) |
| 53 | + numOfComparisons := 0 |
| 54 | + for _, file := range files { |
| 55 | + fmt.Println(filepath.Base(file)) |
| 56 | + for _, expectedFile := range expectedFiles { |
| 57 | + fmt.Println(filepath.Base(expectedFile)) |
| 58 | + if filepath.Base(file) == filepath.Base(expectedFile+"_pass_tracing") { |
| 59 | + f1, err1 := os.ReadFile(file) |
| 60 | + require.NoError(t, err1) |
| 61 | + f2, err2 := os.ReadFile(expectedFile) |
| 62 | + require.NoError(t, err2) |
| 63 | + if !assert.True(t, bytes.Equal(f1, f2)) { |
| 64 | + fmt.Println(k) |
| 65 | + failures = append(failures, k) |
| 66 | + } |
| 67 | + numOfComparisons = numOfComparisons + 1 |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + if numOfFiles != numOfComparisons { |
| 72 | + fmt.Println("numberOfComparisons:", numOfComparisons) |
| 73 | + panic("not all files were compared") |
| 74 | + } |
| 75 | + _, err := Prune(k, "./...", false) |
| 76 | + if err != nil { |
| 77 | + fmt.Println("Prune failed") |
| 78 | + } |
| 79 | + } |
| 80 | + for _, f := range failures { |
| 81 | + fmt.Println("FAILURE : ", f) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestCommands(t *testing.T) { |
| 86 | + err := executeCommand("--dumpcfg", "./testdata/dummy", "./...") |
| 87 | + require.NoError(t, err) |
| 88 | + err = executeCommand("--rootfunctions", "./testdata/dummy", "./...") |
| 89 | + require.NoError(t, err) |
| 90 | + err = executeCommand("--prune", "./testdata/dummy", "./...") |
| 91 | + require.NoError(t, err) |
| 92 | + err = executeCommand("--inject", "./testdata/dummy", "./...") |
| 93 | + require.NoError(t, err) |
| 94 | + err = usage() |
| 95 | + require.NoError(t, err) |
| 96 | +} |
| 97 | + |
| 98 | +func TestCallGraph(t *testing.T) { |
| 99 | + cg := makeCallGraph("./testdata/dummy", "./...") |
| 100 | + dumpCallGraph(cg) |
| 101 | + assert.Equal(t, len(cg), 0, "callgraph should contain 0 elems") |
| 102 | + rf := makeRootFunctions("./testdata/dummy", "./...") |
| 103 | + dumpRootFunctions(rf) |
| 104 | + assert.Equal(t, len(rf), 0, "rootfunctions set should be empty") |
| 105 | +} |
| 106 | + |
| 107 | +func TestArgs(t *testing.T) { |
| 108 | + err := checkArgs(nil) |
| 109 | + require.Error(t, err) |
| 110 | + args := []string{"driver", "--inject", "", "./..."} |
| 111 | + err = checkArgs(args) |
| 112 | + require.NoError(t, err) |
| 113 | +} |
| 114 | + |
| 115 | +func TestUnknown(t *testing.T) { |
| 116 | + err := executeCommand("unknown", "a", "b") |
| 117 | + require.Error(t, err) |
| 118 | +} |
0 commit comments