2014-10-27 19:56:34 +00:00
/ *
2016-06-03 00:25:58 +00:00
Copyright 2014 The Kubernetes Authors .
2014-10-27 19:56:34 +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 .
* /
package kubectl
import (
2015-04-02 15:42:19 +00:00
"bytes"
2016-08-10 17:39:11 +00:00
"encoding/json"
2015-02-26 18:50:12 +00:00
"fmt"
"reflect"
2014-10-27 19:56:34 +00:00
"strings"
"testing"
2014-12-16 22:20:51 +00:00
"time"
2014-10-27 19:56:34 +00:00
2016-05-05 17:30:50 +00:00
"k8s.io/kubernetes/federation/apis/federation"
2016-11-30 07:27:27 +00:00
fedfake "k8s.io/kubernetes/federation/client/clientset_generated/federation_internalclientset/fake"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/resource"
2016-11-18 20:58:22 +00:00
"k8s.io/kubernetes/pkg/api/v1"
2015-10-09 22:04:41 +00:00
"k8s.io/kubernetes/pkg/apis/extensions"
2016-11-18 20:58:22 +00:00
"k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
2016-12-03 19:06:03 +00:00
metav1 "k8s.io/kubernetes/pkg/apis/meta/v1"
2016-11-09 12:39:16 +00:00
"k8s.io/kubernetes/pkg/apis/policy"
2016-09-08 11:03:12 +00:00
"k8s.io/kubernetes/pkg/apis/storage"
2016-12-14 03:39:50 +00:00
versionedfake "k8s.io/kubernetes/pkg/client/clientset_generated/clientset/fake"
2016-09-08 15:50:53 +00:00
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
2016-02-16 22:16:45 +00:00
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
2016-11-09 12:39:16 +00:00
"k8s.io/kubernetes/pkg/util/intstr"
2014-10-27 19:56:34 +00:00
)
type describeClient struct {
T * testing . T
Namespace string
Err error
2016-09-08 15:50:53 +00:00
internalclientset . Interface
2014-10-27 19:56:34 +00:00
}
func TestDescribePod ( t * testing . T ) {
2016-09-08 15:50:53 +00:00
fake := fake . NewSimpleClientset ( & api . Pod {
2015-04-06 23:27:53 +00:00
ObjectMeta : api . ObjectMeta {
Name : "bar" ,
Namespace : "foo" ,
} ,
} )
c := & describeClient { T : t , Namespace : "foo" , Interface : fake }
2014-11-14 01:42:50 +00:00
d := PodDescriber { c }
2016-04-20 17:27:32 +00:00
out , err := d . Describe ( "foo" , "bar" , DescriberSettings { ShowEvents : true } )
2014-10-27 19:56:34 +00:00
if err != nil {
t . Errorf ( "unexpected error: %v" , err )
}
if ! strings . Contains ( out , "bar" ) || ! strings . Contains ( out , "Status:" ) {
t . Errorf ( "unexpected out: %s" , out )
}
}
2016-08-10 17:39:11 +00:00
func TestDescribePodTolerations ( t * testing . T ) {
podTolerations := [ ] api . Toleration { { Key : "key1" , Value : "value1" } ,
{ Key : "key2" , Value : "value2" } }
pt , _ := json . Marshal ( podTolerations )
2016-09-08 15:50:53 +00:00
fake := fake . NewSimpleClientset ( & api . Pod {
2016-08-10 17:39:11 +00:00
ObjectMeta : api . ObjectMeta {
Name : "bar" ,
Namespace : "foo" ,
Annotations : map [ string ] string {
api . TolerationsAnnotationKey : string ( pt ) ,
} ,
} ,
} )
c := & describeClient { T : t , Namespace : "foo" , Interface : fake }
d := PodDescriber { c }
out , err := d . Describe ( "foo" , "bar" , DescriberSettings { } )
if err != nil {
t . Errorf ( "unexpected error: %v" , err )
}
if ! strings . Contains ( out , "key1=value1" ) || ! strings . Contains ( out , "key2=value2" ) || ! strings . Contains ( out , "Tolerations:" ) {
2016-09-15 22:08:57 +00:00
t . Errorf ( "unexpected out: %s" , out )
}
}
func TestDescribeNamespace ( t * testing . T ) {
fake := fake . NewSimpleClientset ( & api . Namespace {
ObjectMeta : api . ObjectMeta {
Name : "myns" ,
} ,
} )
c := & describeClient { T : t , Namespace : "" , Interface : fake }
d := NamespaceDescriber { c }
out , err := d . Describe ( "" , "myns" , DescriberSettings { ShowEvents : true } )
if err != nil {
t . Errorf ( "unexpected error: %v" , err )
}
if ! strings . Contains ( out , "myns" ) {
2016-08-10 17:39:11 +00:00
t . Errorf ( "unexpected out: %s" , out )
}
}
2014-10-27 19:56:34 +00:00
func TestDescribeService ( t * testing . T ) {
2016-09-08 15:50:53 +00:00
fake := fake . NewSimpleClientset ( & api . Service {
2015-04-06 23:27:53 +00:00
ObjectMeta : api . ObjectMeta {
Name : "bar" ,
Namespace : "foo" ,
} ,
} )
c := & describeClient { T : t , Namespace : "foo" , Interface : fake }
2014-11-14 01:42:50 +00:00
d := ServiceDescriber { c }
2016-04-20 17:27:32 +00:00
out , err := d . Describe ( "foo" , "bar" , DescriberSettings { ShowEvents : true } )
2014-10-27 19:56:34 +00:00
if err != nil {
t . Errorf ( "unexpected error: %v" , err )
}
if ! strings . Contains ( out , "Labels:" ) || ! strings . Contains ( out , "bar" ) {
t . Errorf ( "unexpected out: %s" , out )
}
}
2014-12-16 22:20:51 +00:00
func TestPodDescribeResultsSorted ( t * testing . T ) {
// Arrange
2016-09-08 15:50:53 +00:00
fake := fake . NewSimpleClientset (
& api . EventList {
Items : [ ] api . Event {
{
ObjectMeta : api . ObjectMeta { Name : "one" } ,
Source : api . EventSource { Component : "kubelet" } ,
Message : "Item 1" ,
2016-12-03 18:57:26 +00:00
FirstTimestamp : metav1 . NewTime ( time . Date ( 2014 , time . January , 15 , 0 , 0 , 0 , 0 , time . UTC ) ) ,
LastTimestamp : metav1 . NewTime ( time . Date ( 2014 , time . January , 15 , 0 , 0 , 0 , 0 , time . UTC ) ) ,
2016-09-08 15:50:53 +00:00
Count : 1 ,
Type : api . EventTypeNormal ,
} ,
{
ObjectMeta : api . ObjectMeta { Name : "two" } ,
Source : api . EventSource { Component : "scheduler" } ,
Message : "Item 2" ,
2016-12-03 18:57:26 +00:00
FirstTimestamp : metav1 . NewTime ( time . Date ( 1987 , time . June , 17 , 0 , 0 , 0 , 0 , time . UTC ) ) ,
LastTimestamp : metav1 . NewTime ( time . Date ( 1987 , time . June , 17 , 0 , 0 , 0 , 0 , time . UTC ) ) ,
2016-09-08 15:50:53 +00:00
Count : 1 ,
Type : api . EventTypeNormal ,
} ,
{
ObjectMeta : api . ObjectMeta { Name : "three" } ,
Source : api . EventSource { Component : "kubelet" } ,
Message : "Item 3" ,
2016-12-03 18:57:26 +00:00
FirstTimestamp : metav1 . NewTime ( time . Date ( 2002 , time . December , 25 , 0 , 0 , 0 , 0 , time . UTC ) ) ,
LastTimestamp : metav1 . NewTime ( time . Date ( 2002 , time . December , 25 , 0 , 0 , 0 , 0 , time . UTC ) ) ,
2016-09-08 15:50:53 +00:00
Count : 1 ,
Type : api . EventTypeNormal ,
} ,
2014-12-16 22:20:51 +00:00
} ,
} ,
2016-09-08 15:50:53 +00:00
& api . Pod { ObjectMeta : api . ObjectMeta { Namespace : "foo" , Name : "bar" } } ,
)
2015-04-06 23:27:53 +00:00
c := & describeClient { T : t , Namespace : "foo" , Interface : fake }
2014-12-16 22:20:51 +00:00
d := PodDescriber { c }
// Act
2016-04-20 17:27:32 +00:00
out , err := d . Describe ( "foo" , "bar" , DescriberSettings { ShowEvents : true } )
2014-12-16 22:20:51 +00:00
// Assert
if err != nil {
t . Errorf ( "unexpected error: %v" , err )
}
VerifyDatesInOrder ( out , "\n" /* rowDelimiter */ , "\t" /* columnDelimiter */ , t )
}
2015-02-26 18:50:12 +00:00
2016-09-15 15:56:40 +00:00
// VerifyDatesInOrder checks the start of each line for a RFC1123Z date
// and posts error if all subsequent dates are not equal or increasing
func VerifyDatesInOrder (
resultToTest , rowDelimiter , columnDelimiter string , t * testing . T ) {
lines := strings . Split ( resultToTest , rowDelimiter )
var previousTime time . Time
for _ , str := range lines {
columns := strings . Split ( str , columnDelimiter )
if len ( columns ) > 0 {
currentTime , err := time . Parse ( time . RFC1123Z , columns [ 0 ] )
if err == nil {
if previousTime . After ( currentTime ) {
t . Errorf (
"Output is not sorted by time. %s should be listed after %s. Complete output: %s" ,
previousTime . Format ( time . RFC1123Z ) ,
currentTime . Format ( time . RFC1123Z ) ,
resultToTest )
}
previousTime = currentTime
}
}
}
}
2015-04-02 15:42:19 +00:00
func TestDescribeContainers ( t * testing . T ) {
testCases := [ ] struct {
2015-06-05 04:49:01 +00:00
container api . Container
status api . ContainerStatus
2015-04-02 15:42:19 +00:00
expectedElements [ ] string
} {
// Running state.
{
2015-06-05 04:49:01 +00:00
container : api . Container { Name : "test" , Image : "image" } ,
status : api . ContainerStatus {
2015-04-02 15:42:19 +00:00
Name : "test" ,
State : api . ContainerState {
Running : & api . ContainerStateRunning {
2016-12-03 18:57:26 +00:00
StartedAt : metav1 . NewTime ( time . Now ( ) ) ,
2015-04-02 15:42:19 +00:00
} ,
} ,
Ready : true ,
RestartCount : 7 ,
} ,
expectedElements : [ ] string { "test" , "State" , "Running" , "Ready" , "True" , "Restart Count" , "7" , "Image" , "image" , "Started" } ,
} ,
// Waiting state.
{
2015-06-05 04:49:01 +00:00
container : api . Container { Name : "test" , Image : "image" } ,
status : api . ContainerStatus {
2015-04-02 15:42:19 +00:00
Name : "test" ,
State : api . ContainerState {
Waiting : & api . ContainerStateWaiting {
Reason : "potato" ,
} ,
} ,
Ready : true ,
RestartCount : 7 ,
} ,
expectedElements : [ ] string { "test" , "State" , "Waiting" , "Ready" , "True" , "Restart Count" , "7" , "Image" , "image" , "Reason" , "potato" } ,
} ,
// Terminated state.
{
2015-06-05 04:49:01 +00:00
container : api . Container { Name : "test" , Image : "image" } ,
status : api . ContainerStatus {
2015-04-02 15:42:19 +00:00
Name : "test" ,
State : api . ContainerState {
2015-05-27 22:02:11 +00:00
Terminated : & api . ContainerStateTerminated {
2016-12-03 18:57:26 +00:00
StartedAt : metav1 . NewTime ( time . Now ( ) ) ,
FinishedAt : metav1 . NewTime ( time . Now ( ) ) ,
2015-04-02 15:42:19 +00:00
Reason : "potato" ,
ExitCode : 2 ,
} ,
} ,
Ready : true ,
RestartCount : 7 ,
} ,
expectedElements : [ ] string { "test" , "State" , "Terminated" , "Ready" , "True" , "Restart Count" , "7" , "Image" , "image" , "Reason" , "potato" , "Started" , "Finished" , "Exit Code" , "2" } ,
} ,
2015-08-07 01:45:20 +00:00
// Last Terminated
{
container : api . Container { Name : "test" , Image : "image" } ,
status : api . ContainerStatus {
Name : "test" ,
State : api . ContainerState {
Running : & api . ContainerStateRunning {
2016-12-03 18:57:26 +00:00
StartedAt : metav1 . NewTime ( time . Now ( ) ) ,
2015-08-07 01:45:20 +00:00
} ,
} ,
LastTerminationState : api . ContainerState {
Terminated : & api . ContainerStateTerminated {
2016-12-03 18:57:26 +00:00
StartedAt : metav1 . NewTime ( time . Now ( ) . Add ( time . Second * 3 ) ) ,
FinishedAt : metav1 . NewTime ( time . Now ( ) ) ,
2015-08-07 01:45:20 +00:00
Reason : "crashing" ,
ExitCode : 3 ,
} ,
} ,
Ready : true ,
RestartCount : 7 ,
} ,
expectedElements : [ ] string { "test" , "State" , "Terminated" , "Ready" , "True" , "Restart Count" , "7" , "Image" , "image" , "Started" , "Finished" , "Exit Code" , "2" , "crashing" , "3" } ,
} ,
2015-04-02 15:42:19 +00:00
// No state defaults to waiting.
{
2015-06-05 04:49:01 +00:00
container : api . Container { Name : "test" , Image : "image" } ,
status : api . ContainerStatus {
2015-04-02 15:42:19 +00:00
Name : "test" ,
Ready : true ,
RestartCount : 7 ,
} ,
expectedElements : [ ] string { "test" , "State" , "Waiting" , "Ready" , "True" , "Restart Count" , "7" , "Image" , "image" } ,
} ,
2015-10-11 19:14:06 +00:00
// Env
2015-07-04 14:31:15 +00:00
{
2016-11-30 02:57:35 +00:00
container : api . Container { Name : "test" , Image : "image" , Env : [ ] api . EnvVar { { Name : "envname" , Value : "xyz" } } , EnvFrom : [ ] api . EnvFromSource { { ConfigMapRef : & api . ConfigMapEnvSource { LocalObjectReference : api . LocalObjectReference { Name : "a123" } } } } } ,
2015-07-04 14:31:15 +00:00
status : api . ContainerStatus {
Name : "test" ,
Ready : true ,
RestartCount : 7 ,
} ,
2016-11-30 02:57:35 +00:00
expectedElements : [ ] string { "test" , "State" , "Waiting" , "Ready" , "True" , "Restart Count" , "7" , "Image" , "image" , "envname" , "xyz" , "a123\tConfigMap" } ,
} ,
{
container : api . Container { Name : "test" , Image : "image" , Env : [ ] api . EnvVar { { Name : "envname" , Value : "xyz" } } , EnvFrom : [ ] api . EnvFromSource { { Prefix : "p_" , ConfigMapRef : & api . ConfigMapEnvSource { LocalObjectReference : api . LocalObjectReference { Name : "a123" } } } } } ,
status : api . ContainerStatus {
Name : "test" ,
Ready : true ,
RestartCount : 7 ,
} ,
expectedElements : [ ] string { "test" , "State" , "Waiting" , "Ready" , "True" , "Restart Count" , "7" , "Image" , "image" , "envname" , "xyz" , "a123\tConfigMap with prefix 'p_'" } ,
2015-07-04 14:31:15 +00:00
} ,
2015-10-11 19:14:06 +00:00
// Command
{
container : api . Container { Name : "test" , Image : "image" , Command : [ ] string { "sleep" , "1000" } } ,
status : api . ContainerStatus {
Name : "test" ,
Ready : true ,
RestartCount : 7 ,
} ,
expectedElements : [ ] string { "test" , "State" , "Waiting" , "Ready" , "True" , "Restart Count" , "7" , "Image" , "image" , "sleep" , "1000" } ,
} ,
// Args
{
container : api . Container { Name : "test" , Image : "image" , Args : [ ] string { "time" , "1000" } } ,
status : api . ContainerStatus {
Name : "test" ,
Ready : true ,
RestartCount : 7 ,
} ,
expectedElements : [ ] string { "test" , "State" , "Waiting" , "Ready" , "True" , "Restart Count" , "7" , "Image" , "image" , "time" , "1000" } ,
} ,
2015-06-05 04:49:01 +00:00
// Using limits.
{
container : api . Container {
Name : "test" ,
Image : "image" ,
Resources : api . ResourceRequirements {
Limits : api . ResourceList {
api . ResourceName ( api . ResourceCPU ) : resource . MustParse ( "1000" ) ,
api . ResourceName ( api . ResourceMemory ) : resource . MustParse ( "4G" ) ,
api . ResourceName ( api . ResourceStorage ) : resource . MustParse ( "20G" ) ,
} ,
} ,
} ,
status : api . ContainerStatus {
Name : "test" ,
Ready : true ,
RestartCount : 7 ,
} ,
expectedElements : [ ] string { "cpu" , "1k" , "memory" , "4G" , "storage" , "20G" } ,
} ,
2016-05-21 21:47:47 +00:00
// Using requests.
{
container : api . Container {
Name : "test" ,
Image : "image" ,
Resources : api . ResourceRequirements {
Requests : api . ResourceList {
api . ResourceName ( api . ResourceCPU ) : resource . MustParse ( "1000" ) ,
api . ResourceName ( api . ResourceMemory ) : resource . MustParse ( "4G" ) ,
api . ResourceName ( api . ResourceStorage ) : resource . MustParse ( "20G" ) ,
} ,
} ,
} ,
expectedElements : [ ] string { "cpu" , "1k" , "memory" , "4G" , "storage" , "20G" } ,
} ,
2015-04-02 15:42:19 +00:00
}
for i , testCase := range testCases {
out := new ( bytes . Buffer )
2015-06-05 04:49:01 +00:00
pod := api . Pod {
Spec : api . PodSpec {
Containers : [ ] api . Container { testCase . container } ,
} ,
Status : api . PodStatus {
ContainerStatuses : [ ] api . ContainerStatus { testCase . status } ,
} ,
}
2016-11-02 01:54:17 +00:00
writer := & PrefixWriter { out }
describeContainers ( "Containers" , pod . Spec . Containers , pod . Status . ContainerStatuses , EnvValueRetriever ( & pod ) , writer , "" )
2015-04-02 15:42:19 +00:00
output := out . String ( )
for _ , expected := range testCase . expectedElements {
if ! strings . Contains ( output , expected ) {
t . Errorf ( "Test case %d: expected to find %q in output: %q" , i , expected , output )
}
}
}
}
2015-02-26 18:50:12 +00:00
func TestDescribers ( t * testing . T ) {
first := & api . Event { }
second := & api . Pod { }
var third * api . Pod
testErr := fmt . Errorf ( "test" )
d := Describers { }
d . Add (
func ( e * api . Event , p * api . Pod ) ( string , error ) {
if e != first {
t . Errorf ( "first argument not equal: %#v" , e )
}
if p != second {
t . Errorf ( "second argument not equal: %#v" , p )
}
return "test" , testErr
} ,
)
if out , err := d . DescribeObject ( first , second ) ; out != "test" || err != testErr {
t . Errorf ( "unexpected result: %s %v" , out , err )
}
if out , err := d . DescribeObject ( first , second , third ) ; out != "" || err == nil {
t . Errorf ( "unexpected result: %s %v" , out , err )
} else {
if noDescriber , ok := err . ( ErrNoDescriber ) ; ok {
if ! reflect . DeepEqual ( noDescriber . Types , [ ] string { "*api.Event" , "*api.Pod" , "*api.Pod" } ) {
t . Errorf ( "unexpected describer: %v" , err )
}
} else {
t . Errorf ( "unexpected error type: %v" , err )
}
}
d . Add (
func ( e * api . Event ) ( string , error ) {
if e != first {
t . Errorf ( "first argument not equal: %#v" , e )
}
return "simpler" , testErr
} ,
)
if out , err := d . DescribeObject ( first ) ; out != "simpler" || err != testErr {
t . Errorf ( "unexpected result: %s %v" , out , err )
}
}
func TestDefaultDescribers ( t * testing . T ) {
out , err := DefaultObjectDescriber . DescribeObject ( & api . Pod { ObjectMeta : api . ObjectMeta { Name : "foo" } } )
if err != nil {
t . Fatalf ( "unexpected error: %v" , err )
}
if ! strings . Contains ( out , "foo" ) {
t . Errorf ( "unexpected output: %s" , out )
}
out , err = DefaultObjectDescriber . DescribeObject ( & api . Service { ObjectMeta : api . ObjectMeta { Name : "foo" } } )
if err != nil {
t . Fatalf ( "unexpected error: %v" , err )
}
if ! strings . Contains ( out , "foo" ) {
t . Errorf ( "unexpected output: %s" , out )
}
out , err = DefaultObjectDescriber . DescribeObject ( & api . ReplicationController { ObjectMeta : api . ObjectMeta { Name : "foo" } } )
if err != nil {
t . Fatalf ( "unexpected error: %v" , err )
}
if ! strings . Contains ( out , "foo" ) {
t . Errorf ( "unexpected output: %s" , out )
}
out , err = DefaultObjectDescriber . DescribeObject ( & api . Node { ObjectMeta : api . ObjectMeta { Name : "foo" } } )
if err != nil {
t . Fatalf ( "unexpected error: %v" , err )
}
if ! strings . Contains ( out , "foo" ) {
t . Errorf ( "unexpected output: %s" , out )
}
}
2015-07-30 02:19:17 +00:00
func TestGetPodsTotalRequests ( t * testing . T ) {
testCases := [ ] struct {
2015-07-30 21:33:48 +00:00
pods * api . PodList
2015-09-10 00:20:31 +00:00
expectedReqs , expectedLimits map [ api . ResourceName ] resource . Quantity
2015-07-30 02:19:17 +00:00
} {
{
2015-07-30 21:33:48 +00:00
pods : & api . PodList {
Items : [ ] api . Pod {
{
Spec : api . PodSpec {
Containers : [ ] api . Container {
{
Resources : api . ResourceRequirements {
Requests : api . ResourceList {
api . ResourceName ( api . ResourceCPU ) : resource . MustParse ( "1" ) ,
api . ResourceName ( api . ResourceMemory ) : resource . MustParse ( "300Mi" ) ,
api . ResourceName ( api . ResourceStorage ) : resource . MustParse ( "1G" ) ,
} ,
2015-07-30 02:19:17 +00:00
} ,
} ,
2015-07-30 21:33:48 +00:00
{
Resources : api . ResourceRequirements {
Requests : api . ResourceList {
api . ResourceName ( api . ResourceCPU ) : resource . MustParse ( "90m" ) ,
api . ResourceName ( api . ResourceMemory ) : resource . MustParse ( "120Mi" ) ,
api . ResourceName ( api . ResourceStorage ) : resource . MustParse ( "200M" ) ,
} ,
2015-07-30 02:19:17 +00:00
} ,
} ,
} ,
} ,
} ,
2015-07-30 21:33:48 +00:00
{
Spec : api . PodSpec {
Containers : [ ] api . Container {
{
Resources : api . ResourceRequirements {
Requests : api . ResourceList {
api . ResourceName ( api . ResourceCPU ) : resource . MustParse ( "60m" ) ,
api . ResourceName ( api . ResourceMemory ) : resource . MustParse ( "43Mi" ) ,
api . ResourceName ( api . ResourceStorage ) : resource . MustParse ( "500M" ) ,
} ,
2015-07-30 02:19:17 +00:00
} ,
} ,
2015-07-30 21:33:48 +00:00
{
Resources : api . ResourceRequirements {
Requests : api . ResourceList {
api . ResourceName ( api . ResourceCPU ) : resource . MustParse ( "34m" ) ,
api . ResourceName ( api . ResourceMemory ) : resource . MustParse ( "83Mi" ) ,
api . ResourceName ( api . ResourceStorage ) : resource . MustParse ( "700M" ) ,
} ,
2015-07-30 02:19:17 +00:00
} ,
} ,
} ,
} ,
} ,
} ,
} ,
expectedReqs : map [ api . ResourceName ] resource . Quantity {
api . ResourceName ( api . ResourceCPU ) : resource . MustParse ( "1.184" ) ,
api . ResourceName ( api . ResourceMemory ) : resource . MustParse ( "546Mi" ) ,
api . ResourceName ( api . ResourceStorage ) : resource . MustParse ( "2.4G" ) ,
} ,
} ,
}
for _ , testCase := range testCases {
2015-09-10 00:20:31 +00:00
reqs , _ , err := getPodsTotalRequestsAndLimits ( testCase . pods )
2015-07-30 02:19:17 +00:00
if err != nil {
t . Errorf ( "Unexpected error %v" , err )
}
2016-05-17 04:36:56 +00:00
if ! api . Semantic . DeepEqual ( reqs , testCase . expectedReqs ) {
2015-07-30 02:19:17 +00:00
t . Errorf ( "Expected %v, got %v" , testCase . expectedReqs , reqs )
}
}
}
2015-08-14 15:28:16 +00:00
func TestPersistentVolumeDescriber ( t * testing . T ) {
tests := map [ string ] * api . PersistentVolume {
"hostpath" : {
2016-09-08 15:50:53 +00:00
ObjectMeta : api . ObjectMeta { Name : "bar" } ,
2015-08-14 15:28:16 +00:00
Spec : api . PersistentVolumeSpec {
PersistentVolumeSource : api . PersistentVolumeSource {
HostPath : & api . HostPathVolumeSource { } ,
} ,
} ,
} ,
"gce" : {
2016-09-08 15:50:53 +00:00
ObjectMeta : api . ObjectMeta { Name : "bar" } ,
2015-08-14 15:28:16 +00:00
Spec : api . PersistentVolumeSpec {
PersistentVolumeSource : api . PersistentVolumeSource {
GCEPersistentDisk : & api . GCEPersistentDiskVolumeSource { } ,
} ,
} ,
} ,
"ebs" : {
2016-09-08 15:50:53 +00:00
ObjectMeta : api . ObjectMeta { Name : "bar" } ,
2015-08-14 15:28:16 +00:00
Spec : api . PersistentVolumeSpec {
PersistentVolumeSource : api . PersistentVolumeSource {
AWSElasticBlockStore : & api . AWSElasticBlockStoreVolumeSource { } ,
} ,
} ,
} ,
"nfs" : {
2016-09-08 15:50:53 +00:00
ObjectMeta : api . ObjectMeta { Name : "bar" } ,
2015-08-14 15:28:16 +00:00
Spec : api . PersistentVolumeSpec {
PersistentVolumeSource : api . PersistentVolumeSource {
NFS : & api . NFSVolumeSource { } ,
} ,
} ,
} ,
"iscsi" : {
2016-09-08 15:50:53 +00:00
ObjectMeta : api . ObjectMeta { Name : "bar" } ,
2015-08-14 15:28:16 +00:00
Spec : api . PersistentVolumeSpec {
PersistentVolumeSource : api . PersistentVolumeSource {
ISCSI : & api . ISCSIVolumeSource { } ,
} ,
} ,
} ,
"gluster" : {
2016-09-08 15:50:53 +00:00
ObjectMeta : api . ObjectMeta { Name : "bar" } ,
2015-08-14 15:28:16 +00:00
Spec : api . PersistentVolumeSpec {
PersistentVolumeSource : api . PersistentVolumeSource {
Glusterfs : & api . GlusterfsVolumeSource { } ,
} ,
} ,
} ,
"rbd" : {
2016-09-08 15:50:53 +00:00
ObjectMeta : api . ObjectMeta { Name : "bar" } ,
2015-08-14 15:28:16 +00:00
Spec : api . PersistentVolumeSpec {
PersistentVolumeSource : api . PersistentVolumeSource {
RBD : & api . RBDVolumeSource { } ,
} ,
2016-04-20 08:38:19 +00:00
} ,
} ,
"quobyte" : {
2016-09-08 15:50:53 +00:00
ObjectMeta : api . ObjectMeta { Name : "bar" } ,
2016-04-20 08:38:19 +00:00
Spec : api . PersistentVolumeSpec {
PersistentVolumeSource : api . PersistentVolumeSource {
Quobyte : & api . QuobyteVolumeSource { } ,
} ,
2015-08-14 15:28:16 +00:00
} ,
} ,
2016-08-30 15:22:50 +00:00
"cinder" : {
2016-09-08 15:50:53 +00:00
ObjectMeta : api . ObjectMeta { Name : "bar" } ,
2016-08-30 15:22:50 +00:00
Spec : api . PersistentVolumeSpec {
PersistentVolumeSource : api . PersistentVolumeSource {
Cinder : & api . CinderVolumeSource { } ,
} ,
2015-08-14 15:28:16 +00:00
} ,
} ,
}
for name , pv := range tests {
2016-09-08 15:50:53 +00:00
fake := fake . NewSimpleClientset ( pv )
2015-08-14 15:28:16 +00:00
c := PersistentVolumeDescriber { fake }
2016-04-20 17:27:32 +00:00
str , err := c . Describe ( "foo" , "bar" , DescriberSettings { ShowEvents : true } )
2015-08-14 15:28:16 +00:00
if err != nil {
t . Errorf ( "Unexpected error for test %s: %v" , name , err )
}
if str == "" {
t . Errorf ( "Unexpected empty string for test %s. Expected PV Describer output" , name )
}
2015-09-18 20:35:56 +00:00
}
}
2015-08-14 15:28:16 +00:00
2015-09-18 20:35:56 +00:00
func TestDescribeDeployment ( t * testing . T ) {
2016-11-18 20:58:22 +00:00
fake := fake . NewSimpleClientset ( )
versionedFake := versionedfake . NewSimpleClientset ( & v1beta1 . Deployment {
ObjectMeta : v1 . ObjectMeta {
2015-09-18 20:35:56 +00:00
Name : "bar" ,
Namespace : "foo" ,
} ,
2016-11-18 20:58:22 +00:00
Spec : v1beta1 . DeploymentSpec {
2016-12-03 18:57:26 +00:00
Selector : & metav1 . LabelSelector { } ,
2016-11-18 20:58:22 +00:00
Template : v1 . PodTemplateSpec { } ,
2015-09-18 20:35:56 +00:00
} ,
} )
2016-11-18 20:58:22 +00:00
d := DeploymentDescriber { fake , versionedFake }
2016-04-20 17:27:32 +00:00
out , err := d . Describe ( "foo" , "bar" , DescriberSettings { ShowEvents : true } )
2015-09-18 20:35:56 +00:00
if err != nil {
t . Errorf ( "unexpected error: %v" , err )
}
if ! strings . Contains ( out , "bar" ) || ! strings . Contains ( out , "foo" ) {
t . Errorf ( "unexpected out: %s" , out )
2015-08-14 15:28:16 +00:00
}
}
2016-04-20 17:27:32 +00:00
2016-05-05 17:30:50 +00:00
func TestDescribeCluster ( t * testing . T ) {
cluster := federation . Cluster {
ObjectMeta : api . ObjectMeta {
Name : "foo" ,
ResourceVersion : "4" ,
Labels : map [ string ] string {
"name" : "foo" ,
} ,
} ,
Spec : federation . ClusterSpec {
ServerAddressByClientCIDRs : [ ] federation . ServerAddressByClientCIDR {
{
ClientCIDR : "0.0.0.0/0" ,
ServerAddress : "localhost:8888" ,
} ,
} ,
} ,
Status : federation . ClusterStatus {
Conditions : [ ] federation . ClusterCondition {
{ Type : federation . ClusterReady , Status : api . ConditionTrue } ,
} ,
} ,
}
2016-11-30 07:27:27 +00:00
fake := fedfake . NewSimpleClientset ( & cluster )
2016-05-05 17:30:50 +00:00
d := ClusterDescriber { Interface : fake }
out , err := d . Describe ( "any" , "foo" , DescriberSettings { ShowEvents : true } )
if err != nil {
t . Errorf ( "unexpected error: %v" , err )
}
2016-06-28 20:37:03 +00:00
if ! strings . Contains ( out , "foo" ) {
2016-05-05 17:30:50 +00:00
t . Errorf ( "unexpected out: %s" , out )
}
}
2016-09-08 11:03:12 +00:00
func TestDescribeStorageClass ( t * testing . T ) {
2016-09-08 15:50:53 +00:00
f := fake . NewSimpleClientset ( & storage . StorageClass {
2016-09-08 11:03:12 +00:00
ObjectMeta : api . ObjectMeta {
Name : "foo" ,
ResourceVersion : "4" ,
Annotations : map [ string ] string {
"name" : "foo" ,
} ,
} ,
Provisioner : "my-provisioner" ,
Parameters : map [ string ] string {
"param1" : "value1" ,
"param2" : "value2" ,
} ,
} )
s := StorageClassDescriber { f }
out , err := s . Describe ( "" , "foo" , DescriberSettings { ShowEvents : true } )
if err != nil {
t . Errorf ( "unexpected error: %v" , err )
}
if ! strings . Contains ( out , "foo" ) {
t . Errorf ( "unexpected out: %s" , out )
}
}
2016-11-09 12:39:16 +00:00
func TestDescribePodDisruptionBudget ( t * testing . T ) {
f := fake . NewSimpleClientset ( & policy . PodDisruptionBudget {
ObjectMeta : api . ObjectMeta {
Namespace : "ns1" ,
Name : "pdb1" ,
2016-12-03 18:57:26 +00:00
CreationTimestamp : metav1 . Time { Time : time . Now ( ) . Add ( 1.9e9 ) } ,
2016-11-09 12:39:16 +00:00
} ,
Spec : policy . PodDisruptionBudgetSpec {
MinAvailable : intstr . FromInt ( 22 ) ,
} ,
Status : policy . PodDisruptionBudgetStatus {
PodDisruptionsAllowed : 5 ,
} ,
} )
s := PodDisruptionBudgetDescriber { f }
out , err := s . Describe ( "ns1" , "pdb1" , DescriberSettings { ShowEvents : true } )
if err != nil {
t . Errorf ( "unexpected error: %v" , err )
}
if ! strings . Contains ( out , "pdb1" ) {
t . Errorf ( "unexpected out: %s" , out )
}
}
2016-04-20 17:27:32 +00:00
func TestDescribeEvents ( t * testing . T ) {
events := & api . EventList {
Items : [ ] api . Event {
{
2016-06-02 01:47:36 +00:00
ObjectMeta : api . ObjectMeta {
Namespace : "foo" ,
} ,
2016-04-20 17:27:32 +00:00
Source : api . EventSource { Component : "kubelet" } ,
Message : "Item 1" ,
2016-12-03 18:57:26 +00:00
FirstTimestamp : metav1 . NewTime ( time . Date ( 2014 , time . January , 15 , 0 , 0 , 0 , 0 , time . UTC ) ) ,
LastTimestamp : metav1 . NewTime ( time . Date ( 2014 , time . January , 15 , 0 , 0 , 0 , 0 , time . UTC ) ) ,
2016-04-20 17:27:32 +00:00
Count : 1 ,
Type : api . EventTypeNormal ,
} ,
} ,
}
m := map [ string ] Describer {
"DaemonSetDescriber" : & DaemonSetDescriber {
2016-09-08 15:50:53 +00:00
fake . NewSimpleClientset ( & extensions . DaemonSet {
2016-04-20 17:27:32 +00:00
ObjectMeta : api . ObjectMeta {
Name : "bar" ,
Namespace : "foo" ,
} ,
} , events ) ,
} ,
"DeploymentDescriber" : & DeploymentDescriber {
2016-11-18 20:58:22 +00:00
fake . NewSimpleClientset ( events ) ,
versionedfake . NewSimpleClientset ( & v1beta1 . Deployment {
ObjectMeta : v1 . ObjectMeta {
2016-04-20 17:27:32 +00:00
Name : "bar" ,
Namespace : "foo" ,
} ,
2016-11-18 20:58:22 +00:00
Spec : v1beta1 . DeploymentSpec {
2016-12-03 18:57:26 +00:00
Selector : & metav1 . LabelSelector { } ,
2016-11-18 20:58:22 +00:00
} ,
} ) ,
2016-04-20 17:27:32 +00:00
} ,
"EndpointsDescriber" : & EndpointsDescriber {
2016-09-08 15:50:53 +00:00
fake . NewSimpleClientset ( & api . Endpoints {
2016-04-20 17:27:32 +00:00
ObjectMeta : api . ObjectMeta {
Name : "bar" ,
Namespace : "foo" ,
} ,
} , events ) ,
} ,
// TODO(jchaloup): add tests for:
// - HorizontalPodAutoscalerDescriber
// - IngressDescriber
// - JobDescriber
"NodeDescriber" : & NodeDescriber {
2016-09-08 15:50:53 +00:00
fake . NewSimpleClientset ( & api . Node {
2016-04-20 17:27:32 +00:00
ObjectMeta : api . ObjectMeta {
2016-09-08 15:50:53 +00:00
Name : "bar" ,
SelfLink : "url/url/url" ,
2016-04-20 17:27:32 +00:00
} ,
} , events ) ,
} ,
2016-06-15 12:17:02 +00:00
"PersistentVolumeDescriber" : & PersistentVolumeDescriber {
2016-09-08 15:50:53 +00:00
fake . NewSimpleClientset ( & api . PersistentVolume {
2016-06-15 12:17:02 +00:00
ObjectMeta : api . ObjectMeta {
2016-09-08 15:50:53 +00:00
Name : "bar" ,
SelfLink : "url/url/url" ,
2016-06-15 12:17:02 +00:00
} ,
} , events ) ,
} ,
2016-04-20 17:27:32 +00:00
"PodDescriber" : & PodDescriber {
2016-09-08 15:50:53 +00:00
fake . NewSimpleClientset ( & api . Pod {
2016-04-20 17:27:32 +00:00
ObjectMeta : api . ObjectMeta {
Name : "bar" ,
Namespace : "foo" ,
SelfLink : "url/url/url" ,
} ,
} , events ) ,
} ,
"ReplicaSetDescriber" : & ReplicaSetDescriber {
2016-09-08 15:50:53 +00:00
fake . NewSimpleClientset ( & extensions . ReplicaSet {
2016-04-20 17:27:32 +00:00
ObjectMeta : api . ObjectMeta {
Name : "bar" ,
Namespace : "foo" ,
} ,
} , events ) ,
} ,
"ReplicationControllerDescriber" : & ReplicationControllerDescriber {
2016-09-08 15:50:53 +00:00
fake . NewSimpleClientset ( & api . ReplicationController {
2016-04-20 17:27:32 +00:00
ObjectMeta : api . ObjectMeta {
Name : "bar" ,
Namespace : "foo" ,
} ,
} , events ) ,
} ,
"Service" : & ServiceDescriber {
2016-09-08 15:50:53 +00:00
fake . NewSimpleClientset ( & api . Service {
2016-04-20 17:27:32 +00:00
ObjectMeta : api . ObjectMeta {
Name : "bar" ,
Namespace : "foo" ,
} ,
} , events ) ,
} ,
2016-09-08 11:03:12 +00:00
"StorageClass" : & StorageClassDescriber {
2016-09-08 15:50:53 +00:00
fake . NewSimpleClientset ( & storage . StorageClass {
2016-09-08 11:03:12 +00:00
ObjectMeta : api . ObjectMeta {
2016-09-08 15:50:53 +00:00
Name : "bar" ,
2016-09-08 11:03:12 +00:00
} ,
} , events ) ,
} ,
2016-04-20 17:27:32 +00:00
}
for name , d := range m {
out , err := d . Describe ( "foo" , "bar" , DescriberSettings { ShowEvents : true } )
if err != nil {
t . Errorf ( "unexpected error for %q: %v" , name , err )
}
if ! strings . Contains ( out , "bar" ) {
t . Errorf ( "unexpected out for %q: %s" , name , out )
}
if ! strings . Contains ( out , "Events:" ) {
t . Errorf ( "events not found for %q when ShowEvents=true: %s" , name , out )
}
out , err = d . Describe ( "foo" , "bar" , DescriberSettings { ShowEvents : false } )
if err != nil {
t . Errorf ( "unexpected error for %q: %s" , name , err )
}
if ! strings . Contains ( out , "bar" ) {
t . Errorf ( "unexpected out for %q: %s" , name , out )
}
if strings . Contains ( out , "Events:" ) {
t . Errorf ( "events found for %q when ShowEvents=false: %s" , name , out )
}
}
}