2015-03-21 00:22:02 +00:00
/ *
2016-06-03 00:25:58 +00:00
Copyright 2015 The Kubernetes Authors .
2015-03-21 00:22:02 +00:00
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 .
* /
2015-10-12 23:28:23 +00:00
package pod
2015-03-21 00:22:02 +00:00
2015-03-23 19:17:12 +00:00
import (
"reflect"
"testing"
2017-01-11 14:09:48 +00:00
"k8s.io/apimachinery/pkg/types"
2016-11-18 20:50:58 +00:00
"k8s.io/kubernetes/pkg/api/v1"
2016-02-25 23:40:44 +00:00
podtest "k8s.io/kubernetes/pkg/kubelet/pod/testing"
2015-10-09 17:24:31 +00:00
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
2015-03-23 19:17:12 +00:00
)
// Stub out mirror client for testing purpose.
2016-02-25 23:40:44 +00:00
func newTestManager ( ) ( * basicManager , * podtest . FakeMirrorClient ) {
fakeMirrorClient := podtest . NewFakeMirrorClient ( )
2015-10-12 23:28:23 +00:00
manager := NewBasicPodManager ( fakeMirrorClient ) . ( * basicManager )
return manager , fakeMirrorClient
2015-03-23 19:17:12 +00:00
}
// Tests that pods/maps are properly set after the pod update, and the basic
// methods work correctly.
func TestGetSetPods ( t * testing . T ) {
2016-11-18 20:50:58 +00:00
mirrorPod := & v1 . Pod {
ObjectMeta : v1 . ObjectMeta {
2015-03-23 19:17:12 +00:00
UID : "987654321" ,
Name : "bar" ,
Namespace : "default" ,
Annotations : map [ string ] string {
2015-10-09 17:24:31 +00:00
kubetypes . ConfigSourceAnnotationKey : "api" ,
kubetypes . ConfigMirrorAnnotationKey : "mirror" ,
2015-03-23 19:17:12 +00:00
} ,
} ,
}
2016-11-18 20:50:58 +00:00
staticPod := & v1 . Pod {
ObjectMeta : v1 . ObjectMeta {
2015-03-23 19:17:12 +00:00
UID : "123456789" ,
Name : "bar" ,
Namespace : "default" ,
2015-10-09 17:24:31 +00:00
Annotations : map [ string ] string { kubetypes . ConfigSourceAnnotationKey : "file" } ,
2015-03-23 19:17:12 +00:00
} ,
}
2016-11-18 20:50:58 +00:00
expectedPods := [ ] * v1 . Pod {
2015-03-23 19:17:12 +00:00
{
2016-11-18 20:50:58 +00:00
ObjectMeta : v1 . ObjectMeta {
2015-03-23 19:17:12 +00:00
UID : "999999999" ,
Name : "taco" ,
Namespace : "default" ,
2015-10-09 17:24:31 +00:00
Annotations : map [ string ] string { kubetypes . ConfigSourceAnnotationKey : "api" } ,
2015-03-23 19:17:12 +00:00
} ,
} ,
staticPod ,
}
updates := append ( expectedPods , mirrorPod )
2015-10-12 23:28:23 +00:00
podManager , _ := newTestManager ( )
2015-03-23 19:17:12 +00:00
podManager . SetPods ( updates )
2015-03-25 01:05:46 +00:00
2016-08-02 22:13:54 +00:00
// Tests that all regular pods are recorded correctly.
2015-03-23 19:17:12 +00:00
actualPods := podManager . GetPods ( )
2015-03-25 01:05:46 +00:00
if len ( actualPods ) != len ( expectedPods ) {
t . Errorf ( "expected %d pods, got %d pods; expected pods %#v, got pods %#v" , len ( expectedPods ) , len ( actualPods ) ,
expectedPods , actualPods )
2015-03-23 19:17:12 +00:00
}
2015-03-25 01:05:46 +00:00
for _ , expected := range expectedPods {
found := false
for _ , actual := range actualPods {
if actual . UID == expected . UID {
if ! reflect . DeepEqual ( & expected , & actual ) {
t . Errorf ( "pod was recorded incorrectly. expect: %#v, got: %#v" , expected , actual )
}
found = true
break
}
}
if ! found {
t . Errorf ( "pod %q was not found in %#v" , expected . UID , actualPods )
}
2015-03-23 19:17:12 +00:00
}
2015-03-25 01:05:46 +00:00
// Tests UID translation works as expected.
2015-03-23 19:17:12 +00:00
if uid := podManager . TranslatePodUID ( mirrorPod . UID ) ; uid != staticPod . UID {
2015-03-25 01:05:46 +00:00
t . Errorf ( "unable to translate UID %q to the static POD's UID %q; %#v" ,
mirrorPod . UID , staticPod . UID , podManager . mirrorPodByUID )
2015-03-23 19:17:12 +00:00
}
2015-03-25 01:05:46 +00:00
// Test the basic Get methods.
actualPod , ok := podManager . GetPodByFullName ( "bar_default" )
2015-04-03 22:51:50 +00:00
if ! ok || ! reflect . DeepEqual ( actualPod , staticPod ) {
2015-03-23 19:17:12 +00:00
t . Errorf ( "unable to get pod by full name; expected: %#v, got: %#v" , staticPod , actualPod )
}
actualPod , ok = podManager . GetPodByName ( "default" , "bar" )
2015-04-03 22:51:50 +00:00
if ! ok || ! reflect . DeepEqual ( actualPod , staticPod ) {
2015-03-23 19:17:12 +00:00
t . Errorf ( "unable to get pod by name; expected: %#v, got: %#v" , staticPod , actualPod )
}
2015-03-21 00:22:02 +00:00
}
2017-01-03 03:08:39 +00:00
func TestDeletePods ( t * testing . T ) {
mirrorPod := & v1 . Pod {
ObjectMeta : v1 . ObjectMeta {
UID : types . UID ( "mirror-pod-uid" ) ,
Name : "mirror-static-pod-name" ,
Namespace : v1 . NamespaceDefault ,
Annotations : map [ string ] string {
kubetypes . ConfigSourceAnnotationKey : "api" ,
kubetypes . ConfigMirrorAnnotationKey : "mirror" ,
} ,
} ,
}
staticPod := & v1 . Pod {
ObjectMeta : v1 . ObjectMeta {
UID : types . UID ( "static-pod-uid" ) ,
Name : "mirror-static-pod-name" ,
Namespace : v1 . NamespaceDefault ,
Annotations : map [ string ] string { kubetypes . ConfigSourceAnnotationKey : "file" } ,
} ,
}
expectedPods := [ ] * v1 . Pod {
{
ObjectMeta : v1 . ObjectMeta {
UID : types . UID ( "extra-pod-uid" ) ,
Name : "extra-pod-name" ,
Namespace : v1 . NamespaceDefault ,
Annotations : map [ string ] string { kubetypes . ConfigSourceAnnotationKey : "api" } ,
} ,
} ,
staticPod ,
}
updates := append ( expectedPods , mirrorPod )
podManager , _ := newTestManager ( )
podManager . SetPods ( updates )
podManager . DeletePod ( staticPod )
actualPods := podManager . GetPods ( )
if len ( actualPods ) == len ( expectedPods ) {
t . Fatalf ( "Run DeletePod() error, expected %d pods, got %d pods; " , len ( expectedPods ) - 1 , len ( actualPods ) )
}
orphanedMirrorPodNames := podManager . getOrphanedMirrorPodNames ( )
expectedOrphanedMirrorPodNameNum := 1
if len ( orphanedMirrorPodNames ) != expectedOrphanedMirrorPodNameNum {
t . Fatalf ( "Run getOrphanedMirrorPodNames() error, expected %d orphaned mirror pods, got %d orphaned mirror pods; " , expectedOrphanedMirrorPodNameNum , len ( orphanedMirrorPodNames ) )
}
expectedOrphanedMirrorPodName := mirrorPod . Name + "_" + mirrorPod . Namespace
if orphanedMirrorPodNames [ 0 ] != expectedOrphanedMirrorPodName {
t . Fatalf ( "Run getOrphanedMirrorPodNames() error, expected orphaned mirror pod name : %s, got orphaned mirror pod name %s; " , expectedOrphanedMirrorPodName , orphanedMirrorPodNames [ 0 ] )
}
}