Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(unit): add pkg/common unit test. #142

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 202 additions & 0 deletions pkg/common/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package common

import (
"errors"
"testing"

"github.com/chnsz/golangsdk"
"github.com/container-storage-interface/spec/lib/go/csi"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/apimachinery/pkg/util/wait"
)

func TestIsNotFound(t *testing.T) {
tests := []struct {
name string
err error
expected bool
}{
{
name: "test1",
err: status.Error(codes.OK, "OK test"),
expected: false,
},
{
name: "test2",
err: status.Error(codes.NotFound, "NotFound test"),
expected: true,
},
{
name: "test3",
err: status.Error(codes.Internal, "Internal test"),
expected: false,
},
{
name: "test4",
err: golangsdk.ErrDefault400{},
expected: false,
},
{
name: "test5",
err: golangsdk.ErrDefault404{},
expected: true,
},
{
name: "test6",
err: golangsdk.ErrDefault503{},
expected: false,
},
}

for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
isNotFound := IsNotFound(testCase.err)
if testCase.expected != isNotFound {
t.Fatalf("expected: %v, got: %v", testCase.expected, isNotFound)
}
})
}
}

func TestWaitForCompleted(t *testing.T) {
tests := []struct {
name string
condition wait.ConditionFunc
expected bool
expectedTxt string
}{
{
name: "test1",
condition: func() (done bool, err error) {
return true, nil
},
expected: false,
},
{
name: "test2",
condition: func() (done bool, err error) {
return true, errors.New("true message test")
},
expected: true,
expectedTxt: "true message test",
},
{
name: "test3",
condition: func() (done bool, err error) {
return false, nil
},
expected: true,
expectedTxt: "timed out waiting for the condition",
},
{
name: "test4",
condition: func() (done bool, err error) {
return false, errors.New("false message test")
},
expected: true,
expectedTxt: "false message test",
},
}

for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
err := WaitForCompleted(testCase.condition)
if testCase.expected && err == nil {
t.Fatalf("expected error but get nil")
}
if !testCase.expected && err != nil {
t.Fatalf("unexpected err: %v", err)
}
if testCase.expected && err != nil && err.Error() != testCase.expectedTxt {
t.Fatalf("expected: %v, got: %v", testCase.expected, err.Error())
}
})
}
}

func TestGetAZFromTopology(t *testing.T) {
topologyKey := "zone"

testCases := []struct {
name string
req *csi.TopologyRequirement
expected string
}{
{
name: "test1",
req: &csi.TopologyRequirement{
Preferred: []*csi.Topology{{
Segments: map[string]string{
topologyKey: "zone1",
}},
},
},
expected: "zone1",
},
{
name: "test2",
req: &csi.TopologyRequirement{
Requisite: []*csi.Topology{{
Segments: map[string]string{
topologyKey: "zone2",
}},
},
},
expected: "zone2",
},
{
name: "test3",
req: &csi.TopologyRequirement{
Preferred: []*csi.Topology{{
Segments: map[string]string{
topologyKey: "zone1",
}},
},
Requisite: []*csi.Topology{{
Segments: map[string]string{
topologyKey: "zone2",
}},
},
},
expected: "zone1",
},
{
name: "test4",
req: &csi.TopologyRequirement{
Preferred: []*csi.Topology{{
Segments: map[string]string{}},
},
Requisite: []*csi.Topology{{
Segments: map[string]string{}},
},
},
expected: "",
},
{
name: "test5",
req: &csi.TopologyRequirement{
Preferred: []*csi.Topology{{
Segments: map[string]string{
topologyKey: "zone2",
}},
},
Requisite: []*csi.Topology{{
Segments: map[string]string{
topologyKey: "zone2",
}},
},
},
expected: "zone2",
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
result := GetAZFromTopology(testCase.req, topologyKey)
if testCase.expected != result {
t.Fatalf("expected: %v, got: %v", testCase.expected, result)
}
})
}
}
Loading