github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/cloud/pkg/edgecontroller/manager/location_test.go (about)

     1  /*
     2  Copyright 2019 The KubeEdge Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8    http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package manager
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  )
    26  
    27  var (
    28  	configMapKey    = "ObjectMeta1/VolumeConfig1"
    29  	configMapVolume = "VolumeConfig1"
    30  	nodes           = []string{"Node1", "Node2"}
    31  	objectMeta      = "ObjectMeta1"
    32  	secretKey       = "ObjectMeta1/VolumeSecret1"
    33  	secretVolume    = "VolumeSecret1"
    34  )
    35  
    36  // TestAddOrUpdatePod is function to test AddOrUpdatePod
    37  func TestAddOrUpdatePod(t *testing.T) {
    38  	pod := v1.Pod{
    39  		Spec: v1.PodSpec{
    40  			NodeName: "Node1",
    41  			Volumes: []v1.Volume{{
    42  				VolumeSource: v1.VolumeSource{
    43  					ConfigMap: &v1.ConfigMapVolumeSource{LocalObjectReference: v1.LocalObjectReference{Name: configMapVolume}},
    44  					Secret:    &v1.SecretVolumeSource{SecretName: secretVolume},
    45  				},
    46  			}},
    47  			Containers: []v1.Container{{
    48  				EnvFrom: []v1.EnvFromSource{{
    49  					ConfigMapRef: &v1.ConfigMapEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "ContainerConfig1"}},
    50  					SecretRef:    &v1.SecretEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "ContainerSecret1"}},
    51  				}},
    52  			}},
    53  			ImagePullSecrets: []v1.LocalObjectReference{{Name: "ImageSecret1"}},
    54  		},
    55  		ObjectMeta: metav1.ObjectMeta{
    56  			Namespace: objectMeta,
    57  			Name:      "Object1",
    58  		},
    59  	}
    60  	locationCache := LocationCache{}
    61  	locationCache.configMapNode.Store(configMapKey, "Node1")
    62  	locationCache.secretNode.Store(secretKey, nodes)
    63  	tests := []struct {
    64  		name string
    65  		lc   *LocationCache
    66  		pod  v1.Pod
    67  	}{
    68  		{
    69  			name: "TestAddOrUpdatePod(): Case 1: LocationCache is empty",
    70  			lc:   &LocationCache{},
    71  			pod:  pod,
    72  		},
    73  		{
    74  			name: "TestAddOrUpdatePod(): Case 2: LocationCache is not empty",
    75  			lc:   &locationCache,
    76  			pod:  pod,
    77  		},
    78  	}
    79  	for _, test := range tests {
    80  		t.Run(test.name, func(t *testing.T) {
    81  			test.lc.AddOrUpdatePod(test.pod)
    82  		})
    83  	}
    84  }
    85  
    86  // TestConfigMapNodes is function to test ConfigMapNodes
    87  func TestConfigMapNodes(t *testing.T) {
    88  	locationCache := LocationCache{}
    89  	locationCache.configMapNode.Store(configMapKey, nodes)
    90  	tests := []struct {
    91  		name          string
    92  		lc            *LocationCache
    93  		namespace     string
    94  		configMapName string
    95  		nodes         []string
    96  	}{
    97  		{
    98  			name:  "TestConfigMapNodes(): Case 1: LocationCache is empty",
    99  			lc:    &LocationCache{},
   100  			nodes: nil,
   101  		},
   102  		{
   103  			name:          "TestConfigMapNodes(): Case 2: LocationCache is not empty",
   104  			lc:            &locationCache,
   105  			namespace:     objectMeta,
   106  			configMapName: configMapVolume,
   107  			nodes:         nodes,
   108  		},
   109  	}
   110  	for _, test := range tests {
   111  		t.Run(test.name, func(t *testing.T) {
   112  			if nodes := test.lc.ConfigMapNodes(test.namespace, test.configMapName); !reflect.DeepEqual(nodes, test.nodes) {
   113  				t.Errorf("Manager.TestConfigMapNodes() case failed: got = %v, Want = %v", nodes, test.nodes)
   114  			}
   115  		})
   116  	}
   117  }
   118  
   119  // TestSecretNodes is function to test SecretNodes
   120  func TestSecretNodes(t *testing.T) {
   121  	locationCache := LocationCache{}
   122  	locationCache.secretNode.Store(secretKey, nodes)
   123  	tests := []struct {
   124  		name       string
   125  		lc         *LocationCache
   126  		namespace  string
   127  		secretName string
   128  		nodes      []string
   129  	}{
   130  		{
   131  			name:  "TestSecretNodes(): Case 1: LocationCache is empty",
   132  			lc:    &LocationCache{},
   133  			nodes: nil,
   134  		},
   135  		{
   136  			name:       "TestSecretNodes(): Case 2: LocationCache is not empty",
   137  			lc:         &locationCache,
   138  			namespace:  objectMeta,
   139  			secretName: secretVolume,
   140  			nodes:      nodes,
   141  		},
   142  	}
   143  	for _, test := range tests {
   144  		t.Run(test.name, func(t *testing.T) {
   145  			if nodes := test.lc.SecretNodes(test.namespace, test.secretName); !reflect.DeepEqual(nodes, test.nodes) {
   146  				t.Errorf("Manager.TestSecretNodes() case failed: got = %v, Want = %v", nodes, test.nodes)
   147  			}
   148  		})
   149  	}
   150  }
   151  
   152  // TestDeleteConfigMap is function to test DeleteConfigMap
   153  func TestDeleteConfigMap(t *testing.T) {
   154  	locationCache := LocationCache{}
   155  	locationCache.configMapNode.Store(configMapKey, nodes)
   156  	tests := []struct {
   157  		name          string
   158  		lc            *LocationCache
   159  		namespace     string
   160  		configMapName string
   161  		errorWant     bool
   162  	}{
   163  		{
   164  			name:          "TestDeleteConfigMap(): delete configMap from cache",
   165  			lc:            &locationCache,
   166  			namespace:     objectMeta,
   167  			configMapName: configMapVolume,
   168  			errorWant:     false,
   169  		},
   170  	}
   171  	for _, test := range tests {
   172  		t.Run(test.name, func(t *testing.T) {
   173  			test.lc.DeleteConfigMap(test.namespace, test.configMapName)
   174  			if _, got := test.lc.configMapNode.Load(configMapKey); !reflect.DeepEqual(got, test.errorWant) {
   175  				t.Errorf("Manager.TestDeleteConfigMap() case failed: got = %v, Want = %v", got, test.errorWant)
   176  			}
   177  		})
   178  	}
   179  }
   180  
   181  // TestDeleteSecret is function to test DeleteSecret
   182  func TestDeleteSecret(t *testing.T) {
   183  	locationCache := LocationCache{}
   184  	locationCache.secretNode.Store(secretKey, nodes)
   185  	tests := []struct {
   186  		name       string
   187  		lc         *LocationCache
   188  		namespace  string
   189  		secretName string
   190  		errorWant  bool
   191  	}{
   192  		{
   193  			name:       "TestDeleteSecret(): delete secret from cache",
   194  			lc:         &locationCache,
   195  			namespace:  objectMeta,
   196  			secretName: secretVolume,
   197  			errorWant:  false,
   198  		},
   199  	}
   200  	for _, test := range tests {
   201  		t.Run(test.name, func(t *testing.T) {
   202  			test.lc.DeleteSecret(test.namespace, test.secretName)
   203  			if _, got := test.lc.secretNode.Load(secretKey); !reflect.DeepEqual(got, test.errorWant) {
   204  				t.Errorf("Manager.TestDeleteSecret() case failed: got = %v, Want = %v", got, test.errorWant)
   205  			}
   206  		})
   207  	}
   208  }