KubeOS/0012-KubeOS-add-unit-test.patch
Yuhang Wei 1d36b74685 KubeOS:sync code from source master branch
Signed-off-by: Yuhang Wei <weiyuhang3@huawei.com>
2024-02-26 09:54:27 +08:00

192 lines
6.8 KiB
Diff

From 9bd13d2a1b8b7282d7247caf9ba54f11bb297cd5 Mon Sep 17 00:00:00 2001
From: Yuhang Wei <weiyuhang3@huawei.com>
Date: Fri, 11 Aug 2023 20:25:46 +0800
Subject: [PATCH 12/17] KubeOS: add unit test
add containerd_image and disk_image unit test
modify make test command for only testing server and controllers
Signed-off-by: Yuhang Wei <weiyuhang3@huawei.com>
---
Makefile | 2 +-
cmd/agent/server/containerd_image_test.go | 10 ++-
cmd/agent/server/disk_image_test.go | 83 +++++++++++++++++++----
3 files changed, 80 insertions(+), 15 deletions(-)
diff --git a/Makefile b/Makefile
index b5b6161..eddf9e6 100644
--- a/Makefile
+++ b/Makefile
@@ -133,7 +133,7 @@ kustomize:
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v3@v3.8.7)
ARCH := $(shell uname -m)
-TEST_CMD := go test ./... -race -count=1 -timeout=300s -cover -gcflags=all=-l -p 1
+TEST_CMD := go test `go list ./cmd/... | grep -E 'server|controllers'` -race -count=1 -timeout=300s -cover -gcflags=all=-l -p 1
ifeq ($(ARCH), aarch64)
TEST_CMD := ETCD_UNSUPPORTED_ARCH=arm64 $(TEST_CMD)
diff --git a/cmd/agent/server/containerd_image_test.go b/cmd/agent/server/containerd_image_test.go
index d7133c3..85347c8 100644
--- a/cmd/agent/server/containerd_image_test.go
+++ b/cmd/agent/server/containerd_image_test.go
@@ -32,7 +32,6 @@ func Test_conImageHandler_downloadImage(t *testing.T) {
want string
wantErr bool
}{
-
{
name: "pullImageError",
c: conImageHandler{},
@@ -62,6 +61,15 @@ func Test_conImageHandler_downloadImage(t *testing.T) {
want: "update-test1/upadte.img",
wantErr: false,
},
+ {
+ name: "invalid image name",
+ c: conImageHandler{},
+ args: args{
+ req: &pb.UpdateRequest{ContainerImage: "nginx;v1"},
+ },
+ want: "",
+ wantErr: true,
+ },
}
patchPrepareEnv := gomonkey.ApplyFunc(prepareEnv, func() (preparePath, error) {
return preparePath{updatePath: "update-test1/",
diff --git a/cmd/agent/server/disk_image_test.go b/cmd/agent/server/disk_image_test.go
index 71c5de7..265b323 100644
--- a/cmd/agent/server/disk_image_test.go
+++ b/cmd/agent/server/disk_image_test.go
@@ -21,6 +21,7 @@ import (
"crypto/x509/pkix"
"encoding/hex"
"encoding/pem"
+ "fmt"
"io"
"math/big"
"net/http"
@@ -52,36 +53,78 @@ func Test_download(t *testing.T) {
want string
wantErr bool
}{
- // {name: "errornil", args: args{&pb.UpdateRequest{Certs: &pb.CertsInfo{}}}, want: "", wantErr: true},
- // {name: "normal", args: args{&pb.UpdateRequest{ImageUrl: "http://www.openeuler.org/zh/", FlagSafe: true, Certs: &pb.CertsInfo{}}}, want: "/persist/update.img", wantErr: false},
- // {name: "errornodir", args: args{&pb.UpdateRequest{ImageUrl: "http://www.openeuler.org/zh/", FlagSafe: true, Certs: &pb.CertsInfo{}}}, want: "", wantErr: true},
+ {name: "errornil", args: args{&pb.UpdateRequest{Certs: &pb.CertsInfo{}}}, want: "", wantErr: true},
+ {name: "error response", args: args{&pb.UpdateRequest{ImageUrl: "http://www.openeuler.abc", FlagSafe: true, Certs: &pb.CertsInfo{}}}, want: "", wantErr: true},
{
name: "normal",
args: args{
req: &pb.UpdateRequest{
ImageUrl: "http://www.openeuler.org/zh/",
+ FlagSafe: true,
+ Certs: &pb.CertsInfo{},
},
},
want: tmpFileForDownload,
wantErr: false,
},
+ {
+ name: "disk space not enough",
+ args: args{
+ req: &pb.UpdateRequest{
+ ImageUrl: "http://www.openeuler.org/zh/",
+ FlagSafe: true,
+ Certs: &pb.CertsInfo{},
+ },
+ },
+ want: "",
+ wantErr: true,
+ },
}
- patchStatfs := gomonkey.ApplyFunc(syscall.Statfs, func(path string, stat *syscall.Statfs_t) error {
+ var patchStatfs *gomonkey.Patches
+ patchStatfs = gomonkey.ApplyFunc(syscall.Statfs, func(path string, stat *syscall.Statfs_t) error {
stat.Bfree = 3000
stat.Bsize = 4096
return nil
})
defer patchStatfs.Reset()
- patchGetImageUrl := gomonkey.ApplyFuncReturn(getImageURL, &http.Response{
- StatusCode: http.StatusOK,
- ContentLength: 5,
- Body: io.NopCloser(strings.NewReader("hello")),
- }, nil)
+ patchGetImageUrl := gomonkey.ApplyFuncSeq(getImageURL,
+ []gomonkey.OutputCell{
+ {Values: gomonkey.Params{&http.Response{}, fmt.Errorf("error")}},
+ {Values: gomonkey.Params{&http.Response{StatusCode: http.StatusBadRequest, Body: io.NopCloser(strings.NewReader(""))}, nil}},
+ {
+ Values: gomonkey.Params{
+ &http.Response{
+ StatusCode: http.StatusOK,
+ ContentLength: 5,
+ Body: io.NopCloser(strings.NewReader("hello")),
+ },
+ nil,
+ },
+ },
+ {
+ Values: gomonkey.Params{
+ &http.Response{
+ StatusCode: http.StatusOK,
+ ContentLength: 5,
+ Body: io.NopCloser(strings.NewReader("hello")),
+ },
+ nil,
+ },
+ },
+ },
+ )
defer patchGetImageUrl.Reset()
patchOSCreate := gomonkey.ApplyFuncReturn(os.Create, tmpFile, nil)
defer patchOSCreate.Reset()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
+ if tt.name == "disk space not enough" {
+ patchStatfs = gomonkey.ApplyFunc(syscall.Statfs, func(path string, stat *syscall.Statfs_t) error {
+ stat.Bfree = 1
+ stat.Bsize = 4096
+ return nil
+ })
+ }
got, err := download(tt.args.req)
if (err != nil) != tt.wantErr {
t.Errorf("download() error = %v, wantErr %v", err, tt.wantErr)
@@ -160,13 +203,27 @@ func Test_getImageURL(t *testing.T) {
MTLS: false,
Certs: &pb.CertsInfo{},
}}, want: &http.Response{StatusCode: http.StatusOK}, wantErr: false},
+ {name: "httpsLoadCertsError", args: args{req: &pb.UpdateRequest{
+ ImageUrl: "https://www.openeuler.abc/zh/",
+ FlagSafe: true,
+ MTLS: false,
+ Certs: &pb.CertsInfo{},
+ }}, want: &http.Response{}, wantErr: true},
+ {name: "httpsMLTSLoadCertsError", args: args{req: &pb.UpdateRequest{
+ ImageUrl: "https://www.openeuler.abc/zh/",
+ FlagSafe: true,
+ MTLS: true,
+ Certs: &pb.CertsInfo{},
+ }}, want: &http.Response{}, wantErr: true},
}
- patchLoadClientCerts := gomonkey.ApplyFunc(loadClientCerts, func(caCert, clientCert, clientKey string) (*http.Client, error) {
- return &http.Client{}, nil
+ patchLoadClientCerts := gomonkey.ApplyFuncSeq(loadClientCerts, []gomonkey.OutputCell{
+ {Values: gomonkey.Params{&http.Client{}, nil}},
+ {Values: gomonkey.Params{&http.Client{}, fmt.Errorf("error")}},
})
defer patchLoadClientCerts.Reset()
- patchLoadCaCerts := gomonkey.ApplyFunc(loadCaCerts, func(caCert string) (*http.Client, error) {
- return &http.Client{}, nil
+ patchLoadCaCerts := gomonkey.ApplyFuncSeq(loadCaCerts, []gomonkey.OutputCell{
+ {Values: gomonkey.Params{&http.Client{}, nil}},
+ {Values: gomonkey.Params{&http.Client{}, fmt.Errorf("error")}},
})
defer patchLoadCaCerts.Reset()
patchGet := gomonkey.ApplyFunc(http.Get, func(url string) (resp *http.Response, err error) {
--
2.39.0