-
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 unit tests for vt/grpcclient package (#15072)
Signed-off-by: VaibhavMalik4187 <[email protected]> Signed-off-by: Manan Gupta <[email protected]> Co-authored-by: Manan Gupta <[email protected]>
- Loading branch information
1 parent
2aabdd0
commit 095b70a
Showing
6 changed files
with
247 additions
and
20 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
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,87 @@ | ||
/* | ||
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 grpcclient | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func captureOutput(t *testing.T, f func()) string { | ||
oldVal := os.Stderr | ||
t.Cleanup(func() { | ||
// Ensure reset even if deferred function panics | ||
os.Stderr = oldVal | ||
}) | ||
|
||
r, w, err := os.Pipe() | ||
require.NoError(t, err) | ||
|
||
os.Stderr = w | ||
|
||
f() | ||
|
||
err = w.Close() | ||
require.NoError(t, err) | ||
|
||
got, err := io.ReadAll(r) | ||
require.NoError(t, err) | ||
|
||
return string(got) | ||
} | ||
|
||
func TestGlogger(t *testing.T) { | ||
gl := glogger{} | ||
|
||
output := captureOutput(t, func() { | ||
gl.Warning("warning") | ||
}) | ||
require.Contains(t, output, "warning") | ||
|
||
output = captureOutput(t, func() { | ||
gl.Warningln("warningln") | ||
}) | ||
require.Contains(t, output, "warningln\n") | ||
|
||
output = captureOutput(t, func() { | ||
gl.Warningf("formatted %s", "warning") | ||
}) | ||
require.Contains(t, output, "formatted warning") | ||
|
||
} | ||
|
||
func TestGloggerError(t *testing.T) { | ||
gl := glogger{} | ||
|
||
output := captureOutput(t, func() { | ||
gl.Error("error message") | ||
}) | ||
require.Contains(t, output, "error message") | ||
|
||
output = captureOutput(t, func() { | ||
gl.Errorln("error message line") | ||
}) | ||
require.Contains(t, output, "error message line\n") | ||
|
||
output = captureOutput(t, func() { | ||
gl.Errorf("this is a %s error message", "formatted") | ||
}) | ||
require.Contains(t, output, "this is a formatted error 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
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 grpcclient | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
func TestCompressDecompress(t *testing.T) { | ||
snappComp := SnappyCompressor{} | ||
writer, err := snappComp.Compress(&bytes.Buffer{}) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, writer) | ||
|
||
reader, err := snappComp.Decompress(&bytes.Buffer{}) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, reader) | ||
} | ||
|
||
func TestAppendCompression(t *testing.T) { | ||
oldCompression := compression | ||
defer func() { | ||
compression = oldCompression | ||
}() | ||
|
||
dialOpts := []grpc.DialOption{} | ||
dialOpts, err := appendCompression(dialOpts) | ||
require.NoError(t, err) | ||
require.Equal(t, 0, len(dialOpts)) | ||
|
||
// Change the compression to snappy | ||
compression = "snappy" | ||
|
||
dialOpts, err = appendCompression(dialOpts) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, len(dialOpts)) | ||
|
||
// Change the compression to some unknown value | ||
compression = "unknown" | ||
|
||
dialOpts, err = appendCompression(dialOpts) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, len(dialOpts)) | ||
} |
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,4 @@ | ||
{ | ||
"Username": "test-user", | ||
"Password": "test-pass" | ||
} |
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 @@ | ||
{ |