Skip to content

Commit

Permalink
test: pkg/nas/cloud unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iltyty committed Jul 26, 2024
1 parent 627d295 commit add0175
Show file tree
Hide file tree
Showing 8 changed files with 623 additions and 3 deletions.
41 changes: 41 additions & 0 deletions pkg/nas/cloud/nas_client_factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cloud

import (
"github.com/stretchr/testify/assert"
"go.uber.org/ratelimit"
"testing"
)

func TestNasClientFactory(t *testing.T) {
t.Parallel()
actual := NewNasClientFactory()
expected := &NasClientFactory{ratelimit.New(defaultQps)}
assert.Equal(t, expected, actual)
}

func TestNasClientFactoryValidEnv(t *testing.T) {
t.Setenv("NAS_LIMIT_PERSECOND", "3")
expected := &NasClientFactory{ratelimit.New(3)}
actual := NewNasClientFactory()
assert.Equal(t, expected, actual)
}

func TestNasClientFactoryInvalidEnv(t *testing.T) {
t.Setenv("NAS_LIMIT_PERSECOND", "3i")
expected := &NasClientFactory{ratelimit.New(defaultQps)}
actual := NewNasClientFactory()
assert.Equal(t, expected, actual)
}

func TestNasClientFactoryV1(t *testing.T) {
t.Setenv("ACCESS_KEY_ID", "ID")
t.Setenv("ACCESS_KEY_SECRET", "SECRET")
actual, _ := NewNasClientFactory().V1("cn-hangzhou")
assert.NotNil(t, actual)
}

func TestNasClientFactoryV2(t *testing.T) {
t.Parallel()
actual, _ := NewNasClientFactory().V2("cn-hangzhou")
assert.NotNil(t, actual)
}
43 changes: 43 additions & 0 deletions pkg/nas/cloud/nas_client_v1_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cloud

import (
"errors"
aliErrors "github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
"github.com/stretchr/testify/assert"
"testing"
)

const nasV1Region = "cn-hangzhou"

func TestNewNasClientV1(t *testing.T) {
setNasV1AkSkEnv(t)
actual, err := newNasClientV1(nasV1Region)
assert.NoError(t, err)
assert.NotNil(t, actual)
}

func setNasV1AkSkEnv(t *testing.T) {
t.Setenv("ACCESS_KEY_ID", "ID")
t.Setenv("ACCESS_KEY_SECRET", "SECRET")
}

func TestNewNasClientV1Env(t *testing.T) {
setNasV1AkSkEnv(t)
t.Setenv("NAS_ENDPOINT", "nas.cn-hangzhou.aliyuncs.com")
actual, err := newNasClientV1(nasV1Region)
assert.NoError(t, err)
assert.NotNil(t, actual)
}

func TestIsMountTargetNotFoundError(t *testing.T) {
t.Parallel()
actual := IsMountTargetNotFoundError(errors.New(""))
assert.False(t, actual)
}

func TestIsMountTargetNotFoundErrorServerError(t *testing.T) {
t.Parallel()
err := aliErrors.NewServerError(200, `{"Code": "InvalidParam.MountTargetDomain"}`, "")
actual := IsMountTargetNotFoundError(err)
assert.True(t, actual)
}
3 changes: 2 additions & 1 deletion pkg/nas/cloud/nas_client_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cloud
import (
"errors"
"fmt"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/nas/interfaces"
"os"
"strings"

Expand Down Expand Up @@ -48,7 +49,7 @@ func newNasClientV2(region string) (*sdk.Client, error) {
type NasClientV2 struct {
region string
limiter ratelimit.Limiter
client *sdk.Client
client interfaces.NasInterface
}

func (c *NasClientV2) CreateDir(req *sdk.CreateDirRequest) error {
Expand Down
Loading

0 comments on commit add0175

Please sign in to comment.