From: enyinna1234 Date: Fri, 1 Nov 2019 00:31:12 +0000 (-0700) Subject: Refactors unit test code X-Git-Tag: v0.4.0~62 X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=commitdiff_plain;ds=sidebyside;h=refs%2Fchanges%2F93%2F1893%2F1;hp=49296cb7445207dc0ca8cb34cbe90ee29591530c;p=icn.git Refactors unit test code - Removes OS functions from the Utility interface - /image.go - Adds BPA_REST_API unit test Makefile recipe - Removed vendor binary Signed-off-by: Enyinna Ochulor Change-Id: I59696b70a9e5bca9aba70e51e385a2a78d21c760 --- diff --git a/Makefile b/Makefile index 0a8e3be..dbf0439 100644 --- a/Makefile +++ b/Makefile @@ -64,6 +64,9 @@ bpa_rest_api_install: bpa_rest_api_verifier: pushd $(BPA_REST_API) && make e2e_test && popd +bpa_rest_api_unit: + pushd $(BPA_REST_API) && make unit_test && popd + bashate: bashate -i E006,E003,E002,E010,E011,E042,E043 `find . -type f -not -path './cmd/bpa-operator/vendor/*' -name *.sh` diff --git a/cmd/bpa-restapi-agent/Makefile b/cmd/bpa-restapi-agent/Makefile index 6b41947..3ca017f 100644 --- a/cmd/bpa-restapi-agent/Makefile +++ b/cmd/bpa-restapi-agent/Makefile @@ -1,22 +1,19 @@ .PHONY: build -build: untar - go build -mod=vendor -o build/_output/bin/bpa-restapi-agent main.go +build: + go build -o build/_output/bin/bpa-restapi-agent main.go docker: - docker build -t akraino.org/icn/bpa-restapi-agent:latest . -f build/Dockerfile - -untar: - tar -xzvf vendor.tar.gz + docker build -t akraino.org/icn/bpa-restapi-agent:latest . -f build/Dockerfile deploy: docker ./bpa_api_install.sh -test: go_install - go test ./... -v +unit_test: go_install + go test ./internal/app go_install: - install_go.sh + ./install_go.sh e2e_test: deploy ./e2e_test.sh diff --git a/cmd/bpa-restapi-agent/e2e_test.sh b/cmd/bpa-restapi-agent/e2e_test.sh index 09c4582..cc41f21 100755 --- a/cmd/bpa-restapi-agent/e2e_test.sh +++ b/cmd/bpa-restapi-agent/e2e_test.sh @@ -36,19 +36,18 @@ if true ; then EOF fi -curr_status="" +cur_status="" -while [[ $curr_status != "Running" ]]; do +while [[ $cur_status != "Running" ]]; do + + cur_status=$(kubectl get pods | grep bpa-api-deployment | awk '{print $3}') + if [[ $cur_status != "Running" ]]; then + echo "$(date +%H:%M:%S) - BPA-RESTful-API Pod status: $cur_status" + else + break - new_status=$(kubectl get pods | grep bpa-api-deployment | awk '{print $3}') - if [[ $new_status != $curr_status ]]; then - echo "$(date +%H:%M:%S) - BPA-RESTful-API Pod status: $new_status" - curr_status=$new_status - if [[ $new_status == "Running" ]]; then - break - fi fi - if [[ $new_status == "Err"* ]]; then + if [[ $cur_status == "Err"* ]]; then exit 1 fi sleep 10 diff --git a/cmd/bpa-restapi-agent/internal/app/image.go b/cmd/bpa-restapi-agent/internal/app/image.go index 2c117a3..6b58100 100644 --- a/cmd/bpa-restapi-agent/internal/app/image.go +++ b/cmd/bpa-restapi-agent/internal/app/image.go @@ -58,11 +58,8 @@ type Utility interface { DBCreate(storeName string, key ImageKey, meta string, c Image) error DBRead(storeName string, key ImageKey, meta string) ([]byte, error) DBUnmarshal(value []byte) (Image, error) - OSMakeDir(dirpath string, perm int) error - OSCreateFile(filePath string) error GetPath(user *user.User, imageName string, storeName string) (string, string) DBDelete(storeName string, key ImageKey, meta string) error - OSRemove(filePath string) error DBUpdate(storeName string, key ImageKey, tagMeta string, c Image) error } @@ -151,32 +148,15 @@ func (v *ImageClient) CreateFile(c Image) error { if err != nil { return pkgerrors.Wrap(err, "Get file path") } - err = v.util.OSMakeDir(dirPath, 0744) + err = os.MkdirAll(dirPath, 0744) if err != nil { return pkgerrors.Wrap(err, "Make image directory") } - err = v.util.OSCreateFile(filePath) + file, err := os.Create(filePath) if err != nil { return pkgerrors.Wrap(err, "Create image file") } - - return nil -} - -func (d DBService) OSMakeDir(dirPath string, perm int) error { - err := os.MkdirAll(dirPath, 0744) - if err != nil { - return pkgerrors.Wrap(err, "Make image directory") - } - return nil -} - -func (d DBService) OSCreateFile(filePath string) error { - file1, err := os.Create(filePath) - if err != nil { - return pkgerrors.Wrap(err, "Create image file") - } - defer file1.Close() + defer file.Close() return nil } @@ -267,16 +247,10 @@ func (v *ImageClient) Delete(imageName string) error { if err != nil { return pkgerrors.Wrap(err, "Get file path") } - err = v.util.OSRemove(filePath) - - return nil -} - -func (d DBService) OSRemove(filePath string) error { - err := os.Remove(filePath) - if err != nil { - return pkgerrors.Wrap(err, "Delete image file") - } + err = os.Remove(filePath) + if err != nil { + return pkgerrors.Wrap(err, "Delete image file") + } return nil } diff --git a/cmd/bpa-restapi-agent/internal/app/image_test.go b/cmd/bpa-restapi-agent/internal/app/image_test.go index f90a5b3..e609477 100644 --- a/cmd/bpa-restapi-agent/internal/app/image_test.go +++ b/cmd/bpa-restapi-agent/internal/app/image_test.go @@ -2,7 +2,11 @@ package app import ( "fmt" + "io/ioutil" + "log" + "os" "os/user" + "path" "testing" "github.com/stretchr/testify/mock" @@ -51,11 +55,11 @@ func (m *mockValues) DBRead(name string, key ImageKey, meta string) ([]byte, err return args.Get(0).([]byte), args.Error(1) } -func (m *mockValues) DBUnmarshal(value []byte, c Image) error { +func (m *mockValues) DBUnmarshal(value []byte) (Image, error) { fmt.Println("Mocked Mongo DB Unmarshal Operation") - args := m.Called(value, c) + args := m.Called(value) - return args.Error(0) + return args.Get(0).(Image), args.Error(1) } func (m *mockValues) GetPath(u *user.User, imageName string, storeName string) (string, string) { @@ -64,20 +68,6 @@ func (m *mockValues) GetPath(u *user.User, imageName string, storeName string) ( return args.String(0), args.String(1) } -func (m *mockValues) OSMakeDir(dirPath string, perm int) error { - fmt.Println("Mocked OS Create Directory Operation") - args := m.Called(dirPath, perm) - - return args.Error(0) -} - -func (m *mockValues) OSCreateFile(filePath string) error { - fmt.Println("Mocked Create File Operation") - args := m.Called(filePath) - - return args.Error(0) -} - func (m *mockValues) DBDelete(name string, key ImageKey, meta string) error { fmt.Println("Mocked Mongo DB Delete") args := m.Called(name, key, meta) @@ -86,13 +76,6 @@ func (m *mockValues) DBDelete(name string, key ImageKey, meta string) error { } -func (m *mockValues) OSRemove(filePath string) error { - fmt.Println("Mocked OS File Remove") - args := m.Called(filePath) - - return args.Error(0) -} - func (m *mockValues) DBUpdate(s string, k ImageKey, t string, c Image) error { fmt.Println("Mocked Mongo DB Update") args := m.Called(s, k, t, c) @@ -101,45 +84,58 @@ func (m *mockValues) DBUpdate(s string, k ImageKey, t string, c Image) error { } func TestCreate(t *testing.T) { - image := Image{} + dir, err := ioutil.TempDir("", "test_images") + if err != nil { + log.Fatal(err) + } + defer os.RemoveAll(dir) + + image := Image{ + ImageName: "test_asdf", + } arr_data := []byte{} - key := ImageKey{} + key := ImageKey{ImageName:"test_asdf"} myMocks := new(mockValues) // just to get an error value err1 := errors.New("math: square root of negative number") fakeUser := user.User{} u := &fakeUser + file_path := path.Join(dir, "test_asdf") myMocks.On("DBCreate", "test_image", key, "test_meta", image).Return(nil) myMocks.On("DBRead", "test_image", key, "test_meta").Return(arr_data, err1) - myMocks.On("DBUnmarshal", arr_data, image).Return(nil) + myMocks.On("DBUnmarshal", arr_data).Return(image, nil) myMocks.On("GetCurrentUser").Return(&fakeUser, nil) - myMocks.On("GetPath", u, "", "test_image").Return("", "") - myMocks.On("OSMakeDir", "", 0744).Return(nil) - myMocks.On("OSCreateFile", "").Return(nil) + myMocks.On("GetPath", u, "", "test_image").Return(file_path, dir) imageClient := ImageClient{myMocks, "test_image", "test_meta"} - _, err := imageClient.Create(image) + _, err = imageClient.Create(image) if err != nil { t.Errorf("Some error occured!") } } func TestDelete(t *testing.T) { - key := ImageKey{} + tmpfile, err := ioutil.TempFile("", "test_images") + if err != nil { + log.Fatal(err) + } + defer os.Remove(tmpfile.Name()) + + key := ImageKey{ImageName: "test_asdf"} fakeUser := user.User{} u := &fakeUser + file_path := tmpfile.Name() myMocks := new(mockValues) myMocks.On("DBDelete", "test_image", key, "test_meta").Return(nil) myMocks.On("GetCurrentUser").Return(&fakeUser, nil) - myMocks.On("GetPath", u, "", "test_image").Return("", "") - myMocks.On("OSRemove", "").Return(nil) + myMocks.On("GetPath", u, "", "test_image").Return(file_path, "") imageClient := ImageClient{myMocks, "test_image", "test_meta"} - err := imageClient.Delete("") + err = imageClient.Delete("test_asdf") if err != nil { t.Errorf("Some error occured!") } @@ -154,7 +150,7 @@ func TestUpdate(t *testing.T) { myMocks := new(mockValues) myMocks.On("DBRead", "test_image", key, "test_meta").Return(arr_data, nil) - myMocks.On("DBUnmarshal", arr_data, image).Return(nil) + myMocks.On("DBUnmarshal", arr_data).Return(image, nil) myMocks.On("DBUpdate", "test_image", key, "test_meta", image).Return(nil) imageClient := ImageClient{myMocks, "test_image", "test_meta"} _, err := imageClient.Update("", image) diff --git a/cmd/bpa-restapi-agent/vendor.tar.gz b/cmd/bpa-restapi-agent/vendor.tar.gz deleted file mode 100644 index 74490a2..0000000 Binary files a/cmd/bpa-restapi-agent/vendor.tar.gz and /dev/null differ