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

Nas unit tests #1112

Merged
merged 3 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion pkg/nas/accesspoint_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nas

import (
"context"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/nas/interfaces"
"path"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -31,7 +32,7 @@ func newAccesspointController(config *internal.ControllerConfig) (internal.Contr
}

type accesspointController struct {
nasClient *cloud.NasClientV2
nasClient interfaces.NasClientV2Interface
config *internal.ControllerConfig
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/nas/cloud/nas_client_factory.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cloud

import (
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/nas/interfaces"
"os"
"strconv"

sdkv1 "github.com/aliyun/alibaba-cloud-sdk-go/services/nas"
"github.com/sirupsen/logrus"
"go.uber.org/ratelimit"
)
Expand Down Expand Up @@ -36,7 +36,7 @@ func NewNasClientFactory() *NasClientFactory {
// V2 gets a NAS OpenAPI client sourced from github.com/alibabacloud-go/nas-20170626.
// As github.com/aliyun/alibaba-cloud-sdk-go/services/nas won't be updated with new NAS APIs (e.g., access points),
// we will fully migrate to github.com/alibabacloud-go/nas-20170626 in the future.
func (fac *NasClientFactory) V2(region string) (*NasClientV2, error) {
func (fac *NasClientFactory) V2(region string) (interfaces.NasClientV2Interface, error) {
client, err := newNasClientV2(region)
if err != nil {
return nil, err
Expand All @@ -49,6 +49,6 @@ func (fac *NasClientFactory) V2(region string) (*NasClientV2, error) {
}

// Deprecated: NAS openapi client provided by github.com/aliyun/alibaba-cloud-sdk-go/services/nas.
func (fac *NasClientFactory) V1(region string) (*sdkv1.Client, error) {
func (fac *NasClientFactory) V1(region string) (interfaces.NasV1Interface, error) {
return newNasClientV1(region)
}
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)
}
6 changes: 4 additions & 2 deletions pkg/nas/cloud/nas_client_v1.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 All @@ -19,7 +20,7 @@ func init() {
}
}

func newNasClientV1(region string) (*nassdk.Client, error) {
func newNasClientV1(region string) (interfaces.NasV1Interface, error) {
if ep := os.Getenv("NAS_ENDPOINT"); ep != "" {
_ = aliyunep.AddEndpointMapping(region, "Nas", ep)
}
Expand All @@ -34,7 +35,8 @@ func newNasClientV1(region string) (*nassdk.Client, error) {
scheme = "HTTPS"
}
config = config.WithScheme(scheme).WithUserAgent(KubernetesAlicloudIdentity)
return nassdk.NewClientWithOptions(region, config, ac.Credential)
client, err := nassdk.NewClientWithOptions(region, config, ac.Credential)
return client, err
}

func IsMountTargetNotFoundError(err error) bool {
Expand Down
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.NasV2Interface
}

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