Skip to content

Commit

Permalink
Merge pull request #3 from josephthaliath/main
Browse files Browse the repository at this point in the history
Adding Unit-Testing
  • Loading branch information
nephio-prow[bot] committed Dec 5, 2023
2 parents a3dbc1d + 1a647fd commit 9f6a3ee
Show file tree
Hide file tree
Showing 18 changed files with 3,551 additions and 75 deletions.
31 changes: 31 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

# Copyright 2023 The Nephio 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.
packages:
sigs.k8s.io/controller-runtime/pkg/client:
interfaces:
Client:
config:
dir: "internal/controller"
outpkg: "controller"
filename: "mock_{{.InterfaceName}}_test.go" # _test is added so that it is ignored while calculating test-coverage
workload.nephio.org/ran_deployment/internal/controller:
interfaces:
NfResource:
config:
dir: "internal/controller"
outpkg: "controller"
filename: "mock_{{.InterfaceName}}_test.go" # _test is added so that it is ignored while calculating test-coverage
inpackage: True

1 change: 0 additions & 1 deletion .prow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ presubmits:
- "-c"
- |
make unit
presubmits:
- name: oai-operator-lint
decorate: true
run_if_changed: "^.*.go$"
Expand Down
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
GO_VERSION ?= 1.20.5
GOLANG_CI_VER ?= v1.52
GOSEC_VER ?= 2.15.0
MOCKERY_VERSION=2.37.1
TEST_COVERAGE_FILE=lcov.info
TEST_COVERAGE_HTML_FILE=coverage_unit.html
TEST_COVERAGE_FUNC_FILE=func_coverage.out
OS_ARCH ?= $(shell uname -m)
OS ?= $(shell uname)

# CONTAINER_RUNNABLE checks if tests and lint check can be run inside container.
PODMAN ?= $(shell podman -v > /dev/null 2>&1; echo $$?)
Expand Down Expand Up @@ -84,6 +87,22 @@ else
gosec ./...
endif

.PHONY: install-mockery
install-mockery: ## install mockery
ifeq ($(CONTAINER_RUNNABLE), 0)
$(CONTAINER_RUNTIME) pull docker.io/vektra/mockery:v${MOCKERY_VERSION}
else
wget -qO- https://github.com/vektra/mockery/releases/download/v${MOCKERY_VERSION}/mockery_${MOCKERY_VERSION}_${OS}_${OS_ARCH}.tar.gz | sudo tar -xvzf - -C /usr/local/bin
endif

.PHONY: generate-mocks
generate-mocks:
ifeq ($(CONTAINER_RUNNABLE), 0)
sudo $(CONTAINER_RUNTIME) run --security-opt label=disable -v ${PWD}:/src -w /src docker.io/vektra/mockery:v${MOCKERY_VERSION}
else
mockery
endif

.PHONY: unit
unit: ## Run unit tests against code.
ifeq ($(CONTAINER_RUNNABLE), 0)
Expand All @@ -97,6 +116,13 @@ else
go tool cover -func=${TEST_COVERAGE_FILE} -o ${TEST_COVERAGE_FUNC_FILE}
endif

.PHONY: unit_clean
unit_clean: ## clean up the unit test artifacts created
ifeq ($(CONTAINER_RUNNABLE), 0)
$(CONTAINER_RUNTIME) system prune -f
endif
# rm ${TEST_COVERAGE_FILE} ${TEST_COVERAGE_HTML_FILE} ${TEST_COVERAGE_FUNC_FILE} > /dev/null 2>&1
rm -f ${TEST_COVERAGE_FILE} ${TEST_COVERAGE_HTML_FILE} ${TEST_COVERAGE_FUNC_FILE}

##@ Build

Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/go-logr/logr v1.2.4
github.com/nephio-project/api v1.0.1-0.20231127124455-cf14bd57b08d
github.com/stretchr/testify v1.8.1
k8s.io/api v0.27.2
k8s.io/apimachinery v0.27.2
k8s.io/client-go v0.27.2
Expand Down Expand Up @@ -44,11 +45,13 @@ require (
github.com/onsi/ginkgo/v2 v2.10.0 // indirect
github.com/onsi/gomega v1.27.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.15.1 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
Expand Down
32 changes: 0 additions & 32 deletions internal/controller/conversions.go

This file was deleted.

162 changes: 162 additions & 0 deletions internal/controller/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
Copyright 2023 The Nephio 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 controller

import (
"encoding/json"
"reflect"
"testing"

configref "github.com/nephio-project/api/references/v1alpha1"
workloadv1alpha1 "github.com/nephio-project/api/workload/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/log"
)

/*
Generate a List of Kind:Config, With NF-providers as provided in ConfigProvider
*/
func generateConfigInstanceForTesting(configProviders []string) []*configref.Config {

configInstance := []*configref.Config{}

for i := 0; i < len(configProviders); i++ {
nf := workloadv1alpha1.NFDeployment{
ObjectMeta: metav1.ObjectMeta{
Name: "nf-" + configProviders[i],
Namespace: "nf-dummy-du-ns",
},
Spec: workloadv1alpha1.NFDeploymentSpec{
Provider: configProviders[i],
},
}

raw, _ := json.Marshal(nf)
if configProviders[i] == "generate-json-error" {
raw = []byte("String Not JSON Marshal in order to actuate JSON error")
}
yourConfigInstance := configref.Config{
TypeMeta: metav1.TypeMeta{
APIVersion: "ref.nephio.org/v1alpha1",
Kind: "Config",
},
ObjectMeta: metav1.ObjectMeta{
Name: "dummy-name",
Namespace: "dummy-ns",
},
Spec: configref.ConfigSpec{
Config: runtime.RawExtension{
Raw: raw,
},
},
}
configInstance = append(configInstance, &yourConfigInstance)

}
return configInstance
}

/*
configInstancesMap is a Map of []
If Dependency = UPFDeployment, which will lead to kind:Config with Spec.config Having the Dependency kind (UPFDeployment)
So, configInstanceMap: {DependencyKind : [All the dependencies of that king injected]}
*/
func TestGetConfigInstanceByProvider(t *testing.T) {
logger := log.Log

cases := map[string]struct {
configProviders []string
providerToLook string
want string
}{
"Normal": {
configProviders: []string{"cucp.openairinterface.org", "du.openairinterface.org"},
providerToLook: "du.openairinterface.org",
want: "du.openairinterface.org",
},
"Provider Not Found": {
configProviders: []string{"cucp.openairinterface.org", "cuup.openairinterface.org"},
providerToLook: "du.openairinterface.org",
want: "error",
},
"Json Unmarshal Error": {
configProviders: []string{"generate-json-error", "cucp.openairinterface.org", "du.openairinterface.org"},
providerToLook: "du.openairinterface.org",
want: "error",
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
got := getConfigInstanceByProvider(logger, generateConfigInstanceForTesting(tc.configProviders), tc.providerToLook)
if tc.want != "error" {
wanted := workloadv1alpha1.NFDeployment{
ObjectMeta: metav1.ObjectMeta{
Name: "nf-" + tc.want,
Namespace: "nf-dummy-du-ns",
},
Spec: workloadv1alpha1.NFDeploymentSpec{
Provider: tc.want,
},
}
if !reflect.DeepEqual(*got, wanted) {
t.Errorf("getConfigInstanceByProvider returns %v | Wanted %v", got, wanted)
}
} else {
if got != nil {
t.Errorf("getConfigInstanceByProvider returns %v | Wanted nil", got)
}
}

})
}
}

func TestCheckMandatoryKinds(t *testing.T) {
cases := map[string]struct {
configSelfInfo map[string]runtime.RawExtension
want bool
}{
"Normal": {
configSelfInfo: map[string]runtime.RawExtension{
"PLMN": runtime.RawExtension{},
"RANConfig": runtime.RawExtension{},
"OAIConfig": runtime.RawExtension{},
},
want: true,
},
"PLMN missing": {
configSelfInfo: map[string]runtime.RawExtension{
"RANConfig": runtime.RawExtension{},
},
want: false,
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
got := CheckMandatoryKinds(tc.configSelfInfo)
if got != tc.want {
t.Errorf("CheckMandatoryKinds Returned %t wanted %t", got, tc.want)
}

})
}

}
Loading

0 comments on commit 9f6a3ee

Please sign in to comment.