-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for the go/trace package (#15052)
Signed-off-by: VaibhavMalik4187 <vaibhavmalik2018@gmail.com>
- Loading branch information
1 parent
d4f0c2a
commit b28fd5e
Showing
7 changed files
with
331 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package trace | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNoopTracingServer(t *testing.T) { | ||
factoryFunc := tracingBackendFactories["noop"] | ||
tracingSvc, closer, err := factoryFunc("value") | ||
require.NoError(t, err) | ||
require.NoError(t, closer.Close()) | ||
span, err := tracingSvc.NewFromString("parent", "label") | ||
require.NoError(t, err) | ||
require.Empty(t, span) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package trace | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// If captureStdout is false, it will capture the outut of | ||
// os.Stderr | ||
func captureOutput(t *testing.T, f func(), captureStdout bool) string { | ||
oldVal := os.Stderr | ||
if captureStdout { | ||
oldVal = os.Stdout | ||
} | ||
t.Cleanup(func() { | ||
// Ensure reset even if deferred function panics | ||
if captureStdout { | ||
os.Stdout = oldVal | ||
} else { | ||
os.Stderr = oldVal | ||
} | ||
}) | ||
|
||
r, w, _ := os.Pipe() | ||
if captureStdout { | ||
os.Stdout = w | ||
} else { | ||
os.Stderr = w | ||
} | ||
|
||
f() | ||
|
||
w.Close() | ||
got, _ := io.ReadAll(r) | ||
|
||
return string(got) | ||
} | ||
|
||
func TestLoggerLogAndError(t *testing.T) { | ||
logger := traceLogger{} | ||
|
||
// Test Error() output | ||
output := captureOutput(t, func() { | ||
logger.Error("This is an error message") | ||
}, false) | ||
assert.Contains(t, output, "This is an error message") | ||
|
||
// Test Log() output | ||
output = captureOutput(t, func() { | ||
logger.Log("This is an log message") | ||
}, false) | ||
assert.Contains(t, output, "This is an log message") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package trace | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/opentracing/opentracing-go" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetOpenTracingTracer(t *testing.T) { | ||
tracer := datadogTracer{ | ||
actual: opentracing.GlobalTracer(), | ||
} | ||
require.Equal(t, opentracing.GlobalTracer(), tracer.GetOpenTracingTracer()) | ||
} | ||
|
||
func TestNewDataDogTracerHostAndPortNotSet(t *testing.T) { | ||
tracingSvc, closer, err := newDatadogTracer("svc") | ||
expectedErr := "need host and port to datadog agent to use datadog tracing" | ||
require.ErrorContains(t, err, expectedErr) | ||
require.Nil(t, tracingSvc) | ||
require.Nil(t, closer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package trace | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewJaegerTracerFromEnv(t *testing.T) { | ||
tracingSvc, closer, err := newJagerTracerFromEnv("noop") | ||
require.NoError(t, err) | ||
require.NotEmpty(t, tracingSvc) | ||
require.NotEmpty(t, closer) | ||
|
||
tracingSvc, closer, err = newJagerTracerFromEnv("") | ||
require.ErrorContains(t, err, "no service name provided") | ||
require.Empty(t, tracingSvc) | ||
require.Empty(t, closer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package trace | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLogErrorsWhenClosing(t *testing.T) { | ||
logFunc := LogErrorsWhenClosing(&fakeCloser{}) | ||
|
||
got := captureOutput(t, func() { | ||
logFunc() | ||
}, false) | ||
|
||
require.Contains(t, string(got), "test error") | ||
} | ||
|
||
type fakeCloser struct { | ||
} | ||
|
||
func (fc *fakeCloser) Close() error { | ||
return fmt.Errorf("test error") | ||
} |