Merge pull request #2486 from gccio/improve-test-coverage-edgecontroller

edgecontroller: improve test coverage
This commit is contained in:
KubeEdge Bot 2021-01-05 17:39:44 +08:00 committed by GitHub
commit ab89de0626
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 1585 additions and 0 deletions

View File

@ -0,0 +1,60 @@
/*
Copyright 2021 The KubeEdge 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 (
"reflect"
"testing"
"github.com/kubeedge/kubeedge/pkg/apis/componentconfig/cloudcore/v1alpha1"
)
func TestInitConfigure(t *testing.T) {
type args struct {
ec *v1alpha1.EdgeController
kubeAPIConfig *v1alpha1.KubeAPIConfig
nodeName string
edgesite bool
}
ec := &v1alpha1.EdgeController{}
kac := &v1alpha1.KubeAPIConfig{}
nodeName := "NodeA"
tests := []struct {
name string
args args
}{
{
"TestInitCnofigure() Caes 1: init configurae",
args{
ec: ec,
kubeAPIConfig: kac,
nodeName: nodeName,
edgesite: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
InitConfigure(tt.args.ec, tt.args.kubeAPIConfig, tt.args.nodeName, tt.args.edgesite)
if !reflect.DeepEqual(*tt.args.ec, Config.EdgeController) || !reflect.DeepEqual(*tt.args.kubeAPIConfig, Config.KubeAPIConfig) || tt.args.nodeName != Config.NodeName || tt.args.edgesite != Config.EdgeSiteEnable {
t.Errorf("TestInitCnofigure() failed. got: %v/%v/%v/%v want: %v/%v/%v/%v", Config.EdgeController, Config.KubeAPIConfig, Config.NodeName, Config.EdgeSiteEnable, *tt.args.ec, *tt.args.kubeAPIConfig, tt.args.nodeName, tt.args.edgesite)
}
})
}
}

View File

@ -0,0 +1,271 @@
/*
Copyright 2021 The KubeEdge 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 manager
import (
"reflect"
"testing"
"time"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
)
var (
TestOldPodObject = &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
UID: types.UID("commontestpod"),
Name: "TestPod",
Namespace: "default",
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "TestPodContainer",
Image: "busybox",
},
},
},
}
TestNewPodObject = &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
UID: types.UID("commontestpod"),
Name: "TestPod",
Namespace: "default",
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "TestPodContainer",
Image: "nginx",
},
},
},
}
TestDeletingPodObject = &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
UID: types.UID("commontestpod"),
Name: "TestPod",
Namespace: "default",
DeletionTimestamp: &metav1.Time{
Time: time.Now().Add(1 * time.Minute),
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "TestPodContainer",
Image: "nginx",
},
},
},
}
)
func TestCommonResourceEventHandler_obj2Event(t *testing.T) {
type fields struct {
events chan watch.Event
}
type args struct {
t watch.EventType
obj interface{}
}
tests := []struct {
name string
fields fields
args args
}{
{
"TestCommonResourceEventHandler_obj2Event(): Case 1: Test with Pod",
fields{
events: make(chan watch.Event, 1),
},
args{
watch.Added,
TestOldPodObject,
},
},
{
"TestCommonResourceEventHandler_obj2Event(): Case 2: Test with string",
fields{
events: make(chan watch.Event, 1),
},
args{
watch.Added,
"Hello Kubeedge",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &CommonResourceEventHandler{
events: tt.fields.events,
}
c.obj2Event(tt.args.t, tt.args.obj)
if reflect.TypeOf(tt.args.obj).Kind() == reflect.String {
return
}
obj := <-c.events
if !reflect.DeepEqual(obj.Type, tt.args.t) || !reflect.DeepEqual(obj.Object, tt.args.obj) {
t.Errorf("TestCommonResourceEventHandler_obj2Event() failed. got: %v/%v, want %v/%v", obj.Type, obj.Object, tt.args.t, tt.args.obj)
}
})
}
}
func TestCommonResourceEventHandler_OnAdd(t *testing.T) {
type fields struct {
events chan watch.Event
}
type args struct {
obj interface{}
}
tests := []struct {
name string
fields fields
args args
}{
{
"TestCommonResourceEventHandler_OnAdd(): Case 1: Add Pod",
fields{
events: make(chan watch.Event, 1),
},
args{
TestOldPodObject,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &CommonResourceEventHandler{
events: tt.fields.events,
}
c.OnAdd(tt.args.obj)
obj := <-c.events
if !reflect.DeepEqual(watch.Added, obj.Type) || !reflect.DeepEqual(obj.Object, tt.args.obj) {
t.Errorf("TestCommonResourceEventHandler_OnAdd() failed. got: %v/%v, want %v/%v", obj.Type, obj.Object, watch.Added, tt.args.obj)
}
})
}
}
func TestCommonResourceEventHandler_OnUpdate(t *testing.T) {
type fields struct {
events chan watch.Event
}
type args struct {
oldObj interface{}
newObj interface{}
}
tests := []struct {
name string
fields fields
args args
}{
{
"TestCommonResourceEventHandler_OnUpdate(): Case 1: Test with Pod",
fields{
events: make(chan watch.Event, 1),
},
args{
TestOldPodObject,
TestNewPodObject,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &CommonResourceEventHandler{
events: tt.fields.events,
}
c.OnUpdate(tt.args.oldObj, tt.args.newObj)
obj := <-c.events
if !reflect.DeepEqual(watch.Modified, obj.Type) || !reflect.DeepEqual(obj.Object, tt.args.newObj) {
t.Errorf("TestCommonResourceEventHandler_OnUpdate() failed. got: %v/%v, want %v/%v", obj.Type, obj.Object, watch.Modified, tt.args.newObj)
}
})
}
}
func TestCommonResourceEventHandler_OnDelete(t *testing.T) {
type fields struct {
events chan watch.Event
}
type args struct {
obj interface{}
}
tests := []struct {
name string
fields fields
args args
}{
{
"TestCommonResourceEventHandler_OnDelete(): Case 1: Delete Pod",
fields{
events: make(chan watch.Event, 1),
},
args{
TestOldPodObject,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &CommonResourceEventHandler{
events: tt.fields.events,
}
c.OnDelete(tt.args.obj)
obj := <-c.events
if !reflect.DeepEqual(watch.Deleted, obj.Type) || !reflect.DeepEqual(obj.Object, tt.args.obj) {
t.Errorf("TestCommonResourceEventHandler_Delete() failed. got: %v/%v, want %v/%v", obj.Type, obj.Object, watch.Deleted, tt.args.obj)
}
})
}
}
func TestNewCommonResourceEventHandler(t *testing.T) {
type args struct {
events chan watch.Event
}
ch := make(chan watch.Event, 1)
tests := []struct {
name string
args args
want *CommonResourceEventHandler
}{
{
"TestNewCommonResourceEventHandler(): Case 1: New with events",
args{
events: ch,
},
&CommonResourceEventHandler{
events: ch,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewCommonResourceEventHandler(tt.args.events); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewCommonResourceEventHandler() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -0,0 +1,104 @@
/*
Copyright 2021 The KubeEdge 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 manager
import (
"fmt"
"os"
"reflect"
"testing"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"github.com/kubeedge/kubeedge/cloud/pkg/edgecontroller/config"
"github.com/kubeedge/kubeedge/cloud/pkg/edgecontroller/utils"
"github.com/kubeedge/kubeedge/pkg/apis/componentconfig/cloudcore/v1alpha1"
)
func TestConfigMapManager_Events(t *testing.T) {
type fields struct {
events chan watch.Event
}
ch := make(chan watch.Event, 1)
tests := []struct {
name string
fields fields
want chan watch.Event
}{
{
"TestConfigMapManager_Events(): Case 1",
fields{
events: ch,
},
ch,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmm := &ConfigMapManager{
events: tt.fields.events,
}
if got := cmm.Events(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ConfigMapManager.Events() = %v, want %v", got, tt.want)
}
})
}
}
func TestNewConfigMapManager(t *testing.T) {
type args struct {
kubeClient *kubernetes.Clientset
namespace string
}
config.Config.KubeAPIConfig = v1alpha1.KubeAPIConfig{
KubeConfig: fmt.Sprintf("%s/.kube/config", os.Getenv("HOME")),
QPS: 100,
Burst: 200,
ContentType: "application/vnd.kubernetes.protobuf",
}
config.Config.Buffer = &v1alpha1.EdgeControllerBuffer{
ConfigMapEvent: 1024,
}
cli, err := utils.KubeClient()
if err != nil {
t.Skip("No k8s cluster config file in $HOME/.kube/config, skip it.")
return
}
tests := []struct {
name string
args args
}{
{
"TestNewConfigMapManager(): Case 1",
args{
cli,
v1.NamespaceAll,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
NewConfigMapManager(tt.args.kubeClient, tt.args.namespace)
})
}
}

View File

@ -0,0 +1,104 @@
/*
Copyright 2021 The KubeEdge 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 manager
import (
"fmt"
"os"
"reflect"
"testing"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"github.com/kubeedge/kubeedge/cloud/pkg/edgecontroller/config"
"github.com/kubeedge/kubeedge/cloud/pkg/edgecontroller/utils"
"github.com/kubeedge/kubeedge/pkg/apis/componentconfig/cloudcore/v1alpha1"
)
func TestEndpointsManager_Events(t *testing.T) {
type fields struct {
events chan watch.Event
}
ch := make(chan watch.Event, 1)
tests := []struct {
name string
fields fields
want chan watch.Event
}{
{
"TestEndpointsManager_Events(): Case 1",
fields{
events: ch,
},
ch,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sm := &EndpointsManager{
events: tt.fields.events,
}
if got := sm.Events(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("EndpointsManager.Events() = %v, want %v", got, tt.want)
}
})
}
}
func TestNewEndpointsManager(t *testing.T) {
type args struct {
kubeClient *kubernetes.Clientset
namespace string
}
config.Config.KubeAPIConfig = v1alpha1.KubeAPIConfig{
KubeConfig: fmt.Sprintf("%s/.kube/config", os.Getenv("HOME")),
QPS: 100,
Burst: 200,
ContentType: "application/vnd.kubernetes.protobuf",
}
config.Config.Buffer = &v1alpha1.EdgeControllerBuffer{
ConfigMapEvent: 1024,
}
cli, err := utils.KubeClient()
if err != nil {
t.Skip("No k8s cluster config file in $HOME/.kube/config, skip it.")
return
}
tests := []struct {
name string
args args
}{
{
"TestNewConfigMapManager(): Case 1",
args{
cli,
v1.NamespaceAll,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
NewEndpointsManager(tt.args.kubeClient, tt.args.namespace)
})
}
}

View File

@ -0,0 +1,102 @@
/*
Copyright 2021 The KubeEdge 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 manager
import (
"fmt"
"os"
"reflect"
"testing"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"github.com/kubeedge/kubeedge/cloud/pkg/edgecontroller/config"
"github.com/kubeedge/kubeedge/cloud/pkg/edgecontroller/utils"
"github.com/kubeedge/kubeedge/pkg/apis/componentconfig/cloudcore/v1alpha1"
)
func TestNodesManager_Events(t *testing.T) {
type fields struct {
events chan watch.Event
}
ch := make(chan watch.Event, 1)
tests := []struct {
name string
fields fields
want chan watch.Event
}{
{
"TestNodesManager_Events(): Case 1",
fields{
events: ch,
},
ch,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
nm := &NodesManager{
events: tt.fields.events,
}
if got := nm.Events(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NodesManager.Events() = %v, want %v", got, tt.want)
}
})
}
}
func TestNewNodesManager(t *testing.T) {
type args struct {
kubeClient *kubernetes.Clientset
namespace string
}
config.Config.KubeAPIConfig = v1alpha1.KubeAPIConfig{
KubeConfig: fmt.Sprintf("%s/.kube/config", os.Getenv("HOME")),
QPS: 100,
Burst: 200,
ContentType: "application/vnd.kubernetes.protobuf",
}
config.Config.Buffer = &v1alpha1.EdgeControllerBuffer{
ConfigMapEvent: 1024,
}
cli, err := utils.KubeClient()
if err != nil {
t.Skip("No k8s cluster config file in $HOME/.kube/config, skip it.")
return
}
tests := []struct {
name string
args args
}{
{
"TestNewNodesManager(): Case 1",
args{
cli,
v1.NamespaceAll,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
NewNodesManager(tt.args.kubeClient, tt.args.namespace)
})
}
}

View File

@ -0,0 +1,274 @@
/*
Copyright 2021 The KubeEdge 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 manager
import (
"fmt"
"os"
"reflect"
"sync"
"testing"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"github.com/kubeedge/kubeedge/cloud/pkg/edgecontroller/config"
"github.com/kubeedge/kubeedge/cloud/pkg/edgecontroller/utils"
"github.com/kubeedge/kubeedge/pkg/apis/componentconfig/cloudcore/v1alpha1"
)
func TestPodManager_isPodUpdated(t *testing.T) {
type args struct {
old *CachePod
new *v1.Pod
}
tests := []struct {
name string
args args
want bool
}{
{
"TestPodManager_isPodUpdated(): Case 1: check differet pod",
args{
&CachePod{
ObjectMeta: TestOldPodObject.ObjectMeta,
Spec: TestOldPodObject.Spec,
},
TestNewPodObject,
},
true,
},
{
"TestPodManager_isPodUpdated(): Case 2: check same pod",
args{
&CachePod{
ObjectMeta: TestOldPodObject.ObjectMeta,
Spec: TestOldPodObject.Spec,
},
TestOldPodObject,
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pm := &PodManager{
realEvents: make(chan watch.Event, 1),
mergedEvents: make(chan watch.Event, 1),
pods: sync.Map{},
}
if got := pm.isPodUpdated(tt.args.old, tt.args.new); got != tt.want {
t.Errorf("PodManager.isPodUpdated() = %v, want %v", got, tt.want)
}
})
}
}
func TestPodManager_merge(t *testing.T) {
type fields struct {
realEvents chan watch.Event
mergedEvents chan watch.Event
pods *CachePod
}
type args struct {
event watch.Event
}
tests := []struct {
name string
fields fields
args args
}{
{
"TestPodManager_merge(): Case 1: Add pod, deletiontimestamp is nil",
fields{
realEvents: make(chan watch.Event, 1),
mergedEvents: make(chan watch.Event, 1),
pods: nil,
},
args{
event: watch.Event{Type: watch.Added, Object: TestOldPodObject},
},
},
{
"TestPodManager_merge(): Case 2: Add pod, deletiontimestamp is not nil",
fields{
realEvents: make(chan watch.Event, 1),
mergedEvents: make(chan watch.Event, 1),
pods: nil,
},
args{
event: watch.Event{Type: watch.Added, Object: TestDeletingPodObject},
},
},
{
"TestPodManager_merge(): Case 3: Modified pod",
fields{
realEvents: make(chan watch.Event, 1),
mergedEvents: make(chan watch.Event, 1),
pods: &CachePod{ObjectMeta: TestOldPodObject.ObjectMeta, Spec: TestOldPodObject.Spec},
},
args{
event: watch.Event{Type: watch.Modified, Object: TestNewPodObject},
},
},
{
"TestPodManager_merge(): Case 4: Modified pod, new pod is same with old pod",
fields{
realEvents: make(chan watch.Event, 1),
mergedEvents: make(chan watch.Event, 1),
pods: &CachePod{ObjectMeta: TestOldPodObject.ObjectMeta, Spec: TestOldPodObject.Spec},
},
args{
event: watch.Event{Type: watch.Modified, Object: TestOldPodObject},
},
},
{
"TestPodManager_merge(): Case 5: Modified pod, pod not exist",
fields{
realEvents: make(chan watch.Event, 1),
mergedEvents: make(chan watch.Event, 1),
pods: nil,
},
args{
event: watch.Event{Type: watch.Modified, Object: TestNewPodObject},
},
},
{
"TestPodManager_merge(): Case 6: Deleted pod",
fields{
realEvents: make(chan watch.Event, 1),
mergedEvents: make(chan watch.Event, 1),
pods: &CachePod{ObjectMeta: TestOldPodObject.ObjectMeta, Spec: TestOldPodObject.Spec},
},
args{
event: watch.Event{Type: watch.Deleted, Object: TestOldPodObject},
},
},
{
"TestPodManager_merge(): Case 7: Invalid event type",
fields{
realEvents: make(chan watch.Event, 1),
mergedEvents: make(chan watch.Event, 1),
pods: nil,
},
args{
event: watch.Event{Type: "InvalidType", Object: TestOldPodObject},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pm := &PodManager{
realEvents: tt.fields.realEvents,
mergedEvents: tt.fields.mergedEvents,
pods: sync.Map{},
}
if tt.fields.pods != nil {
pm.pods.Store(tt.fields.pods.GetUID(), tt.fields.pods)
}
go pm.merge()
tt.fields.realEvents <- tt.args.event
})
}
}
func TestPodManager_Events(t *testing.T) {
type fields struct {
realEvents chan watch.Event
mergedEvents chan watch.Event
}
mergeEventsCh := make(chan watch.Event, 1)
tests := []struct {
name string
fields fields
want chan watch.Event
}{
{
"TestPodManager_Events(): Case 1",
fields{
make(chan watch.Event, 1),
mergeEventsCh,
},
mergeEventsCh,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pm := &PodManager{
realEvents: tt.fields.realEvents,
mergedEvents: tt.fields.mergedEvents,
pods: sync.Map{},
}
if got := pm.Events(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("PodManager.Events() = %v, want %v", got, tt.want)
}
})
}
}
func TestNewPodManager(t *testing.T) {
type args struct {
kubeClient *kubernetes.Clientset
namespace string
nodeName string
}
config.Config.KubeAPIConfig = v1alpha1.KubeAPIConfig{
KubeConfig: fmt.Sprintf("%s/.kube/config", os.Getenv("HOME")),
QPS: 100,
Burst: 200,
ContentType: "application/vnd.kubernetes.protobuf",
}
config.Config.Buffer = &v1alpha1.EdgeControllerBuffer{
ConfigMapEvent: 1024,
}
cli, err := utils.KubeClient()
if err != nil {
t.Skip("No k8s cluster config file in $HOME/.kube/config, skip it.")
return
}
tests := []struct {
name string
args args
}{
{
"TestNewPodManager(): Case 1: with nodename",
args{
cli,
v1.NamespaceAll,
"nodename",
},
},
{
"TestNewPodManager(): Case 2: without nodename",
args{
cli,
v1.NamespaceAll,
"",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
NewPodManager(tt.args.kubeClient, tt.args.namespace, tt.args.nodeName)
})
}
}

View File

@ -0,0 +1,102 @@
/*
Copyright 2021 The KubeEdge 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 manager
import (
"fmt"
"os"
"reflect"
"testing"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"github.com/kubeedge/kubeedge/cloud/pkg/edgecontroller/config"
"github.com/kubeedge/kubeedge/cloud/pkg/edgecontroller/utils"
"github.com/kubeedge/kubeedge/pkg/apis/componentconfig/cloudcore/v1alpha1"
)
func TestSecretManager_Events(t *testing.T) {
type fields struct {
events chan watch.Event
}
ch := make(chan watch.Event, 1)
tests := []struct {
name string
fields fields
want chan watch.Event
}{
{
"TestSecretManager_Events(): Case 1",
fields{
events: ch,
},
ch,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sm := &SecretManager{
events: tt.fields.events,
}
if got := sm.Events(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("SecretManager.Events() = %v, want %v", got, tt.want)
}
})
}
}
func TestNewSecretManager(t *testing.T) {
type args struct {
kubeClient *kubernetes.Clientset
namespace string
}
config.Config.KubeAPIConfig = v1alpha1.KubeAPIConfig{
KubeConfig: fmt.Sprintf("%s/.kube/config", os.Getenv("HOME")),
QPS: 100,
Burst: 200,
ContentType: "application/vnd.kubernetes.protobuf",
}
config.Config.Buffer = &v1alpha1.EdgeControllerBuffer{
ConfigMapEvent: 1024,
}
cli, err := utils.KubeClient()
if err != nil {
t.Skip("No k8s cluster config file in $HOME/.kube/config, skip it.")
return
}
tests := []struct {
name string
args args
}{
{
"TestNewSecretManager(): Case 1",
args{
cli,
v1.NamespaceAll,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
NewSecretManager(tt.args.kubeClient, tt.args.namespace)
})
}
}

View File

@ -0,0 +1,102 @@
/*
Copyright 2021 The KubeEdge 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 manager
import (
"fmt"
"os"
"reflect"
"testing"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"github.com/kubeedge/kubeedge/cloud/pkg/edgecontroller/config"
"github.com/kubeedge/kubeedge/cloud/pkg/edgecontroller/utils"
"github.com/kubeedge/kubeedge/pkg/apis/componentconfig/cloudcore/v1alpha1"
)
func TestServiceManager_Events(t *testing.T) {
type fields struct {
events chan watch.Event
}
ch := make(chan watch.Event, 1)
tests := []struct {
name string
fields fields
want chan watch.Event
}{
{
"TestServiceManager_Events(): Case 1",
fields{
events: ch,
},
ch,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sm := &ServiceManager{
events: tt.fields.events,
}
if got := sm.Events(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ServiceManager.Events() = %v, want %v", got, tt.want)
}
})
}
}
func TestNewServiceManager(t *testing.T) {
type args struct {
kubeClient *kubernetes.Clientset
namespace string
}
config.Config.KubeAPIConfig = v1alpha1.KubeAPIConfig{
KubeConfig: fmt.Sprintf("%s/.kube/config", os.Getenv("HOME")),
QPS: 100,
Burst: 200,
ContentType: "application/vnd.kubernetes.protobuf",
}
config.Config.Buffer = &v1alpha1.EdgeControllerBuffer{
ConfigMapEvent: 1024,
}
cli, err := utils.KubeClient()
if err != nil {
t.Skip("No k8s cluster config file in $HOME/.kube/config, skip it.")
return
}
tests := []struct {
name string
args args
}{
{
"TestNewServiceManager(): Case 1",
args{
cli,
v1.NamespaceAll,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
NewServiceManager(tt.args.kubeClient, tt.args.namespace)
})
}
}

View File

@ -0,0 +1,128 @@
/*
Copyright 2021 The KubeEdge 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 messagelayer
import (
"reflect"
"testing"
beehiveContext "github.com/kubeedge/beehive/pkg/core/context"
"github.com/kubeedge/beehive/pkg/core/model"
"github.com/kubeedge/kubeedge/cloud/pkg/edgecontroller/config"
"github.com/kubeedge/kubeedge/pkg/apis/componentconfig/cloudcore/v1alpha1"
)
const (
thisModuleName = "testcore"
sendModuleName = "testcore"
receiveModuleName = "testcore"
responseModuleName = "testcore"
)
func init() {
beehiveContext.InitContext(beehiveContext.MsgCtxTypeChannel)
beehiveContext.AddModule(receiveModuleName)
beehiveContext.AddModuleGroup(receiveModuleName, receiveModuleName)
config.Config.Context = &v1alpha1.EdgeControllerContext{
SendModule: sendModuleName,
ReceiveModule: receiveModuleName,
ResponseModule: responseModuleName,
}
}
func TestContextMessageLayer_Send_Receive_Response(t *testing.T) {
type args struct {
message model.Message
edgesite bool
}
tests := []struct {
name string
args args
wantErr bool
}{
{
"TestContextMessageLayer_Send_Receive_Response(): Case 1: is edgesite",
args{
model.Message{
Router: model.MessageRoute{
Source: sendModuleName,
Group: receiveModuleName,
},
Content: "Hello Kubeedge",
},
true,
},
false,
},
{
"TestContextMessageLayer_Send_Receive_Response(): Case 2: is not edgesite",
args{
model.Message{
Router: model.MessageRoute{
Source: sendModuleName,
Group: receiveModuleName,
},
Content: "Hello Kubeedge",
},
false,
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cml := &ContextMessageLayer{
SendModuleName: sendModuleName,
ReceiveModuleName: receiveModuleName,
ResponseModuleName: responseModuleName,
}
if err := cml.Send(tt.args.message); (err != nil) != tt.wantErr {
t.Errorf("ContextMessageLayer.Send() error = %v, wantErr %v", err, tt.wantErr)
}
got, err := cml.Receive()
if err != nil {
t.Errorf("ContextMessageLayer.Receive() failed. err: %v", err)
return
}
if !reflect.DeepEqual(got, tt.args.message) {
t.Errorf("ContextMessageLayer.Receive() failed. got: %v, want:%v", got, tt.args.message)
}
config.Config.EdgeSiteEnable = tt.args.edgesite
cml.Response(got)
})
}
}
func TestNewContextMessageLayer(t *testing.T) {
tests := []struct {
name string
}{
{
"TestNewContextmessageLayer(): Case 1: create message layer",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewContextMessageLayer(); got == nil {
t.Errorf("NewContextMessageLayer() = %v", got)
}
})
}
}

View File

@ -0,0 +1,338 @@
/*
Copyright 2021 The KubeEdge 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 messagelayer
import (
"fmt"
"testing"
"github.com/kubeedge/beehive/pkg/core/model"
"github.com/kubeedge/kubeedge/cloud/pkg/edgecontroller/config"
)
const (
NodeID = "NodeA"
ResourceID = "ResA"
Namespace = "default"
ResourceType = "pod"
)
func TestBuildResource(t *testing.T) {
type args struct {
nodeID string
namespace string
resourceType string
resourceID string
isEdgeSite bool
}
tests := []struct {
name string
args args
wantResource string
wantErr error
}{
{
"TestBuildResource(): Case 1: not edgesite, no node ID, no namespace.",
args{
nodeID: "",
namespace: "",
resourceType: ResourceType,
resourceID: ResourceID,
isEdgeSite: false,
},
"",
fmt.Errorf("required parameter are not set (node id, namespace or resource type)"),
},
{
"TestBuildResource(): Case 2: is edgesite, no node ID, no namespace ",
args{
nodeID: "",
namespace: "",
resourceType: ResourceType,
resourceID: ResourceID,
isEdgeSite: true,
},
"",
fmt.Errorf("required parameter are not set (namespace or resource type)"),
},
{
"TestBuildResource(): Case 3: is edgesite, has nodeID, namespace",
args{
nodeID: NodeID,
namespace: Namespace,
resourceType: ResourceType,
resourceID: ResourceID,
isEdgeSite: true,
},
fmt.Sprintf("%s/%s/%s", Namespace, ResourceType, ResourceID),
nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config.Config.EdgeSiteEnable = tt.args.isEdgeSite
gotResource, err := BuildResource(tt.args.nodeID, tt.args.namespace, tt.args.resourceType, tt.args.resourceID)
if err != nil && err.Error() != tt.wantErr.Error() {
t.Errorf("BuildResource() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotResource != tt.wantResource {
t.Errorf("BuildResource() = %v, want %v", gotResource, tt.wantResource)
}
})
}
}
func TestGetNodeID(t *testing.T) {
type args struct {
msg model.Message
}
tests := []struct {
name string
args args
want string
wantErr error
}{
{
"TestGetNodeID() Case 1: has node ID",
args{
msg: model.Message{
Router: model.MessageRoute{
Resource: fmt.Sprintf("node/%s/%s/%s/%s", NodeID, Namespace, ResourceType, ResourceID),
},
},
},
NodeID,
nil,
},
{
"TestGetNodeID() Case 2: no node ID",
args{msg: model.Message{}},
"",
fmt.Errorf("node id not found"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetNodeID(tt.args.msg)
if err != nil && err.Error() != tt.wantErr.Error() {
t.Errorf("GetNodeID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("GetNodeID() = %v, want %v", got, tt.want)
}
})
}
}
func TestGetNamespace(t *testing.T) {
type args struct {
msg model.Message
isEdgeSite bool
}
tests := []struct {
name string
args args
want string
wantErr error
}{
{
"TestGetNodeID() Case 1: is not edgesite, has namespace",
args{
msg: model.Message{
Router: model.MessageRoute{
Resource: fmt.Sprintf("node/%s/%s/%s/%s", NodeID, Namespace, ResourceType, ResourceID),
},
},
isEdgeSite: false,
},
Namespace,
nil,
},
{
"TestGetNodeID() Case 2: is edgesite, has namespace",
args{
msg: model.Message{
Router: model.MessageRoute{
Resource: fmt.Sprintf("%s/%s/%s", Namespace, ResourceType, ResourceID),
},
},
isEdgeSite: true,
},
Namespace,
nil,
},
{
"TestGetNodeID() Case 3: is edgesite, no namespace",
args{
msg: model.Message{},
isEdgeSite: true,
},
"",
fmt.Errorf("namespace not found"),
},
{
"TestGetNodeID() Case 4: not edgesite, no namespace",
args{
msg: model.Message{},
isEdgeSite: false,
},
"",
fmt.Errorf("namespace not found"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config.Config.EdgeSiteEnable = tt.args.isEdgeSite
got, err := GetNamespace(tt.args.msg)
if err != nil && err.Error() != tt.wantErr.Error() {
t.Errorf("GetNamespace() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("GetNamespace() = %v, want %v", got, tt.want)
}
})
}
}
func TestGetResourceType(t *testing.T) {
type args struct {
msg model.Message
isEdgeSite bool
}
tests := []struct {
name string
args args
want string
wantErr error
}{
{
"TestGetNodeID() Case 1: is not edgesite, has resourceType",
args{
msg: model.Message{
Router: model.MessageRoute{
Resource: fmt.Sprintf("node/%s/%s/%s/%s", NodeID, Namespace, ResourceType, ResourceID),
},
},
isEdgeSite: false,
},
ResourceType,
nil,
},
{
"TestGetNodeID() Case 2: is edgesite, has resourceType",
args{
msg: model.Message{
Router: model.MessageRoute{
Resource: fmt.Sprintf("%s/%s/%s", Namespace, ResourceType, ResourceID),
},
},
isEdgeSite: true,
},
ResourceType,
nil,
},
{
"TestGetNodeID() Case 3: no resourceType",
args{
msg: model.Message{},
isEdgeSite: true,
},
"",
fmt.Errorf("resource type not found"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config.Config.EdgeSiteEnable = tt.args.isEdgeSite
got, err := GetResourceType(tt.args.msg)
if err != nil && err.Error() != tt.wantErr.Error() {
t.Errorf("GetResourceType() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("GetResourceType() = %v, want %v", got, tt.want)
}
})
}
}
func TestGetResourceName(t *testing.T) {
type args struct {
msg model.Message
isEdgeSite bool
}
tests := []struct {
name string
args args
want string
wantErr error
}{
{
"TestGetNodeID() Case 1: is not edgesite, has resourceName",
args{
msg: model.Message{
Router: model.MessageRoute{
Resource: fmt.Sprintf("node/%s/%s/%s/%s", NodeID, Namespace, ResourceType, ResourceID),
},
},
isEdgeSite: false,
},
ResourceID,
nil,
},
{
"TestGetNodeID() Case 2: is edgesite, has resourceName",
args{
msg: model.Message{
Router: model.MessageRoute{
Resource: fmt.Sprintf("%s/%s/%s", Namespace, ResourceType, ResourceID),
},
},
isEdgeSite: true,
},
ResourceID,
nil,
},
{
"TestGetNodeID() Case 3: no resourceName",
args{
msg: model.Message{},
isEdgeSite: true,
},
"",
fmt.Errorf("resource name not found"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config.Config.EdgeSiteEnable = tt.args.isEdgeSite
got, err := GetResourceName(tt.args.msg)
if err != nil && err.Error() != tt.wantErr.Error() {
t.Errorf("GetResourceName() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("GetResourceName() = %v, want %v", got, tt.want)
}
})
}
}