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 .
* /
2017-02-19 03:40:38 +00:00
package internalversion
2014-10-27 19:56:34 +00:00
import (
2015-04-02 15:42:19 +00:00
"bytes"
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
2017-01-25 13:39:54 +00:00
apiequality "k8s.io/apimachinery/pkg/api/equality"
2017-01-25 13:13:07 +00:00
"k8s.io/apimachinery/pkg/api/resource"
2017-01-11 14:09:48 +00:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2017-04-07 00:14:16 +00:00
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2017-01-27 20:42:17 +00:00
"k8s.io/apimachinery/pkg/util/intstr"
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"
2016-11-18 20:58:22 +00:00
"k8s.io/kubernetes/pkg/api/v1"
2016-11-30 20:58:58 +00:00
"k8s.io/kubernetes/pkg/apis/autoscaling"
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-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"
2017-02-19 22:37:24 +00:00
"k8s.io/kubernetes/pkg/printers"
2017-01-05 21:57:01 +00:00
"k8s.io/kubernetes/pkg/util"
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 {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2015-04-06 23:27:53 +00:00
Name : "bar" ,
Namespace : "foo" ,
} ,
} )
c := & describeClient { T : t , Namespace : "foo" , Interface : fake }
2014-11-14 01:42:50 +00:00
d := PodDescriber { c }
2017-02-19 22:37:24 +00:00
out , err := d . Describe ( "foo" , "bar" , printers . 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 ) {
2016-09-08 15:50:53 +00:00
fake := fake . NewSimpleClientset ( & api . Pod {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-08-10 17:39:11 +00:00
Name : "bar" ,
Namespace : "foo" ,
2017-02-07 14:08:45 +00:00
} ,
Spec : api . PodSpec {
Tolerations : [ ] api . Toleration {
{ Key : "key1" , Value : "value1" } ,
2017-02-27 13:28:45 +00:00
{ Key : "key2" , Value : "value2" , Effect : api . TaintEffectNoExecute , TolerationSeconds : & [ ] int64 { 300 } [ 0 ] } ,
2016-08-10 17:39:11 +00:00
} ,
} ,
} )
c := & describeClient { T : t , Namespace : "foo" , Interface : fake }
d := PodDescriber { c }
2017-02-19 22:37:24 +00:00
out , err := d . Describe ( "foo" , "bar" , printers . DescriberSettings { } )
2016-08-10 17:39:11 +00:00
if err != nil {
t . Errorf ( "unexpected error: %v" , err )
}
2017-02-27 13:28:45 +00:00
if ! strings . Contains ( out , "key1=value1" ) || ! strings . Contains ( out , "key2=value2:NoExecute for 300s" ) || ! 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 {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-09-15 22:08:57 +00:00
Name : "myns" ,
} ,
} )
c := & describeClient { T : t , Namespace : "" , Interface : fake }
d := NamespaceDescriber { c }
2017-02-19 22:37:24 +00:00
out , err := d . Describe ( "" , "myns" , printers . DescriberSettings { ShowEvents : true } )
2016-09-15 22:08:57 +00:00
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 {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2015-04-06 23:27:53 +00:00
Name : "bar" ,
Namespace : "foo" ,
} ,
} )
c := & describeClient { T : t , Namespace : "foo" , Interface : fake }
2014-11-14 01:42:50 +00:00
d := ServiceDescriber { c }
2017-02-19 22:37:24 +00:00
out , err := d . Describe ( "foo" , "bar" , printers . 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 {
{
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta { Name : "one" } ,
2016-09-08 15:50:53 +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-09-08 15:50:53 +00:00
Count : 1 ,
Type : api . EventTypeNormal ,
} ,
{
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta { Name : "two" } ,
2016-09-08 15:50:53 +00:00
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 ,
} ,
{
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta { Name : "three" } ,
2016-09-08 15:50:53 +00:00
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
} ,
} ,
2017-01-17 03:38:19 +00:00
& api . Pod { ObjectMeta : metav1 . ObjectMeta { Namespace : "foo" , Name : "bar" } } ,
2016-09-08 15:50:53 +00:00
)
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
2017-02-19 22:37:24 +00:00
out , err := d . Describe ( "foo" , "bar" , printers . 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 ) {
2017-01-17 00:30:22 +00:00
trueVal := true
2015-04-02 15:42:19 +00:00
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 ,
} ,
2017-01-17 00:30:22 +00:00
expectedElements : [ ] string { "test" , "State" , "Waiting" , "Ready" , "True" , "Restart Count" , "7" , "Image" , "image" , "envname" , "xyz" , "a123\tConfigMap\tOptional: false" } ,
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 { { Prefix : "p_" , ConfigMapRef : & api . ConfigMapEnvSource { LocalObjectReference : api . LocalObjectReference { Name : "a123" } } } } } ,
status : api . ContainerStatus {
Name : "test" ,
Ready : true ,
RestartCount : 7 ,
} ,
2017-01-17 00:30:22 +00:00
expectedElements : [ ] string { "test" , "State" , "Waiting" , "Ready" , "True" , "Restart Count" , "7" , "Image" , "image" , "envname" , "xyz" , "a123\tConfigMap with prefix 'p_'\tOptional: false" } ,
2015-07-04 14:31:15 +00:00
} ,
2017-01-04 20:50:11 +00:00
{
2017-01-17 00:30:22 +00:00
container : api . Container { Name : "test" , Image : "image" , Env : [ ] api . EnvVar { { Name : "envname" , Value : "xyz" } } , EnvFrom : [ ] api . EnvFromSource { { ConfigMapRef : & api . ConfigMapEnvSource { Optional : & trueVal , LocalObjectReference : api . LocalObjectReference { Name : "a123" } } } } } ,
2017-01-04 20:50:11 +00:00
status : api . ContainerStatus {
Name : "test" ,
Ready : true ,
RestartCount : 7 ,
} ,
2017-01-17 00:30:22 +00:00
expectedElements : [ ] string { "test" , "State" , "Waiting" , "Ready" , "True" , "Restart Count" , "7" , "Image" , "image" , "envname" , "xyz" , "a123\tConfigMap\tOptional: true" } ,
} ,
{
container : api . Container { Name : "test" , Image : "image" , Env : [ ] api . EnvVar { { Name : "envname" , Value : "xyz" } } , EnvFrom : [ ] api . EnvFromSource { { SecretRef : & api . SecretEnvSource { LocalObjectReference : api . LocalObjectReference { Name : "a123" } , Optional : & trueVal } } } } ,
status : api . ContainerStatus {
Name : "test" ,
Ready : true ,
RestartCount : 7 ,
} ,
expectedElements : [ ] string { "test" , "State" , "Waiting" , "Ready" , "True" , "Restart Count" , "7" , "Image" , "image" , "envname" , "xyz" , "a123\tSecret\tOptional: true" } ,
2017-01-04 20:50:11 +00:00
} ,
{
container : api . Container { Name : "test" , Image : "image" , Env : [ ] api . EnvVar { { Name : "envname" , Value : "xyz" } } , EnvFrom : [ ] api . EnvFromSource { { Prefix : "p_" , SecretRef : & api . SecretEnvSource { LocalObjectReference : api . LocalObjectReference { Name : "a123" } } } } } ,
status : api . ContainerStatus {
Name : "test" ,
Ready : true ,
RestartCount : 7 ,
} ,
2017-01-17 00:30:22 +00:00
expectedElements : [ ] string { "test" , "State" , "Waiting" , "Ready" , "True" , "Restart Count" , "7" , "Image" , "image" , "envname" , "xyz" , "a123\tSecret with prefix 'p_'\tOptional: false" } ,
2017-01-04 20:50:11 +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 } ,
} ,
}
2017-03-14 14:31:13 +00:00
writer := NewPrefixWriter ( out )
2016-11-02 01:54:17 +00:00
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 {
2017-02-19 22:37:24 +00:00
if noDescriber , ok := err . ( printers . ErrNoDescriber ) ; ok {
2015-02-26 18:50:12 +00:00
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 ) {
2017-01-17 03:38:19 +00:00
out , err := DefaultObjectDescriber . DescribeObject ( & api . Pod { ObjectMeta : metav1 . ObjectMeta { Name : "foo" } } )
2015-02-26 18:50:12 +00:00
if err != nil {
t . Fatalf ( "unexpected error: %v" , err )
}
if ! strings . Contains ( out , "foo" ) {
t . Errorf ( "unexpected output: %s" , out )
}
2017-01-17 03:38:19 +00:00
out , err = DefaultObjectDescriber . DescribeObject ( & api . Service { ObjectMeta : metav1 . ObjectMeta { Name : "foo" } } )
2015-02-26 18:50:12 +00:00
if err != nil {
t . Fatalf ( "unexpected error: %v" , err )
}
if ! strings . Contains ( out , "foo" ) {
t . Errorf ( "unexpected output: %s" , out )
}
2017-01-17 03:38:19 +00:00
out , err = DefaultObjectDescriber . DescribeObject ( & api . ReplicationController { ObjectMeta : metav1 . ObjectMeta { Name : "foo" } } )
2015-02-26 18:50:12 +00:00
if err != nil {
t . Fatalf ( "unexpected error: %v" , err )
}
if ! strings . Contains ( out , "foo" ) {
t . Errorf ( "unexpected output: %s" , out )
}
2017-01-17 03:38:19 +00:00
out , err = DefaultObjectDescriber . DescribeObject ( & api . Node { ObjectMeta : metav1 . ObjectMeta { Name : "foo" } } )
2015-02-26 18:50:12 +00:00
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 )
}
2017-01-25 13:39:54 +00:00
if ! apiequality . 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" : {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta { Name : "bar" } ,
2015-08-14 15:28:16 +00:00
Spec : api . PersistentVolumeSpec {
PersistentVolumeSource : api . PersistentVolumeSource {
HostPath : & api . HostPathVolumeSource { } ,
} ,
} ,
} ,
"gce" : {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta { Name : "bar" } ,
2015-08-14 15:28:16 +00:00
Spec : api . PersistentVolumeSpec {
PersistentVolumeSource : api . PersistentVolumeSource {
GCEPersistentDisk : & api . GCEPersistentDiskVolumeSource { } ,
} ,
} ,
} ,
"ebs" : {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta { Name : "bar" } ,
2015-08-14 15:28:16 +00:00
Spec : api . PersistentVolumeSpec {
PersistentVolumeSource : api . PersistentVolumeSource {
AWSElasticBlockStore : & api . AWSElasticBlockStoreVolumeSource { } ,
} ,
} ,
} ,
"nfs" : {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta { Name : "bar" } ,
2015-08-14 15:28:16 +00:00
Spec : api . PersistentVolumeSpec {
PersistentVolumeSource : api . PersistentVolumeSource {
NFS : & api . NFSVolumeSource { } ,
} ,
} ,
} ,
"iscsi" : {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta { Name : "bar" } ,
2015-08-14 15:28:16 +00:00
Spec : api . PersistentVolumeSpec {
PersistentVolumeSource : api . PersistentVolumeSource {
ISCSI : & api . ISCSIVolumeSource { } ,
} ,
} ,
} ,
"gluster" : {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta { Name : "bar" } ,
2015-08-14 15:28:16 +00:00
Spec : api . PersistentVolumeSpec {
PersistentVolumeSource : api . PersistentVolumeSource {
Glusterfs : & api . GlusterfsVolumeSource { } ,
} ,
} ,
} ,
"rbd" : {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . 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" : {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . 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" : {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . 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 }
2017-02-19 22:37:24 +00:00
str , err := c . Describe ( "foo" , "bar" , printers . 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 {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2015-09-18 20:35:56 +00:00
Name : "bar" ,
Namespace : "foo" ,
} ,
2016-11-18 20:58:22 +00:00
Spec : v1beta1 . DeploymentSpec {
2017-01-05 21:57:01 +00:00
Replicas : util . Int32Ptr ( 1 ) ,
2016-12-03 18:57:26 +00:00
Selector : & metav1 . LabelSelector { } ,
2017-02-28 22:52:28 +00:00
Template : v1 . PodTemplateSpec {
Spec : v1 . PodSpec {
Containers : [ ] v1 . Container {
{ Image : "mytest-image:latest" } ,
} ,
} ,
} ,
2015-09-18 20:35:56 +00:00
} ,
} )
2016-11-18 20:58:22 +00:00
d := DeploymentDescriber { fake , versionedFake }
2017-02-19 22:37:24 +00:00
out , err := d . Describe ( "foo" , "bar" , printers . DescriberSettings { ShowEvents : true } )
2015-09-18 20:35:56 +00:00
if err != nil {
t . Errorf ( "unexpected error: %v" , err )
}
2017-02-28 22:52:28 +00:00
if ! strings . Contains ( out , "bar" ) || ! strings . Contains ( out , "foo" ) || ! strings . Contains ( out , "Containers:" ) || ! strings . Contains ( out , "mytest-image:latest" ) {
2015-09-18 20:35:56 +00:00
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 {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-05-05 17:30:50 +00:00
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 }
2017-02-19 22:37:24 +00:00
out , err := d . Describe ( "any" , "foo" , printers . DescriberSettings { ShowEvents : true } )
2016-05-05 17:30:50 +00:00
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 {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-09-08 11:03:12 +00:00
Name : "foo" ,
ResourceVersion : "4" ,
Annotations : map [ string ] string {
"name" : "foo" ,
} ,
} ,
Provisioner : "my-provisioner" ,
Parameters : map [ string ] string {
"param1" : "value1" ,
"param2" : "value2" ,
} ,
} )
s := StorageClassDescriber { f }
2017-02-19 22:37:24 +00:00
out , err := s . Describe ( "" , "foo" , printers . DescriberSettings { ShowEvents : true } )
2016-09-08 11:03:12 +00:00
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 {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-11-09 12:39:16 +00:00
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 }
2017-02-19 22:37:24 +00:00
out , err := s . Describe ( "ns1" , "pdb1" , printers . DescriberSettings { ShowEvents : true } )
2016-11-09 12:39:16 +00:00
if err != nil {
t . Errorf ( "unexpected error: %v" , err )
}
if ! strings . Contains ( out , "pdb1" ) {
t . Errorf ( "unexpected out: %s" , out )
}
}
2016-11-30 20:58:58 +00:00
func TestDescribeHorizontalPodAutoscaler ( t * testing . T ) {
minReplicasVal := int32 ( 2 )
targetUtilizationVal := int32 ( 80 )
currentUtilizationVal := int32 ( 50 )
tests := [ ] struct {
name string
hpa autoscaling . HorizontalPodAutoscaler
} {
{
"minReplicas unset" ,
autoscaling . HorizontalPodAutoscaler {
Spec : autoscaling . HorizontalPodAutoscalerSpec {
ScaleTargetRef : autoscaling . CrossVersionObjectReference {
Name : "some-rc" ,
Kind : "ReplicationController" ,
} ,
MaxReplicas : 10 ,
} ,
Status : autoscaling . HorizontalPodAutoscalerStatus {
CurrentReplicas : 4 ,
DesiredReplicas : 5 ,
} ,
} ,
} ,
{
"pods source type (no current)" ,
autoscaling . HorizontalPodAutoscaler {
Spec : autoscaling . HorizontalPodAutoscalerSpec {
ScaleTargetRef : autoscaling . CrossVersionObjectReference {
Name : "some-rc" ,
Kind : "ReplicationController" ,
} ,
MinReplicas : & minReplicasVal ,
MaxReplicas : 10 ,
Metrics : [ ] autoscaling . MetricSpec {
{
Type : autoscaling . PodsMetricSourceType ,
Pods : & autoscaling . PodsMetricSource {
MetricName : "some-pods-metric" ,
TargetAverageValue : * resource . NewMilliQuantity ( 100 , resource . DecimalSI ) ,
} ,
} ,
} ,
} ,
Status : autoscaling . HorizontalPodAutoscalerStatus {
CurrentReplicas : 4 ,
DesiredReplicas : 5 ,
} ,
} ,
} ,
{
"pods source type (with current)" ,
autoscaling . HorizontalPodAutoscaler {
Spec : autoscaling . HorizontalPodAutoscalerSpec {
ScaleTargetRef : autoscaling . CrossVersionObjectReference {
Name : "some-rc" ,
Kind : "ReplicationController" ,
} ,
MinReplicas : & minReplicasVal ,
MaxReplicas : 10 ,
Metrics : [ ] autoscaling . MetricSpec {
{
Type : autoscaling . PodsMetricSourceType ,
Pods : & autoscaling . PodsMetricSource {
MetricName : "some-pods-metric" ,
TargetAverageValue : * resource . NewMilliQuantity ( 100 , resource . DecimalSI ) ,
} ,
} ,
} ,
} ,
Status : autoscaling . HorizontalPodAutoscalerStatus {
CurrentReplicas : 4 ,
DesiredReplicas : 5 ,
CurrentMetrics : [ ] autoscaling . MetricStatus {
{
Type : autoscaling . PodsMetricSourceType ,
Pods : & autoscaling . PodsMetricStatus {
MetricName : "some-pods-metric" ,
CurrentAverageValue : * resource . NewMilliQuantity ( 50 , resource . DecimalSI ) ,
} ,
} ,
} ,
} ,
} ,
} ,
{
"object source type (no current)" ,
autoscaling . HorizontalPodAutoscaler {
Spec : autoscaling . HorizontalPodAutoscalerSpec {
ScaleTargetRef : autoscaling . CrossVersionObjectReference {
Name : "some-rc" ,
Kind : "ReplicationController" ,
} ,
MinReplicas : & minReplicasVal ,
MaxReplicas : 10 ,
Metrics : [ ] autoscaling . MetricSpec {
{
Type : autoscaling . ObjectMetricSourceType ,
Object : & autoscaling . ObjectMetricSource {
Target : autoscaling . CrossVersionObjectReference {
Name : "some-service" ,
Kind : "Service" ,
} ,
MetricName : "some-service-metric" ,
TargetValue : * resource . NewMilliQuantity ( 100 , resource . DecimalSI ) ,
} ,
} ,
} ,
} ,
Status : autoscaling . HorizontalPodAutoscalerStatus {
CurrentReplicas : 4 ,
DesiredReplicas : 5 ,
} ,
} ,
} ,
{
"object source type (with current)" ,
autoscaling . HorizontalPodAutoscaler {
Spec : autoscaling . HorizontalPodAutoscalerSpec {
ScaleTargetRef : autoscaling . CrossVersionObjectReference {
Name : "some-rc" ,
Kind : "ReplicationController" ,
} ,
MinReplicas : & minReplicasVal ,
MaxReplicas : 10 ,
Metrics : [ ] autoscaling . MetricSpec {
{
Type : autoscaling . ObjectMetricSourceType ,
Object : & autoscaling . ObjectMetricSource {
Target : autoscaling . CrossVersionObjectReference {
Name : "some-service" ,
Kind : "Service" ,
} ,
MetricName : "some-service-metric" ,
TargetValue : * resource . NewMilliQuantity ( 100 , resource . DecimalSI ) ,
} ,
} ,
} ,
} ,
Status : autoscaling . HorizontalPodAutoscalerStatus {
CurrentReplicas : 4 ,
DesiredReplicas : 5 ,
CurrentMetrics : [ ] autoscaling . MetricStatus {
{
Type : autoscaling . ObjectMetricSourceType ,
Object : & autoscaling . ObjectMetricStatus {
Target : autoscaling . CrossVersionObjectReference {
Name : "some-service" ,
Kind : "Service" ,
} ,
MetricName : "some-service-metric" ,
CurrentValue : * resource . NewMilliQuantity ( 50 , resource . DecimalSI ) ,
} ,
} ,
} ,
} ,
} ,
} ,
{
"resource source type, target average value (no current)" ,
autoscaling . HorizontalPodAutoscaler {
Spec : autoscaling . HorizontalPodAutoscalerSpec {
ScaleTargetRef : autoscaling . CrossVersionObjectReference {
Name : "some-rc" ,
Kind : "ReplicationController" ,
} ,
MinReplicas : & minReplicasVal ,
MaxReplicas : 10 ,
Metrics : [ ] autoscaling . MetricSpec {
{
Type : autoscaling . ResourceMetricSourceType ,
Resource : & autoscaling . ResourceMetricSource {
Name : api . ResourceCPU ,
TargetAverageValue : resource . NewMilliQuantity ( 100 , resource . DecimalSI ) ,
} ,
} ,
} ,
} ,
Status : autoscaling . HorizontalPodAutoscalerStatus {
CurrentReplicas : 4 ,
DesiredReplicas : 5 ,
} ,
} ,
} ,
{
"resource source type, target average value (with current)" ,
autoscaling . HorizontalPodAutoscaler {
Spec : autoscaling . HorizontalPodAutoscalerSpec {
ScaleTargetRef : autoscaling . CrossVersionObjectReference {
Name : "some-rc" ,
Kind : "ReplicationController" ,
} ,
MinReplicas : & minReplicasVal ,
MaxReplicas : 10 ,
Metrics : [ ] autoscaling . MetricSpec {
{
Type : autoscaling . ResourceMetricSourceType ,
Resource : & autoscaling . ResourceMetricSource {
Name : api . ResourceCPU ,
TargetAverageValue : resource . NewMilliQuantity ( 100 , resource . DecimalSI ) ,
} ,
} ,
} ,
} ,
Status : autoscaling . HorizontalPodAutoscalerStatus {
CurrentReplicas : 4 ,
DesiredReplicas : 5 ,
CurrentMetrics : [ ] autoscaling . MetricStatus {
{
Type : autoscaling . ResourceMetricSourceType ,
Resource : & autoscaling . ResourceMetricStatus {
Name : api . ResourceCPU ,
CurrentAverageValue : * resource . NewMilliQuantity ( 50 , resource . DecimalSI ) ,
} ,
} ,
} ,
} ,
} ,
} ,
{
"resource source type, target utilization (no current)" ,
autoscaling . HorizontalPodAutoscaler {
Spec : autoscaling . HorizontalPodAutoscalerSpec {
ScaleTargetRef : autoscaling . CrossVersionObjectReference {
Name : "some-rc" ,
Kind : "ReplicationController" ,
} ,
MinReplicas : & minReplicasVal ,
MaxReplicas : 10 ,
Metrics : [ ] autoscaling . MetricSpec {
{
Type : autoscaling . ResourceMetricSourceType ,
Resource : & autoscaling . ResourceMetricSource {
Name : api . ResourceCPU ,
TargetAverageUtilization : & targetUtilizationVal ,
} ,
} ,
} ,
} ,
Status : autoscaling . HorizontalPodAutoscalerStatus {
CurrentReplicas : 4 ,
DesiredReplicas : 5 ,
} ,
} ,
} ,
{
"resource source type, target utilization (with current)" ,
autoscaling . HorizontalPodAutoscaler {
Spec : autoscaling . HorizontalPodAutoscalerSpec {
ScaleTargetRef : autoscaling . CrossVersionObjectReference {
Name : "some-rc" ,
Kind : "ReplicationController" ,
} ,
MinReplicas : & minReplicasVal ,
MaxReplicas : 10 ,
Metrics : [ ] autoscaling . MetricSpec {
{
Type : autoscaling . ResourceMetricSourceType ,
Resource : & autoscaling . ResourceMetricSource {
Name : api . ResourceCPU ,
TargetAverageUtilization : & targetUtilizationVal ,
} ,
} ,
} ,
} ,
Status : autoscaling . HorizontalPodAutoscalerStatus {
CurrentReplicas : 4 ,
DesiredReplicas : 5 ,
CurrentMetrics : [ ] autoscaling . MetricStatus {
{
Type : autoscaling . ResourceMetricSourceType ,
Resource : & autoscaling . ResourceMetricStatus {
Name : api . ResourceCPU ,
CurrentAverageUtilization : & currentUtilizationVal ,
CurrentAverageValue : * resource . NewMilliQuantity ( 40 , resource . DecimalSI ) ,
} ,
} ,
} ,
} ,
} ,
} ,
{
"multiple metrics" ,
autoscaling . HorizontalPodAutoscaler {
Spec : autoscaling . HorizontalPodAutoscalerSpec {
ScaleTargetRef : autoscaling . CrossVersionObjectReference {
Name : "some-rc" ,
Kind : "ReplicationController" ,
} ,
MinReplicas : & minReplicasVal ,
MaxReplicas : 10 ,
Metrics : [ ] autoscaling . MetricSpec {
{
Type : autoscaling . PodsMetricSourceType ,
Pods : & autoscaling . PodsMetricSource {
MetricName : "some-pods-metric" ,
TargetAverageValue : * resource . NewMilliQuantity ( 100 , resource . DecimalSI ) ,
} ,
} ,
{
Type : autoscaling . ResourceMetricSourceType ,
Resource : & autoscaling . ResourceMetricSource {
Name : api . ResourceCPU ,
TargetAverageUtilization : & targetUtilizationVal ,
} ,
} ,
{
Type : autoscaling . PodsMetricSourceType ,
Pods : & autoscaling . PodsMetricSource {
MetricName : "other-pods-metric" ,
TargetAverageValue : * resource . NewMilliQuantity ( 400 , resource . DecimalSI ) ,
} ,
} ,
} ,
} ,
Status : autoscaling . HorizontalPodAutoscalerStatus {
CurrentReplicas : 4 ,
DesiredReplicas : 5 ,
CurrentMetrics : [ ] autoscaling . MetricStatus {
{
Type : autoscaling . PodsMetricSourceType ,
Pods : & autoscaling . PodsMetricStatus {
MetricName : "some-pods-metric" ,
CurrentAverageValue : * resource . NewMilliQuantity ( 50 , resource . DecimalSI ) ,
} ,
} ,
{
Type : autoscaling . ResourceMetricSourceType ,
Resource : & autoscaling . ResourceMetricStatus {
Name : api . ResourceCPU ,
CurrentAverageUtilization : & currentUtilizationVal ,
CurrentAverageValue : * resource . NewMilliQuantity ( 40 , resource . DecimalSI ) ,
} ,
} ,
} ,
} ,
} ,
} ,
}
for _ , test := range tests {
test . hpa . ObjectMeta = metav1 . ObjectMeta {
Name : "bar" ,
Namespace : "foo" ,
}
fake := fake . NewSimpleClientset ( & test . hpa )
desc := HorizontalPodAutoscalerDescriber { fake }
2017-02-19 22:37:24 +00:00
str , err := desc . Describe ( "foo" , "bar" , printers . DescriberSettings { ShowEvents : true } )
2016-11-30 20:58:58 +00:00
if err != nil {
t . Errorf ( "Unexpected error for test %s: %v" , test . name , err )
}
if str == "" {
t . Errorf ( "Unexpected empty string for test %s. Expected HPA Describer output" , test . name )
}
t . Logf ( "Description for %q:\n%s" , test . name , str )
}
}
2016-04-20 17:27:32 +00:00
func TestDescribeEvents ( t * testing . T ) {
events := & api . EventList {
Items : [ ] api . Event {
{
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-06-02 01:47:36 +00:00
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 ,
} ,
} ,
}
2017-02-19 22:37:24 +00:00
m := map [ string ] printers . Describer {
2016-04-20 17:27:32 +00:00
"DaemonSetDescriber" : & DaemonSetDescriber {
2016-09-08 15:50:53 +00:00
fake . NewSimpleClientset ( & extensions . DaemonSet {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-04-20 17:27:32 +00:00
Name : "bar" ,
Namespace : "foo" ,
} ,
} , events ) ,
} ,
"DeploymentDescriber" : & DeploymentDescriber {
2016-11-18 20:58:22 +00:00
fake . NewSimpleClientset ( events ) ,
versionedfake . NewSimpleClientset ( & v1beta1 . Deployment {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-04-20 17:27:32 +00:00
Name : "bar" ,
Namespace : "foo" ,
} ,
2016-11-18 20:58:22 +00:00
Spec : v1beta1 . DeploymentSpec {
2017-01-05 21:57:01 +00:00
Replicas : util . Int32Ptr ( 1 ) ,
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 {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-04-20 17:27:32 +00:00
Name : "bar" ,
Namespace : "foo" ,
} ,
} , events ) ,
} ,
// TODO(jchaloup): add tests for:
// - IngressDescriber
// - JobDescriber
"NodeDescriber" : & NodeDescriber {
2016-09-08 15:50:53 +00:00
fake . NewSimpleClientset ( & api . Node {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . 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 {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . 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 {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-04-20 17:27:32 +00:00
Name : "bar" ,
Namespace : "foo" ,
SelfLink : "url/url/url" ,
} ,
} , events ) ,
} ,
"ReplicaSetDescriber" : & ReplicaSetDescriber {
2016-09-08 15:50:53 +00:00
fake . NewSimpleClientset ( & extensions . ReplicaSet {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-04-20 17:27:32 +00:00
Name : "bar" ,
Namespace : "foo" ,
} ,
} , events ) ,
} ,
"ReplicationControllerDescriber" : & ReplicationControllerDescriber {
2016-09-08 15:50:53 +00:00
fake . NewSimpleClientset ( & api . ReplicationController {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-04-20 17:27:32 +00:00
Name : "bar" ,
Namespace : "foo" ,
} ,
} , events ) ,
} ,
"Service" : & ServiceDescriber {
2016-09-08 15:50:53 +00:00
fake . NewSimpleClientset ( & api . Service {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-04-20 17:27:32 +00:00
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 {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-09-08 15:50:53 +00:00
Name : "bar" ,
2016-09-08 11:03:12 +00:00
} ,
} , events ) ,
} ,
2016-11-30 20:58:58 +00:00
"HorizontalPodAutoscaler" : & HorizontalPodAutoscalerDescriber {
fake . NewSimpleClientset ( & autoscaling . HorizontalPodAutoscaler {
ObjectMeta : metav1 . ObjectMeta {
Name : "bar" ,
Namespace : "foo" ,
} ,
2017-03-06 14:07:48 +00:00
} , events ) ,
} ,
"ConfigMap" : & ConfigMapDescriber {
fake . NewSimpleClientset ( & api . ConfigMap {
ObjectMeta : metav1 . ObjectMeta {
Name : "bar" ,
Namespace : "foo" ,
} ,
2016-11-30 20:58:58 +00:00
} , events ) ,
} ,
2016-04-20 17:27:32 +00:00
}
for name , d := range m {
2017-02-19 22:37:24 +00:00
out , err := d . Describe ( "foo" , "bar" , printers . DescriberSettings { ShowEvents : true } )
2016-04-20 17:27:32 +00:00
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 )
}
2017-02-19 22:37:24 +00:00
out , err = d . Describe ( "foo" , "bar" , printers . DescriberSettings { ShowEvents : false } )
2016-04-20 17:27:32 +00:00
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 )
}
}
}
2016-10-20 02:34:57 +00:00
func TestPrintLabelsMultiline ( t * testing . T ) {
var maxLenAnnotationStr string = "MaxLenAnnotation=Multicast addressing can be used in the link layer (Layer 2 in the OSI model), such as Ethernet multicast, and at the internet layer (Layer 3 for OSI) for Internet Protocol Version 4 "
testCases := [ ] struct {
annotations map [ string ] string
expectPrint string
} {
{
annotations : map [ string ] string { "col1" : "asd" , "COL2" : "zxc" } ,
expectPrint : "Annotations:\tCOL2=zxc\n\tcol1=asd\n" ,
} ,
{
annotations : map [ string ] string { "MaxLenAnnotation" : maxLenAnnotationStr [ 17 : ] } ,
expectPrint : "Annotations:\t" + maxLenAnnotationStr + "\n" ,
} ,
{
annotations : map [ string ] string { "MaxLenAnnotation" : maxLenAnnotationStr [ 17 : ] + "1" } ,
expectPrint : "Annotations:\t" + maxLenAnnotationStr + "...\n" ,
} ,
{
annotations : map [ string ] string { } ,
expectPrint : "Annotations:\t<none>\n" ,
} ,
}
for i , testCase := range testCases {
out := new ( bytes . Buffer )
2017-03-14 14:31:13 +00:00
writer := NewPrefixWriter ( out )
2016-10-20 02:34:57 +00:00
printAnnotationsMultiline ( writer , "Annotations" , testCase . annotations )
output := out . String ( )
if output != testCase . expectPrint {
t . Errorf ( "Test case %d: expected to find %q in output: %q" , i , testCase . expectPrint , output )
}
}
}
2017-04-07 00:14:16 +00:00
func TestDescribeUnstructuredContent ( t * testing . T ) {
testCases := [ ] struct {
expected string
unexpected string
} {
{
expected : ` API Version : v1
Dummy 2 : present
Items :
Item Bool : true
Item Int : 42
Kind : Test
Metadata :
Creation Timestamp : 2017 - 04 - 01 T00 : 00 : 00 Z
Name : MyName
Namespace : MyNamespace
Resource Version : 123
UID : 00000000 - 0000 - 0000 - 0000 - 000000000001
Status : ok
URL : http : //localhost
` ,
} ,
{
unexpected : "\nDummy 1:\tpresent\n" ,
} ,
{
unexpected : "Dummy 1" ,
} ,
{
unexpected : "Dummy 3" ,
} ,
{
unexpected : "Dummy3" ,
} ,
{
unexpected : "dummy3" ,
} ,
{
unexpected : "dummy 3" ,
} ,
}
out := new ( bytes . Buffer )
w := NewPrefixWriter ( out )
obj := & unstructured . Unstructured {
Object : map [ string ] interface { } {
"apiVersion" : "v1" ,
"kind" : "Test" ,
"dummy1" : "present" ,
"dummy2" : "present" ,
"metadata" : map [ string ] interface { } {
"name" : "MyName" ,
"namespace" : "MyNamespace" ,
"creationTimestamp" : "2017-04-01T00:00:00Z" ,
"resourceVersion" : 123 ,
"uid" : "00000000-0000-0000-0000-000000000001" ,
"dummy3" : "present" ,
} ,
"items" : [ ] interface { } {
map [ string ] interface { } {
"itemBool" : true ,
"itemInt" : 42 ,
} ,
} ,
"url" : "http://localhost" ,
"status" : "ok" ,
} ,
}
printUnstructuredContent ( w , LEVEL_0 , obj . UnstructuredContent ( ) , "" , ".dummy1" , ".metadata.dummy3" )
output := out . String ( )
for _ , test := range testCases {
if len ( test . expected ) > 0 {
if ! strings . Contains ( output , test . expected ) {
t . Errorf ( "Expected to find %q in: %q" , test . expected , output )
}
}
if len ( test . unexpected ) > 0 {
if strings . Contains ( output , test . unexpected ) {
t . Errorf ( "Didn't expect to find %q in: %q" , test . unexpected , output )
}
}
}
}