Skip to content

Commit

Permalink
chore: Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mowangdk committed Aug 29, 2022
1 parent 83aa6d9 commit 43344d5
Show file tree
Hide file tree
Showing 8 changed files with 477 additions and 63 deletions.
8 changes: 7 additions & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ coverage:
project:
default:
target: auto
threshold: 2%
threshold: 2%
ignore:
- "**/*_mock.go"
- "**/*_test.go"
- "**/global.go"
- "go.mod"
- "go.sum"
2 changes: 1 addition & 1 deletion build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ set -x
NRM_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
NRM_BUILD_DIR=${NRM_ROOT}/build
NRM_OUTPUT_DIR=${NRM_BUILD_DIR}/_output
NRM_BUILD_IMAGE="golang:1.13.3-alpine"
NRM_BUILD_IMAGE="golang:1.17"

GIT_VERSION="v1.0"
GIT_VERSION=(${VERSION:-${GIT_VERSION}})
Expand Down
21 changes: 12 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module github.com/openyurtio/node-resource-manager

go 1.18
go 1.17

require (
github.com/aliyun/alibaba-cloud-sdk-go v1.61.895
github.com/golang/mock v1.3.1
github.com/golang/mock v1.4.1
github.com/stretchr/testify v1.6.1
gopkg.in/h2non/gock.v1 v1.1.2
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.20.2
k8s.io/apimachinery v0.20.2
Expand All @@ -22,18 +23,20 @@ require (
github.com/golang/protobuf v1.4.3 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/googleapis/gnostic v0.4.1 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
github.com/json-iterator/go v1.1.10 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6 // indirect
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
golang.org/x/text v0.3.4 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
google.golang.org/appengine v1.6.5 // indirect
google.golang.org/protobuf v1.25.0 // indirect
Expand All @@ -46,6 +49,6 @@ require (
)

replace (
k8s.io/api => k8s.io/api v0.19.2
k8s.io/client-go => k8s.io/client-go v0.19.2
k8s.io/api => k8s.io/api v0.20.2
k8s.io/client-go => k8s.io/client-go v0.20.2
)
154 changes: 111 additions & 43 deletions go.sum

Large diffs are not rendered by default.

10 changes: 1 addition & 9 deletions pkg/config/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,7 @@ func GetDefaultAK() (string, string, string) {
}

func GetLocalAK() (string, string) {
var accessKeyID, accessSecret string
// first check if the environment setting
accessKeyID = os.Getenv("ACCESS_KEY_ID")
accessSecret = os.Getenv("ACCESS_KEY_SECRET")
if accessKeyID != "" && accessSecret != "" {
return accessKeyID, accessSecret
}

return accessKeyID, accessSecret
return os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET")
}

// RoleAuth define STS Token Response
Expand Down
175 changes: 175 additions & 0 deletions pkg/config/cloud_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
Copyright 2021 The OpenYurt 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 config

import (
"errors"
"os"
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)

func TestGetDefaultAK(t *testing.T) {
defer gock.Off()
testExamples := []struct {
key string
value string
stsToken string
roleName string
httpReturnData string

expectKey string
expectValue string
expectSTSToken string
}{
{
key: "xxx1",
value: "vvv1",
expectKey: "xxx1",
expectValue: "vvv1",
expectSTSToken: "",
},
{
key: "",
value: "",
stsToken: "",
roleName: "test1",
httpReturnData: "{\"AccessKeyId\": \"test1\", \"AccessKeySecret\": \"keySecret1\", \"Expiration\": \"2022-08-22T14:03:26Z\", \"SecurityToken\": \"token\",\"LastUpdated\": \"2022-08-22T08:03:26Z\", \"Code\": \"Success\"}",
expectKey: "test1",
},
}

for _, test := range testExamples {
if test.roleName == "" {
os.Setenv("ACCESS_KEY_ID", test.key)
os.Setenv("ACCESS_KEY_SECRET", test.value)
actualKey, actualValue, actualSTSToken := GetDefaultAK()
assert.Equal(t, test.expectKey, actualKey)
assert.Equal(t, test.expectValue, actualValue)
assert.Equal(t, test.expectSTSToken, actualSTSToken)
} else {
os.Setenv("ACCESS_KEY_ID", "")
os.Setenv("ACCESS_KEY_SECRET", "")
gock.New("http://100.100.100.200").
Get("/latest/meta-data/ram/security-credentials/").
Reply(200).
BodyString(test.roleName)
gock.New("http://100.100.100.200").
Get("/latest/meta-data/ram/security-credentials/" + test.roleName).
Reply(200).
BodyString(test.httpReturnData)
actualKey, _, _ := GetDefaultAK()
assert.Equal(t, test.expectKey, actualKey)
}
}

}

func TestGetLocalAK(t *testing.T) {
testExamples := []struct {
key string
value string

expectKey string
expectValue string
}{
{
key: "xxx1",
value: "vvv1",
expectKey: "xxx1",
expectValue: "vvv1",
},
}
for _, test := range testExamples {
os.Setenv("ACCESS_KEY_ID", test.key)
os.Setenv("ACCESS_KEY_SECRET", test.value)
actualKey, actualValue := GetLocalAK()
assert.Equal(t, test.expectKey, actualKey)
assert.Equal(t, test.expectValue, actualValue)

}
}

func TestGetSTSAK(t *testing.T) {
defer gock.Off()

testExamples := []struct {
roleName string
roleError error
credReturnData string
credError error
expectKeyID string
}{
{
roleName: "test1",
roleError: errors.New("test"),
expectKeyID: "",
},
{
roleName: "test2",
credReturnData: "{\"AccessKeyId\": \"test2\", \"AccessKeySecret\": \"keySecret2\", \"Expiration\": \"2022-08-22T14:03:26Z\", \"SecurityToken\": \"token\",\"LastUpdated\": \"2022-08-22T08:03:26Z\", \"Code\": \"Success\"}",
credError: errors.New("test"),
expectKeyID: "",
},
{
roleName: "test3",
credReturnData: "{\"AccessKeyId\": \"test2\", \"AccessKeySecret\": \"keySecret2\", \"Expiration\": \"2022-08-22T14:03:26Z\", \"SecurityToken\": \"token\",\"LastUpdated\": \"2022-08-22T08:03:26Z\", \"Code\": 'Success\"}",
expectKeyID: "",
},
{
roleName: "test4",
credReturnData: "{\"AccessKeyId\": \"test4\", \"AccessKeySecret\": \"keySecret2\", \"Expiration\": \"2022-08-22T14:03:26Z\", \"SecurityToken\": \"token\",\"LastUpdated\": \"2022-08-22T08:03:26Z\", \"Code\": \"Success\"}",
expectKeyID: "test4",
},
}

for _, test := range testExamples {
if test.roleError != nil {
gock.New("http://100.100.100.200").
Get("/latest/meta-data/ram/security-credentials/").
ReplyError(test.roleError)
actualKey, _, _ := GetSTSAK()
assert.Equal(t, test.expectKeyID, actualKey)
continue
}
if test.credError != nil {
gock.New("http://100.100.100.200").
Get("/latest/meta-data/ram/security-credentials/").
Reply(200).
BodyString(test.roleName)
gock.New("http://100.100.100.200").
Get("/latest/meta-data/ram/security-credentials/" + test.roleName).
ReplyError(test.credError)
actualKey, _, _ := GetSTSAK()
assert.Equal(t, test.expectKeyID, actualKey)
continue
}

gock.New("http://100.100.100.200").
Get("/latest/meta-data/ram/security-credentials/").
Reply(200).
BodyString(test.roleName)
gock.New("http://100.100.100.200").
Get("/latest/meta-data/ram/security-credentials/" + test.roleName).
Reply(200).
BodyString(test.credReturnData)
actualKey, _, _ := GetSTSAK()
assert.Equal(t, test.expectKeyID, actualKey)
}
}
59 changes: 59 additions & 0 deletions pkg/err/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2021 The OpenYurt 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 err

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestExistsFormatErr(t *testing.T) {
test := struct {
fsType string
existingFormat string
mountErr error
expectString string
}{
fsType: "ext4",
existingFormat: "xfs",
mountErr: fmt.Errorf("error mount"),
expectString: "Failed to mount the volume as :ext4, volume has already contains xfs, volume Mount error: error mount",
}

exErr := ExistsFormatErr{
FsType: test.fsType,
ExistingFormat: test.existingFormat,
MountErr: test.mountErr,
}
assert.Equal(t, test.expectString, exErr.Error())
}

func TestDeviceNotExistsErr(t *testing.T) {
test := struct {
device string
expectString string
}{
device: "/dev/vdc",
expectString: "Device [/dev/vdc] not exists in current node",
}
dr := DeviceNotExistsErr{
Device: test.device,
}
assert.Equal(t, test.expectString, dr.Error())
}
Loading

0 comments on commit 43344d5

Please sign in to comment.