fork of https://github.com/sourcegraph/zoekt
1package main
2
3import (
4 "bytes"
5 "context"
6 "errors"
7 "log"
8 "os"
9 "os/exec"
10 "reflect"
11 "strings"
12 "testing"
13 "time"
14)
15
16var cmdTimeout = 100 * time.Millisecond
17
18func captureOutput(f func()) string {
19 var buf bytes.Buffer
20 log.SetOutput(&buf)
21 defer func() { log.SetOutput(os.Stderr) }()
22 f()
23 return buf.String()
24}
25
26func TestLoggedRun(t *testing.T) {
27 ctx, cancel := context.WithTimeout(context.Background(), cmdTimeout)
28 defer cancel()
29
30 cmd := exec.CommandContext(ctx, "echo", "-n", "1")
31
32 stdout := captureOutput(func() {
33 loggedRun(cmd)
34 })
35
36 if !strings.Contains(stdout, "run [echo -n 1]") {
37 t.Errorf("loggedRun output is incorrect: %v", stdout)
38 }
39}
40
41func TestLoggedRunFailure(t *testing.T) {
42 ctx, cancel := context.WithTimeout(context.Background(), cmdTimeout)
43 defer cancel()
44
45 cmd := exec.CommandContext(ctx, "false")
46
47 stdout := captureOutput(func() {
48 loggedRun(cmd)
49 })
50
51 if !strings.Contains(stdout, "failed: exit status 1") {
52 t.Errorf("loggedRun output is incorrect: %v", stdout)
53 }
54}
55
56func TestInitMetrics(t *testing.T) {
57 server := indexServer{}
58
59 server.initMetrics()
60
61 if server.promRegistry == nil {
62 t.Errorf("promRegistry shouldn't be nil")
63 }
64
65 if server.metricsRequestsTotal == nil {
66 t.Errorf("metricsRequestsTotal shouldn't be nil")
67 }
68}
69
70func TestIndexRepository(t *testing.T) {
71 var cmdHistory [][]string
72
73 executeCmd = func(ctx context.Context, name string, arg ...string) (err error) {
74 currentCmd := append([]string{name}, arg...)
75 cmdHistory = append(cmdHistory, currentCmd)
76
77 return
78 }
79
80 opts := Options{
81 indexTimeout: cmdTimeout,
82 repoDir: "/repo_dir",
83 indexDir: "/index_dir",
84 }
85
86 req := indexRequest{
87 CloneURL: "https://example.com/repository.git",
88 RepoID: 100,
89 }
90
91 _, err := indexRepository(opts, req)
92 if err != nil {
93 t.Fatal(err)
94 }
95
96 expectedHistory := [][]string{
97 {"zoekt-git-clone", "-dest", "/repo_dir", "-name", "100", "-repoid", "100", "https://example.com/repository.git"},
98 {"git", "-C", "/repo_dir/100.git", "fetch"},
99 {"zoekt-git-index", "-index", "/index_dir", "/repo_dir/100.git"},
100 }
101
102 if !reflect.DeepEqual(cmdHistory, expectedHistory) {
103 t.Errorf("cmdHistory output is incorrect: %v, expected output: %v", cmdHistory, expectedHistory)
104 }
105}
106
107func TestIndexRepositoryWhenErr(t *testing.T) {
108 var cmdHistory [][]string
109
110 executeCmd = func(ctx context.Context, name string, arg ...string) (err error) {
111 currentCmd := append([]string{name}, arg...)
112 cmdHistory = append(cmdHistory, currentCmd)
113
114 if len(cmdHistory) > 1 {
115 return errors.New("command failed")
116 }
117
118 return
119 }
120
121 opts := Options{
122 indexTimeout: cmdTimeout,
123 repoDir: "/repo_dir",
124 indexDir: "/index_dir",
125 }
126
127 req := indexRequest{
128 CloneURL: "https://example.com/repository.git",
129 RepoID: 100,
130 }
131
132 _, err := indexRepository(opts, req)
133
134 if err == nil {
135 t.Errorf("Error is empty, when it should be present")
136 }
137
138 expectedHistory := [][]string{
139 {"zoekt-git-clone", "-dest", "/repo_dir", "-name", "100", "-repoid", "100", "https://example.com/repository.git"},
140 {"git", "-C", "/repo_dir/100.git", "fetch"},
141 }
142
143 if !reflect.DeepEqual(cmdHistory, expectedHistory) {
144 t.Errorf("cmdHistory output is incorrect: %v, expected output: %v", cmdHistory, expectedHistory)
145 }
146}