From d976977f297fbdb209760b328e271777f565f02a Mon Sep 17 00:00:00 2001 From: Mohamed Abukar Date: Wed, 20 Nov 2019 20:39:06 +0200 Subject: [PATCH] Run unit tests Change-Id: Ia28ec0495aaa8684ba037e36fc09b2c69322d5ef Signed-off-by: Mohamed Abukar --- Dockerfile | 9 +++- Makefile | 41 -------------- build/make.go.mk | 116 ---------------------------------------- config/appmgr.yaml | 4 +- container-tag.yaml | 2 +- pkg/appmgr/appmgr.go | 2 +- pkg/restful/restful.go | 6 +-- pkg/resthooks/resthooks.go | 17 ++++-- pkg/resthooks/resthooks_test.go | 40 +++++++------- pkg/resthooks/types.go | 8 +++ 10 files changed, 56 insertions(+), 189 deletions(-) delete mode 100644 Makefile delete mode 100755 build/make.go.mk diff --git a/Dockerfile b/Dockerfile index 027e372..ef526e8 100755 --- a/Dockerfile +++ b/Dockerfile @@ -52,13 +52,18 @@ RUN GO111MODULE=on go mod download # build and test COPY . /go/src/ws +# Generate Swagger code RUN /go/bin/swagger generate server -f api/appmgr_rest_api.yaml --name AppManager -t pkg/ --exclude-main COPY . /go/src/ws -RUN make -C /go/src/ws go-build +# Build the code +RUN GO111MODULE=on GO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /go/src/ws/cache/go/cmd/appmgr cmd/appmgr.go -RUN make -C /go/src/ws go-test-fmt +# Run unit tests +RUN GO111MODULE=on GO_ENABLED=0 GOOS=linux go test -p 1 -cover ./pkg/cm/ ./pkg/helm/ ./pkg/resthooks/ + +RUN gofmt -l $(find cmd/ pkg/ -name '*.go' -not -name '*_test.go') CMD ["/bin/bash"] diff --git a/Makefile b/Makefile deleted file mode 100644 index 7a8aeca..0000000 --- a/Makefile +++ /dev/null @@ -1,41 +0,0 @@ - -# Copyright (c) 2019 AT&T Intellectual Property. -# Copyright (c) 2019 Nokia. -# -# 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. - -.DEFAULT: go-build - -default: go-build - -build: go-build - -test: go-test - -#------------------------------------------------------------------------------ -# -#------------------------------------------------------------------------------ -ROOT_DIR:=$(dir $(abspath $(lastword $(MAKEFILE_LIST)))) -CACHE_DIR?=$(abspath $(ROOT_DIR)/cache) - -#------------------------------------------------------------------------------ -# -# Build and test targets -# -#------------------------------------------------------------------------------ - -XAPP_NAME:=appmgr -XAPP_ROOT:=cmd -XAPP_TESTENV:="RMR_SEED_RT=config/uta_rtg.rt CFG_FILE=$(ROOT_DIR)helm_chart/uemgr/descriptors/config-file.json" -include build/make.go.mk - diff --git a/build/make.go.mk b/build/make.go.mk deleted file mode 100755 index 2208487..0000000 --- a/build/make.go.mk +++ /dev/null @@ -1,116 +0,0 @@ -# Copyright (c) 2019 AT&T Intellectual Property. -# Copyright (c) 2019 Nokia. -# -# 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. - - -#------------------------------------------------------------------------------ -# -#------------------------------------------------------------------------------ -ifndef ROOT_DIR -$(error ROOT_DIR NOT DEFINED) -endif -ifndef CACHE_DIR -$(error CACHE_DIR NOT DEFINED) -endif - -#------------------------------------------------------------------------------ -# -#------------------------------------------------------------------------------ - -GO_CACHE_DIR?=$(abspath $(CACHE_DIR)/go) - -#------------------------------------------------------------------------------ -# -#------------------------------------------------------------------------------ -ifndef MAKE_GO_TARGETS -MAKE_GO_TARGETS:=1 - - -.PHONY: FORCE go-build go-test go-test-fmt go-fmt go-clean - -FORCE: - - -GOOS=$(shell go env GOOS) -GOCMD=go -GOBUILD=$(GOCMD) build -a -installsuffix cgo -GORUN=$(GOCMD) run -a -installsuffix cgo -GOCLEAN=$(GOCMD) clean -GOTEST=$(GOCMD) test -v -GOGET=$(GOCMD) get - -GOFILES:=$(shell find $(ROOT_DIR) -name '*.go' -not -name '*_test.go') -GOALLFILES:=$(shell find $(ROOT_DIR) -name '*.go') -GOMODFILES:=go.mod go.sum - - -.SECONDEXPANSION: -$(GO_CACHE_DIR)/%: $(GOFILES) $(GOMODFILES) $$(BUILDDEPS) - @echo "Building:\t$*" - GO111MODULE=on GO_ENABLED=0 GOOS=linux $(GOBUILD) -o $@ cmd/appmgr.go - - -.SECONDEXPANSION: -$(GO_CACHE_DIR)/%_test: $(GOALLFILES) $(GOMODFILES) $$(BUILDDEPS) FORCE - @echo "Testing:\t$*" - GO111MODULE=on GO_ENABLED=0 GOOS=linux $(GOTEST) -coverprofile $(COVEROUT) ./pkg/resthooks/ ./pkg/helm/ ./pkg/cm/ - test -e $@ && (eval $(TESTENV) $@ -test.coverprofile $(COVEROUT) || false) || true - test -e $@ && (go tool cover -html=$(COVEROUT) -o $(COVERHTML) || false) || true - - -.SECONDEXPANSION: -go-build: GO_TARGETS:= -go-build: $$(GO_TARGETS) - -.SECONDEXPANSION: -go-test: GO_TARGETS:= -go-test: $$(GO_TARGETS) - -go-test-fmt: $(GOFILES) - @(RESULT="$$(gofmt -l $^)"; test -z "$${RESULT}" || (echo -e "gofmt failed:\n$${RESULT}" && false) ) - -go-fmt: $(GOFILES) - gofmt -w -s $^ - -go-mod-tidy: FORCE - GO111MODULE=on go mod tidy - -go-mod-download: FORCE - GO111MODULE=on go mod download - -go-clean: GO_TARGETS:= -go-clean: - @echo " > Cleaning build cache" - @-rm -rf $(GO_TARGETS)* 2> /dev/null - go clean 2> /dev/null - - -endif - -#------------------------------------------------------------------------------ -# -#-------------------------------------------------------------------- ---------- - -$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME): BUILDDEPS:=$(XAPP_BUILDDEPS) - - -$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test: BUILDDEPS:=$(XAPP_BUILDDEPS) -$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test: COVEROUT:=$(abspath $(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_cover.out) -$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test: COVERHTML:=$(abspath $(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_cover.html) -$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test: TESTENV:=$(XAPP_TESTENV) - -go-build: GO_TARGETS+=$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME) -go-test: GO_TARGETS+=$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test -go-clean: GO_TARGETS+=$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME) $(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test - diff --git a/config/appmgr.yaml b/config/appmgr.yaml index 58545fc..8f24929 100755 --- a/config/appmgr.yaml +++ b/config/appmgr.yaml @@ -13,8 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -"local": - "host": ":8080" "helm": "host": "192.168.0.12:31807" "repo": "http://192.168.0.6/charts" @@ -26,7 +24,7 @@ "helm-password-file": "./helm_repo_password" "retry": 1 "xapp": - "namespace": "default" + "namespace": "ricxapp" "tarDir": "/tmp" "schema": "descriptors/schema.json" "config": "config/config-file.json" diff --git a/container-tag.yaml b/container-tag.yaml index 3724441..98ef6c2 100755 --- a/container-tag.yaml +++ b/container-tag.yaml @@ -1,4 +1,4 @@ # The Jenkins job uses this string for the tag in the image name # for example nexus3.o-ran-sc.org:10004/my-image-name:my-tag --- -tag: '0.2.0' +tag: '0.3.0' diff --git a/pkg/appmgr/appmgr.go b/pkg/appmgr/appmgr.go index 28e98f9..03bcdcf 100755 --- a/pkg/appmgr/appmgr.go +++ b/pkg/appmgr/appmgr.go @@ -68,5 +68,5 @@ func loadConfig() { func Init() { loadConfig() Logger = logger.NewLogger("appmgr") - Logger.SetMdc("xm", "0.2.0") + Logger.SetMdc("xm", "0.3.0") } diff --git a/pkg/restful/restful.go b/pkg/restful/restful.go index c0ef580..b2a94e3 100755 --- a/pkg/restful/restful.go +++ b/pkg/restful/restful.go @@ -43,7 +43,7 @@ func NewRestful() *Restful { r := &Restful{ helm: helm.NewHelm(), cm: cm.NewCM(), - rh: resthooks.NewResthook(), + rh: resthooks.NewResthook(true), ready: false, } r.api = r.SetupHandler() @@ -53,8 +53,8 @@ func NewRestful() *Restful { func (r *Restful) Run() { server := restapi.NewServer(r.api) defer server.Shutdown() - server.Port = 8080 //viper.GetInt("local.port") - server.Host = "0.0.0.0" //viper.GetString("local.host") + server.Port = 8080 + server.Host = "0.0.0.0" appmgr.Logger.Info("Xapp manager started ... serving on %s:%d\n", server.Host, server.Port) diff --git a/pkg/resthooks/resthooks.go b/pkg/resthooks/resthooks.go index 5076b06..471eec1 100755 --- a/pkg/resthooks/resthooks.go +++ b/pkg/resthooks/resthooks.go @@ -32,13 +32,17 @@ import ( "gerrit.oran-osc.org/r/ric-plt/appmgr/pkg/models" ) -func NewResthook() *Resthook { +func NewResthook(restoreData bool) *Resthook { rh := &Resthook{ client: &http.Client{}, db: sdl.NewSdlInstance("appmgr", sdl.NewDatabase()), } - rh.subscriptions = rh.RestoreSubscriptions() + if restoreData { + rh.subscriptions = rh.RestoreSubscriptions() + } else { + rh.subscriptions = cmap.New() + } return rh } @@ -122,7 +126,14 @@ func (rh *Resthook) NotifyClients(xapps models.AllDeployedXapps, et models.Event } func (rh *Resthook) notify(xapps models.AllDeployedXapps, et models.EventType, s SubscriptionInfo, seq int64) error { - notif := models.SubscriptionNotification{ID: s.Id, Version: seq, EventType: et, XApps: xapps} + xappData, err := json.Marshal(xapps) + if err != nil { + appmgr.Logger.Info("json.Marshal failed: %v", err) + return err + } + + // TODO: Use models.SubscriptionNotification instead of internal ... + notif := SubscriptionNotification{ID: s.Id, Version: seq, Event: string(et), XApps: string(xappData)} jsonData, err := json.Marshal(notif) if err != nil { appmgr.Logger.Info("json.Marshal failed: %v", err) diff --git a/pkg/resthooks/resthooks_test.go b/pkg/resthooks/resthooks_test.go index 018dee8..176fd2d 100755 --- a/pkg/resthooks/resthooks_test.go +++ b/pkg/resthooks/resthooks_test.go @@ -28,6 +28,7 @@ import ( "gerrit.oran-osc.org/r/ric-plt/appmgr/pkg/models" ) +var rh *Resthook var resp models.SubscriptionResponse // Test cases @@ -35,88 +36,89 @@ func TestMain(m *testing.M) { appmgr.Init() appmgr.Logger.SetLevel(0) + rh = NewResthook(false) code := m.Run() os.Exit(code) } func TestAddSubscriptionSuccess(t *testing.T) { - resp := NewResthook().AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook")) + resp := rh.AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook")) assert.Equal(t, resp.Version, int64(0)) assert.Equal(t, resp.EventType, models.EventTypeCreated) } func TestAddSubscriptionExists(t *testing.T) { - resp := NewResthook().AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook")) + resp := rh.AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook")) assert.Equal(t, resp.Version, int64(0)) assert.Equal(t, resp.EventType, models.EventType("")) } func TestDeletesubscriptionSuccess(t *testing.T) { - resp := NewResthook().AddSubscription(CreateSubscription(models.EventTypeDeleted, int64(5), int64(10), "http://localhost:8087/xapps_hook2")) + resp := rh.AddSubscription(CreateSubscription(models.EventTypeDeleted, int64(5), int64(10), "http://localhost:8087/xapps_hook2")) assert.Equal(t, resp.Version, int64(0)) assert.Equal(t, resp.EventType, models.EventTypeDeleted) - resp, ok := NewResthook().DeleteSubscription(resp.ID) + resp, ok := rh.DeleteSubscription(resp.ID) assert.Equal(t, ok, true) assert.Equal(t, resp.Version, int64(0)) assert.Equal(t, resp.EventType, models.EventTypeDeleted) } func TestDeletesubscriptionInvalid(t *testing.T) { - resp, ok := NewResthook().DeleteSubscription("Non-existent-ID") + resp, ok := rh.DeleteSubscription("Non-existent-ID") assert.Equal(t, ok, false) assert.Equal(t, resp.Version, int64(0)) assert.Equal(t, resp.EventType, models.EventType("")) } func TestModifySubscriptionSuccess(t *testing.T) { - resp := NewResthook().AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook2")) + resp := rh.AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook2")) assert.Equal(t, resp.Version, int64(0)) assert.Equal(t, resp.EventType, models.EventTypeCreated) - resp, ok := NewResthook().ModifySubscription(resp.ID, CreateSubscription(models.EventTypeModified, int64(5), int64(10), "http://localhost:8087/xapps_hook2")) + resp, ok := rh.ModifySubscription(resp.ID, CreateSubscription(models.EventTypeModified, int64(5), int64(10), "http://localhost:8087/xapps_hook2")) assert.Equal(t, ok, true) assert.Equal(t, resp.Version, int64(0)) assert.Equal(t, resp.EventType, models.EventTypeModified) } func TestModifysubscriptionInvalid(t *testing.T) { - resp, ok := NewResthook().DeleteSubscription("Non-existent-ID") + resp, ok := rh.DeleteSubscription("Non-existent-ID") assert.Equal(t, ok, false) assert.Equal(t, resp.Version, int64(0)) assert.Equal(t, resp.EventType, models.EventType("")) } func TestGetAllSubscriptionSuccess(t *testing.T) { - NewResthook().FlushSubscriptions() - subscriptions := NewResthook().GetAllSubscriptions() + rh.FlushSubscriptions() + subscriptions := rh.GetAllSubscriptions() assert.Equal(t, len(subscriptions), 0) - NewResthook().AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook")) - NewResthook().AddSubscription(CreateSubscription(models.EventTypeModified, int64(5), int64(10), "http://localhost:8087/xapps_hook2")) + rh.AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook")) + rh.AddSubscription(CreateSubscription(models.EventTypeModified, int64(5), int64(10), "http://localhost:8087/xapps_hook2")) - subscriptions = NewResthook().GetAllSubscriptions() + subscriptions = rh.GetAllSubscriptions() assert.Equal(t, len(subscriptions), 2) } func TestGetSubscriptionByIdSuccess(t *testing.T) { - NewResthook().FlushSubscriptions() + rh.FlushSubscriptions() sub1 := CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook") sub2 := CreateSubscription(models.EventTypeModified, int64(5), int64(10), "http://localhost:8087/xapps_hook2") - r1 := NewResthook().AddSubscription(sub1) - r2 := NewResthook().AddSubscription(sub2) + r1 := rh.AddSubscription(sub1) + r2 := rh.AddSubscription(sub2) - resp1, ok := NewResthook().GetSubscriptionById(r1.ID) + resp1, ok := rh.GetSubscriptionById(r1.ID) assert.Equal(t, ok, true) assert.Equal(t, resp1.Data, sub1.Data) - resp2, ok := NewResthook().GetSubscriptionById(r2.ID) + resp2, ok := rh.GetSubscriptionById(r2.ID) assert.Equal(t, ok, true) assert.Equal(t, resp2.Data, sub2.Data) } func TestTeardown(t *testing.T) { - NewResthook().FlushSubscriptions() + rh.FlushSubscriptions() } func CreateSubscription(et models.EventType, maxRetries, retryTimer int64, targetUrl string) models.SubscriptionRequest { diff --git a/pkg/resthooks/types.go b/pkg/resthooks/types.go index 830576b..e613476 100755 --- a/pkg/resthooks/types.go +++ b/pkg/resthooks/types.go @@ -39,3 +39,11 @@ type Resthook struct { db *sdl.SdlInstance Seq int64 } + +// TODO: remove this when RTMGR changes done +type SubscriptionNotification struct { + Event string `json:"eventType,omitempty"` + ID string `json:"id,omitempty"` + Version int64 `json:"version,omitempty"` + XApps string `json:"xApps,omitempty"` +} -- 2.16.6