printers_test.go 154 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package internalversion
  14. import (
  15. "math"
  16. "reflect"
  17. "testing"
  18. "time"
  19. "k8s.io/apimachinery/pkg/api/resource"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/runtime/schema"
  22. "k8s.io/apimachinery/pkg/util/diff"
  23. "k8s.io/apimachinery/pkg/util/intstr"
  24. "k8s.io/kubernetes/pkg/apis/apps"
  25. "k8s.io/kubernetes/pkg/apis/autoscaling"
  26. "k8s.io/kubernetes/pkg/apis/batch"
  27. "k8s.io/kubernetes/pkg/apis/certificates"
  28. "k8s.io/kubernetes/pkg/apis/coordination"
  29. api "k8s.io/kubernetes/pkg/apis/core"
  30. "k8s.io/kubernetes/pkg/apis/discovery"
  31. "k8s.io/kubernetes/pkg/apis/flowcontrol"
  32. "k8s.io/kubernetes/pkg/apis/networking"
  33. nodeapi "k8s.io/kubernetes/pkg/apis/node"
  34. "k8s.io/kubernetes/pkg/apis/policy"
  35. "k8s.io/kubernetes/pkg/apis/rbac"
  36. "k8s.io/kubernetes/pkg/apis/scheduling"
  37. "k8s.io/kubernetes/pkg/apis/storage"
  38. "k8s.io/kubernetes/pkg/printers"
  39. utilpointer "k8s.io/utils/pointer"
  40. )
  41. func TestFormatResourceName(t *testing.T) {
  42. tests := []struct {
  43. kind schema.GroupKind
  44. name string
  45. want string
  46. }{
  47. {schema.GroupKind{}, "", ""},
  48. {schema.GroupKind{}, "name", "name"},
  49. {schema.GroupKind{Kind: "Kind"}, "", "kind/"}, // should not happen in practice
  50. {schema.GroupKind{Kind: "Kind"}, "name", "kind/name"},
  51. {schema.GroupKind{Group: "group", Kind: "Kind"}, "name", "kind.group/name"},
  52. }
  53. for _, tt := range tests {
  54. if got := formatResourceName(tt.kind, tt.name, true); got != tt.want {
  55. t.Errorf("formatResourceName(%q, %q) = %q, want %q", tt.kind, tt.name, got, tt.want)
  56. }
  57. }
  58. }
  59. type TestPrintHandler struct {
  60. numCalls int
  61. }
  62. func (t *TestPrintHandler) TableHandler(columnDefinitions []metav1.TableColumnDefinition, printFunc interface{}) error {
  63. t.numCalls++
  64. return nil
  65. }
  66. func (t *TestPrintHandler) getNumCalls() int {
  67. return t.numCalls
  68. }
  69. func TestAllHandlers(t *testing.T) {
  70. h := &TestPrintHandler{numCalls: 0}
  71. AddHandlers(h)
  72. if h.getNumCalls() == 0 {
  73. t.Error("TableHandler not called in AddHandlers")
  74. }
  75. }
  76. func TestPrintEvent(t *testing.T) {
  77. tests := []struct {
  78. event api.Event
  79. options printers.GenerateOptions
  80. expected []metav1.TableRow
  81. }{
  82. // Basic event; no generate options
  83. {
  84. event: api.Event{
  85. Source: api.EventSource{Component: "kubelet"},
  86. InvolvedObject: api.ObjectReference{
  87. Kind: "Pod",
  88. Name: "Pod Name",
  89. FieldPath: "spec.containers{foo}",
  90. },
  91. Reason: "Event Reason",
  92. Message: "Message Data",
  93. FirstTimestamp: metav1.Time{Time: time.Now().UTC().AddDate(0, 0, -3)},
  94. LastTimestamp: metav1.Time{Time: time.Now().UTC().AddDate(0, 0, -2)},
  95. Count: 6,
  96. Type: api.EventTypeNormal,
  97. ObjectMeta: metav1.ObjectMeta{Name: "event1"},
  98. },
  99. options: printers.GenerateOptions{},
  100. // Columns: Last Seen, Type, Reason, Object, Message
  101. expected: []metav1.TableRow{{Cells: []interface{}{"2d", "Normal", "Event Reason", "pod/Pod Name", "Message Data"}}},
  102. },
  103. // Basic event; generate options=Wide
  104. {
  105. event: api.Event{
  106. Source: api.EventSource{
  107. Component: "kubelet",
  108. Host: "Node1",
  109. },
  110. InvolvedObject: api.ObjectReference{
  111. Kind: "Deployment",
  112. Name: "Deployment Name",
  113. FieldPath: "spec.containers{foo}",
  114. },
  115. Reason: "Event Reason",
  116. Message: "Message Data",
  117. FirstTimestamp: metav1.Time{Time: time.Now().UTC().AddDate(0, 0, -3)},
  118. LastTimestamp: metav1.Time{Time: time.Now().UTC().AddDate(0, 0, -2)},
  119. Count: 6,
  120. Type: api.EventTypeWarning,
  121. ObjectMeta: metav1.ObjectMeta{Name: "event2"},
  122. },
  123. options: printers.GenerateOptions{Wide: true},
  124. // Columns: Last Seen, Type, Reason, Object, Subobject, Message, First Seen, Count, Name
  125. expected: []metav1.TableRow{{Cells: []interface{}{"2d", "Warning", "Event Reason", "deployment/Deployment Name", "spec.containers{foo}", "kubelet, Node1", "Message Data", "3d", int64(6), "event2"}}},
  126. },
  127. }
  128. for i, test := range tests {
  129. rows, err := printEvent(&test.event, test.options)
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. for i := range rows {
  134. rows[i].Object.Object = nil
  135. }
  136. if !reflect.DeepEqual(test.expected, rows) {
  137. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  138. }
  139. }
  140. }
  141. func TestPrintEventsResultSorted(t *testing.T) {
  142. eventList := api.EventList{
  143. Items: []api.Event{
  144. {
  145. Source: api.EventSource{Component: "kubelet"},
  146. Message: "Item 1",
  147. FirstTimestamp: metav1.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
  148. LastTimestamp: metav1.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
  149. Count: 1,
  150. Type: api.EventTypeNormal,
  151. },
  152. {
  153. Source: api.EventSource{Component: "scheduler"},
  154. Message: "Item 2",
  155. FirstTimestamp: metav1.NewTime(time.Date(1987, time.June, 17, 0, 0, 0, 0, time.UTC)),
  156. LastTimestamp: metav1.NewTime(time.Date(1987, time.June, 17, 0, 0, 0, 0, time.UTC)),
  157. Count: 1,
  158. Type: api.EventTypeNormal,
  159. },
  160. {
  161. Source: api.EventSource{Component: "kubelet"},
  162. Message: "Item 3",
  163. FirstTimestamp: metav1.NewTime(time.Date(2002, time.December, 25, 0, 0, 0, 0, time.UTC)),
  164. LastTimestamp: metav1.NewTime(time.Date(2002, time.December, 25, 0, 0, 0, 0, time.UTC)),
  165. Count: 1,
  166. Type: api.EventTypeNormal,
  167. },
  168. },
  169. }
  170. rows, err := printEventList(&eventList, printers.GenerateOptions{})
  171. if err != nil {
  172. t.Fatal(err)
  173. }
  174. if len(rows) != 3 {
  175. t.Errorf("Generate Event List: Wrong number of table rows returned. Expected 3, got (%d)", len(rows))
  176. }
  177. // Verify the watch event dates are in order.
  178. firstRow := rows[0]
  179. message1 := firstRow.Cells[4]
  180. if message1.(string) != "Item 1" {
  181. t.Errorf("Wrong event ordering: expecting (Item 1), got (%s)", message1)
  182. }
  183. secondRow := rows[1]
  184. message2 := secondRow.Cells[4]
  185. if message2 != "Item 2" {
  186. t.Errorf("Wrong event ordering: expecting (Item 2), got (%s)", message2)
  187. }
  188. thirdRow := rows[2]
  189. message3 := thirdRow.Cells[4]
  190. if message3 != "Item 3" {
  191. t.Errorf("Wrong event ordering: expecting (Item 3), got (%s)", message3)
  192. }
  193. }
  194. func TestPrintNamespace(t *testing.T) {
  195. tests := []struct {
  196. namespace api.Namespace
  197. expected []metav1.TableRow
  198. }{
  199. // Basic namespace with status and age.
  200. {
  201. namespace: api.Namespace{
  202. ObjectMeta: metav1.ObjectMeta{
  203. Name: "namespace1",
  204. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  205. },
  206. Status: api.NamespaceStatus{
  207. Phase: "FooStatus",
  208. },
  209. },
  210. // Columns: Name, Status, Age
  211. expected: []metav1.TableRow{{Cells: []interface{}{"namespace1", "FooStatus", "0s"}}},
  212. },
  213. // Basic namespace without status or age.
  214. {
  215. namespace: api.Namespace{
  216. ObjectMeta: metav1.ObjectMeta{
  217. Name: "namespace2",
  218. },
  219. },
  220. // Columns: Name, Status, Age
  221. expected: []metav1.TableRow{{Cells: []interface{}{"namespace2", "", "<unknown>"}}},
  222. },
  223. }
  224. for i, test := range tests {
  225. rows, err := printNamespace(&test.namespace, printers.GenerateOptions{})
  226. if err != nil {
  227. t.Fatal(err)
  228. }
  229. for i := range rows {
  230. rows[i].Object.Object = nil
  231. }
  232. if !reflect.DeepEqual(test.expected, rows) {
  233. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  234. }
  235. }
  236. }
  237. func TestPrintSecret(t *testing.T) {
  238. tests := []struct {
  239. secret api.Secret
  240. expected []metav1.TableRow
  241. }{
  242. // Basic namespace with type, data, and age.
  243. {
  244. secret: api.Secret{
  245. ObjectMeta: metav1.ObjectMeta{
  246. Name: "secret1",
  247. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  248. },
  249. Type: "kubernetes.io/service-account-token",
  250. Data: map[string][]byte{
  251. "token": []byte("secret data"),
  252. },
  253. },
  254. // Columns: Name, Type, Data, Age
  255. expected: []metav1.TableRow{{Cells: []interface{}{"secret1", "kubernetes.io/service-account-token", int64(1), "0s"}}},
  256. },
  257. // Basic namespace with type and age; no data.
  258. {
  259. secret: api.Secret{
  260. ObjectMeta: metav1.ObjectMeta{
  261. Name: "secret1",
  262. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  263. },
  264. Type: "kubernetes.io/service-account-token",
  265. },
  266. // Columns: Name, Type, Data, Age
  267. expected: []metav1.TableRow{{Cells: []interface{}{"secret1", "kubernetes.io/service-account-token", int64(0), "0s"}}},
  268. },
  269. }
  270. for i, test := range tests {
  271. rows, err := printSecret(&test.secret, printers.GenerateOptions{})
  272. if err != nil {
  273. t.Fatal(err)
  274. }
  275. for i := range rows {
  276. rows[i].Object.Object = nil
  277. }
  278. if !reflect.DeepEqual(test.expected, rows) {
  279. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  280. }
  281. }
  282. }
  283. func TestPrintServiceAccount(t *testing.T) {
  284. tests := []struct {
  285. serviceAccount api.ServiceAccount
  286. expected []metav1.TableRow
  287. }{
  288. // Basic service account without secrets
  289. {
  290. serviceAccount: api.ServiceAccount{
  291. ObjectMeta: metav1.ObjectMeta{
  292. Name: "sa1",
  293. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  294. },
  295. Secrets: []api.ObjectReference{},
  296. },
  297. // Columns: Name, (Num) Secrets, Age
  298. expected: []metav1.TableRow{{Cells: []interface{}{"sa1", int64(0), "0s"}}},
  299. },
  300. // Basic service account with two secrets.
  301. {
  302. serviceAccount: api.ServiceAccount{
  303. ObjectMeta: metav1.ObjectMeta{
  304. Name: "sa1",
  305. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  306. },
  307. Secrets: []api.ObjectReference{
  308. {Name: "Secret1"},
  309. {Name: "Secret2"},
  310. },
  311. },
  312. // Columns: Name, (Num) Secrets, Age
  313. expected: []metav1.TableRow{{Cells: []interface{}{"sa1", int64(2), "0s"}}},
  314. },
  315. }
  316. for i, test := range tests {
  317. rows, err := printServiceAccount(&test.serviceAccount, printers.GenerateOptions{})
  318. if err != nil {
  319. t.Fatal(err)
  320. }
  321. for i := range rows {
  322. rows[i].Object.Object = nil
  323. }
  324. if !reflect.DeepEqual(test.expected, rows) {
  325. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  326. }
  327. }
  328. }
  329. func TestPrintNodeStatus(t *testing.T) {
  330. table := []struct {
  331. node api.Node
  332. expected []metav1.TableRow
  333. }{
  334. {
  335. node: api.Node{
  336. ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
  337. Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: api.NodeReady, Status: api.ConditionTrue}}},
  338. },
  339. // Columns: Name, Status, Roles, Age, KubeletVersion
  340. expected: []metav1.TableRow{{Cells: []interface{}{"foo1", "Ready", "<none>", "<unknown>", ""}}},
  341. },
  342. {
  343. node: api.Node{
  344. ObjectMeta: metav1.ObjectMeta{Name: "foo2"},
  345. Spec: api.NodeSpec{Unschedulable: true},
  346. Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: api.NodeReady, Status: api.ConditionTrue}}},
  347. },
  348. // Columns: Name, Status, Roles, Age, KubeletVersion
  349. expected: []metav1.TableRow{{Cells: []interface{}{"foo2", "Ready,SchedulingDisabled", "<none>", "<unknown>", ""}}},
  350. },
  351. {
  352. node: api.Node{
  353. ObjectMeta: metav1.ObjectMeta{Name: "foo3"},
  354. Status: api.NodeStatus{Conditions: []api.NodeCondition{
  355. {Type: api.NodeReady, Status: api.ConditionTrue},
  356. {Type: api.NodeReady, Status: api.ConditionTrue}}},
  357. },
  358. // Columns: Name, Status, Roles, Age, KubeletVersion
  359. expected: []metav1.TableRow{{Cells: []interface{}{"foo3", "Ready", "<none>", "<unknown>", ""}}},
  360. },
  361. {
  362. node: api.Node{
  363. ObjectMeta: metav1.ObjectMeta{Name: "foo4"},
  364. Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: api.NodeReady, Status: api.ConditionFalse}}},
  365. },
  366. // Columns: Name, Status, Roles, Age, KubeletVersion
  367. expected: []metav1.TableRow{{Cells: []interface{}{"foo4", "NotReady", "<none>", "<unknown>", ""}}},
  368. },
  369. {
  370. node: api.Node{
  371. ObjectMeta: metav1.ObjectMeta{Name: "foo5"},
  372. Spec: api.NodeSpec{Unschedulable: true},
  373. Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: api.NodeReady, Status: api.ConditionFalse}}},
  374. },
  375. // Columns: Name, Status, Roles, Age, KubeletVersion
  376. expected: []metav1.TableRow{{Cells: []interface{}{"foo5", "NotReady,SchedulingDisabled", "<none>", "<unknown>", ""}}},
  377. },
  378. {
  379. node: api.Node{
  380. ObjectMeta: metav1.ObjectMeta{Name: "foo6"},
  381. Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: "InvalidValue", Status: api.ConditionTrue}}},
  382. },
  383. // Columns: Name, Status, Roles, Age, KubeletVersion
  384. expected: []metav1.TableRow{{Cells: []interface{}{"foo6", "Unknown", "<none>", "<unknown>", ""}}},
  385. },
  386. {
  387. node: api.Node{
  388. ObjectMeta: metav1.ObjectMeta{Name: "foo7"},
  389. Status: api.NodeStatus{Conditions: []api.NodeCondition{{}}},
  390. },
  391. // Columns: Name, Status, Roles, Age, KubeletVersion
  392. expected: []metav1.TableRow{{Cells: []interface{}{"foo7", "Unknown", "<none>", "<unknown>", ""}}},
  393. },
  394. {
  395. node: api.Node{
  396. ObjectMeta: metav1.ObjectMeta{Name: "foo8"},
  397. Spec: api.NodeSpec{Unschedulable: true},
  398. Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: "InvalidValue", Status: api.ConditionTrue}}},
  399. },
  400. // Columns: Name, Status, Roles, Age, KubeletVersion
  401. expected: []metav1.TableRow{{Cells: []interface{}{"foo8", "Unknown,SchedulingDisabled", "<none>", "<unknown>", ""}}},
  402. },
  403. {
  404. node: api.Node{
  405. ObjectMeta: metav1.ObjectMeta{Name: "foo9"},
  406. Spec: api.NodeSpec{Unschedulable: true},
  407. Status: api.NodeStatus{Conditions: []api.NodeCondition{{}}},
  408. },
  409. // Columns: Name, Status, Roles, Age, KubeletVersion
  410. expected: []metav1.TableRow{{Cells: []interface{}{"foo9", "Unknown,SchedulingDisabled", "<none>", "<unknown>", ""}}},
  411. },
  412. }
  413. for i, test := range table {
  414. rows, err := printNode(&test.node, printers.GenerateOptions{})
  415. if err != nil {
  416. t.Fatalf("Error generating table rows for Node: %#v", err)
  417. }
  418. for i := range rows {
  419. rows[i].Object.Object = nil
  420. }
  421. if !reflect.DeepEqual(test.expected, rows) {
  422. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  423. }
  424. }
  425. }
  426. func TestPrintNodeRole(t *testing.T) {
  427. table := []struct {
  428. node api.Node
  429. expected []metav1.TableRow
  430. }{
  431. {
  432. node: api.Node{
  433. ObjectMeta: metav1.ObjectMeta{Name: "foo9"},
  434. },
  435. // Columns: Name, Status, Roles, Age, KubeletVersion
  436. expected: []metav1.TableRow{{Cells: []interface{}{"foo9", "Unknown", "<none>", "<unknown>", ""}}},
  437. },
  438. {
  439. node: api.Node{
  440. ObjectMeta: metav1.ObjectMeta{
  441. Name: "foo10",
  442. Labels: map[string]string{"node-role.kubernetes.io/master": "", "node-role.kubernetes.io/proxy": "", "kubernetes.io/role": "node"},
  443. },
  444. },
  445. // Columns: Name, Status, Roles, Age, KubeletVersion
  446. expected: []metav1.TableRow{{Cells: []interface{}{"foo10", "Unknown", "master,node,proxy", "<unknown>", ""}}},
  447. },
  448. {
  449. node: api.Node{
  450. ObjectMeta: metav1.ObjectMeta{
  451. Name: "foo11",
  452. Labels: map[string]string{"kubernetes.io/role": "node"},
  453. },
  454. },
  455. // Columns: Name, Status, Roles, Age, KubeletVersion
  456. expected: []metav1.TableRow{{Cells: []interface{}{"foo11", "Unknown", "node", "<unknown>", ""}}},
  457. },
  458. }
  459. for i, test := range table {
  460. rows, err := printNode(&test.node, printers.GenerateOptions{})
  461. if err != nil {
  462. t.Fatalf("An error occurred generating table rows for Node: %#v", err)
  463. }
  464. for i := range rows {
  465. rows[i].Object.Object = nil
  466. }
  467. if !reflect.DeepEqual(test.expected, rows) {
  468. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  469. }
  470. }
  471. }
  472. func TestPrintNodeOSImage(t *testing.T) {
  473. table := []struct {
  474. node api.Node
  475. expected []metav1.TableRow
  476. }{
  477. {
  478. node: api.Node{
  479. ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
  480. Status: api.NodeStatus{
  481. NodeInfo: api.NodeSystemInfo{OSImage: "fake-os-image"},
  482. Addresses: []api.NodeAddress{{Type: api.NodeExternalIP, Address: "1.1.1.1"}},
  483. },
  484. },
  485. // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
  486. expected: []metav1.TableRow{
  487. {
  488. Cells: []interface{}{"foo1", "Unknown", "<none>", "<unknown>", "", "<none>", "1.1.1.1", "fake-os-image", "<unknown>", "<unknown>"},
  489. },
  490. },
  491. },
  492. {
  493. node: api.Node{
  494. ObjectMeta: metav1.ObjectMeta{Name: "foo2"},
  495. Status: api.NodeStatus{
  496. NodeInfo: api.NodeSystemInfo{KernelVersion: "fake-kernel-version"},
  497. Addresses: []api.NodeAddress{{Type: api.NodeExternalIP, Address: "1.1.1.1"}},
  498. },
  499. },
  500. // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
  501. expected: []metav1.TableRow{
  502. {
  503. Cells: []interface{}{"foo2", "Unknown", "<none>", "<unknown>", "", "<none>", "1.1.1.1", "<unknown>", "fake-kernel-version", "<unknown>"},
  504. },
  505. },
  506. },
  507. }
  508. for i, test := range table {
  509. rows, err := printNode(&test.node, printers.GenerateOptions{Wide: true})
  510. if err != nil {
  511. t.Fatalf("An error occurred generating table for Node: %#v", err)
  512. }
  513. for i := range rows {
  514. rows[i].Object.Object = nil
  515. }
  516. if !reflect.DeepEqual(test.expected, rows) {
  517. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  518. }
  519. }
  520. }
  521. func TestPrintNodeKernelVersion(t *testing.T) {
  522. table := []struct {
  523. node api.Node
  524. expected []metav1.TableRow
  525. }{
  526. {
  527. node: api.Node{
  528. ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
  529. Status: api.NodeStatus{
  530. NodeInfo: api.NodeSystemInfo{KernelVersion: "fake-kernel-version"},
  531. Addresses: []api.NodeAddress{{Type: api.NodeExternalIP, Address: "1.1.1.1"}},
  532. },
  533. },
  534. // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
  535. expected: []metav1.TableRow{
  536. {
  537. Cells: []interface{}{"foo1", "Unknown", "<none>", "<unknown>", "", "<none>", "1.1.1.1", "<unknown>", "fake-kernel-version", "<unknown>"},
  538. },
  539. },
  540. },
  541. {
  542. node: api.Node{
  543. ObjectMeta: metav1.ObjectMeta{Name: "foo2"},
  544. Status: api.NodeStatus{
  545. NodeInfo: api.NodeSystemInfo{OSImage: "fake-os-image"},
  546. Addresses: []api.NodeAddress{{Type: api.NodeExternalIP, Address: "1.1.1.1"}},
  547. },
  548. },
  549. // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
  550. expected: []metav1.TableRow{
  551. {
  552. Cells: []interface{}{"foo2", "Unknown", "<none>", "<unknown>", "", "<none>", "1.1.1.1", "fake-os-image", "<unknown>", "<unknown>"},
  553. },
  554. },
  555. },
  556. }
  557. for i, test := range table {
  558. rows, err := printNode(&test.node, printers.GenerateOptions{Wide: true})
  559. if err != nil {
  560. t.Fatalf("An error occurred generating table rows Node: %#v", err)
  561. }
  562. for i := range rows {
  563. rows[i].Object.Object = nil
  564. }
  565. if !reflect.DeepEqual(test.expected, rows) {
  566. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  567. }
  568. }
  569. }
  570. func TestPrintNodeContainerRuntimeVersion(t *testing.T) {
  571. table := []struct {
  572. node api.Node
  573. expected []metav1.TableRow
  574. }{
  575. {
  576. node: api.Node{
  577. ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
  578. Status: api.NodeStatus{
  579. NodeInfo: api.NodeSystemInfo{ContainerRuntimeVersion: "foo://1.2.3"},
  580. Addresses: []api.NodeAddress{{Type: api.NodeExternalIP, Address: "1.1.1.1"}},
  581. },
  582. },
  583. // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
  584. expected: []metav1.TableRow{
  585. {
  586. Cells: []interface{}{"foo1", "Unknown", "<none>", "<unknown>", "", "<none>", "1.1.1.1", "<unknown>", "<unknown>", "foo://1.2.3"},
  587. },
  588. },
  589. },
  590. {
  591. node: api.Node{
  592. ObjectMeta: metav1.ObjectMeta{Name: "foo2"},
  593. Status: api.NodeStatus{
  594. NodeInfo: api.NodeSystemInfo{},
  595. Addresses: []api.NodeAddress{{Type: api.NodeExternalIP, Address: "1.1.1.1"}},
  596. },
  597. },
  598. // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
  599. expected: []metav1.TableRow{
  600. {
  601. Cells: []interface{}{"foo2", "Unknown", "<none>", "<unknown>", "", "<none>", "1.1.1.1", "<unknown>", "<unknown>", "<unknown>"},
  602. },
  603. },
  604. },
  605. }
  606. for i, test := range table {
  607. rows, err := printNode(&test.node, printers.GenerateOptions{Wide: true})
  608. if err != nil {
  609. t.Fatalf("An error occurred generating table rows Node: %#v", err)
  610. }
  611. for i := range rows {
  612. rows[i].Object.Object = nil
  613. }
  614. if !reflect.DeepEqual(test.expected, rows) {
  615. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  616. }
  617. }
  618. }
  619. func TestPrintNodeName(t *testing.T) {
  620. table := []struct {
  621. node api.Node
  622. expected []metav1.TableRow
  623. }{
  624. {
  625. node: api.Node{
  626. ObjectMeta: metav1.ObjectMeta{Name: "127.0.0.1"},
  627. Status: api.NodeStatus{},
  628. },
  629. // Columns: Name, Status, Roles, Age, KubeletVersion
  630. expected: []metav1.TableRow{{Cells: []interface{}{"127.0.0.1", "Unknown", "<none>", "<unknown>", ""}}}},
  631. {
  632. node: api.Node{
  633. ObjectMeta: metav1.ObjectMeta{Name: ""},
  634. Status: api.NodeStatus{},
  635. },
  636. // Columns: Name, Status, Roles, Age, KubeletVersion
  637. expected: []metav1.TableRow{{Cells: []interface{}{"", "Unknown", "<none>", "<unknown>", ""}}},
  638. },
  639. }
  640. for i, test := range table {
  641. rows, err := printNode(&test.node, printers.GenerateOptions{})
  642. if err != nil {
  643. t.Fatalf("An error occurred generating table rows Node: %#v", err)
  644. }
  645. for i := range rows {
  646. rows[i].Object.Object = nil
  647. }
  648. if !reflect.DeepEqual(test.expected, rows) {
  649. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  650. }
  651. }
  652. }
  653. func TestPrintNodeExternalIP(t *testing.T) {
  654. table := []struct {
  655. node api.Node
  656. expected []metav1.TableRow
  657. }{
  658. {
  659. node: api.Node{
  660. ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
  661. Status: api.NodeStatus{Addresses: []api.NodeAddress{{Type: api.NodeExternalIP, Address: "1.1.1.1"}}},
  662. },
  663. // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
  664. expected: []metav1.TableRow{
  665. {
  666. Cells: []interface{}{"foo1", "Unknown", "<none>", "<unknown>", "", "<none>", "1.1.1.1", "<unknown>", "<unknown>", "<unknown>"},
  667. },
  668. },
  669. },
  670. {
  671. node: api.Node{
  672. ObjectMeta: metav1.ObjectMeta{Name: "foo2"},
  673. Status: api.NodeStatus{Addresses: []api.NodeAddress{{Type: api.NodeInternalIP, Address: "1.1.1.1"}}},
  674. },
  675. // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
  676. expected: []metav1.TableRow{
  677. {
  678. Cells: []interface{}{"foo2", "Unknown", "<none>", "<unknown>", "", "1.1.1.1", "<none>", "<unknown>", "<unknown>", "<unknown>"},
  679. },
  680. },
  681. },
  682. {
  683. node: api.Node{
  684. ObjectMeta: metav1.ObjectMeta{Name: "foo3"},
  685. Status: api.NodeStatus{Addresses: []api.NodeAddress{
  686. {Type: api.NodeExternalIP, Address: "2.2.2.2"},
  687. {Type: api.NodeInternalIP, Address: "3.3.3.3"},
  688. {Type: api.NodeExternalIP, Address: "4.4.4.4"},
  689. }},
  690. },
  691. expected: []metav1.TableRow{
  692. {
  693. Cells: []interface{}{"foo3", "Unknown", "<none>", "<unknown>", "", "3.3.3.3", "2.2.2.2", "<unknown>", "<unknown>", "<unknown>"},
  694. },
  695. },
  696. },
  697. }
  698. for i, test := range table {
  699. rows, err := printNode(&test.node, printers.GenerateOptions{Wide: true})
  700. if err != nil {
  701. t.Fatalf("An error occurred generating table rows Node: %#v", err)
  702. }
  703. for i := range rows {
  704. rows[i].Object.Object = nil
  705. }
  706. if !reflect.DeepEqual(test.expected, rows) {
  707. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  708. }
  709. }
  710. }
  711. func TestPrintNodeInternalIP(t *testing.T) {
  712. table := []struct {
  713. node api.Node
  714. expected []metav1.TableRow
  715. }{
  716. {
  717. node: api.Node{
  718. ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
  719. Status: api.NodeStatus{Addresses: []api.NodeAddress{{Type: api.NodeInternalIP, Address: "1.1.1.1"}}},
  720. },
  721. // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
  722. expected: []metav1.TableRow{
  723. {
  724. Cells: []interface{}{"foo1", "Unknown", "<none>", "<unknown>", "", "1.1.1.1", "<none>", "<unknown>", "<unknown>", "<unknown>"},
  725. },
  726. },
  727. },
  728. {
  729. node: api.Node{
  730. ObjectMeta: metav1.ObjectMeta{Name: "foo2"},
  731. Status: api.NodeStatus{Addresses: []api.NodeAddress{{Type: api.NodeExternalIP, Address: "1.1.1.1"}}},
  732. },
  733. // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
  734. expected: []metav1.TableRow{
  735. {
  736. Cells: []interface{}{"foo2", "Unknown", "<none>", "<unknown>", "", "<none>", "1.1.1.1", "<unknown>", "<unknown>", "<unknown>"},
  737. },
  738. },
  739. },
  740. {
  741. node: api.Node{
  742. ObjectMeta: metav1.ObjectMeta{Name: "foo3"},
  743. Status: api.NodeStatus{Addresses: []api.NodeAddress{
  744. {Type: api.NodeInternalIP, Address: "2.2.2.2"},
  745. {Type: api.NodeExternalIP, Address: "3.3.3.3"},
  746. {Type: api.NodeInternalIP, Address: "4.4.4.4"},
  747. }},
  748. },
  749. // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
  750. expected: []metav1.TableRow{
  751. {
  752. Cells: []interface{}{"foo3", "Unknown", "<none>", "<unknown>", "", "2.2.2.2", "3.3.3.3", "<unknown>", "<unknown>", "<unknown>"},
  753. },
  754. },
  755. },
  756. }
  757. for i, test := range table {
  758. rows, err := printNode(&test.node, printers.GenerateOptions{Wide: true})
  759. if err != nil {
  760. t.Fatalf("An error occurred generating table rows Node: %#v", err)
  761. }
  762. for i := range rows {
  763. rows[i].Object.Object = nil
  764. }
  765. if !reflect.DeepEqual(test.expected, rows) {
  766. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  767. }
  768. }
  769. }
  770. func TestPrintIngress(t *testing.T) {
  771. ingress := networking.Ingress{
  772. ObjectMeta: metav1.ObjectMeta{
  773. Name: "test1",
  774. CreationTimestamp: metav1.Time{Time: time.Now().AddDate(-10, 0, 0)},
  775. },
  776. Spec: networking.IngressSpec{
  777. Backend: &networking.IngressBackend{
  778. ServiceName: "svc",
  779. ServicePort: intstr.FromInt(93),
  780. },
  781. },
  782. Status: networking.IngressStatus{
  783. LoadBalancer: api.LoadBalancerStatus{
  784. Ingress: []api.LoadBalancerIngress{
  785. {
  786. IP: "2.3.4.5",
  787. Hostname: "localhost.localdomain",
  788. },
  789. },
  790. },
  791. },
  792. }
  793. // Columns: Name, Hosts, Address, Ports, Age
  794. expected := []metav1.TableRow{{Cells: []interface{}{"test1", "*", "2.3.4.5", "80", "10y"}}}
  795. rows, err := printIngress(&ingress, printers.GenerateOptions{})
  796. if err != nil {
  797. t.Fatalf("Error generating table rows for Ingress: %#v", err)
  798. }
  799. rows[0].Object.Object = nil
  800. if !reflect.DeepEqual(expected, rows) {
  801. t.Errorf("mismatch: %s", diff.ObjectReflectDiff(expected, rows))
  802. }
  803. }
  804. func TestPrintServiceLoadBalancer(t *testing.T) {
  805. tests := []struct {
  806. service api.Service
  807. options printers.GenerateOptions
  808. expected []metav1.TableRow
  809. }{
  810. // Test load balancer service with multiple external IP's
  811. {
  812. service: api.Service{
  813. ObjectMeta: metav1.ObjectMeta{Name: "service1"},
  814. Spec: api.ServiceSpec{
  815. ClusterIP: "1.2.3.4",
  816. Type: "LoadBalancer",
  817. Ports: []api.ServicePort{{Port: 80, Protocol: "TCP"}},
  818. },
  819. Status: api.ServiceStatus{
  820. LoadBalancer: api.LoadBalancerStatus{
  821. Ingress: []api.LoadBalancerIngress{{IP: "2.3.4.5"}, {IP: "3.4.5.6"}}},
  822. },
  823. },
  824. options: printers.GenerateOptions{},
  825. // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
  826. expected: []metav1.TableRow{{Cells: []interface{}{"service1", "LoadBalancer", "1.2.3.4", "2.3.4.5,3.4.5.6", "80/TCP", "<unknown>"}}},
  827. },
  828. // Test load balancer service with pending external IP.
  829. {
  830. service: api.Service{
  831. ObjectMeta: metav1.ObjectMeta{Name: "service2"},
  832. Spec: api.ServiceSpec{
  833. ClusterIP: "1.3.4.5",
  834. Type: "LoadBalancer",
  835. Ports: []api.ServicePort{{Port: 80, Protocol: "TCP"}, {Port: 8090, Protocol: "UDP"}, {Port: 8000, Protocol: "TCP"}, {Port: 7777, Protocol: "SCTP"}},
  836. },
  837. },
  838. options: printers.GenerateOptions{},
  839. // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
  840. expected: []metav1.TableRow{{Cells: []interface{}{"service2", "LoadBalancer", "1.3.4.5", "<pending>", "80/TCP,8090/UDP,8000/TCP,7777/SCTP", "<unknown>"}}},
  841. },
  842. // Test load balancer service with multiple ports.
  843. {
  844. service: api.Service{
  845. ObjectMeta: metav1.ObjectMeta{Name: "service3"},
  846. Spec: api.ServiceSpec{
  847. ClusterIP: "1.4.5.6",
  848. Type: "LoadBalancer",
  849. Ports: []api.ServicePort{{Port: 80, Protocol: "TCP"}, {Port: 8090, Protocol: "UDP"}, {Port: 8000, Protocol: "TCP"}},
  850. },
  851. Status: api.ServiceStatus{
  852. LoadBalancer: api.LoadBalancerStatus{
  853. Ingress: []api.LoadBalancerIngress{{IP: "2.3.4.5"}}},
  854. },
  855. },
  856. options: printers.GenerateOptions{},
  857. // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
  858. expected: []metav1.TableRow{{Cells: []interface{}{"service3", "LoadBalancer", "1.4.5.6", "2.3.4.5", "80/TCP,8090/UDP,8000/TCP", "<unknown>"}}},
  859. },
  860. // Long external IP's list gets elided.
  861. {
  862. service: api.Service{
  863. ObjectMeta: metav1.ObjectMeta{Name: "service4"},
  864. Spec: api.ServiceSpec{
  865. ClusterIP: "1.5.6.7",
  866. Type: "LoadBalancer",
  867. Ports: []api.ServicePort{{Port: 80, Protocol: "TCP"}, {Port: 8090, Protocol: "UDP"}, {Port: 8000, Protocol: "TCP"}},
  868. },
  869. Status: api.ServiceStatus{
  870. LoadBalancer: api.LoadBalancerStatus{
  871. Ingress: []api.LoadBalancerIngress{{IP: "2.3.4.5"}, {IP: "3.4.5.6"}, {IP: "5.6.7.8", Hostname: "host5678"}}},
  872. },
  873. },
  874. options: printers.GenerateOptions{},
  875. // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
  876. expected: []metav1.TableRow{{Cells: []interface{}{"service4", "LoadBalancer", "1.5.6.7", "2.3.4.5,3.4.5...", "80/TCP,8090/UDP,8000/TCP", "<unknown>"}}},
  877. },
  878. // Generate options: Wide, includes selectors.
  879. {
  880. service: api.Service{
  881. ObjectMeta: metav1.ObjectMeta{Name: "service4"},
  882. Spec: api.ServiceSpec{
  883. ClusterIP: "1.5.6.7",
  884. Type: "LoadBalancer",
  885. Ports: []api.ServicePort{{Port: 80, Protocol: "TCP"}, {Port: 8090, Protocol: "UDP"}, {Port: 8000, Protocol: "TCP"}},
  886. Selector: map[string]string{"foo": "bar"},
  887. },
  888. Status: api.ServiceStatus{
  889. LoadBalancer: api.LoadBalancerStatus{
  890. Ingress: []api.LoadBalancerIngress{{IP: "2.3.4.5"}, {IP: "3.4.5.6"}, {IP: "5.6.7.8", Hostname: "host5678"}}},
  891. },
  892. },
  893. options: printers.GenerateOptions{Wide: true},
  894. // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
  895. expected: []metav1.TableRow{{Cells: []interface{}{"service4", "LoadBalancer", "1.5.6.7", "2.3.4.5,3.4.5.6,5.6.7.8", "80/TCP,8090/UDP,8000/TCP", "<unknown>", "foo=bar"}}},
  896. },
  897. }
  898. for i, test := range tests {
  899. rows, err := printService(&test.service, test.options)
  900. if err != nil {
  901. t.Fatalf("Error printing table rows for Service: %#v", err)
  902. }
  903. for i := range rows {
  904. rows[i].Object.Object = nil
  905. }
  906. if !reflect.DeepEqual(test.expected, rows) {
  907. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  908. }
  909. }
  910. }
  911. func TestPrintPod(t *testing.T) {
  912. tests := []struct {
  913. pod api.Pod
  914. expect []metav1.TableRow
  915. }{
  916. {
  917. // Test name, num of containers, restarts, container ready status
  918. api.Pod{
  919. ObjectMeta: metav1.ObjectMeta{Name: "test1"},
  920. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  921. Status: api.PodStatus{
  922. Phase: "podPhase",
  923. ContainerStatuses: []api.ContainerStatus{
  924. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  925. {RestartCount: 3},
  926. },
  927. },
  928. },
  929. []metav1.TableRow{{Cells: []interface{}{"test1", "1/2", "podPhase", int64(6), "<unknown>"}}},
  930. },
  931. {
  932. // Test container error overwrites pod phase
  933. api.Pod{
  934. ObjectMeta: metav1.ObjectMeta{Name: "test2"},
  935. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  936. Status: api.PodStatus{
  937. Phase: "podPhase",
  938. ContainerStatuses: []api.ContainerStatus{
  939. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  940. {State: api.ContainerState{Waiting: &api.ContainerStateWaiting{Reason: "ContainerWaitingReason"}}, RestartCount: 3},
  941. },
  942. },
  943. },
  944. []metav1.TableRow{{Cells: []interface{}{"test2", "1/2", "ContainerWaitingReason", int64(6), "<unknown>"}}},
  945. },
  946. {
  947. // Test the same as the above but with Terminated state and the first container overwrites the rest
  948. api.Pod{
  949. ObjectMeta: metav1.ObjectMeta{Name: "test3"},
  950. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  951. Status: api.PodStatus{
  952. Phase: "podPhase",
  953. ContainerStatuses: []api.ContainerStatus{
  954. {State: api.ContainerState{Waiting: &api.ContainerStateWaiting{Reason: "ContainerWaitingReason"}}, RestartCount: 3},
  955. {State: api.ContainerState{Terminated: &api.ContainerStateTerminated{Reason: "ContainerTerminatedReason"}}, RestartCount: 3},
  956. },
  957. },
  958. },
  959. []metav1.TableRow{{Cells: []interface{}{"test3", "0/2", "ContainerWaitingReason", int64(6), "<unknown>"}}},
  960. },
  961. {
  962. // Test ready is not enough for reporting running
  963. api.Pod{
  964. ObjectMeta: metav1.ObjectMeta{Name: "test4"},
  965. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  966. Status: api.PodStatus{
  967. Phase: "podPhase",
  968. ContainerStatuses: []api.ContainerStatus{
  969. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  970. {Ready: true, RestartCount: 3},
  971. },
  972. },
  973. },
  974. []metav1.TableRow{{Cells: []interface{}{"test4", "1/2", "podPhase", int64(6), "<unknown>"}}},
  975. },
  976. {
  977. // Test ready is not enough for reporting running
  978. api.Pod{
  979. ObjectMeta: metav1.ObjectMeta{Name: "test5"},
  980. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  981. Status: api.PodStatus{
  982. Reason: "podReason",
  983. Phase: "podPhase",
  984. ContainerStatuses: []api.ContainerStatus{
  985. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  986. {Ready: true, RestartCount: 3},
  987. },
  988. },
  989. },
  990. []metav1.TableRow{{Cells: []interface{}{"test5", "1/2", "podReason", int64(6), "<unknown>"}}},
  991. },
  992. {
  993. // Test pod has 2 containers, one is running and the other is completed.
  994. api.Pod{
  995. ObjectMeta: metav1.ObjectMeta{Name: "test6"},
  996. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  997. Status: api.PodStatus{
  998. Phase: "Running",
  999. Reason: "",
  1000. ContainerStatuses: []api.ContainerStatus{
  1001. {Ready: true, RestartCount: 3, State: api.ContainerState{Terminated: &api.ContainerStateTerminated{Reason: "Completed", ExitCode: 0}}},
  1002. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1003. },
  1004. },
  1005. },
  1006. []metav1.TableRow{{Cells: []interface{}{"test6", "1/2", "Running", int64(6), "<unknown>"}}},
  1007. },
  1008. }
  1009. for i, test := range tests {
  1010. rows, err := printPod(&test.pod, printers.GenerateOptions{})
  1011. if err != nil {
  1012. t.Fatal(err)
  1013. }
  1014. for i := range rows {
  1015. rows[i].Object.Object = nil
  1016. }
  1017. if !reflect.DeepEqual(test.expect, rows) {
  1018. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expect, rows))
  1019. }
  1020. }
  1021. }
  1022. func TestPrintPodwide(t *testing.T) {
  1023. condition1 := "condition1"
  1024. condition2 := "condition2"
  1025. condition3 := "condition3"
  1026. tests := []struct {
  1027. pod api.Pod
  1028. expect []metav1.TableRow
  1029. }{
  1030. {
  1031. // Test when the NodeName and PodIP are not none
  1032. api.Pod{
  1033. ObjectMeta: metav1.ObjectMeta{Name: "test1"},
  1034. Spec: api.PodSpec{
  1035. Containers: make([]api.Container, 2),
  1036. NodeName: "test1",
  1037. ReadinessGates: []api.PodReadinessGate{
  1038. {
  1039. ConditionType: api.PodConditionType(condition1),
  1040. },
  1041. {
  1042. ConditionType: api.PodConditionType(condition2),
  1043. },
  1044. {
  1045. ConditionType: api.PodConditionType(condition3),
  1046. },
  1047. },
  1048. },
  1049. Status: api.PodStatus{
  1050. Conditions: []api.PodCondition{
  1051. {
  1052. Type: api.PodConditionType(condition1),
  1053. Status: api.ConditionFalse,
  1054. },
  1055. {
  1056. Type: api.PodConditionType(condition2),
  1057. Status: api.ConditionTrue,
  1058. },
  1059. },
  1060. Phase: "podPhase",
  1061. PodIPs: []api.PodIP{{IP: "1.1.1.1"}},
  1062. ContainerStatuses: []api.ContainerStatus{
  1063. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1064. {RestartCount: 3},
  1065. },
  1066. NominatedNodeName: "node1",
  1067. },
  1068. },
  1069. []metav1.TableRow{{Cells: []interface{}{"test1", "1/2", "podPhase", int64(6), "<unknown>", "1.1.1.1", "test1", "node1", "1/3"}}},
  1070. },
  1071. {
  1072. // Test when the NodeName and PodIP are not none
  1073. api.Pod{
  1074. ObjectMeta: metav1.ObjectMeta{Name: "test1"},
  1075. Spec: api.PodSpec{
  1076. Containers: make([]api.Container, 2),
  1077. NodeName: "test1",
  1078. ReadinessGates: []api.PodReadinessGate{
  1079. {
  1080. ConditionType: api.PodConditionType(condition1),
  1081. },
  1082. {
  1083. ConditionType: api.PodConditionType(condition2),
  1084. },
  1085. {
  1086. ConditionType: api.PodConditionType(condition3),
  1087. },
  1088. },
  1089. },
  1090. Status: api.PodStatus{
  1091. Conditions: []api.PodCondition{
  1092. {
  1093. Type: api.PodConditionType(condition1),
  1094. Status: api.ConditionFalse,
  1095. },
  1096. {
  1097. Type: api.PodConditionType(condition2),
  1098. Status: api.ConditionTrue,
  1099. },
  1100. },
  1101. Phase: "podPhase",
  1102. PodIPs: []api.PodIP{{IP: "1.1.1.1"}, {IP: "2001:db8::"}},
  1103. ContainerStatuses: []api.ContainerStatus{
  1104. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1105. {RestartCount: 3},
  1106. },
  1107. NominatedNodeName: "node1",
  1108. },
  1109. },
  1110. []metav1.TableRow{{Cells: []interface{}{"test1", "1/2", "podPhase", int64(6), "<unknown>", "1.1.1.1", "test1", "node1", "1/3"}}},
  1111. },
  1112. {
  1113. // Test when the NodeName and PodIP are none
  1114. api.Pod{
  1115. ObjectMeta: metav1.ObjectMeta{Name: "test2"},
  1116. Spec: api.PodSpec{
  1117. Containers: make([]api.Container, 2),
  1118. NodeName: "",
  1119. },
  1120. Status: api.PodStatus{
  1121. Phase: "podPhase",
  1122. ContainerStatuses: []api.ContainerStatus{
  1123. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1124. {State: api.ContainerState{Waiting: &api.ContainerStateWaiting{Reason: "ContainerWaitingReason"}}, RestartCount: 3},
  1125. },
  1126. },
  1127. },
  1128. []metav1.TableRow{{Cells: []interface{}{"test2", "1/2", "ContainerWaitingReason", int64(6), "<unknown>", "<none>", "<none>", "<none>", "<none>"}}},
  1129. },
  1130. }
  1131. for i, test := range tests {
  1132. rows, err := printPod(&test.pod, printers.GenerateOptions{Wide: true})
  1133. if err != nil {
  1134. t.Fatal(err)
  1135. }
  1136. for i := range rows {
  1137. rows[i].Object.Object = nil
  1138. }
  1139. if !reflect.DeepEqual(test.expect, rows) {
  1140. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expect, rows))
  1141. }
  1142. }
  1143. }
  1144. func TestPrintPodConditions(t *testing.T) {
  1145. runningPod := &api.Pod{
  1146. ObjectMeta: metav1.ObjectMeta{Name: "test1", Labels: map[string]string{"a": "1", "b": "2"}},
  1147. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1148. Status: api.PodStatus{
  1149. Phase: "Running",
  1150. ContainerStatuses: []api.ContainerStatus{
  1151. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1152. {RestartCount: 3},
  1153. },
  1154. },
  1155. }
  1156. succeededPod := &api.Pod{
  1157. ObjectMeta: metav1.ObjectMeta{Name: "test1", Labels: map[string]string{"a": "1", "b": "2"}},
  1158. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1159. Status: api.PodStatus{
  1160. Phase: "Succeeded",
  1161. ContainerStatuses: []api.ContainerStatus{
  1162. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1163. {RestartCount: 3},
  1164. },
  1165. },
  1166. }
  1167. failedPod := &api.Pod{
  1168. ObjectMeta: metav1.ObjectMeta{Name: "test2", Labels: map[string]string{"b": "2"}},
  1169. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1170. Status: api.PodStatus{
  1171. Phase: "Failed",
  1172. ContainerStatuses: []api.ContainerStatus{
  1173. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1174. {RestartCount: 3},
  1175. },
  1176. },
  1177. }
  1178. tests := []struct {
  1179. pod *api.Pod
  1180. expect []metav1.TableRow
  1181. }{
  1182. // Should not have TableRowCondition
  1183. {
  1184. pod: runningPod,
  1185. // Columns: Name, Ready, Reason, Restarts, Age
  1186. expect: []metav1.TableRow{{Cells: []interface{}{"test1", "1/2", "Running", int64(6), "<unknown>"}}},
  1187. },
  1188. // Should have TableRowCondition: podSuccessConditions
  1189. {
  1190. pod: succeededPod,
  1191. expect: []metav1.TableRow{
  1192. {
  1193. // Columns: Name, Ready, Reason, Restarts, Age
  1194. Cells: []interface{}{"test1", "1/2", "Succeeded", int64(6), "<unknown>"},
  1195. Conditions: podSuccessConditions,
  1196. },
  1197. },
  1198. },
  1199. // Should have TableRowCondition: podFailedCondition
  1200. {
  1201. pod: failedPod,
  1202. expect: []metav1.TableRow{
  1203. {
  1204. // Columns: Name, Ready, Reason, Restarts, Age
  1205. Cells: []interface{}{"test2", "1/2", "Failed", int64(6), "<unknown>"},
  1206. Conditions: podFailedConditions,
  1207. },
  1208. },
  1209. },
  1210. }
  1211. for i, test := range tests {
  1212. rows, err := printPod(test.pod, printers.GenerateOptions{})
  1213. if err != nil {
  1214. t.Fatal(err)
  1215. }
  1216. for i := range rows {
  1217. rows[i].Object.Object = nil
  1218. }
  1219. if !reflect.DeepEqual(test.expect, rows) {
  1220. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expect, rows))
  1221. }
  1222. }
  1223. }
  1224. func TestPrintPodList(t *testing.T) {
  1225. tests := []struct {
  1226. pods api.PodList
  1227. expect []metav1.TableRow
  1228. }{
  1229. // Test podList's pod: name, num of containers, restarts, container ready status
  1230. {
  1231. api.PodList{
  1232. Items: []api.Pod{
  1233. {
  1234. ObjectMeta: metav1.ObjectMeta{Name: "test1"},
  1235. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1236. Status: api.PodStatus{
  1237. Phase: "podPhase",
  1238. ContainerStatuses: []api.ContainerStatus{
  1239. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1240. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1241. },
  1242. },
  1243. },
  1244. {
  1245. ObjectMeta: metav1.ObjectMeta{Name: "test2"},
  1246. Spec: api.PodSpec{Containers: make([]api.Container, 1)},
  1247. Status: api.PodStatus{
  1248. Phase: "podPhase",
  1249. ContainerStatuses: []api.ContainerStatus{
  1250. {Ready: true, RestartCount: 1, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1251. },
  1252. },
  1253. },
  1254. },
  1255. },
  1256. []metav1.TableRow{{Cells: []interface{}{"test1", "2/2", "podPhase", int64(6), "<unknown>"}}, {Cells: []interface{}{"test2", "1/1", "podPhase", int64(1), "<unknown>"}}},
  1257. },
  1258. }
  1259. for _, test := range tests {
  1260. rows, err := printPodList(&test.pods, printers.GenerateOptions{})
  1261. if err != nil {
  1262. t.Fatal(err)
  1263. }
  1264. for i := range rows {
  1265. rows[i].Object.Object = nil
  1266. }
  1267. if !reflect.DeepEqual(test.expect, rows) {
  1268. t.Errorf("mismatch: %s", diff.ObjectReflectDiff(test.expect, rows))
  1269. }
  1270. }
  1271. }
  1272. func TestPrintNonTerminatedPod(t *testing.T) {
  1273. tests := []struct {
  1274. pod api.Pod
  1275. expect []metav1.TableRow
  1276. }{
  1277. {
  1278. // Test pod phase Running should be printed
  1279. api.Pod{
  1280. ObjectMeta: metav1.ObjectMeta{Name: "test1"},
  1281. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1282. Status: api.PodStatus{
  1283. Phase: api.PodRunning,
  1284. ContainerStatuses: []api.ContainerStatus{
  1285. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1286. {RestartCount: 3},
  1287. },
  1288. },
  1289. },
  1290. // Columns: Name, Ready, Reason, Restarts, Age
  1291. []metav1.TableRow{{Cells: []interface{}{"test1", "1/2", "Running", int64(6), "<unknown>"}}},
  1292. },
  1293. {
  1294. // Test pod phase Pending should be printed
  1295. api.Pod{
  1296. ObjectMeta: metav1.ObjectMeta{Name: "test2"},
  1297. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1298. Status: api.PodStatus{
  1299. Phase: api.PodPending,
  1300. ContainerStatuses: []api.ContainerStatus{
  1301. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1302. {RestartCount: 3},
  1303. },
  1304. },
  1305. },
  1306. // Columns: Name, Ready, Reason, Restarts, Age
  1307. []metav1.TableRow{{Cells: []interface{}{"test2", "1/2", "Pending", int64(6), "<unknown>"}}},
  1308. },
  1309. {
  1310. // Test pod phase Unknown should be printed
  1311. api.Pod{
  1312. ObjectMeta: metav1.ObjectMeta{Name: "test3"},
  1313. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1314. Status: api.PodStatus{
  1315. Phase: api.PodUnknown,
  1316. ContainerStatuses: []api.ContainerStatus{
  1317. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1318. {RestartCount: 3},
  1319. },
  1320. },
  1321. },
  1322. // Columns: Name, Ready, Reason, Restarts, Age
  1323. []metav1.TableRow{{Cells: []interface{}{"test3", "1/2", "Unknown", int64(6), "<unknown>"}}},
  1324. },
  1325. {
  1326. // Test pod phase Succeeded should be printed
  1327. api.Pod{
  1328. ObjectMeta: metav1.ObjectMeta{Name: "test4"},
  1329. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1330. Status: api.PodStatus{
  1331. Phase: api.PodSucceeded,
  1332. ContainerStatuses: []api.ContainerStatus{
  1333. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1334. {RestartCount: 3},
  1335. },
  1336. },
  1337. },
  1338. // Columns: Name, Ready, Reason, Restarts, Age
  1339. []metav1.TableRow{
  1340. {
  1341. Cells: []interface{}{"test4", "1/2", "Succeeded", int64(6), "<unknown>"},
  1342. Conditions: podSuccessConditions,
  1343. },
  1344. },
  1345. },
  1346. {
  1347. // Test pod phase Failed shouldn't be printed
  1348. api.Pod{
  1349. ObjectMeta: metav1.ObjectMeta{Name: "test5"},
  1350. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1351. Status: api.PodStatus{
  1352. Phase: api.PodFailed,
  1353. ContainerStatuses: []api.ContainerStatus{
  1354. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1355. {Ready: true, RestartCount: 3},
  1356. },
  1357. },
  1358. },
  1359. // Columns: Name, Ready, Reason, Restarts, Age
  1360. []metav1.TableRow{
  1361. {
  1362. Cells: []interface{}{"test5", "1/2", "Failed", int64(6), "<unknown>"},
  1363. Conditions: podFailedConditions,
  1364. },
  1365. },
  1366. },
  1367. }
  1368. for i, test := range tests {
  1369. rows, err := printPod(&test.pod, printers.GenerateOptions{})
  1370. if err != nil {
  1371. t.Fatal(err)
  1372. }
  1373. for i := range rows {
  1374. rows[i].Object.Object = nil
  1375. }
  1376. if !reflect.DeepEqual(test.expect, rows) {
  1377. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expect, rows))
  1378. }
  1379. }
  1380. }
  1381. func TestPrintPodTemplate(t *testing.T) {
  1382. tests := []struct {
  1383. podTemplate api.PodTemplate
  1384. options printers.GenerateOptions
  1385. expected []metav1.TableRow
  1386. }{
  1387. // Test basic pod template with no containers.
  1388. {
  1389. podTemplate: api.PodTemplate{
  1390. ObjectMeta: metav1.ObjectMeta{Name: "pod-template-1"},
  1391. Template: api.PodTemplateSpec{
  1392. ObjectMeta: metav1.ObjectMeta{Name: "pod-template-1"},
  1393. Spec: api.PodSpec{
  1394. Containers: []api.Container{},
  1395. },
  1396. },
  1397. },
  1398. options: printers.GenerateOptions{},
  1399. // Columns: Name, Containers, Images, Pod Labels
  1400. expected: []metav1.TableRow{{Cells: []interface{}{"pod-template-1", "", "", "<none>"}}},
  1401. },
  1402. // Test basic pod template with two containers.
  1403. {
  1404. podTemplate: api.PodTemplate{
  1405. ObjectMeta: metav1.ObjectMeta{Name: "pod-template-2"},
  1406. Template: api.PodTemplateSpec{
  1407. ObjectMeta: metav1.ObjectMeta{Name: "pod-template-2"},
  1408. Spec: api.PodSpec{
  1409. Containers: []api.Container{
  1410. {
  1411. Name: "fake-container1",
  1412. Image: "fake-image1",
  1413. },
  1414. {
  1415. Name: "fake-container2",
  1416. Image: "fake-image2",
  1417. },
  1418. },
  1419. },
  1420. },
  1421. },
  1422. options: printers.GenerateOptions{},
  1423. // Columns: Name, Containers, Images, Pod Labels
  1424. expected: []metav1.TableRow{{Cells: []interface{}{"pod-template-2", "fake-container1,fake-container2", "fake-image1,fake-image2", "<none>"}}},
  1425. },
  1426. // Test basic pod template with pod labels
  1427. {
  1428. podTemplate: api.PodTemplate{
  1429. ObjectMeta: metav1.ObjectMeta{Name: "pod-template-3"},
  1430. Template: api.PodTemplateSpec{
  1431. ObjectMeta: metav1.ObjectMeta{
  1432. Name: "pod-template-3",
  1433. Labels: map[string]string{"foo": "bar"},
  1434. },
  1435. Spec: api.PodSpec{
  1436. Containers: []api.Container{},
  1437. },
  1438. },
  1439. },
  1440. options: printers.GenerateOptions{},
  1441. // Columns: Name, Containers, Images, Pod Labels
  1442. expected: []metav1.TableRow{{Cells: []interface{}{"pod-template-3", "", "", "foo=bar"}}},
  1443. },
  1444. }
  1445. for i, test := range tests {
  1446. rows, err := printPodTemplate(&test.podTemplate, test.options)
  1447. if err != nil {
  1448. t.Fatal(err)
  1449. }
  1450. for i := range rows {
  1451. rows[i].Object.Object = nil
  1452. }
  1453. if !reflect.DeepEqual(test.expected, rows) {
  1454. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  1455. }
  1456. }
  1457. }
  1458. func TestPrintPodTemplateList(t *testing.T) {
  1459. templateList := api.PodTemplateList{
  1460. Items: []api.PodTemplate{
  1461. {
  1462. ObjectMeta: metav1.ObjectMeta{Name: "pod-template-1"},
  1463. Template: api.PodTemplateSpec{
  1464. ObjectMeta: metav1.ObjectMeta{
  1465. Name: "pod-template-2",
  1466. Labels: map[string]string{"foo": "bar"},
  1467. },
  1468. Spec: api.PodSpec{
  1469. Containers: []api.Container{},
  1470. },
  1471. },
  1472. },
  1473. {
  1474. ObjectMeta: metav1.ObjectMeta{Name: "pod-template-2"},
  1475. Template: api.PodTemplateSpec{
  1476. ObjectMeta: metav1.ObjectMeta{
  1477. Name: "pod-template-2",
  1478. Labels: map[string]string{"a": "b"},
  1479. },
  1480. Spec: api.PodSpec{
  1481. Containers: []api.Container{},
  1482. },
  1483. },
  1484. },
  1485. },
  1486. }
  1487. // Columns: Name, Containers, Images, Pod Labels
  1488. expectedRows := []metav1.TableRow{
  1489. {Cells: []interface{}{"pod-template-1", "", "", "foo=bar"}},
  1490. {Cells: []interface{}{"pod-template-2", "", "", "a=b"}},
  1491. }
  1492. rows, err := printPodTemplateList(&templateList, printers.GenerateOptions{})
  1493. if err != nil {
  1494. t.Fatalf("Error printing pod template list: %#v", err)
  1495. }
  1496. for i := range rows {
  1497. rows[i].Object.Object = nil
  1498. }
  1499. if !reflect.DeepEqual(expectedRows, rows) {
  1500. t.Errorf("mismatch: %s", diff.ObjectReflectDiff(expectedRows, rows))
  1501. }
  1502. }
  1503. type stringTestList []struct {
  1504. name, got, exp string
  1505. }
  1506. func TestTranslateTimestampSince(t *testing.T) {
  1507. tl := stringTestList{
  1508. {"a while from now", translateTimestampSince(metav1.Time{Time: time.Now().Add(2.1e9)}), "<invalid>"},
  1509. {"almost now", translateTimestampSince(metav1.Time{Time: time.Now().Add(1.9e9)}), "0s"},
  1510. {"now", translateTimestampSince(metav1.Time{Time: time.Now()}), "0s"},
  1511. {"unknown", translateTimestampSince(metav1.Time{}), "<unknown>"},
  1512. {"30 seconds ago", translateTimestampSince(metav1.Time{Time: time.Now().Add(-3e10)}), "30s"},
  1513. {"5 minutes ago", translateTimestampSince(metav1.Time{Time: time.Now().Add(-3e11)}), "5m"},
  1514. {"an hour ago", translateTimestampSince(metav1.Time{Time: time.Now().Add(-6e12)}), "100m"},
  1515. {"2 days ago", translateTimestampSince(metav1.Time{Time: time.Now().UTC().AddDate(0, 0, -2)}), "2d"},
  1516. {"months ago", translateTimestampSince(metav1.Time{Time: time.Now().UTC().AddDate(0, 0, -90)}), "90d"},
  1517. {"10 years ago", translateTimestampSince(metav1.Time{Time: time.Now().UTC().AddDate(-10, 0, 0)}), "10y"},
  1518. }
  1519. for _, test := range tl {
  1520. if test.got != test.exp {
  1521. t.Errorf("On %v, expected '%v', but got '%v'",
  1522. test.name, test.exp, test.got)
  1523. }
  1524. }
  1525. }
  1526. func TestTranslateTimestampUntil(t *testing.T) {
  1527. // Since this method compares the time with time.Now() internally,
  1528. // small buffers of 0.1 seconds are added on comparing times to consider method call overhead.
  1529. // Otherwise, the output strings become shorter than expected.
  1530. const buf = 1e8
  1531. tl := stringTestList{
  1532. {"a while ago", translateTimestampUntil(metav1.Time{Time: time.Now().Add(-2.1e9)}), "<invalid>"},
  1533. {"almost now", translateTimestampUntil(metav1.Time{Time: time.Now().Add(-1.9e9)}), "0s"},
  1534. {"now", translateTimestampUntil(metav1.Time{Time: time.Now()}), "0s"},
  1535. {"unknown", translateTimestampUntil(metav1.Time{}), "<unknown>"},
  1536. {"in 30 seconds", translateTimestampUntil(metav1.Time{Time: time.Now().Add(3e10 + buf)}), "30s"},
  1537. {"in 5 minutes", translateTimestampUntil(metav1.Time{Time: time.Now().Add(3e11 + buf)}), "5m"},
  1538. {"in an hour", translateTimestampUntil(metav1.Time{Time: time.Now().Add(6e12 + buf)}), "100m"},
  1539. {"in 2 days", translateTimestampUntil(metav1.Time{Time: time.Now().UTC().AddDate(0, 0, 2).Add(buf)}), "2d"},
  1540. {"in months", translateTimestampUntil(metav1.Time{Time: time.Now().UTC().AddDate(0, 0, 90).Add(buf)}), "90d"},
  1541. {"in 10 years", translateTimestampUntil(metav1.Time{Time: time.Now().UTC().AddDate(10, 0, 0).Add(buf)}), "10y"},
  1542. }
  1543. for _, test := range tl {
  1544. if test.got != test.exp {
  1545. t.Errorf("On %v, expected '%v', but got '%v'",
  1546. test.name, test.exp, test.got)
  1547. }
  1548. }
  1549. }
  1550. func TestPrintDeployment(t *testing.T) {
  1551. testDeployment := apps.Deployment{
  1552. ObjectMeta: metav1.ObjectMeta{
  1553. Name: "test1",
  1554. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  1555. },
  1556. Spec: apps.DeploymentSpec{
  1557. Replicas: 5,
  1558. Template: api.PodTemplateSpec{
  1559. Spec: api.PodSpec{
  1560. Containers: []api.Container{
  1561. {
  1562. Name: "fake-container1",
  1563. Image: "fake-image1",
  1564. },
  1565. {
  1566. Name: "fake-container2",
  1567. Image: "fake-image2",
  1568. },
  1569. },
  1570. },
  1571. },
  1572. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
  1573. },
  1574. Status: apps.DeploymentStatus{
  1575. Replicas: 10,
  1576. UpdatedReplicas: 2,
  1577. AvailableReplicas: 1,
  1578. UnavailableReplicas: 4,
  1579. },
  1580. }
  1581. tests := []struct {
  1582. deployment apps.Deployment
  1583. options printers.GenerateOptions
  1584. expected []metav1.TableRow
  1585. }{
  1586. // Test Deployment with no generate options.
  1587. {
  1588. deployment: testDeployment,
  1589. options: printers.GenerateOptions{},
  1590. // Columns: Name, ReadyReplicas, UpdatedReplicas, AvailableReplicas, Age
  1591. expected: []metav1.TableRow{{Cells: []interface{}{"test1", "0/5", int64(2), int64(1), "0s"}}},
  1592. },
  1593. // Test generate options: Wide.
  1594. {
  1595. deployment: testDeployment,
  1596. options: printers.GenerateOptions{Wide: true},
  1597. // Columns: Name, ReadyReplicas, UpdatedReplicas, AvailableReplicas, Age, Containers, Images, Selectors
  1598. expected: []metav1.TableRow{{Cells: []interface{}{"test1", "0/5", int64(2), int64(1), "0s", "fake-container1,fake-container2", "fake-image1,fake-image2", "foo=bar"}}},
  1599. },
  1600. }
  1601. for i, test := range tests {
  1602. rows, err := printDeployment(&test.deployment, test.options)
  1603. if err != nil {
  1604. t.Fatal(err)
  1605. }
  1606. for i := range rows {
  1607. rows[i].Object.Object = nil
  1608. }
  1609. if !reflect.DeepEqual(test.expected, rows) {
  1610. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  1611. }
  1612. }
  1613. }
  1614. func TestPrintDaemonSet(t *testing.T) {
  1615. testDaemonSet := apps.DaemonSet{
  1616. ObjectMeta: metav1.ObjectMeta{
  1617. Name: "test1",
  1618. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  1619. },
  1620. Spec: apps.DaemonSetSpec{
  1621. Template: api.PodTemplateSpec{
  1622. Spec: api.PodSpec{
  1623. Containers: []api.Container{
  1624. {
  1625. Name: "fake-container1",
  1626. Image: "fake-image1",
  1627. },
  1628. {
  1629. Name: "fake-container2",
  1630. Image: "fake-image2",
  1631. },
  1632. },
  1633. },
  1634. },
  1635. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
  1636. },
  1637. Status: apps.DaemonSetStatus{
  1638. CurrentNumberScheduled: 2,
  1639. DesiredNumberScheduled: 3,
  1640. NumberReady: 1,
  1641. UpdatedNumberScheduled: 2,
  1642. NumberAvailable: 0,
  1643. },
  1644. }
  1645. tests := []struct {
  1646. daemonSet apps.DaemonSet
  1647. options printers.GenerateOptions
  1648. expected []metav1.TableRow
  1649. }{
  1650. // Test generate daemon set with no generate options.
  1651. {
  1652. daemonSet: testDaemonSet,
  1653. options: printers.GenerateOptions{},
  1654. // Columns: Name, Num Desired, Num Current, Num Ready, Num Updated, Num Available, Selectors, Age
  1655. expected: []metav1.TableRow{{Cells: []interface{}{"test1", int64(3), int64(2), int64(1), int64(2), int64(0), "<none>", "0s"}}},
  1656. },
  1657. // Test generate daemon set with "Wide" generate options.
  1658. {
  1659. daemonSet: testDaemonSet,
  1660. options: printers.GenerateOptions{Wide: true},
  1661. // Columns: Name, Num Desired, Num Current, Num Ready, Num Updated, Num Available, Node Selectors, Age, Containers, Images, Labels
  1662. expected: []metav1.TableRow{{Cells: []interface{}{"test1", int64(3), int64(2), int64(1), int64(2), int64(0), "<none>", "0s", "fake-container1,fake-container2", "fake-image1,fake-image2", "foo=bar"}}},
  1663. },
  1664. }
  1665. for i, test := range tests {
  1666. rows, err := printDaemonSet(&test.daemonSet, test.options)
  1667. if err != nil {
  1668. t.Fatal(err)
  1669. }
  1670. for i := range rows {
  1671. rows[i].Object.Object = nil
  1672. }
  1673. if !reflect.DeepEqual(test.expected, rows) {
  1674. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  1675. }
  1676. }
  1677. }
  1678. func TestPrintDaemonSetList(t *testing.T) {
  1679. daemonSetList := apps.DaemonSetList{
  1680. Items: []apps.DaemonSet{
  1681. {
  1682. ObjectMeta: metav1.ObjectMeta{
  1683. Name: "daemonset1",
  1684. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  1685. },
  1686. Spec: apps.DaemonSetSpec{
  1687. Template: api.PodTemplateSpec{
  1688. Spec: api.PodSpec{
  1689. Containers: []api.Container{
  1690. {
  1691. Name: "fake-container1",
  1692. Image: "fake-image1",
  1693. },
  1694. {
  1695. Name: "fake-container2",
  1696. Image: "fake-image2",
  1697. },
  1698. },
  1699. },
  1700. },
  1701. },
  1702. Status: apps.DaemonSetStatus{
  1703. CurrentNumberScheduled: 2,
  1704. DesiredNumberScheduled: 3,
  1705. NumberReady: 1,
  1706. UpdatedNumberScheduled: 2,
  1707. NumberAvailable: 0,
  1708. },
  1709. },
  1710. {
  1711. ObjectMeta: metav1.ObjectMeta{
  1712. Name: "daemonset2",
  1713. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  1714. },
  1715. Spec: apps.DaemonSetSpec{
  1716. Template: api.PodTemplateSpec{},
  1717. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
  1718. },
  1719. Status: apps.DaemonSetStatus{
  1720. CurrentNumberScheduled: 4,
  1721. DesiredNumberScheduled: 2,
  1722. NumberReady: 9,
  1723. UpdatedNumberScheduled: 3,
  1724. NumberAvailable: 3,
  1725. },
  1726. },
  1727. },
  1728. }
  1729. // Columns: Name, Num Desired, Num Current, Num Ready, Num Updated, Num Available, Selectors, Age
  1730. expectedRows := []metav1.TableRow{
  1731. {Cells: []interface{}{"daemonset1", int64(3), int64(2), int64(1), int64(2), int64(0), "<none>", "0s"}},
  1732. {Cells: []interface{}{"daemonset2", int64(2), int64(4), int64(9), int64(3), int64(3), "<none>", "0s"}},
  1733. }
  1734. rows, err := printDaemonSetList(&daemonSetList, printers.GenerateOptions{})
  1735. if err != nil {
  1736. t.Fatalf("Error printing daemon set list: %#v", err)
  1737. }
  1738. for i := range rows {
  1739. rows[i].Object.Object = nil
  1740. }
  1741. if !reflect.DeepEqual(expectedRows, rows) {
  1742. t.Errorf("mismatch: %s", diff.ObjectReflectDiff(expectedRows, rows))
  1743. }
  1744. }
  1745. func TestPrintJob(t *testing.T) {
  1746. now := time.Now()
  1747. completions := int32(2)
  1748. tests := []struct {
  1749. job batch.Job
  1750. options printers.GenerateOptions
  1751. expected []metav1.TableRow
  1752. }{
  1753. {
  1754. // Generate table rows for Job with no generate options.
  1755. job: batch.Job{
  1756. ObjectMeta: metav1.ObjectMeta{
  1757. Name: "job1",
  1758. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  1759. },
  1760. Spec: batch.JobSpec{
  1761. Completions: &completions,
  1762. Template: api.PodTemplateSpec{
  1763. Spec: api.PodSpec{
  1764. Containers: []api.Container{
  1765. {
  1766. Name: "fake-job-container1",
  1767. Image: "fake-job-image1",
  1768. },
  1769. {
  1770. Name: "fake-job-container2",
  1771. Image: "fake-job-image2",
  1772. },
  1773. },
  1774. },
  1775. },
  1776. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"job-label": "job-lable-value"}},
  1777. },
  1778. Status: batch.JobStatus{
  1779. Succeeded: 1,
  1780. },
  1781. },
  1782. options: printers.GenerateOptions{},
  1783. // Columns: Name, Completions, Duration, Age
  1784. expected: []metav1.TableRow{{Cells: []interface{}{"job1", "1/2", "", "0s"}}},
  1785. },
  1786. // Generate table rows for Job with generate options "Wide".
  1787. {
  1788. job: batch.Job{
  1789. ObjectMeta: metav1.ObjectMeta{
  1790. Name: "job1",
  1791. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  1792. },
  1793. Spec: batch.JobSpec{
  1794. Completions: &completions,
  1795. Template: api.PodTemplateSpec{
  1796. Spec: api.PodSpec{
  1797. Containers: []api.Container{
  1798. {
  1799. Name: "fake-job-container1",
  1800. Image: "fake-job-image1",
  1801. },
  1802. {
  1803. Name: "fake-job-container2",
  1804. Image: "fake-job-image2",
  1805. },
  1806. },
  1807. },
  1808. },
  1809. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"job-label": "job-label-value"}},
  1810. },
  1811. Status: batch.JobStatus{
  1812. Succeeded: 1,
  1813. },
  1814. },
  1815. options: printers.GenerateOptions{Wide: true},
  1816. // Columns: Name, Completions, Duration, Age, Containers, Images, Selectors
  1817. expected: []metav1.TableRow{
  1818. {
  1819. Cells: []interface{}{"job1", "1/2", "", "0s", "fake-job-container1,fake-job-container2", "fake-job-image1,fake-job-image2", "job-label=job-label-value"},
  1820. },
  1821. },
  1822. },
  1823. // Job with ten-year age.
  1824. {
  1825. job: batch.Job{
  1826. ObjectMeta: metav1.ObjectMeta{
  1827. Name: "job2",
  1828. CreationTimestamp: metav1.Time{Time: time.Now().AddDate(-10, 0, 0)},
  1829. },
  1830. Spec: batch.JobSpec{
  1831. Completions: nil,
  1832. },
  1833. Status: batch.JobStatus{
  1834. Succeeded: 0,
  1835. },
  1836. },
  1837. options: printers.GenerateOptions{},
  1838. // Columns: Name, Completions, Duration, Age
  1839. expected: []metav1.TableRow{{Cells: []interface{}{"job2", "0/1", "", "10y"}}},
  1840. },
  1841. // Job with duration.
  1842. {
  1843. job: batch.Job{
  1844. ObjectMeta: metav1.ObjectMeta{
  1845. Name: "job3",
  1846. CreationTimestamp: metav1.Time{Time: time.Now().AddDate(-10, 0, 0)},
  1847. },
  1848. Spec: batch.JobSpec{
  1849. Completions: nil,
  1850. },
  1851. Status: batch.JobStatus{
  1852. Succeeded: 0,
  1853. StartTime: &metav1.Time{Time: now.Add(time.Minute)},
  1854. CompletionTime: &metav1.Time{Time: now.Add(31 * time.Minute)},
  1855. },
  1856. },
  1857. options: printers.GenerateOptions{},
  1858. // Columns: Name, Completions, Duration, Age
  1859. expected: []metav1.TableRow{{Cells: []interface{}{"job3", "0/1", "30m", "10y"}}},
  1860. },
  1861. {
  1862. job: batch.Job{
  1863. ObjectMeta: metav1.ObjectMeta{
  1864. Name: "job4",
  1865. CreationTimestamp: metav1.Time{Time: time.Now().AddDate(-10, 0, 0)},
  1866. },
  1867. Spec: batch.JobSpec{
  1868. Completions: nil,
  1869. },
  1870. Status: batch.JobStatus{
  1871. Succeeded: 0,
  1872. StartTime: &metav1.Time{Time: time.Now().Add(-20 * time.Minute)},
  1873. },
  1874. },
  1875. options: printers.GenerateOptions{},
  1876. // Columns: Name, Completions, Duration, Age
  1877. expected: []metav1.TableRow{{Cells: []interface{}{"job4", "0/1", "20m", "10y"}}},
  1878. },
  1879. }
  1880. for i, test := range tests {
  1881. rows, err := printJob(&test.job, test.options)
  1882. if err != nil {
  1883. t.Fatal(err)
  1884. }
  1885. for i := range rows {
  1886. rows[i].Object.Object = nil
  1887. }
  1888. if !reflect.DeepEqual(test.expected, rows) {
  1889. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  1890. }
  1891. }
  1892. }
  1893. func TestPrintJobList(t *testing.T) {
  1894. completions := int32(2)
  1895. jobList := batch.JobList{
  1896. Items: []batch.Job{
  1897. {
  1898. ObjectMeta: metav1.ObjectMeta{
  1899. Name: "job1",
  1900. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  1901. },
  1902. Spec: batch.JobSpec{
  1903. Completions: &completions,
  1904. Template: api.PodTemplateSpec{
  1905. Spec: api.PodSpec{
  1906. Containers: []api.Container{
  1907. {
  1908. Name: "fake-job-container1",
  1909. Image: "fake-job-image1",
  1910. },
  1911. {
  1912. Name: "fake-job-container2",
  1913. Image: "fake-job-image2",
  1914. },
  1915. },
  1916. },
  1917. },
  1918. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"job-label": "job-lable-value"}},
  1919. },
  1920. Status: batch.JobStatus{
  1921. Succeeded: 1,
  1922. },
  1923. },
  1924. {
  1925. ObjectMeta: metav1.ObjectMeta{
  1926. Name: "job2",
  1927. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  1928. },
  1929. Spec: batch.JobSpec{
  1930. Completions: &completions,
  1931. Template: api.PodTemplateSpec{
  1932. Spec: api.PodSpec{
  1933. Containers: []api.Container{
  1934. {
  1935. Name: "fake-job-container1",
  1936. Image: "fake-job-image1",
  1937. },
  1938. {
  1939. Name: "fake-job-container2",
  1940. Image: "fake-job-image2",
  1941. },
  1942. },
  1943. },
  1944. },
  1945. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"job-label": "job-lable-value"}},
  1946. },
  1947. Status: batch.JobStatus{
  1948. Succeeded: 2,
  1949. StartTime: &metav1.Time{Time: time.Now().Add(-20 * time.Minute)},
  1950. },
  1951. },
  1952. },
  1953. }
  1954. // Columns: Name, Completions, Duration, Age
  1955. expectedRows := []metav1.TableRow{
  1956. {Cells: []interface{}{"job1", "1/2", "", "0s"}},
  1957. {Cells: []interface{}{"job2", "2/2", "20m", "0s"}},
  1958. }
  1959. rows, err := printJobList(&jobList, printers.GenerateOptions{})
  1960. if err != nil {
  1961. t.Fatalf("Error printing job list: %#v", err)
  1962. }
  1963. for i := range rows {
  1964. rows[i].Object.Object = nil
  1965. }
  1966. if !reflect.DeepEqual(expectedRows, rows) {
  1967. t.Errorf("mismatch: %s", diff.ObjectReflectDiff(expectedRows, rows))
  1968. }
  1969. }
  1970. func TestPrintHPA(t *testing.T) {
  1971. minReplicasVal := int32(2)
  1972. targetUtilizationVal := int32(80)
  1973. currentUtilizationVal := int32(50)
  1974. metricLabelSelector, err := metav1.ParseToLabelSelector("label=value")
  1975. if err != nil {
  1976. t.Errorf("unable to parse label selector: %v", err)
  1977. }
  1978. tests := []struct {
  1979. hpa autoscaling.HorizontalPodAutoscaler
  1980. expected []metav1.TableRow
  1981. }{
  1982. // minReplicas unset
  1983. {
  1984. hpa: autoscaling.HorizontalPodAutoscaler{
  1985. ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
  1986. Spec: autoscaling.HorizontalPodAutoscalerSpec{
  1987. ScaleTargetRef: autoscaling.CrossVersionObjectReference{
  1988. Name: "some-rc",
  1989. Kind: "ReplicationController",
  1990. },
  1991. MaxReplicas: 10,
  1992. },
  1993. Status: autoscaling.HorizontalPodAutoscalerStatus{
  1994. CurrentReplicas: 4,
  1995. DesiredReplicas: 5,
  1996. },
  1997. },
  1998. // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
  1999. expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "<none>", "<unset>", int64(10), int64(4), "<unknown>"}}},
  2000. },
  2001. // external source type, target average value (no current)
  2002. {
  2003. hpa: autoscaling.HorizontalPodAutoscaler{
  2004. ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
  2005. Spec: autoscaling.HorizontalPodAutoscalerSpec{
  2006. ScaleTargetRef: autoscaling.CrossVersionObjectReference{
  2007. Name: "some-rc",
  2008. Kind: "ReplicationController",
  2009. },
  2010. MinReplicas: &minReplicasVal,
  2011. MaxReplicas: 10,
  2012. Metrics: []autoscaling.MetricSpec{
  2013. {
  2014. Type: autoscaling.ExternalMetricSourceType,
  2015. External: &autoscaling.ExternalMetricSource{
  2016. Metric: autoscaling.MetricIdentifier{
  2017. Name: "some-external-metric",
  2018. Selector: metricLabelSelector,
  2019. },
  2020. Target: autoscaling.MetricTarget{
  2021. Type: autoscaling.AverageValueMetricType,
  2022. AverageValue: resource.NewMilliQuantity(100, resource.DecimalSI),
  2023. },
  2024. },
  2025. },
  2026. },
  2027. },
  2028. Status: autoscaling.HorizontalPodAutoscalerStatus{
  2029. CurrentReplicas: 4,
  2030. DesiredReplicas: 5,
  2031. },
  2032. },
  2033. // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
  2034. expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "<unknown>/100m (avg)", "2", int64(10), int64(4), "<unknown>"}}},
  2035. },
  2036. // external source type, target average value
  2037. {
  2038. hpa: autoscaling.HorizontalPodAutoscaler{
  2039. ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
  2040. Spec: autoscaling.HorizontalPodAutoscalerSpec{
  2041. ScaleTargetRef: autoscaling.CrossVersionObjectReference{
  2042. Name: "some-rc",
  2043. Kind: "ReplicationController",
  2044. },
  2045. MinReplicas: &minReplicasVal,
  2046. MaxReplicas: 10,
  2047. Metrics: []autoscaling.MetricSpec{
  2048. {
  2049. Type: autoscaling.ExternalMetricSourceType,
  2050. External: &autoscaling.ExternalMetricSource{
  2051. Metric: autoscaling.MetricIdentifier{
  2052. Name: "some-external-metric",
  2053. Selector: metricLabelSelector,
  2054. },
  2055. Target: autoscaling.MetricTarget{
  2056. Type: autoscaling.AverageValueMetricType,
  2057. AverageValue: resource.NewMilliQuantity(100, resource.DecimalSI),
  2058. },
  2059. },
  2060. },
  2061. },
  2062. },
  2063. Status: autoscaling.HorizontalPodAutoscalerStatus{
  2064. CurrentReplicas: 4,
  2065. DesiredReplicas: 5,
  2066. CurrentMetrics: []autoscaling.MetricStatus{
  2067. {
  2068. Type: autoscaling.ExternalMetricSourceType,
  2069. External: &autoscaling.ExternalMetricStatus{
  2070. Metric: autoscaling.MetricIdentifier{
  2071. Name: "some-external-metric",
  2072. Selector: metricLabelSelector,
  2073. },
  2074. Current: autoscaling.MetricValueStatus{
  2075. AverageValue: resource.NewMilliQuantity(50, resource.DecimalSI),
  2076. },
  2077. },
  2078. },
  2079. },
  2080. },
  2081. },
  2082. // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
  2083. expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "50m/100m (avg)", "2", int64(10), int64(4), "<unknown>"}}},
  2084. },
  2085. // external source type, target value (no current)
  2086. {
  2087. hpa: autoscaling.HorizontalPodAutoscaler{
  2088. ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
  2089. Spec: autoscaling.HorizontalPodAutoscalerSpec{
  2090. ScaleTargetRef: autoscaling.CrossVersionObjectReference{
  2091. Name: "some-rc",
  2092. Kind: "ReplicationController",
  2093. },
  2094. MinReplicas: &minReplicasVal,
  2095. MaxReplicas: 10,
  2096. Metrics: []autoscaling.MetricSpec{
  2097. {
  2098. Type: autoscaling.ExternalMetricSourceType,
  2099. External: &autoscaling.ExternalMetricSource{
  2100. Metric: autoscaling.MetricIdentifier{
  2101. Name: "some-service-metric",
  2102. Selector: metricLabelSelector,
  2103. },
  2104. Target: autoscaling.MetricTarget{
  2105. Type: autoscaling.ValueMetricType,
  2106. Value: resource.NewMilliQuantity(100, resource.DecimalSI),
  2107. },
  2108. },
  2109. },
  2110. },
  2111. },
  2112. Status: autoscaling.HorizontalPodAutoscalerStatus{
  2113. CurrentReplicas: 4,
  2114. DesiredReplicas: 5,
  2115. },
  2116. },
  2117. // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
  2118. expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "<unknown>/100m", "2", int64(10), int64(4), "<unknown>"}}},
  2119. },
  2120. // external source type, target value
  2121. {
  2122. hpa: autoscaling.HorizontalPodAutoscaler{
  2123. ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
  2124. Spec: autoscaling.HorizontalPodAutoscalerSpec{
  2125. ScaleTargetRef: autoscaling.CrossVersionObjectReference{
  2126. Name: "some-rc",
  2127. Kind: "ReplicationController",
  2128. },
  2129. MinReplicas: &minReplicasVal,
  2130. MaxReplicas: 10,
  2131. Metrics: []autoscaling.MetricSpec{
  2132. {
  2133. Type: autoscaling.ExternalMetricSourceType,
  2134. External: &autoscaling.ExternalMetricSource{
  2135. Metric: autoscaling.MetricIdentifier{
  2136. Name: "some-external-metric",
  2137. Selector: metricLabelSelector,
  2138. },
  2139. Target: autoscaling.MetricTarget{
  2140. Type: autoscaling.ValueMetricType,
  2141. Value: resource.NewMilliQuantity(100, resource.DecimalSI),
  2142. },
  2143. },
  2144. },
  2145. },
  2146. },
  2147. Status: autoscaling.HorizontalPodAutoscalerStatus{
  2148. CurrentReplicas: 4,
  2149. DesiredReplicas: 5,
  2150. CurrentMetrics: []autoscaling.MetricStatus{
  2151. {
  2152. Type: autoscaling.ExternalMetricSourceType,
  2153. External: &autoscaling.ExternalMetricStatus{
  2154. Metric: autoscaling.MetricIdentifier{
  2155. Name: "some-external-metric",
  2156. },
  2157. Current: autoscaling.MetricValueStatus{
  2158. Value: resource.NewMilliQuantity(50, resource.DecimalSI),
  2159. },
  2160. },
  2161. },
  2162. },
  2163. },
  2164. },
  2165. // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
  2166. expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "50m/100m", "2", int64(10), int64(4), "<unknown>"}}},
  2167. },
  2168. // pods source type (no current)
  2169. {
  2170. hpa: autoscaling.HorizontalPodAutoscaler{
  2171. ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
  2172. Spec: autoscaling.HorizontalPodAutoscalerSpec{
  2173. ScaleTargetRef: autoscaling.CrossVersionObjectReference{
  2174. Name: "some-rc",
  2175. Kind: "ReplicationController",
  2176. },
  2177. MinReplicas: &minReplicasVal,
  2178. MaxReplicas: 10,
  2179. Metrics: []autoscaling.MetricSpec{
  2180. {
  2181. Type: autoscaling.PodsMetricSourceType,
  2182. Pods: &autoscaling.PodsMetricSource{
  2183. Metric: autoscaling.MetricIdentifier{
  2184. Name: "some-pods-metric",
  2185. },
  2186. Target: autoscaling.MetricTarget{
  2187. Type: autoscaling.AverageValueMetricType,
  2188. AverageValue: resource.NewMilliQuantity(100, resource.DecimalSI),
  2189. },
  2190. },
  2191. },
  2192. },
  2193. },
  2194. Status: autoscaling.HorizontalPodAutoscalerStatus{
  2195. CurrentReplicas: 4,
  2196. DesiredReplicas: 5,
  2197. },
  2198. },
  2199. // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
  2200. expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "<unknown>/100m", "2", int64(10), int64(4), "<unknown>"}}},
  2201. },
  2202. // pods source type
  2203. {
  2204. hpa: autoscaling.HorizontalPodAutoscaler{
  2205. ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
  2206. Spec: autoscaling.HorizontalPodAutoscalerSpec{
  2207. ScaleTargetRef: autoscaling.CrossVersionObjectReference{
  2208. Name: "some-rc",
  2209. Kind: "ReplicationController",
  2210. },
  2211. MinReplicas: &minReplicasVal,
  2212. MaxReplicas: 10,
  2213. Metrics: []autoscaling.MetricSpec{
  2214. {
  2215. Type: autoscaling.PodsMetricSourceType,
  2216. Pods: &autoscaling.PodsMetricSource{
  2217. Metric: autoscaling.MetricIdentifier{
  2218. Name: "some-pods-metric",
  2219. },
  2220. Target: autoscaling.MetricTarget{
  2221. Type: autoscaling.AverageValueMetricType,
  2222. AverageValue: resource.NewMilliQuantity(100, resource.DecimalSI),
  2223. },
  2224. },
  2225. },
  2226. },
  2227. },
  2228. Status: autoscaling.HorizontalPodAutoscalerStatus{
  2229. CurrentReplicas: 4,
  2230. DesiredReplicas: 5,
  2231. CurrentMetrics: []autoscaling.MetricStatus{
  2232. {
  2233. Type: autoscaling.PodsMetricSourceType,
  2234. Pods: &autoscaling.PodsMetricStatus{
  2235. Metric: autoscaling.MetricIdentifier{
  2236. Name: "some-pods-metric",
  2237. },
  2238. Current: autoscaling.MetricValueStatus{
  2239. AverageValue: resource.NewMilliQuantity(50, resource.DecimalSI),
  2240. },
  2241. },
  2242. },
  2243. },
  2244. },
  2245. },
  2246. // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
  2247. expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "50m/100m", "2", int64(10), int64(4), "<unknown>"}}},
  2248. },
  2249. // object source type (no current)
  2250. {
  2251. hpa: autoscaling.HorizontalPodAutoscaler{
  2252. ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
  2253. Spec: autoscaling.HorizontalPodAutoscalerSpec{
  2254. ScaleTargetRef: autoscaling.CrossVersionObjectReference{
  2255. Name: "some-rc",
  2256. Kind: "ReplicationController",
  2257. },
  2258. MinReplicas: &minReplicasVal,
  2259. MaxReplicas: 10,
  2260. Metrics: []autoscaling.MetricSpec{
  2261. {
  2262. Type: autoscaling.ObjectMetricSourceType,
  2263. Object: &autoscaling.ObjectMetricSource{
  2264. DescribedObject: autoscaling.CrossVersionObjectReference{
  2265. Name: "some-service",
  2266. Kind: "Service",
  2267. },
  2268. Metric: autoscaling.MetricIdentifier{
  2269. Name: "some-service-metric",
  2270. },
  2271. Target: autoscaling.MetricTarget{
  2272. Type: autoscaling.ValueMetricType,
  2273. Value: resource.NewMilliQuantity(100, resource.DecimalSI),
  2274. },
  2275. },
  2276. },
  2277. },
  2278. },
  2279. Status: autoscaling.HorizontalPodAutoscalerStatus{
  2280. CurrentReplicas: 4,
  2281. DesiredReplicas: 5,
  2282. },
  2283. },
  2284. // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
  2285. expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "<unknown>/100m", "2", int64(10), int64(4), "<unknown>"}}},
  2286. },
  2287. // object source type
  2288. {
  2289. hpa: autoscaling.HorizontalPodAutoscaler{
  2290. ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
  2291. Spec: autoscaling.HorizontalPodAutoscalerSpec{
  2292. ScaleTargetRef: autoscaling.CrossVersionObjectReference{
  2293. Name: "some-rc",
  2294. Kind: "ReplicationController",
  2295. },
  2296. MinReplicas: &minReplicasVal,
  2297. MaxReplicas: 10,
  2298. Metrics: []autoscaling.MetricSpec{
  2299. {
  2300. Type: autoscaling.ObjectMetricSourceType,
  2301. Object: &autoscaling.ObjectMetricSource{
  2302. DescribedObject: autoscaling.CrossVersionObjectReference{
  2303. Name: "some-service",
  2304. Kind: "Service",
  2305. },
  2306. Metric: autoscaling.MetricIdentifier{
  2307. Name: "some-service-metric",
  2308. },
  2309. Target: autoscaling.MetricTarget{
  2310. Type: autoscaling.ValueMetricType,
  2311. Value: resource.NewMilliQuantity(100, resource.DecimalSI),
  2312. },
  2313. },
  2314. },
  2315. },
  2316. },
  2317. Status: autoscaling.HorizontalPodAutoscalerStatus{
  2318. CurrentReplicas: 4,
  2319. DesiredReplicas: 5,
  2320. CurrentMetrics: []autoscaling.MetricStatus{
  2321. {
  2322. Type: autoscaling.ObjectMetricSourceType,
  2323. Object: &autoscaling.ObjectMetricStatus{
  2324. DescribedObject: autoscaling.CrossVersionObjectReference{
  2325. Name: "some-service",
  2326. Kind: "Service",
  2327. },
  2328. Metric: autoscaling.MetricIdentifier{
  2329. Name: "some-service-metric",
  2330. },
  2331. Current: autoscaling.MetricValueStatus{
  2332. Value: resource.NewMilliQuantity(50, resource.DecimalSI),
  2333. },
  2334. },
  2335. },
  2336. },
  2337. },
  2338. },
  2339. // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
  2340. expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "50m/100m", "2", int64(10), int64(4), "<unknown>"}}},
  2341. },
  2342. // resource source type, targetVal (no current)
  2343. {
  2344. hpa: autoscaling.HorizontalPodAutoscaler{
  2345. ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
  2346. Spec: autoscaling.HorizontalPodAutoscalerSpec{
  2347. ScaleTargetRef: autoscaling.CrossVersionObjectReference{
  2348. Name: "some-rc",
  2349. Kind: "ReplicationController",
  2350. },
  2351. MinReplicas: &minReplicasVal,
  2352. MaxReplicas: 10,
  2353. Metrics: []autoscaling.MetricSpec{
  2354. {
  2355. Type: autoscaling.ResourceMetricSourceType,
  2356. Resource: &autoscaling.ResourceMetricSource{
  2357. Name: api.ResourceCPU,
  2358. Target: autoscaling.MetricTarget{
  2359. Type: autoscaling.AverageValueMetricType,
  2360. AverageValue: resource.NewMilliQuantity(100, resource.DecimalSI),
  2361. },
  2362. },
  2363. },
  2364. },
  2365. },
  2366. Status: autoscaling.HorizontalPodAutoscalerStatus{
  2367. CurrentReplicas: 4,
  2368. DesiredReplicas: 5,
  2369. },
  2370. },
  2371. // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
  2372. expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "<unknown>/100m", "2", int64(10), int64(4), "<unknown>"}}},
  2373. },
  2374. // resource source type, targetVal
  2375. {
  2376. hpa: autoscaling.HorizontalPodAutoscaler{
  2377. ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
  2378. Spec: autoscaling.HorizontalPodAutoscalerSpec{
  2379. ScaleTargetRef: autoscaling.CrossVersionObjectReference{
  2380. Name: "some-rc",
  2381. Kind: "ReplicationController",
  2382. },
  2383. MinReplicas: &minReplicasVal,
  2384. MaxReplicas: 10,
  2385. Metrics: []autoscaling.MetricSpec{
  2386. {
  2387. Type: autoscaling.ResourceMetricSourceType,
  2388. Resource: &autoscaling.ResourceMetricSource{
  2389. Name: api.ResourceCPU,
  2390. Target: autoscaling.MetricTarget{
  2391. Type: autoscaling.AverageValueMetricType,
  2392. AverageValue: resource.NewMilliQuantity(100, resource.DecimalSI),
  2393. },
  2394. },
  2395. },
  2396. },
  2397. },
  2398. Status: autoscaling.HorizontalPodAutoscalerStatus{
  2399. CurrentReplicas: 4,
  2400. DesiredReplicas: 5,
  2401. CurrentMetrics: []autoscaling.MetricStatus{
  2402. {
  2403. Type: autoscaling.ResourceMetricSourceType,
  2404. Resource: &autoscaling.ResourceMetricStatus{
  2405. Name: api.ResourceCPU,
  2406. Current: autoscaling.MetricValueStatus{
  2407. AverageValue: resource.NewMilliQuantity(50, resource.DecimalSI),
  2408. },
  2409. },
  2410. },
  2411. },
  2412. },
  2413. },
  2414. // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
  2415. expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "50m/100m", "2", int64(10), int64(4), "<unknown>"}}},
  2416. },
  2417. // resource source type, targetUtil (no current)
  2418. {
  2419. hpa: autoscaling.HorizontalPodAutoscaler{
  2420. ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
  2421. Spec: autoscaling.HorizontalPodAutoscalerSpec{
  2422. ScaleTargetRef: autoscaling.CrossVersionObjectReference{
  2423. Name: "some-rc",
  2424. Kind: "ReplicationController",
  2425. },
  2426. MinReplicas: &minReplicasVal,
  2427. MaxReplicas: 10,
  2428. Metrics: []autoscaling.MetricSpec{
  2429. {
  2430. Type: autoscaling.ResourceMetricSourceType,
  2431. Resource: &autoscaling.ResourceMetricSource{
  2432. Name: api.ResourceCPU,
  2433. Target: autoscaling.MetricTarget{
  2434. Type: autoscaling.UtilizationMetricType,
  2435. AverageUtilization: &targetUtilizationVal,
  2436. },
  2437. },
  2438. },
  2439. },
  2440. },
  2441. Status: autoscaling.HorizontalPodAutoscalerStatus{
  2442. CurrentReplicas: 4,
  2443. DesiredReplicas: 5,
  2444. },
  2445. },
  2446. // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
  2447. expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "<unknown>/80%", "2", int64(10), int64(4), "<unknown>"}}},
  2448. },
  2449. // resource source type, targetUtil
  2450. {
  2451. hpa: autoscaling.HorizontalPodAutoscaler{
  2452. ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
  2453. Spec: autoscaling.HorizontalPodAutoscalerSpec{
  2454. ScaleTargetRef: autoscaling.CrossVersionObjectReference{
  2455. Name: "some-rc",
  2456. Kind: "ReplicationController",
  2457. },
  2458. MinReplicas: &minReplicasVal,
  2459. MaxReplicas: 10,
  2460. Metrics: []autoscaling.MetricSpec{
  2461. {
  2462. Type: autoscaling.ResourceMetricSourceType,
  2463. Resource: &autoscaling.ResourceMetricSource{
  2464. Name: api.ResourceCPU,
  2465. Target: autoscaling.MetricTarget{
  2466. Type: autoscaling.UtilizationMetricType,
  2467. AverageUtilization: &targetUtilizationVal,
  2468. },
  2469. },
  2470. },
  2471. },
  2472. },
  2473. Status: autoscaling.HorizontalPodAutoscalerStatus{
  2474. CurrentReplicas: 4,
  2475. DesiredReplicas: 5,
  2476. CurrentMetrics: []autoscaling.MetricStatus{
  2477. {
  2478. Type: autoscaling.ResourceMetricSourceType,
  2479. Resource: &autoscaling.ResourceMetricStatus{
  2480. Name: api.ResourceCPU,
  2481. Current: autoscaling.MetricValueStatus{
  2482. AverageUtilization: &currentUtilizationVal,
  2483. AverageValue: resource.NewMilliQuantity(40, resource.DecimalSI),
  2484. },
  2485. },
  2486. },
  2487. },
  2488. },
  2489. },
  2490. // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
  2491. expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "50%/80%", "2", int64(10), int64(4), "<unknown>"}}},
  2492. },
  2493. // multiple specs
  2494. {
  2495. hpa: autoscaling.HorizontalPodAutoscaler{
  2496. ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
  2497. Spec: autoscaling.HorizontalPodAutoscalerSpec{
  2498. ScaleTargetRef: autoscaling.CrossVersionObjectReference{
  2499. Name: "some-rc",
  2500. Kind: "ReplicationController",
  2501. },
  2502. MinReplicas: &minReplicasVal,
  2503. MaxReplicas: 10,
  2504. Metrics: []autoscaling.MetricSpec{
  2505. {
  2506. Type: autoscaling.PodsMetricSourceType,
  2507. Pods: &autoscaling.PodsMetricSource{
  2508. Metric: autoscaling.MetricIdentifier{
  2509. Name: "some-pods-metric",
  2510. },
  2511. Target: autoscaling.MetricTarget{
  2512. Type: autoscaling.AverageValueMetricType,
  2513. AverageValue: resource.NewMilliQuantity(100, resource.DecimalSI),
  2514. },
  2515. },
  2516. },
  2517. {
  2518. Type: autoscaling.ResourceMetricSourceType,
  2519. Resource: &autoscaling.ResourceMetricSource{
  2520. Name: api.ResourceCPU,
  2521. Target: autoscaling.MetricTarget{
  2522. Type: autoscaling.UtilizationMetricType,
  2523. AverageUtilization: &targetUtilizationVal,
  2524. },
  2525. },
  2526. },
  2527. {
  2528. Type: autoscaling.PodsMetricSourceType,
  2529. Pods: &autoscaling.PodsMetricSource{
  2530. Metric: autoscaling.MetricIdentifier{
  2531. Name: "other-pods-metric",
  2532. },
  2533. Target: autoscaling.MetricTarget{
  2534. Type: autoscaling.AverageValueMetricType,
  2535. AverageValue: resource.NewMilliQuantity(400, resource.DecimalSI),
  2536. },
  2537. },
  2538. },
  2539. },
  2540. },
  2541. Status: autoscaling.HorizontalPodAutoscalerStatus{
  2542. CurrentReplicas: 4,
  2543. DesiredReplicas: 5,
  2544. CurrentMetrics: []autoscaling.MetricStatus{
  2545. {
  2546. Type: autoscaling.PodsMetricSourceType,
  2547. Pods: &autoscaling.PodsMetricStatus{
  2548. Metric: autoscaling.MetricIdentifier{
  2549. Name: "some-pods-metric",
  2550. },
  2551. Current: autoscaling.MetricValueStatus{
  2552. AverageValue: resource.NewMilliQuantity(50, resource.DecimalSI),
  2553. },
  2554. },
  2555. },
  2556. {
  2557. Type: autoscaling.ResourceMetricSourceType,
  2558. Resource: &autoscaling.ResourceMetricStatus{
  2559. Name: api.ResourceCPU,
  2560. Current: autoscaling.MetricValueStatus{
  2561. AverageUtilization: &currentUtilizationVal,
  2562. AverageValue: resource.NewMilliQuantity(40, resource.DecimalSI),
  2563. },
  2564. },
  2565. },
  2566. },
  2567. },
  2568. },
  2569. // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
  2570. expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "50m/100m, 50%/80% + 1 more...", "2", int64(10), int64(4), "<unknown>"}}},
  2571. },
  2572. }
  2573. for i, test := range tests {
  2574. rows, err := printHorizontalPodAutoscaler(&test.hpa, printers.GenerateOptions{})
  2575. if err != nil {
  2576. t.Fatal(err)
  2577. }
  2578. for i := range rows {
  2579. rows[i].Object.Object = nil
  2580. }
  2581. if !reflect.DeepEqual(test.expected, rows) {
  2582. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  2583. }
  2584. }
  2585. }
  2586. func TestPrintService(t *testing.T) {
  2587. singleExternalIP := []string{"80.11.12.10"}
  2588. mulExternalIP := []string{"80.11.12.10", "80.11.12.11"}
  2589. tests := []struct {
  2590. service api.Service
  2591. options printers.GenerateOptions
  2592. expected []metav1.TableRow
  2593. }{
  2594. {
  2595. // Test name, cluster ip, port with protocol
  2596. service: api.Service{
  2597. ObjectMeta: metav1.ObjectMeta{Name: "test1"},
  2598. Spec: api.ServiceSpec{
  2599. Type: api.ServiceTypeClusterIP,
  2600. Ports: []api.ServicePort{
  2601. {
  2602. Protocol: "tcp",
  2603. Port: 2233,
  2604. },
  2605. },
  2606. ClusterIP: "10.9.8.7",
  2607. Selector: map[string]string{"foo": "bar"}, // Does NOT get printed.
  2608. },
  2609. },
  2610. options: printers.GenerateOptions{},
  2611. // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
  2612. expected: []metav1.TableRow{{Cells: []interface{}{"test1", "ClusterIP", "10.9.8.7", "<none>", "2233/tcp", "<unknown>"}}},
  2613. },
  2614. {
  2615. // Test generate options: Wide includes selectors.
  2616. service: api.Service{
  2617. ObjectMeta: metav1.ObjectMeta{Name: "test1"},
  2618. Spec: api.ServiceSpec{
  2619. Type: api.ServiceTypeClusterIP,
  2620. Ports: []api.ServicePort{
  2621. {
  2622. Protocol: "tcp",
  2623. Port: 2233,
  2624. },
  2625. },
  2626. ClusterIP: "10.9.8.7",
  2627. Selector: map[string]string{"foo": "bar"},
  2628. },
  2629. },
  2630. options: printers.GenerateOptions{Wide: true},
  2631. // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age, Selector
  2632. expected: []metav1.TableRow{{Cells: []interface{}{"test1", "ClusterIP", "10.9.8.7", "<none>", "2233/tcp", "<unknown>", "foo=bar"}}},
  2633. },
  2634. {
  2635. // Test NodePort service
  2636. service: api.Service{
  2637. ObjectMeta: metav1.ObjectMeta{Name: "test2"},
  2638. Spec: api.ServiceSpec{
  2639. Type: api.ServiceTypeNodePort,
  2640. Ports: []api.ServicePort{
  2641. {
  2642. Protocol: "tcp",
  2643. Port: 8888,
  2644. NodePort: 9999,
  2645. },
  2646. },
  2647. ClusterIP: "10.9.8.7",
  2648. },
  2649. },
  2650. options: printers.GenerateOptions{},
  2651. // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
  2652. expected: []metav1.TableRow{{Cells: []interface{}{"test2", "NodePort", "10.9.8.7", "<none>", "8888:9999/tcp", "<unknown>"}}},
  2653. },
  2654. {
  2655. // Test LoadBalancer service
  2656. service: api.Service{
  2657. ObjectMeta: metav1.ObjectMeta{Name: "test3"},
  2658. Spec: api.ServiceSpec{
  2659. Type: api.ServiceTypeLoadBalancer,
  2660. Ports: []api.ServicePort{
  2661. {
  2662. Protocol: "tcp",
  2663. Port: 8888,
  2664. },
  2665. },
  2666. ClusterIP: "10.9.8.7",
  2667. },
  2668. },
  2669. options: printers.GenerateOptions{},
  2670. // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
  2671. expected: []metav1.TableRow{{Cells: []interface{}{"test3", "LoadBalancer", "10.9.8.7", "<pending>", "8888/tcp", "<unknown>"}}},
  2672. },
  2673. {
  2674. // Test LoadBalancer service with single ExternalIP and no LoadBalancerStatus
  2675. service: api.Service{
  2676. ObjectMeta: metav1.ObjectMeta{Name: "test4"},
  2677. Spec: api.ServiceSpec{
  2678. Type: api.ServiceTypeLoadBalancer,
  2679. Ports: []api.ServicePort{
  2680. {
  2681. Protocol: "tcp",
  2682. Port: 8888,
  2683. },
  2684. },
  2685. ClusterIP: "10.9.8.7",
  2686. ExternalIPs: singleExternalIP,
  2687. },
  2688. },
  2689. options: printers.GenerateOptions{},
  2690. // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
  2691. expected: []metav1.TableRow{{Cells: []interface{}{"test4", "LoadBalancer", "10.9.8.7", "80.11.12.10", "8888/tcp", "<unknown>"}}},
  2692. },
  2693. {
  2694. // Test LoadBalancer service with single ExternalIP
  2695. service: api.Service{
  2696. ObjectMeta: metav1.ObjectMeta{Name: "test5"},
  2697. Spec: api.ServiceSpec{
  2698. Type: api.ServiceTypeLoadBalancer,
  2699. Ports: []api.ServicePort{
  2700. {
  2701. Protocol: "tcp",
  2702. Port: 8888,
  2703. },
  2704. },
  2705. ClusterIP: "10.9.8.7",
  2706. ExternalIPs: singleExternalIP,
  2707. },
  2708. Status: api.ServiceStatus{
  2709. LoadBalancer: api.LoadBalancerStatus{
  2710. Ingress: []api.LoadBalancerIngress{
  2711. {
  2712. IP: "3.4.5.6",
  2713. Hostname: "test.cluster.com",
  2714. },
  2715. },
  2716. },
  2717. },
  2718. },
  2719. options: printers.GenerateOptions{},
  2720. // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
  2721. expected: []metav1.TableRow{{Cells: []interface{}{"test5", "LoadBalancer", "10.9.8.7", "3.4.5.6,80.11.12.10", "8888/tcp", "<unknown>"}}},
  2722. },
  2723. {
  2724. // Test LoadBalancer service with mul ExternalIPs
  2725. service: api.Service{
  2726. ObjectMeta: metav1.ObjectMeta{Name: "test6"},
  2727. Spec: api.ServiceSpec{
  2728. Type: api.ServiceTypeLoadBalancer,
  2729. Ports: []api.ServicePort{
  2730. {
  2731. Protocol: "tcp",
  2732. Port: 8888,
  2733. },
  2734. },
  2735. ClusterIP: "10.9.8.7",
  2736. ExternalIPs: mulExternalIP,
  2737. },
  2738. Status: api.ServiceStatus{
  2739. LoadBalancer: api.LoadBalancerStatus{
  2740. Ingress: []api.LoadBalancerIngress{
  2741. {
  2742. IP: "2.3.4.5",
  2743. Hostname: "test.cluster.local",
  2744. },
  2745. {
  2746. IP: "3.4.5.6",
  2747. Hostname: "test.cluster.com",
  2748. },
  2749. },
  2750. },
  2751. },
  2752. },
  2753. options: printers.GenerateOptions{},
  2754. // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
  2755. expected: []metav1.TableRow{{Cells: []interface{}{"test6", "LoadBalancer", "10.9.8.7", "2.3.4.5,3.4.5.6,80.11.12.10,80.11.12.11", "8888/tcp", "<unknown>"}}},
  2756. },
  2757. {
  2758. // Test ExternalName service
  2759. service: api.Service{
  2760. ObjectMeta: metav1.ObjectMeta{Name: "test7"},
  2761. Spec: api.ServiceSpec{
  2762. Type: api.ServiceTypeExternalName,
  2763. ExternalName: "my.database.example.com",
  2764. },
  2765. },
  2766. options: printers.GenerateOptions{},
  2767. // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
  2768. expected: []metav1.TableRow{{Cells: []interface{}{"test7", "ExternalName", "<none>", "my.database.example.com", "<none>", "<unknown>"}}},
  2769. },
  2770. }
  2771. for i, test := range tests {
  2772. rows, err := printService(&test.service, test.options)
  2773. if err != nil {
  2774. t.Fatal(err)
  2775. }
  2776. for i := range rows {
  2777. rows[i].Object.Object = nil
  2778. }
  2779. if !reflect.DeepEqual(test.expected, rows) {
  2780. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  2781. }
  2782. }
  2783. }
  2784. func TestPrintServiceList(t *testing.T) {
  2785. serviceList := api.ServiceList{
  2786. Items: []api.Service{
  2787. {
  2788. ObjectMeta: metav1.ObjectMeta{Name: "service1"},
  2789. Spec: api.ServiceSpec{
  2790. Type: api.ServiceTypeClusterIP,
  2791. Ports: []api.ServicePort{
  2792. {
  2793. Protocol: "tcp",
  2794. Port: 2233,
  2795. },
  2796. },
  2797. ClusterIP: "10.9.8.7",
  2798. },
  2799. },
  2800. {
  2801. ObjectMeta: metav1.ObjectMeta{Name: "service2"},
  2802. Spec: api.ServiceSpec{
  2803. Type: api.ServiceTypeNodePort,
  2804. Ports: []api.ServicePort{
  2805. {
  2806. Protocol: "udp",
  2807. Port: 5566,
  2808. },
  2809. },
  2810. ClusterIP: "1.2.3.4",
  2811. },
  2812. },
  2813. },
  2814. }
  2815. // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
  2816. expectedRows := []metav1.TableRow{
  2817. {Cells: []interface{}{"service1", "ClusterIP", "10.9.8.7", "<none>", "2233/tcp", "<unknown>"}},
  2818. {Cells: []interface{}{"service2", "NodePort", "1.2.3.4", "<none>", "5566/udp", "<unknown>"}},
  2819. }
  2820. rows, err := printServiceList(&serviceList, printers.GenerateOptions{})
  2821. if err != nil {
  2822. t.Fatalf("Error printing service list: %#v", err)
  2823. }
  2824. for i := range rows {
  2825. rows[i].Object.Object = nil
  2826. }
  2827. if !reflect.DeepEqual(expectedRows, rows) {
  2828. t.Errorf("mismatch: %s", diff.ObjectReflectDiff(expectedRows, rows))
  2829. }
  2830. }
  2831. func TestPrintPodDisruptionBudget(t *testing.T) {
  2832. minAvailable := intstr.FromInt(22)
  2833. maxUnavailable := intstr.FromInt(11)
  2834. tests := []struct {
  2835. pdb policy.PodDisruptionBudget
  2836. expected []metav1.TableRow
  2837. }{
  2838. // Min Available set, no Max Available.
  2839. {
  2840. pdb: policy.PodDisruptionBudget{
  2841. ObjectMeta: metav1.ObjectMeta{
  2842. Namespace: "ns1",
  2843. Name: "pdb1",
  2844. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  2845. },
  2846. Spec: policy.PodDisruptionBudgetSpec{
  2847. MinAvailable: &minAvailable,
  2848. },
  2849. Status: policy.PodDisruptionBudgetStatus{
  2850. DisruptionsAllowed: 5,
  2851. },
  2852. },
  2853. // Columns: Name, Min Available, Max Available, Allowed Disruptions, Age
  2854. expected: []metav1.TableRow{{Cells: []interface{}{"pdb1", "22", "N/A", int64(5), "0s"}}},
  2855. },
  2856. // Max Available set, no Min Available.
  2857. {
  2858. pdb: policy.PodDisruptionBudget{
  2859. ObjectMeta: metav1.ObjectMeta{
  2860. Namespace: "ns2",
  2861. Name: "pdb2",
  2862. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  2863. },
  2864. Spec: policy.PodDisruptionBudgetSpec{
  2865. MaxUnavailable: &maxUnavailable,
  2866. },
  2867. Status: policy.PodDisruptionBudgetStatus{
  2868. DisruptionsAllowed: 5,
  2869. },
  2870. },
  2871. // Columns: Name, Min Available, Max Available, Allowed Disruptions, Age
  2872. expected: []metav1.TableRow{{Cells: []interface{}{"pdb2", "N/A", "11", int64(5), "0s"}}},
  2873. }}
  2874. for i, test := range tests {
  2875. rows, err := printPodDisruptionBudget(&test.pdb, printers.GenerateOptions{})
  2876. if err != nil {
  2877. t.Fatal(err)
  2878. }
  2879. for i := range rows {
  2880. rows[i].Object.Object = nil
  2881. }
  2882. if !reflect.DeepEqual(test.expected, rows) {
  2883. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  2884. }
  2885. }
  2886. }
  2887. func TestPrintPodDisruptionBudgetList(t *testing.T) {
  2888. minAvailable := intstr.FromInt(22)
  2889. maxUnavailable := intstr.FromInt(11)
  2890. pdbList := policy.PodDisruptionBudgetList{
  2891. Items: []policy.PodDisruptionBudget{
  2892. {
  2893. ObjectMeta: metav1.ObjectMeta{
  2894. Namespace: "ns1",
  2895. Name: "pdb1",
  2896. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  2897. },
  2898. Spec: policy.PodDisruptionBudgetSpec{
  2899. MaxUnavailable: &maxUnavailable,
  2900. },
  2901. Status: policy.PodDisruptionBudgetStatus{
  2902. DisruptionsAllowed: 5,
  2903. },
  2904. },
  2905. {
  2906. ObjectMeta: metav1.ObjectMeta{
  2907. Namespace: "ns2",
  2908. Name: "pdb2",
  2909. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  2910. },
  2911. Spec: policy.PodDisruptionBudgetSpec{
  2912. MinAvailable: &minAvailable,
  2913. },
  2914. Status: policy.PodDisruptionBudgetStatus{
  2915. DisruptionsAllowed: 3,
  2916. },
  2917. },
  2918. },
  2919. }
  2920. // Columns: Name, Min Available, Max Available, Allowed Disruptions, Age
  2921. expectedRows := []metav1.TableRow{
  2922. {Cells: []interface{}{"pdb1", "N/A", "11", int64(5), "0s"}},
  2923. {Cells: []interface{}{"pdb2", "22", "N/A", int64(3), "0s"}},
  2924. }
  2925. rows, err := printPodDisruptionBudgetList(&pdbList, printers.GenerateOptions{})
  2926. if err != nil {
  2927. t.Fatalf("Error printing pod template list: %#v", err)
  2928. }
  2929. for i := range rows {
  2930. rows[i].Object.Object = nil
  2931. }
  2932. if !reflect.DeepEqual(expectedRows, rows) {
  2933. t.Errorf("mismatch: %s", diff.ObjectReflectDiff(expectedRows, rows))
  2934. }
  2935. }
  2936. func TestPrintControllerRevision(t *testing.T) {
  2937. tests := []struct {
  2938. history apps.ControllerRevision
  2939. expected []metav1.TableRow
  2940. }{
  2941. {
  2942. history: apps.ControllerRevision{
  2943. ObjectMeta: metav1.ObjectMeta{
  2944. Name: "test1",
  2945. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  2946. OwnerReferences: []metav1.OwnerReference{
  2947. {
  2948. Controller: boolP(true),
  2949. APIVersion: "apps/v1",
  2950. Kind: "DaemonSet",
  2951. Name: "foo",
  2952. },
  2953. },
  2954. },
  2955. Revision: 1,
  2956. },
  2957. expected: []metav1.TableRow{{Cells: []interface{}{"test1", "daemonset.apps/foo", int64(1), "0s"}}},
  2958. },
  2959. {
  2960. history: apps.ControllerRevision{
  2961. ObjectMeta: metav1.ObjectMeta{
  2962. Name: "test2",
  2963. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  2964. OwnerReferences: []metav1.OwnerReference{
  2965. {
  2966. Controller: boolP(false),
  2967. Kind: "ABC",
  2968. Name: "foo",
  2969. },
  2970. },
  2971. },
  2972. Revision: 2,
  2973. },
  2974. expected: []metav1.TableRow{{Cells: []interface{}{"test2", "<none>", int64(2), "0s"}}},
  2975. },
  2976. {
  2977. history: apps.ControllerRevision{
  2978. ObjectMeta: metav1.ObjectMeta{
  2979. Name: "test3",
  2980. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  2981. OwnerReferences: []metav1.OwnerReference{},
  2982. },
  2983. Revision: 3,
  2984. },
  2985. expected: []metav1.TableRow{{Cells: []interface{}{"test3", "<none>", int64(3), "0s"}}},
  2986. },
  2987. {
  2988. history: apps.ControllerRevision{
  2989. ObjectMeta: metav1.ObjectMeta{
  2990. Name: "test4",
  2991. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  2992. OwnerReferences: nil,
  2993. },
  2994. Revision: 4,
  2995. },
  2996. expected: []metav1.TableRow{{Cells: []interface{}{"test4", "<none>", int64(4), "0s"}}},
  2997. },
  2998. }
  2999. for i, test := range tests {
  3000. rows, err := printControllerRevision(&test.history, printers.GenerateOptions{})
  3001. if err != nil {
  3002. t.Fatal(err)
  3003. }
  3004. for i := range rows {
  3005. rows[i].Object.Object = nil
  3006. }
  3007. if !reflect.DeepEqual(test.expected, rows) {
  3008. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  3009. }
  3010. }
  3011. }
  3012. func boolP(b bool) *bool {
  3013. return &b
  3014. }
  3015. func TestPrintConfigMap(t *testing.T) {
  3016. tests := []struct {
  3017. configMap api.ConfigMap
  3018. expected []metav1.TableRow
  3019. }{
  3020. // Basic config map with no data.
  3021. {
  3022. configMap: api.ConfigMap{
  3023. ObjectMeta: metav1.ObjectMeta{
  3024. Name: "configmap1",
  3025. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  3026. },
  3027. },
  3028. // Columns: Name, Data, Age
  3029. expected: []metav1.TableRow{{Cells: []interface{}{"configmap1", int64(0), "0s"}}},
  3030. },
  3031. // Basic config map with one data entry
  3032. {
  3033. configMap: api.ConfigMap{
  3034. ObjectMeta: metav1.ObjectMeta{
  3035. Name: "configmap2",
  3036. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  3037. },
  3038. Data: map[string]string{
  3039. "foo": "bar",
  3040. },
  3041. },
  3042. // Columns: Name, (Num) Data, Age
  3043. expected: []metav1.TableRow{{Cells: []interface{}{"configmap2", int64(1), "0s"}}},
  3044. },
  3045. // Basic config map with one data and one binary data entry.
  3046. {
  3047. configMap: api.ConfigMap{
  3048. ObjectMeta: metav1.ObjectMeta{
  3049. Name: "configmap3",
  3050. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  3051. },
  3052. Data: map[string]string{
  3053. "foo": "bar",
  3054. },
  3055. BinaryData: map[string][]byte{
  3056. "bin": []byte("binary data"),
  3057. },
  3058. },
  3059. // Columns: Name, (Num) Data, Age
  3060. expected: []metav1.TableRow{{Cells: []interface{}{"configmap3", int64(2), "0s"}}},
  3061. },
  3062. }
  3063. for i, test := range tests {
  3064. rows, err := printConfigMap(&test.configMap, printers.GenerateOptions{})
  3065. if err != nil {
  3066. t.Fatal(err)
  3067. }
  3068. for i := range rows {
  3069. rows[i].Object.Object = nil
  3070. }
  3071. if !reflect.DeepEqual(test.expected, rows) {
  3072. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  3073. }
  3074. }
  3075. }
  3076. func TestPrintNetworkPolicy(t *testing.T) {
  3077. tests := []struct {
  3078. policy networking.NetworkPolicy
  3079. expected []metav1.TableRow
  3080. }{
  3081. // Basic network policy with empty spec.
  3082. {
  3083. policy: networking.NetworkPolicy{
  3084. ObjectMeta: metav1.ObjectMeta{
  3085. Name: "policy1",
  3086. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  3087. },
  3088. Spec: networking.NetworkPolicySpec{},
  3089. },
  3090. // Columns: Name, Pod-Selector, Age
  3091. expected: []metav1.TableRow{{Cells: []interface{}{"policy1", "<none>", "0s"}}},
  3092. },
  3093. // Basic network policy with pod selector.
  3094. {
  3095. policy: networking.NetworkPolicy{
  3096. ObjectMeta: metav1.ObjectMeta{
  3097. Name: "policy2",
  3098. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  3099. },
  3100. Spec: networking.NetworkPolicySpec{
  3101. PodSelector: metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
  3102. },
  3103. },
  3104. // Columns: Name, Pod-Selector, Age
  3105. expected: []metav1.TableRow{{Cells: []interface{}{"policy2", "foo=bar", "0s"}}},
  3106. },
  3107. }
  3108. for i, test := range tests {
  3109. rows, err := printNetworkPolicy(&test.policy, printers.GenerateOptions{})
  3110. if err != nil {
  3111. t.Fatal(err)
  3112. }
  3113. for i := range rows {
  3114. rows[i].Object.Object = nil
  3115. }
  3116. if !reflect.DeepEqual(test.expected, rows) {
  3117. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  3118. }
  3119. }
  3120. }
  3121. func TestPrintRoleBinding(t *testing.T) {
  3122. tests := []struct {
  3123. binding rbac.RoleBinding
  3124. options printers.GenerateOptions
  3125. expected []metav1.TableRow
  3126. }{
  3127. // Basic role binding
  3128. {
  3129. binding: rbac.RoleBinding{
  3130. ObjectMeta: metav1.ObjectMeta{
  3131. Name: "binding1",
  3132. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  3133. },
  3134. Subjects: []rbac.Subject{
  3135. {
  3136. Kind: "User",
  3137. Name: "system:kube-controller-manager",
  3138. },
  3139. },
  3140. RoleRef: rbac.RoleRef{
  3141. Kind: "Role",
  3142. Name: "extension-apiserver-authentication-reader",
  3143. },
  3144. },
  3145. options: printers.GenerateOptions{},
  3146. // Columns: Name, Age
  3147. expected: []metav1.TableRow{{Cells: []interface{}{"binding1", "Role/extension-apiserver-authentication-reader", "0s"}}},
  3148. },
  3149. // Generate options=Wide; print subject and roles.
  3150. {
  3151. binding: rbac.RoleBinding{
  3152. ObjectMeta: metav1.ObjectMeta{
  3153. Name: "binding2",
  3154. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  3155. },
  3156. Subjects: []rbac.Subject{
  3157. {
  3158. Kind: "User",
  3159. Name: "user-name",
  3160. },
  3161. {
  3162. Kind: "Group",
  3163. Name: "group-name",
  3164. },
  3165. {
  3166. Kind: "ServiceAccount",
  3167. Name: "service-account-name",
  3168. Namespace: "service-account-namespace",
  3169. },
  3170. },
  3171. RoleRef: rbac.RoleRef{
  3172. Kind: "Role",
  3173. Name: "role-name",
  3174. },
  3175. },
  3176. options: printers.GenerateOptions{Wide: true},
  3177. // Columns: Name, Age, Role, Users, Groups, ServiceAccounts
  3178. expected: []metav1.TableRow{{Cells: []interface{}{"binding2", "Role/role-name", "0s", "user-name", "group-name", "service-account-namespace/service-account-name"}}},
  3179. },
  3180. }
  3181. for i, test := range tests {
  3182. rows, err := printRoleBinding(&test.binding, test.options)
  3183. if err != nil {
  3184. t.Fatal(err)
  3185. }
  3186. for i := range rows {
  3187. rows[i].Object.Object = nil
  3188. }
  3189. if !reflect.DeepEqual(test.expected, rows) {
  3190. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  3191. }
  3192. }
  3193. }
  3194. func TestPrintClusterRoleBinding(t *testing.T) {
  3195. tests := []struct {
  3196. binding rbac.ClusterRoleBinding
  3197. options printers.GenerateOptions
  3198. expected []metav1.TableRow
  3199. }{
  3200. // Basic cluster role binding
  3201. {
  3202. binding: rbac.ClusterRoleBinding{
  3203. ObjectMeta: metav1.ObjectMeta{
  3204. Name: "binding1",
  3205. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  3206. },
  3207. Subjects: []rbac.Subject{
  3208. {
  3209. Kind: "User",
  3210. Name: "system:kube-controller-manager",
  3211. },
  3212. },
  3213. RoleRef: rbac.RoleRef{
  3214. Kind: "Role",
  3215. Name: "extension-apiserver-authentication-reader",
  3216. },
  3217. },
  3218. options: printers.GenerateOptions{},
  3219. // Columns: Name, Age
  3220. expected: []metav1.TableRow{{Cells: []interface{}{"binding1", "Role/extension-apiserver-authentication-reader", "0s"}}},
  3221. },
  3222. // Generate options=Wide; print subject and roles.
  3223. {
  3224. binding: rbac.ClusterRoleBinding{
  3225. ObjectMeta: metav1.ObjectMeta{
  3226. Name: "binding2",
  3227. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  3228. },
  3229. Subjects: []rbac.Subject{
  3230. {
  3231. Kind: "User",
  3232. Name: "user-name",
  3233. },
  3234. {
  3235. Kind: "Group",
  3236. Name: "group-name",
  3237. },
  3238. {
  3239. Kind: "ServiceAccount",
  3240. Name: "service-account-name",
  3241. Namespace: "service-account-namespace",
  3242. },
  3243. },
  3244. RoleRef: rbac.RoleRef{
  3245. Kind: "Role",
  3246. Name: "role-name",
  3247. },
  3248. },
  3249. options: printers.GenerateOptions{Wide: true},
  3250. // Columns: Name, Age, Role, Users, Groups, ServiceAccounts
  3251. expected: []metav1.TableRow{{Cells: []interface{}{"binding2", "Role/role-name", "0s", "user-name", "group-name", "service-account-namespace/service-account-name"}}},
  3252. },
  3253. }
  3254. for i, test := range tests {
  3255. rows, err := printClusterRoleBinding(&test.binding, test.options)
  3256. if err != nil {
  3257. t.Fatal(err)
  3258. }
  3259. for i := range rows {
  3260. rows[i].Object.Object = nil
  3261. }
  3262. if !reflect.DeepEqual(test.expected, rows) {
  3263. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  3264. }
  3265. }
  3266. }
  3267. func TestPrintCertificateSigningRequest(t *testing.T) {
  3268. tests := []struct {
  3269. csr certificates.CertificateSigningRequest
  3270. expected []metav1.TableRow
  3271. }{
  3272. // Basic CSR with no spec or status; defaults to status: Pending.
  3273. {
  3274. csr: certificates.CertificateSigningRequest{
  3275. ObjectMeta: metav1.ObjectMeta{
  3276. Name: "csr1",
  3277. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  3278. },
  3279. Spec: certificates.CertificateSigningRequestSpec{},
  3280. Status: certificates.CertificateSigningRequestStatus{},
  3281. },
  3282. // Columns: Name, Age, Requestor, Condition
  3283. expected: []metav1.TableRow{{Cells: []interface{}{"csr1", "0s", "", "Pending"}}},
  3284. },
  3285. // Basic CSR with Spec and Status=Approved.
  3286. {
  3287. csr: certificates.CertificateSigningRequest{
  3288. ObjectMeta: metav1.ObjectMeta{
  3289. Name: "csr2",
  3290. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  3291. },
  3292. Spec: certificates.CertificateSigningRequestSpec{
  3293. Username: "CSR Requestor",
  3294. },
  3295. Status: certificates.CertificateSigningRequestStatus{
  3296. Conditions: []certificates.CertificateSigningRequestCondition{
  3297. {
  3298. Type: certificates.CertificateApproved,
  3299. },
  3300. },
  3301. },
  3302. },
  3303. // Columns: Name, Age, Requestor, Condition
  3304. expected: []metav1.TableRow{{Cells: []interface{}{"csr2", "0s", "CSR Requestor", "Approved"}}},
  3305. },
  3306. // Basic CSR with Spec and Status=Approved; certificate issued.
  3307. {
  3308. csr: certificates.CertificateSigningRequest{
  3309. ObjectMeta: metav1.ObjectMeta{
  3310. Name: "csr2",
  3311. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  3312. },
  3313. Spec: certificates.CertificateSigningRequestSpec{
  3314. Username: "CSR Requestor",
  3315. },
  3316. Status: certificates.CertificateSigningRequestStatus{
  3317. Conditions: []certificates.CertificateSigningRequestCondition{
  3318. {
  3319. Type: certificates.CertificateApproved,
  3320. },
  3321. },
  3322. Certificate: []byte("cert data"),
  3323. },
  3324. },
  3325. // Columns: Name, Age, Requestor, Condition
  3326. expected: []metav1.TableRow{{Cells: []interface{}{"csr2", "0s", "CSR Requestor", "Approved,Issued"}}},
  3327. },
  3328. // Basic CSR with Spec and Status=Denied.
  3329. {
  3330. csr: certificates.CertificateSigningRequest{
  3331. ObjectMeta: metav1.ObjectMeta{
  3332. Name: "csr3",
  3333. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  3334. },
  3335. Spec: certificates.CertificateSigningRequestSpec{
  3336. Username: "CSR Requestor",
  3337. },
  3338. Status: certificates.CertificateSigningRequestStatus{
  3339. Conditions: []certificates.CertificateSigningRequestCondition{
  3340. {
  3341. Type: certificates.CertificateDenied,
  3342. },
  3343. },
  3344. },
  3345. },
  3346. // Columns: Name, Age, Requestor, Condition
  3347. expected: []metav1.TableRow{{Cells: []interface{}{"csr3", "0s", "CSR Requestor", "Denied"}}},
  3348. },
  3349. }
  3350. for i, test := range tests {
  3351. rows, err := printCertificateSigningRequest(&test.csr, printers.GenerateOptions{})
  3352. if err != nil {
  3353. t.Fatal(err)
  3354. }
  3355. for i := range rows {
  3356. rows[i].Object.Object = nil
  3357. }
  3358. if !reflect.DeepEqual(test.expected, rows) {
  3359. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  3360. }
  3361. }
  3362. }
  3363. func TestPrintReplicationController(t *testing.T) {
  3364. tests := []struct {
  3365. rc api.ReplicationController
  3366. options printers.GenerateOptions
  3367. expected []metav1.TableRow
  3368. }{
  3369. // Basic print replication controller without replicas or status.
  3370. {
  3371. rc: api.ReplicationController{
  3372. ObjectMeta: metav1.ObjectMeta{
  3373. Name: "rc1",
  3374. Namespace: "test-namespace",
  3375. },
  3376. Spec: api.ReplicationControllerSpec{
  3377. Selector: map[string]string{"a": "b"},
  3378. Template: &api.PodTemplateSpec{
  3379. ObjectMeta: metav1.ObjectMeta{
  3380. Labels: map[string]string{"a": "b"},
  3381. },
  3382. Spec: api.PodSpec{
  3383. Containers: []api.Container{
  3384. {
  3385. Name: "test",
  3386. Image: "test_image",
  3387. ImagePullPolicy: api.PullIfNotPresent,
  3388. TerminationMessagePolicy: api.TerminationMessageReadFile,
  3389. },
  3390. },
  3391. RestartPolicy: api.RestartPolicyAlways,
  3392. DNSPolicy: api.DNSClusterFirst,
  3393. },
  3394. },
  3395. },
  3396. },
  3397. options: printers.GenerateOptions{},
  3398. // Columns: Name, Desired, Current, Ready, Age
  3399. expected: []metav1.TableRow{{Cells: []interface{}{"rc1", int64(0), int64(0), int64(0), "<unknown>"}}},
  3400. },
  3401. // Basic print replication controller with replicas; does not print containers or labels
  3402. {
  3403. rc: api.ReplicationController{
  3404. ObjectMeta: metav1.ObjectMeta{
  3405. Name: "rc1",
  3406. Namespace: "test-namespace",
  3407. },
  3408. Spec: api.ReplicationControllerSpec{
  3409. Replicas: 5,
  3410. Selector: map[string]string{"a": "b"},
  3411. Template: &api.PodTemplateSpec{
  3412. ObjectMeta: metav1.ObjectMeta{
  3413. Labels: map[string]string{"a": "b"},
  3414. },
  3415. Spec: api.PodSpec{
  3416. Containers: []api.Container{
  3417. {
  3418. Name: "test",
  3419. Image: "test_image",
  3420. ImagePullPolicy: api.PullIfNotPresent,
  3421. TerminationMessagePolicy: api.TerminationMessageReadFile,
  3422. },
  3423. },
  3424. RestartPolicy: api.RestartPolicyAlways,
  3425. DNSPolicy: api.DNSClusterFirst,
  3426. },
  3427. },
  3428. },
  3429. Status: api.ReplicationControllerStatus{
  3430. Replicas: 3,
  3431. ReadyReplicas: 1,
  3432. },
  3433. },
  3434. options: printers.GenerateOptions{},
  3435. // Columns: Name, Desired, Current, Ready, Age
  3436. expected: []metav1.TableRow{{Cells: []interface{}{"rc1", int64(5), int64(3), int64(1), "<unknown>"}}},
  3437. },
  3438. // Generate options: Wide; print containers and labels.
  3439. {
  3440. rc: api.ReplicationController{
  3441. ObjectMeta: metav1.ObjectMeta{
  3442. Name: "rc1",
  3443. },
  3444. Spec: api.ReplicationControllerSpec{
  3445. Replicas: 5,
  3446. Selector: map[string]string{"a": "b"},
  3447. Template: &api.PodTemplateSpec{
  3448. ObjectMeta: metav1.ObjectMeta{
  3449. Labels: map[string]string{"a": "b"},
  3450. },
  3451. Spec: api.PodSpec{
  3452. Containers: []api.Container{
  3453. {
  3454. Name: "test",
  3455. Image: "test_image",
  3456. ImagePullPolicy: api.PullIfNotPresent,
  3457. TerminationMessagePolicy: api.TerminationMessageReadFile,
  3458. },
  3459. },
  3460. RestartPolicy: api.RestartPolicyAlways,
  3461. DNSPolicy: api.DNSClusterFirst,
  3462. },
  3463. },
  3464. },
  3465. Status: api.ReplicationControllerStatus{
  3466. Replicas: 3,
  3467. ReadyReplicas: 1,
  3468. },
  3469. },
  3470. options: printers.GenerateOptions{Wide: true},
  3471. // Columns: Name, Desired, Current, Ready, Age, Containers, Images, Selector
  3472. expected: []metav1.TableRow{{Cells: []interface{}{"rc1", int64(5), int64(3), int64(1), "<unknown>", "test", "test_image", "a=b"}}},
  3473. },
  3474. }
  3475. for i, test := range tests {
  3476. rows, err := printReplicationController(&test.rc, test.options)
  3477. if err != nil {
  3478. t.Fatal(err)
  3479. }
  3480. for i := range rows {
  3481. rows[i].Object.Object = nil
  3482. }
  3483. if !reflect.DeepEqual(test.expected, rows) {
  3484. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  3485. }
  3486. }
  3487. }
  3488. func TestPrintReplicaSet(t *testing.T) {
  3489. tests := []struct {
  3490. replicaSet apps.ReplicaSet
  3491. options printers.GenerateOptions
  3492. expected []metav1.TableRow
  3493. }{
  3494. // Generate options empty
  3495. {
  3496. replicaSet: apps.ReplicaSet{
  3497. ObjectMeta: metav1.ObjectMeta{
  3498. Name: "test1",
  3499. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  3500. },
  3501. Spec: apps.ReplicaSetSpec{
  3502. Replicas: 5,
  3503. Template: api.PodTemplateSpec{
  3504. Spec: api.PodSpec{
  3505. Containers: []api.Container{
  3506. {
  3507. Name: "fake-container1",
  3508. Image: "fake-image1",
  3509. },
  3510. {
  3511. Name: "fake-container2",
  3512. Image: "fake-image2",
  3513. },
  3514. },
  3515. },
  3516. },
  3517. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
  3518. },
  3519. Status: apps.ReplicaSetStatus{
  3520. Replicas: 5,
  3521. ReadyReplicas: 2,
  3522. },
  3523. },
  3524. options: printers.GenerateOptions{},
  3525. // Columns: Name, Desired, Current, Ready, Age
  3526. expected: []metav1.TableRow{{Cells: []interface{}{"test1", int64(5), int64(5), int64(2), "0s"}}},
  3527. },
  3528. // Generate options "Wide"
  3529. {
  3530. replicaSet: apps.ReplicaSet{
  3531. ObjectMeta: metav1.ObjectMeta{
  3532. Name: "test1",
  3533. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  3534. },
  3535. Spec: apps.ReplicaSetSpec{
  3536. Replicas: 5,
  3537. Template: api.PodTemplateSpec{
  3538. Spec: api.PodSpec{
  3539. Containers: []api.Container{
  3540. {
  3541. Name: "fake-container1",
  3542. Image: "fake-image1",
  3543. },
  3544. {
  3545. Name: "fake-container2",
  3546. Image: "fake-image2",
  3547. },
  3548. },
  3549. },
  3550. },
  3551. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
  3552. },
  3553. Status: apps.ReplicaSetStatus{
  3554. Replicas: 5,
  3555. ReadyReplicas: 2,
  3556. },
  3557. },
  3558. options: printers.GenerateOptions{Wide: true},
  3559. // Columns: Name, Desired, Current, Ready, Age, Containers, Images, Selector
  3560. expected: []metav1.TableRow{{Cells: []interface{}{"test1", int64(5), int64(5), int64(2), "0s", "fake-container1,fake-container2", "fake-image1,fake-image2", "foo=bar"}}},
  3561. },
  3562. }
  3563. for i, test := range tests {
  3564. rows, err := printReplicaSet(&test.replicaSet, test.options)
  3565. if err != nil {
  3566. t.Fatal(err)
  3567. }
  3568. for i := range rows {
  3569. rows[i].Object.Object = nil
  3570. }
  3571. if !reflect.DeepEqual(test.expected, rows) {
  3572. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  3573. }
  3574. }
  3575. }
  3576. func TestPrintReplicaSetList(t *testing.T) {
  3577. replicaSetList := apps.ReplicaSetList{
  3578. Items: []apps.ReplicaSet{
  3579. {
  3580. ObjectMeta: metav1.ObjectMeta{
  3581. Name: "replicaset1",
  3582. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  3583. },
  3584. Spec: apps.ReplicaSetSpec{
  3585. Replicas: 5,
  3586. Template: api.PodTemplateSpec{
  3587. Spec: api.PodSpec{
  3588. Containers: []api.Container{
  3589. {
  3590. Name: "fake-container1",
  3591. Image: "fake-image1",
  3592. },
  3593. {
  3594. Name: "fake-container2",
  3595. Image: "fake-image2",
  3596. },
  3597. },
  3598. },
  3599. },
  3600. },
  3601. Status: apps.ReplicaSetStatus{
  3602. Replicas: 5,
  3603. ReadyReplicas: 2,
  3604. },
  3605. },
  3606. {
  3607. ObjectMeta: metav1.ObjectMeta{
  3608. Name: "replicaset2",
  3609. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  3610. },
  3611. Spec: apps.ReplicaSetSpec{
  3612. Replicas: 4,
  3613. Template: api.PodTemplateSpec{},
  3614. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
  3615. },
  3616. Status: apps.ReplicaSetStatus{
  3617. Replicas: 3,
  3618. ReadyReplicas: 1,
  3619. },
  3620. },
  3621. },
  3622. }
  3623. // Columns: Name, Desired, Current, Ready, Age
  3624. expectedRows := []metav1.TableRow{
  3625. {Cells: []interface{}{"replicaset1", int64(5), int64(5), int64(2), "0s"}},
  3626. {Cells: []interface{}{"replicaset2", int64(4), int64(3), int64(1), "0s"}},
  3627. }
  3628. rows, err := printReplicaSetList(&replicaSetList, printers.GenerateOptions{})
  3629. if err != nil {
  3630. t.Fatalf("Error printing replica set list: %#v", err)
  3631. }
  3632. for i := range rows {
  3633. rows[i].Object.Object = nil
  3634. }
  3635. if !reflect.DeepEqual(expectedRows, rows) {
  3636. t.Errorf("mismatch: %s", diff.ObjectReflectDiff(expectedRows, rows))
  3637. }
  3638. }
  3639. func TestPrintStatefulSet(t *testing.T) {
  3640. tests := []struct {
  3641. statefulSet apps.StatefulSet
  3642. options printers.GenerateOptions
  3643. expected []metav1.TableRow
  3644. }{
  3645. // Basic stateful set; no generate options.
  3646. {
  3647. statefulSet: apps.StatefulSet{
  3648. ObjectMeta: metav1.ObjectMeta{
  3649. Name: "test1",
  3650. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  3651. },
  3652. Spec: apps.StatefulSetSpec{
  3653. Replicas: 5,
  3654. Template: api.PodTemplateSpec{
  3655. Spec: api.PodSpec{
  3656. Containers: []api.Container{
  3657. {
  3658. Name: "fake-container1",
  3659. Image: "fake-image1",
  3660. },
  3661. {
  3662. Name: "fake-container2",
  3663. Image: "fake-image2",
  3664. },
  3665. },
  3666. },
  3667. },
  3668. },
  3669. Status: apps.StatefulSetStatus{
  3670. Replicas: 5,
  3671. ReadyReplicas: 2,
  3672. },
  3673. },
  3674. options: printers.GenerateOptions{},
  3675. // Columns: Name, Ready, Age
  3676. expected: []metav1.TableRow{{Cells: []interface{}{"test1", "2/5", "0s"}}},
  3677. },
  3678. // Generate options "Wide"; includes containers and images.
  3679. {
  3680. statefulSet: apps.StatefulSet{
  3681. ObjectMeta: metav1.ObjectMeta{
  3682. Name: "test1",
  3683. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  3684. },
  3685. Spec: apps.StatefulSetSpec{
  3686. Replicas: 5,
  3687. Template: api.PodTemplateSpec{
  3688. Spec: api.PodSpec{
  3689. Containers: []api.Container{
  3690. {
  3691. Name: "fake-container1",
  3692. Image: "fake-image1",
  3693. },
  3694. {
  3695. Name: "fake-container2",
  3696. Image: "fake-image2",
  3697. },
  3698. },
  3699. },
  3700. },
  3701. },
  3702. Status: apps.StatefulSetStatus{
  3703. Replicas: 5,
  3704. ReadyReplicas: 2,
  3705. },
  3706. },
  3707. options: printers.GenerateOptions{Wide: true},
  3708. // Columns: Name, Ready, Age, Containers, Images
  3709. expected: []metav1.TableRow{{Cells: []interface{}{"test1", "2/5", "0s", "fake-container1,fake-container2", "fake-image1,fake-image2"}}},
  3710. },
  3711. }
  3712. for i, test := range tests {
  3713. rows, err := printStatefulSet(&test.statefulSet, test.options)
  3714. if err != nil {
  3715. t.Fatal(err)
  3716. }
  3717. for i := range rows {
  3718. rows[i].Object.Object = nil
  3719. }
  3720. if !reflect.DeepEqual(test.expected, rows) {
  3721. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  3722. }
  3723. }
  3724. }
  3725. func TestPrintPersistentVolume(t *testing.T) {
  3726. myScn := "my-scn"
  3727. claimRef := api.ObjectReference{
  3728. Name: "test",
  3729. Namespace: "default",
  3730. }
  3731. tests := []struct {
  3732. pv api.PersistentVolume
  3733. expected []metav1.TableRow
  3734. }{
  3735. {
  3736. // Test bound
  3737. pv: api.PersistentVolume{
  3738. ObjectMeta: metav1.ObjectMeta{
  3739. Name: "test1",
  3740. },
  3741. Spec: api.PersistentVolumeSpec{
  3742. ClaimRef: &claimRef,
  3743. AccessModes: []api.PersistentVolumeAccessMode{api.ReadOnlyMany},
  3744. Capacity: map[api.ResourceName]resource.Quantity{
  3745. api.ResourceStorage: resource.MustParse("4Gi"),
  3746. },
  3747. },
  3748. Status: api.PersistentVolumeStatus{
  3749. Phase: api.VolumeBound,
  3750. },
  3751. },
  3752. expected: []metav1.TableRow{{Cells: []interface{}{"test1", "4Gi", "ROX", "", "Bound", "default/test", "", "", "<unknown>", "<unset>"}}},
  3753. },
  3754. {
  3755. // Test failed
  3756. pv: api.PersistentVolume{
  3757. ObjectMeta: metav1.ObjectMeta{
  3758. Name: "test2",
  3759. },
  3760. Spec: api.PersistentVolumeSpec{
  3761. ClaimRef: &claimRef,
  3762. AccessModes: []api.PersistentVolumeAccessMode{api.ReadOnlyMany},
  3763. Capacity: map[api.ResourceName]resource.Quantity{
  3764. api.ResourceStorage: resource.MustParse("4Gi"),
  3765. },
  3766. },
  3767. Status: api.PersistentVolumeStatus{
  3768. Phase: api.VolumeFailed,
  3769. },
  3770. },
  3771. expected: []metav1.TableRow{{Cells: []interface{}{"test2", "4Gi", "ROX", "", "Failed", "default/test", "", "", "<unknown>", "<unset>"}}},
  3772. },
  3773. {
  3774. // Test pending
  3775. pv: api.PersistentVolume{
  3776. ObjectMeta: metav1.ObjectMeta{
  3777. Name: "test3",
  3778. },
  3779. Spec: api.PersistentVolumeSpec{
  3780. ClaimRef: &claimRef,
  3781. AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteMany},
  3782. Capacity: map[api.ResourceName]resource.Quantity{
  3783. api.ResourceStorage: resource.MustParse("10Gi"),
  3784. },
  3785. },
  3786. Status: api.PersistentVolumeStatus{
  3787. Phase: api.VolumePending,
  3788. },
  3789. },
  3790. expected: []metav1.TableRow{{Cells: []interface{}{"test3", "10Gi", "RWX", "", "Pending", "default/test", "", "", "<unknown>", "<unset>"}}},
  3791. },
  3792. {
  3793. // Test pending, storageClass
  3794. pv: api.PersistentVolume{
  3795. ObjectMeta: metav1.ObjectMeta{
  3796. Name: "test4",
  3797. },
  3798. Spec: api.PersistentVolumeSpec{
  3799. ClaimRef: &claimRef,
  3800. StorageClassName: myScn,
  3801. AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce},
  3802. Capacity: map[api.ResourceName]resource.Quantity{
  3803. api.ResourceStorage: resource.MustParse("10Gi"),
  3804. },
  3805. },
  3806. Status: api.PersistentVolumeStatus{
  3807. Phase: api.VolumePending,
  3808. },
  3809. },
  3810. expected: []metav1.TableRow{{Cells: []interface{}{"test4", "10Gi", "RWO", "", "Pending", "default/test", "my-scn", "", "<unknown>", "<unset>"}}},
  3811. },
  3812. {
  3813. // Test available
  3814. pv: api.PersistentVolume{
  3815. ObjectMeta: metav1.ObjectMeta{
  3816. Name: "test5",
  3817. },
  3818. Spec: api.PersistentVolumeSpec{
  3819. ClaimRef: &claimRef,
  3820. StorageClassName: myScn,
  3821. AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce},
  3822. Capacity: map[api.ResourceName]resource.Quantity{
  3823. api.ResourceStorage: resource.MustParse("10Gi"),
  3824. },
  3825. },
  3826. Status: api.PersistentVolumeStatus{
  3827. Phase: api.VolumeAvailable,
  3828. },
  3829. },
  3830. expected: []metav1.TableRow{{Cells: []interface{}{"test5", "10Gi", "RWO", "", "Available", "default/test", "my-scn", "", "<unknown>", "<unset>"}}},
  3831. },
  3832. {
  3833. // Test released
  3834. pv: api.PersistentVolume{
  3835. ObjectMeta: metav1.ObjectMeta{
  3836. Name: "test6",
  3837. },
  3838. Spec: api.PersistentVolumeSpec{
  3839. ClaimRef: &claimRef,
  3840. StorageClassName: myScn,
  3841. AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce},
  3842. Capacity: map[api.ResourceName]resource.Quantity{
  3843. api.ResourceStorage: resource.MustParse("10Gi"),
  3844. },
  3845. },
  3846. Status: api.PersistentVolumeStatus{
  3847. Phase: api.VolumeReleased,
  3848. },
  3849. },
  3850. expected: []metav1.TableRow{{Cells: []interface{}{"test6", "10Gi", "RWO", "", "Released", "default/test", "my-scn", "", "<unknown>", "<unset>"}}},
  3851. },
  3852. }
  3853. for i, test := range tests {
  3854. rows, err := printPersistentVolume(&test.pv, printers.GenerateOptions{})
  3855. if err != nil {
  3856. t.Fatal(err)
  3857. }
  3858. for i := range rows {
  3859. rows[i].Object.Object = nil
  3860. }
  3861. if !reflect.DeepEqual(test.expected, rows) {
  3862. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  3863. }
  3864. }
  3865. }
  3866. func TestPrintPersistentVolumeClaim(t *testing.T) {
  3867. volumeMode := api.PersistentVolumeFilesystem
  3868. myScn := "my-scn"
  3869. tests := []struct {
  3870. pvc api.PersistentVolumeClaim
  3871. expected []metav1.TableRow
  3872. }{
  3873. {
  3874. // Test name, num of containers, restarts, container ready status
  3875. pvc: api.PersistentVolumeClaim{
  3876. ObjectMeta: metav1.ObjectMeta{
  3877. Name: "test1",
  3878. },
  3879. Spec: api.PersistentVolumeClaimSpec{
  3880. VolumeName: "my-volume",
  3881. VolumeMode: &volumeMode,
  3882. },
  3883. Status: api.PersistentVolumeClaimStatus{
  3884. Phase: api.ClaimBound,
  3885. AccessModes: []api.PersistentVolumeAccessMode{api.ReadOnlyMany},
  3886. Capacity: map[api.ResourceName]resource.Quantity{
  3887. api.ResourceStorage: resource.MustParse("4Gi"),
  3888. },
  3889. },
  3890. },
  3891. expected: []metav1.TableRow{{Cells: []interface{}{"test1", "Bound", "my-volume", "4Gi", "ROX", "", "<unknown>", "Filesystem"}}},
  3892. },
  3893. {
  3894. // Test name, num of containers, restarts, container ready status
  3895. pvc: api.PersistentVolumeClaim{
  3896. ObjectMeta: metav1.ObjectMeta{
  3897. Name: "test2",
  3898. },
  3899. Spec: api.PersistentVolumeClaimSpec{
  3900. VolumeMode: &volumeMode,
  3901. },
  3902. Status: api.PersistentVolumeClaimStatus{
  3903. Phase: api.ClaimLost,
  3904. AccessModes: []api.PersistentVolumeAccessMode{api.ReadOnlyMany},
  3905. Capacity: map[api.ResourceName]resource.Quantity{
  3906. api.ResourceStorage: resource.MustParse("4Gi"),
  3907. },
  3908. },
  3909. },
  3910. expected: []metav1.TableRow{{Cells: []interface{}{"test2", "Lost", "", "", "", "", "<unknown>", "Filesystem"}}},
  3911. },
  3912. {
  3913. // Test name, num of containers, restarts, container ready status
  3914. pvc: api.PersistentVolumeClaim{
  3915. ObjectMeta: metav1.ObjectMeta{
  3916. Name: "test3",
  3917. },
  3918. Spec: api.PersistentVolumeClaimSpec{
  3919. VolumeName: "my-volume",
  3920. VolumeMode: &volumeMode,
  3921. },
  3922. Status: api.PersistentVolumeClaimStatus{
  3923. Phase: api.ClaimPending,
  3924. AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteMany},
  3925. Capacity: map[api.ResourceName]resource.Quantity{
  3926. api.ResourceStorage: resource.MustParse("10Gi"),
  3927. },
  3928. },
  3929. },
  3930. expected: []metav1.TableRow{{Cells: []interface{}{"test3", "Pending", "my-volume", "10Gi", "RWX", "", "<unknown>", "Filesystem"}}},
  3931. },
  3932. {
  3933. // Test name, num of containers, restarts, container ready status
  3934. pvc: api.PersistentVolumeClaim{
  3935. ObjectMeta: metav1.ObjectMeta{
  3936. Name: "test4",
  3937. },
  3938. Spec: api.PersistentVolumeClaimSpec{
  3939. VolumeName: "my-volume",
  3940. StorageClassName: &myScn,
  3941. VolumeMode: &volumeMode,
  3942. },
  3943. Status: api.PersistentVolumeClaimStatus{
  3944. Phase: api.ClaimPending,
  3945. AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce},
  3946. Capacity: map[api.ResourceName]resource.Quantity{
  3947. api.ResourceStorage: resource.MustParse("10Gi"),
  3948. },
  3949. },
  3950. },
  3951. expected: []metav1.TableRow{{Cells: []interface{}{"test4", "Pending", "my-volume", "10Gi", "RWO", "my-scn", "<unknown>", "Filesystem"}}},
  3952. },
  3953. {
  3954. // Test name, num of containers, restarts, container ready status
  3955. pvc: api.PersistentVolumeClaim{
  3956. ObjectMeta: metav1.ObjectMeta{
  3957. Name: "test5",
  3958. },
  3959. Spec: api.PersistentVolumeClaimSpec{
  3960. VolumeName: "my-volume",
  3961. StorageClassName: &myScn,
  3962. },
  3963. Status: api.PersistentVolumeClaimStatus{
  3964. Phase: api.ClaimPending,
  3965. AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce},
  3966. Capacity: map[api.ResourceName]resource.Quantity{
  3967. api.ResourceStorage: resource.MustParse("10Gi"),
  3968. },
  3969. },
  3970. },
  3971. expected: []metav1.TableRow{{Cells: []interface{}{"test5", "Pending", "my-volume", "10Gi", "RWO", "my-scn", "<unknown>", "<unset>"}}},
  3972. },
  3973. }
  3974. for i, test := range tests {
  3975. rows, err := printPersistentVolumeClaim(&test.pvc, printers.GenerateOptions{})
  3976. if err != nil {
  3977. t.Fatal(err)
  3978. }
  3979. for i := range rows {
  3980. rows[i].Object.Object = nil
  3981. }
  3982. if !reflect.DeepEqual(test.expected, rows) {
  3983. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  3984. }
  3985. }
  3986. }
  3987. func TestPrintComponentStatus(t *testing.T) {
  3988. tests := []struct {
  3989. componentStatus api.ComponentStatus
  3990. expected []metav1.TableRow
  3991. }{
  3992. // Basic component status without conditions
  3993. {
  3994. componentStatus: api.ComponentStatus{
  3995. ObjectMeta: metav1.ObjectMeta{
  3996. Name: "cs1",
  3997. },
  3998. Conditions: []api.ComponentCondition{},
  3999. },
  4000. // Columns: Name, Status, Message, Error
  4001. expected: []metav1.TableRow{{Cells: []interface{}{"cs1", "Unknown", "", ""}}},
  4002. },
  4003. // Basic component status with healthy condition.
  4004. {
  4005. componentStatus: api.ComponentStatus{
  4006. ObjectMeta: metav1.ObjectMeta{
  4007. Name: "cs2",
  4008. },
  4009. Conditions: []api.ComponentCondition{
  4010. {
  4011. Type: "Healthy",
  4012. Status: api.ConditionTrue,
  4013. Message: "test message",
  4014. Error: "test error",
  4015. },
  4016. },
  4017. },
  4018. // Columns: Name, Status, Message, Error
  4019. expected: []metav1.TableRow{{Cells: []interface{}{"cs2", "Healthy", "test message", "test error"}}},
  4020. },
  4021. // Basic component status with healthy condition.
  4022. {
  4023. componentStatus: api.ComponentStatus{
  4024. ObjectMeta: metav1.ObjectMeta{
  4025. Name: "cs3",
  4026. },
  4027. Conditions: []api.ComponentCondition{
  4028. {
  4029. Type: "Healthy",
  4030. Status: api.ConditionFalse,
  4031. Message: "test message",
  4032. Error: "test error",
  4033. },
  4034. },
  4035. },
  4036. // Columns: Name, Status, Message, Error
  4037. expected: []metav1.TableRow{{Cells: []interface{}{"cs3", "Unhealthy", "test message", "test error"}}},
  4038. },
  4039. }
  4040. for i, test := range tests {
  4041. rows, err := printComponentStatus(&test.componentStatus, printers.GenerateOptions{})
  4042. if err != nil {
  4043. t.Fatal(err)
  4044. }
  4045. for i := range rows {
  4046. rows[i].Object.Object = nil
  4047. }
  4048. if !reflect.DeepEqual(test.expected, rows) {
  4049. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  4050. }
  4051. }
  4052. }
  4053. func TestPrintCronJob(t *testing.T) {
  4054. completions := int32(2)
  4055. suspend := false
  4056. tests := []struct {
  4057. cronjob batch.CronJob
  4058. options printers.GenerateOptions
  4059. expected []metav1.TableRow
  4060. }{
  4061. // Basic cron job; does not print containers, images, or labels.
  4062. {
  4063. cronjob: batch.CronJob{
  4064. ObjectMeta: metav1.ObjectMeta{
  4065. Name: "cronjob1",
  4066. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  4067. },
  4068. Spec: batch.CronJobSpec{
  4069. Schedule: "0/5 * * * ?",
  4070. Suspend: &suspend,
  4071. JobTemplate: batch.JobTemplateSpec{
  4072. Spec: batch.JobSpec{
  4073. Completions: &completions,
  4074. Template: api.PodTemplateSpec{
  4075. Spec: api.PodSpec{
  4076. Containers: []api.Container{
  4077. {
  4078. Name: "fake-job-container1",
  4079. Image: "fake-job-image1",
  4080. },
  4081. {
  4082. Name: "fake-job-container2",
  4083. Image: "fake-job-image2",
  4084. },
  4085. },
  4086. },
  4087. },
  4088. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"a": "b"}},
  4089. },
  4090. },
  4091. },
  4092. Status: batch.CronJobStatus{
  4093. LastScheduleTime: &metav1.Time{Time: time.Now().Add(1.9e9)},
  4094. },
  4095. },
  4096. options: printers.GenerateOptions{},
  4097. // Columns: Name, Schedule, Suspend, Active, Last Schedule, Age
  4098. expected: []metav1.TableRow{{Cells: []interface{}{"cronjob1", "0/5 * * * ?", "False", int64(0), "0s", "0s"}}},
  4099. },
  4100. // Generate options: Wide; prints containers, images, and labels.
  4101. {
  4102. cronjob: batch.CronJob{
  4103. ObjectMeta: metav1.ObjectMeta{
  4104. Name: "cronjob1",
  4105. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  4106. },
  4107. Spec: batch.CronJobSpec{
  4108. Schedule: "0/5 * * * ?",
  4109. Suspend: &suspend,
  4110. JobTemplate: batch.JobTemplateSpec{
  4111. Spec: batch.JobSpec{
  4112. Completions: &completions,
  4113. Template: api.PodTemplateSpec{
  4114. Spec: api.PodSpec{
  4115. Containers: []api.Container{
  4116. {
  4117. Name: "fake-job-container1",
  4118. Image: "fake-job-image1",
  4119. },
  4120. {
  4121. Name: "fake-job-container2",
  4122. Image: "fake-job-image2",
  4123. },
  4124. },
  4125. },
  4126. },
  4127. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"a": "b"}},
  4128. },
  4129. },
  4130. },
  4131. Status: batch.CronJobStatus{
  4132. LastScheduleTime: &metav1.Time{Time: time.Now().Add(1.9e9)},
  4133. },
  4134. },
  4135. options: printers.GenerateOptions{Wide: true},
  4136. // Columns: Name, Schedule, Suspend, Active, Last Schedule, Age
  4137. expected: []metav1.TableRow{{Cells: []interface{}{"cronjob1", "0/5 * * * ?", "False", int64(0), "0s", "0s", "fake-job-container1,fake-job-container2", "fake-job-image1,fake-job-image2", "a=b"}}},
  4138. },
  4139. // CronJob with Last Schedule and Age
  4140. {
  4141. cronjob: batch.CronJob{
  4142. ObjectMeta: metav1.ObjectMeta{
  4143. Name: "cronjob2",
  4144. CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
  4145. },
  4146. Spec: batch.CronJobSpec{
  4147. Schedule: "0/5 * * * ?",
  4148. Suspend: &suspend,
  4149. },
  4150. Status: batch.CronJobStatus{
  4151. LastScheduleTime: &metav1.Time{Time: time.Now().Add(-3e10)},
  4152. },
  4153. },
  4154. options: printers.GenerateOptions{},
  4155. // Columns: Name, Schedule, Suspend, Active, Last Schedule, Age
  4156. expected: []metav1.TableRow{{Cells: []interface{}{"cronjob2", "0/5 * * * ?", "False", int64(0), "30s", "5m"}}},
  4157. },
  4158. // CronJob without Last Schedule
  4159. {
  4160. cronjob: batch.CronJob{
  4161. ObjectMeta: metav1.ObjectMeta{
  4162. Name: "cronjob3",
  4163. CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
  4164. },
  4165. Spec: batch.CronJobSpec{
  4166. Schedule: "0/5 * * * ?",
  4167. Suspend: &suspend,
  4168. },
  4169. Status: batch.CronJobStatus{},
  4170. },
  4171. options: printers.GenerateOptions{},
  4172. // Columns: Name, Schedule, Suspend, Active, Last Schedule, Age
  4173. expected: []metav1.TableRow{{Cells: []interface{}{"cronjob3", "0/5 * * * ?", "False", int64(0), "<none>", "5m"}}},
  4174. },
  4175. }
  4176. for i, test := range tests {
  4177. rows, err := printCronJob(&test.cronjob, test.options)
  4178. if err != nil {
  4179. t.Fatal(err)
  4180. }
  4181. for i := range rows {
  4182. rows[i].Object.Object = nil
  4183. }
  4184. if !reflect.DeepEqual(test.expected, rows) {
  4185. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  4186. }
  4187. }
  4188. }
  4189. func TestPrintCronJobList(t *testing.T) {
  4190. completions := int32(2)
  4191. suspend := false
  4192. cronJobList := batch.CronJobList{
  4193. Items: []batch.CronJob{
  4194. {
  4195. ObjectMeta: metav1.ObjectMeta{
  4196. Name: "cronjob1",
  4197. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  4198. },
  4199. Spec: batch.CronJobSpec{
  4200. Schedule: "0/5 * * * ?",
  4201. Suspend: &suspend,
  4202. JobTemplate: batch.JobTemplateSpec{
  4203. Spec: batch.JobSpec{
  4204. Completions: &completions,
  4205. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"a": "b"}},
  4206. },
  4207. },
  4208. },
  4209. Status: batch.CronJobStatus{
  4210. LastScheduleTime: &metav1.Time{Time: time.Now().Add(1.9e9)},
  4211. },
  4212. },
  4213. {
  4214. ObjectMeta: metav1.ObjectMeta{
  4215. Name: "cronjob2",
  4216. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  4217. },
  4218. Spec: batch.CronJobSpec{
  4219. Schedule: "4/5 1 1 1 ?",
  4220. Suspend: &suspend,
  4221. JobTemplate: batch.JobTemplateSpec{
  4222. Spec: batch.JobSpec{
  4223. Completions: &completions,
  4224. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"a": "b"}},
  4225. },
  4226. },
  4227. },
  4228. Status: batch.CronJobStatus{
  4229. LastScheduleTime: &metav1.Time{Time: time.Now().Add(-20 * time.Minute)},
  4230. },
  4231. },
  4232. },
  4233. }
  4234. // Columns: Name, Schedule, Suspend, Active, Last Schedule, Age
  4235. expectedRows := []metav1.TableRow{
  4236. {Cells: []interface{}{"cronjob1", "0/5 * * * ?", "False", int64(0), "0s", "0s"}},
  4237. {Cells: []interface{}{"cronjob2", "4/5 1 1 1 ?", "False", int64(0), "20m", "0s"}},
  4238. }
  4239. rows, err := printCronJobList(&cronJobList, printers.GenerateOptions{})
  4240. if err != nil {
  4241. t.Fatalf("Error printing job list: %#v", err)
  4242. }
  4243. for i := range rows {
  4244. rows[i].Object.Object = nil
  4245. }
  4246. if !reflect.DeepEqual(expectedRows, rows) {
  4247. t.Errorf("mismatch: %s", diff.ObjectReflectDiff(expectedRows, rows))
  4248. }
  4249. }
  4250. func TestPrintStorageClass(t *testing.T) {
  4251. policyDelte := api.PersistentVolumeReclaimDelete
  4252. policyRetain := api.PersistentVolumeReclaimRetain
  4253. bindModeImmediate := storage.VolumeBindingImmediate
  4254. bindModeWait := storage.VolumeBindingWaitForFirstConsumer
  4255. tests := []struct {
  4256. sc storage.StorageClass
  4257. expected []metav1.TableRow
  4258. }{
  4259. {
  4260. sc: storage.StorageClass{
  4261. ObjectMeta: metav1.ObjectMeta{
  4262. Name: "sc1",
  4263. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  4264. },
  4265. Provisioner: "kubernetes.io/glusterfs",
  4266. },
  4267. expected: []metav1.TableRow{{Cells: []interface{}{"sc1", "kubernetes.io/glusterfs", "Delete",
  4268. "Immediate", false, "0s"}}},
  4269. },
  4270. {
  4271. sc: storage.StorageClass{
  4272. ObjectMeta: metav1.ObjectMeta{
  4273. Name: "sc2",
  4274. CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
  4275. },
  4276. Provisioner: "kubernetes.io/nfs",
  4277. },
  4278. expected: []metav1.TableRow{{Cells: []interface{}{"sc2", "kubernetes.io/nfs", "Delete",
  4279. "Immediate", false, "5m"}}},
  4280. },
  4281. {
  4282. sc: storage.StorageClass{
  4283. ObjectMeta: metav1.ObjectMeta{
  4284. Name: "sc3",
  4285. CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
  4286. },
  4287. Provisioner: "kubernetes.io/nfs",
  4288. ReclaimPolicy: &policyDelte,
  4289. },
  4290. expected: []metav1.TableRow{{Cells: []interface{}{"sc3", "kubernetes.io/nfs", "Delete",
  4291. "Immediate", false, "5m"}}},
  4292. },
  4293. {
  4294. sc: storage.StorageClass{
  4295. ObjectMeta: metav1.ObjectMeta{
  4296. Name: "sc4",
  4297. CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
  4298. },
  4299. Provisioner: "kubernetes.io/nfs",
  4300. ReclaimPolicy: &policyRetain,
  4301. VolumeBindingMode: &bindModeImmediate,
  4302. },
  4303. expected: []metav1.TableRow{{Cells: []interface{}{"sc4", "kubernetes.io/nfs", "Retain",
  4304. "Immediate", false, "5m"}}},
  4305. },
  4306. {
  4307. sc: storage.StorageClass{
  4308. ObjectMeta: metav1.ObjectMeta{
  4309. Name: "sc5",
  4310. CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
  4311. },
  4312. Provisioner: "kubernetes.io/nfs",
  4313. ReclaimPolicy: &policyRetain,
  4314. VolumeBindingMode: &bindModeWait,
  4315. },
  4316. expected: []metav1.TableRow{{Cells: []interface{}{"sc5", "kubernetes.io/nfs", "Retain",
  4317. "WaitForFirstConsumer", false, "5m"}}},
  4318. },
  4319. {
  4320. sc: storage.StorageClass{
  4321. ObjectMeta: metav1.ObjectMeta{
  4322. Name: "sc6",
  4323. CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
  4324. },
  4325. Provisioner: "kubernetes.io/nfs",
  4326. ReclaimPolicy: &policyRetain,
  4327. AllowVolumeExpansion: boolP(true),
  4328. VolumeBindingMode: &bindModeWait,
  4329. },
  4330. expected: []metav1.TableRow{{Cells: []interface{}{"sc6", "kubernetes.io/nfs", "Retain",
  4331. "WaitForFirstConsumer", true, "5m"}}},
  4332. },
  4333. }
  4334. for i, test := range tests {
  4335. rows, err := printStorageClass(&test.sc, printers.GenerateOptions{})
  4336. if err != nil {
  4337. t.Fatal(err)
  4338. }
  4339. for i := range rows {
  4340. rows[i].Object.Object = nil
  4341. }
  4342. if !reflect.DeepEqual(test.expected, rows) {
  4343. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  4344. }
  4345. }
  4346. }
  4347. func TestPrintLease(t *testing.T) {
  4348. holder1 := "holder1"
  4349. holder2 := "holder2"
  4350. tests := []struct {
  4351. lease coordination.Lease
  4352. expected []metav1.TableRow
  4353. }{
  4354. {
  4355. lease: coordination.Lease{
  4356. ObjectMeta: metav1.ObjectMeta{
  4357. Name: "lease1",
  4358. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  4359. },
  4360. Spec: coordination.LeaseSpec{
  4361. HolderIdentity: &holder1,
  4362. },
  4363. },
  4364. expected: []metav1.TableRow{{Cells: []interface{}{"lease1", "holder1", "0s"}}},
  4365. },
  4366. {
  4367. lease: coordination.Lease{
  4368. ObjectMeta: metav1.ObjectMeta{
  4369. Name: "lease2",
  4370. CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
  4371. },
  4372. Spec: coordination.LeaseSpec{
  4373. HolderIdentity: &holder2,
  4374. },
  4375. },
  4376. expected: []metav1.TableRow{{Cells: []interface{}{"lease2", "holder2", "5m"}}},
  4377. },
  4378. }
  4379. for i, test := range tests {
  4380. rows, err := printLease(&test.lease, printers.GenerateOptions{})
  4381. if err != nil {
  4382. t.Fatal(err)
  4383. }
  4384. for i := range rows {
  4385. rows[i].Object.Object = nil
  4386. }
  4387. if !reflect.DeepEqual(test.expected, rows) {
  4388. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  4389. }
  4390. }
  4391. }
  4392. func TestPrintPriorityClass(t *testing.T) {
  4393. tests := []struct {
  4394. pc scheduling.PriorityClass
  4395. expected []metav1.TableRow
  4396. }{
  4397. {
  4398. pc: scheduling.PriorityClass{
  4399. ObjectMeta: metav1.ObjectMeta{
  4400. Name: "pc1",
  4401. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  4402. },
  4403. Value: 1,
  4404. },
  4405. expected: []metav1.TableRow{{Cells: []interface{}{"pc1", int64(1), bool(false), "0s"}}},
  4406. },
  4407. {
  4408. pc: scheduling.PriorityClass{
  4409. ObjectMeta: metav1.ObjectMeta{
  4410. Name: "pc2",
  4411. CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
  4412. },
  4413. Value: 1000000000,
  4414. GlobalDefault: true,
  4415. },
  4416. expected: []metav1.TableRow{{Cells: []interface{}{"pc2", int64(1000000000), bool(true), "5m"}}},
  4417. },
  4418. }
  4419. for i, test := range tests {
  4420. rows, err := printPriorityClass(&test.pc, printers.GenerateOptions{})
  4421. if err != nil {
  4422. t.Fatal(err)
  4423. }
  4424. for i := range rows {
  4425. rows[i].Object.Object = nil
  4426. }
  4427. if !reflect.DeepEqual(test.expected, rows) {
  4428. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  4429. }
  4430. }
  4431. }
  4432. func TestPrintRuntimeClass(t *testing.T) {
  4433. tests := []struct {
  4434. rc nodeapi.RuntimeClass
  4435. expected []metav1.TableRow
  4436. }{
  4437. {
  4438. rc: nodeapi.RuntimeClass{
  4439. ObjectMeta: metav1.ObjectMeta{
  4440. Name: "rc1",
  4441. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  4442. },
  4443. Handler: "h1",
  4444. },
  4445. expected: []metav1.TableRow{{Cells: []interface{}{"rc1", "h1", "0s"}}},
  4446. },
  4447. {
  4448. rc: nodeapi.RuntimeClass{
  4449. ObjectMeta: metav1.ObjectMeta{
  4450. Name: "rc2",
  4451. CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
  4452. },
  4453. Handler: "h2",
  4454. },
  4455. expected: []metav1.TableRow{{Cells: []interface{}{"rc2", "h2", "5m"}}},
  4456. },
  4457. }
  4458. for i, test := range tests {
  4459. rows, err := printRuntimeClass(&test.rc, printers.GenerateOptions{})
  4460. if err != nil {
  4461. t.Fatal(err)
  4462. }
  4463. for i := range rows {
  4464. rows[i].Object.Object = nil
  4465. }
  4466. if !reflect.DeepEqual(test.expected, rows) {
  4467. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  4468. }
  4469. }
  4470. }
  4471. func TestPrintEndpoint(t *testing.T) {
  4472. tests := []struct {
  4473. endpoint api.Endpoints
  4474. expected []metav1.TableRow
  4475. }{
  4476. // Basic endpoint with no IP's
  4477. {
  4478. endpoint: api.Endpoints{
  4479. ObjectMeta: metav1.ObjectMeta{
  4480. Name: "endpoint1",
  4481. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  4482. },
  4483. },
  4484. // Columns: Name, Endpoints, Age
  4485. expected: []metav1.TableRow{{Cells: []interface{}{"endpoint1", "<none>", "0s"}}},
  4486. },
  4487. // Endpoint with no ports
  4488. {
  4489. endpoint: api.Endpoints{
  4490. ObjectMeta: metav1.ObjectMeta{
  4491. Name: "endpoint3",
  4492. CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
  4493. },
  4494. Subsets: []api.EndpointSubset{
  4495. {
  4496. Addresses: []api.EndpointAddress{
  4497. {
  4498. IP: "1.2.3.4",
  4499. },
  4500. {
  4501. IP: "5.6.7.8",
  4502. },
  4503. },
  4504. },
  4505. },
  4506. },
  4507. // Columns: Name, Endpoints, Age
  4508. expected: []metav1.TableRow{{Cells: []interface{}{"endpoint3", "1.2.3.4,5.6.7.8", "5m"}}},
  4509. },
  4510. // Basic endpoint with two IP's and one port
  4511. {
  4512. endpoint: api.Endpoints{
  4513. ObjectMeta: metav1.ObjectMeta{
  4514. Name: "endpoint2",
  4515. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  4516. },
  4517. Subsets: []api.EndpointSubset{
  4518. {
  4519. Addresses: []api.EndpointAddress{
  4520. {
  4521. IP: "1.2.3.4",
  4522. },
  4523. {
  4524. IP: "5.6.7.8",
  4525. },
  4526. },
  4527. Ports: []api.EndpointPort{
  4528. {
  4529. Port: 8001,
  4530. Protocol: "tcp",
  4531. },
  4532. },
  4533. },
  4534. },
  4535. },
  4536. // Columns: Name, Endpoints, Age
  4537. expected: []metav1.TableRow{{Cells: []interface{}{"endpoint2", "1.2.3.4:8001,5.6.7.8:8001", "0s"}}},
  4538. },
  4539. // Basic endpoint with greater than three IP's triggering "more" string
  4540. {
  4541. endpoint: api.Endpoints{
  4542. ObjectMeta: metav1.ObjectMeta{
  4543. Name: "endpoint2",
  4544. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  4545. },
  4546. Subsets: []api.EndpointSubset{
  4547. {
  4548. Addresses: []api.EndpointAddress{
  4549. {
  4550. IP: "1.2.3.4",
  4551. },
  4552. {
  4553. IP: "5.6.7.8",
  4554. },
  4555. {
  4556. IP: "9.8.7.6",
  4557. },
  4558. {
  4559. IP: "6.6.6.6",
  4560. },
  4561. },
  4562. Ports: []api.EndpointPort{
  4563. {
  4564. Port: 8001,
  4565. Protocol: "tcp",
  4566. },
  4567. },
  4568. },
  4569. },
  4570. },
  4571. // Columns: Name, Endpoints, Age
  4572. expected: []metav1.TableRow{{Cells: []interface{}{"endpoint2", "1.2.3.4:8001,5.6.7.8:8001,9.8.7.6:8001 + 1 more...", "0s"}}},
  4573. },
  4574. }
  4575. for i, test := range tests {
  4576. rows, err := printEndpoints(&test.endpoint, printers.GenerateOptions{})
  4577. if err != nil {
  4578. t.Fatal(err)
  4579. }
  4580. for i := range rows {
  4581. rows[i].Object.Object = nil
  4582. }
  4583. if !reflect.DeepEqual(test.expected, rows) {
  4584. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  4585. }
  4586. }
  4587. }
  4588. func TestPrintEndpointSlice(t *testing.T) {
  4589. tcpProtocol := api.ProtocolTCP
  4590. tests := []struct {
  4591. endpointSlice discovery.EndpointSlice
  4592. expected []metav1.TableRow
  4593. }{
  4594. {
  4595. endpointSlice: discovery.EndpointSlice{
  4596. ObjectMeta: metav1.ObjectMeta{
  4597. Name: "abcslice.123",
  4598. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  4599. },
  4600. AddressType: discovery.AddressTypeIPv4,
  4601. Ports: []discovery.EndpointPort{{
  4602. Name: utilpointer.StringPtr("http"),
  4603. Port: utilpointer.Int32Ptr(80),
  4604. Protocol: &tcpProtocol,
  4605. }},
  4606. Endpoints: []discovery.Endpoint{{
  4607. Addresses: []string{"10.1.2.3", "2001:db8::1234:5678"},
  4608. }},
  4609. },
  4610. // Columns: Name, AddressType, Ports, Endpoints, Age
  4611. expected: []metav1.TableRow{{Cells: []interface{}{"abcslice.123", "IPv4", "80", "10.1.2.3,2001:db8::1234:5678", "0s"}}},
  4612. }, {
  4613. endpointSlice: discovery.EndpointSlice{
  4614. ObjectMeta: metav1.ObjectMeta{
  4615. Name: "longerslicename.123",
  4616. CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
  4617. },
  4618. AddressType: discovery.AddressTypeIPv6,
  4619. Ports: []discovery.EndpointPort{{
  4620. Name: utilpointer.StringPtr("http"),
  4621. Port: utilpointer.Int32Ptr(80),
  4622. Protocol: &tcpProtocol,
  4623. }, {
  4624. Name: utilpointer.StringPtr("https"),
  4625. Port: utilpointer.Int32Ptr(443),
  4626. Protocol: &tcpProtocol,
  4627. }},
  4628. Endpoints: []discovery.Endpoint{{
  4629. Addresses: []string{"10.1.2.3", "2001:db8::1234:5678"},
  4630. }, {
  4631. Addresses: []string{"10.2.3.4", "2001:db8::2345:6789"},
  4632. }},
  4633. },
  4634. // Columns: Name, AddressType, Ports, Endpoints, Age
  4635. expected: []metav1.TableRow{{Cells: []interface{}{"longerslicename.123", "IPv6", "80,443", "10.1.2.3,2001:db8::1234:5678,10.2.3.4 + 1 more...", "5m"}}},
  4636. }, {
  4637. endpointSlice: discovery.EndpointSlice{
  4638. ObjectMeta: metav1.ObjectMeta{
  4639. Name: "multiportslice.123",
  4640. CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
  4641. },
  4642. AddressType: discovery.AddressTypeIPv4,
  4643. Ports: []discovery.EndpointPort{{
  4644. Name: utilpointer.StringPtr("http"),
  4645. Port: utilpointer.Int32Ptr(80),
  4646. Protocol: &tcpProtocol,
  4647. }, {
  4648. Name: utilpointer.StringPtr("https"),
  4649. Port: utilpointer.Int32Ptr(443),
  4650. Protocol: &tcpProtocol,
  4651. }, {
  4652. Name: utilpointer.StringPtr("extra1"),
  4653. Port: utilpointer.Int32Ptr(3000),
  4654. Protocol: &tcpProtocol,
  4655. }, {
  4656. Name: utilpointer.StringPtr("extra2"),
  4657. Port: utilpointer.Int32Ptr(3001),
  4658. Protocol: &tcpProtocol,
  4659. }},
  4660. Endpoints: []discovery.Endpoint{{
  4661. Addresses: []string{"10.1.2.3", "2001:db8::1234:5678"},
  4662. }, {
  4663. Addresses: []string{"10.2.3.4", "2001:db8::2345:6789"},
  4664. }},
  4665. },
  4666. // Columns: Name, AddressType, Ports, Endpoints, Age
  4667. expected: []metav1.TableRow{{Cells: []interface{}{"multiportslice.123", "IPv4", "80,443,3000 + 1 more...", "10.1.2.3,2001:db8::1234:5678,10.2.3.4 + 1 more...", "5m"}}},
  4668. },
  4669. }
  4670. for i, test := range tests {
  4671. rows, err := printEndpointSlice(&test.endpointSlice, printers.GenerateOptions{})
  4672. if err != nil {
  4673. t.Fatal(err)
  4674. }
  4675. for i := range rows {
  4676. rows[i].Object.Object = nil
  4677. }
  4678. if !reflect.DeepEqual(test.expected, rows) {
  4679. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  4680. }
  4681. }
  4682. }
  4683. func TestPrintFlowSchema(t *testing.T) {
  4684. all := []string{"*"}
  4685. tests := []struct {
  4686. fs flowcontrol.FlowSchema
  4687. expected []metav1.TableRow
  4688. }{
  4689. {
  4690. fs: flowcontrol.FlowSchema{
  4691. ObjectMeta: metav1.ObjectMeta{
  4692. Name: "all-matcher",
  4693. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  4694. },
  4695. Spec: flowcontrol.FlowSchemaSpec{
  4696. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{Name: "allee"},
  4697. MatchingPrecedence: math.MaxInt32,
  4698. DistinguisherMethod: &flowcontrol.FlowDistinguisherMethod{Type: flowcontrol.FlowDistinguisherMethodByUserType},
  4699. Rules: []flowcontrol.PolicyRulesWithSubjects{{
  4700. Subjects: []flowcontrol.Subject{{
  4701. Kind: flowcontrol.SubjectKindGroup,
  4702. Group: &flowcontrol.GroupSubject{Name: "system:authenticated"},
  4703. }},
  4704. ResourceRules: []flowcontrol.ResourcePolicyRule{{
  4705. Verbs: all,
  4706. APIGroups: all,
  4707. Resources: all,
  4708. ClusterScope: true,
  4709. Namespaces: all,
  4710. }},
  4711. }, {
  4712. Subjects: []flowcontrol.Subject{{
  4713. Kind: flowcontrol.SubjectKindGroup,
  4714. Group: &flowcontrol.GroupSubject{Name: "system:unauthenticated"},
  4715. }},
  4716. ResourceRules: []flowcontrol.ResourcePolicyRule{{
  4717. Verbs: all,
  4718. APIGroups: all,
  4719. Resources: all,
  4720. ClusterScope: true,
  4721. Namespaces: all,
  4722. }},
  4723. }, {
  4724. Subjects: []flowcontrol.Subject{{
  4725. Kind: flowcontrol.SubjectKindGroup,
  4726. Group: &flowcontrol.GroupSubject{Name: "system:authenticated"},
  4727. }, {
  4728. Kind: flowcontrol.SubjectKindGroup,
  4729. Group: &flowcontrol.GroupSubject{Name: "system:unauthenticated"},
  4730. }},
  4731. NonResourceRules: []flowcontrol.NonResourcePolicyRule{{
  4732. Verbs: all,
  4733. NonResourceURLs: all,
  4734. }},
  4735. }},
  4736. },
  4737. },
  4738. // Columns: Name, PriorityLevelName, MatchingPrecedence, DistinguisherMethod, Age, MissingPL
  4739. expected: []metav1.TableRow{{Cells: []interface{}{"all-matcher", "allee", int32(math.MaxInt32), "ByUser", "0s", "?"}}},
  4740. }, {
  4741. fs: flowcontrol.FlowSchema{
  4742. ObjectMeta: metav1.ObjectMeta{
  4743. Name: "some-matcher",
  4744. CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
  4745. },
  4746. Spec: flowcontrol.FlowSchemaSpec{
  4747. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{Name: "allee"},
  4748. MatchingPrecedence: 0,
  4749. DistinguisherMethod: &flowcontrol.FlowDistinguisherMethod{Type: flowcontrol.FlowDistinguisherMethodByNamespaceType},
  4750. Rules: []flowcontrol.PolicyRulesWithSubjects{{
  4751. Subjects: []flowcontrol.Subject{{
  4752. Kind: flowcontrol.SubjectKindGroup,
  4753. Group: &flowcontrol.GroupSubject{Name: "system:unauthenticated"},
  4754. }},
  4755. ResourceRules: []flowcontrol.ResourcePolicyRule{{
  4756. Verbs: all,
  4757. APIGroups: all,
  4758. Resources: all,
  4759. ClusterScope: true,
  4760. Namespaces: all,
  4761. }},
  4762. }, {
  4763. Subjects: []flowcontrol.Subject{{
  4764. Kind: flowcontrol.SubjectKindGroup,
  4765. Group: &flowcontrol.GroupSubject{Name: "system:authenticated"},
  4766. }, {
  4767. Kind: flowcontrol.SubjectKindGroup,
  4768. Group: &flowcontrol.GroupSubject{Name: "system:unauthenticated"},
  4769. }},
  4770. NonResourceRules: []flowcontrol.NonResourcePolicyRule{{
  4771. Verbs: all,
  4772. NonResourceURLs: all,
  4773. }},
  4774. }},
  4775. },
  4776. Status: flowcontrol.FlowSchemaStatus{
  4777. Conditions: []flowcontrol.FlowSchemaCondition{{
  4778. Type: flowcontrol.FlowSchemaConditionDangling,
  4779. Status: "True",
  4780. LastTransitionTime: metav1.Time{Time: time.Now().Add(-time.Hour)},
  4781. }},
  4782. },
  4783. },
  4784. // Columns: Name, PriorityLevelName, MatchingPrecedence, DistinguisherMethod, Age, MissingPL
  4785. expected: []metav1.TableRow{{Cells: []interface{}{"some-matcher", "allee", int32(0), "ByNamespace", "5m", "True"}}},
  4786. }, {
  4787. fs: flowcontrol.FlowSchema{
  4788. ObjectMeta: metav1.ObjectMeta{
  4789. Name: "exempt",
  4790. CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
  4791. },
  4792. Spec: flowcontrol.FlowSchemaSpec{
  4793. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{Name: "allee"},
  4794. MatchingPrecedence: 0,
  4795. DistinguisherMethod: nil,
  4796. Rules: []flowcontrol.PolicyRulesWithSubjects{{
  4797. Subjects: []flowcontrol.Subject{{
  4798. Kind: flowcontrol.SubjectKindGroup,
  4799. Group: &flowcontrol.GroupSubject{Name: "system:masters"},
  4800. }},
  4801. ResourceRules: []flowcontrol.ResourcePolicyRule{{
  4802. Verbs: all,
  4803. APIGroups: all,
  4804. Resources: all,
  4805. ClusterScope: true,
  4806. Namespaces: all,
  4807. }},
  4808. }},
  4809. },
  4810. },
  4811. // Columns: Name, PriorityLevelName, MatchingPrecedence, DistinguisherMethod, Age, MissingPL
  4812. expected: []metav1.TableRow{{Cells: []interface{}{"exempt", "allee", int32(0), "<none>", "5m", "?"}}},
  4813. },
  4814. }
  4815. for i, test := range tests {
  4816. rows, err := printFlowSchema(&test.fs, printers.GenerateOptions{})
  4817. if err != nil {
  4818. t.Fatal(err)
  4819. }
  4820. for i := range rows {
  4821. rows[i].Object.Object = nil
  4822. }
  4823. if !reflect.DeepEqual(test.expected, rows) {
  4824. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  4825. }
  4826. }
  4827. }
  4828. func TestPrintPriorityLevelConfiguration(t *testing.T) {
  4829. tests := []struct {
  4830. pl flowcontrol.PriorityLevelConfiguration
  4831. expected []metav1.TableRow
  4832. }{
  4833. {
  4834. pl: flowcontrol.PriorityLevelConfiguration{
  4835. ObjectMeta: metav1.ObjectMeta{
  4836. Name: "unlimited",
  4837. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  4838. },
  4839. Spec: flowcontrol.PriorityLevelConfigurationSpec{
  4840. Type: flowcontrol.PriorityLevelEnablementExempt,
  4841. },
  4842. },
  4843. // Columns: Name, Type, AssuredConcurrencyShares, Queues, HandSize, QueueLengthLimit, Age
  4844. expected: []metav1.TableRow{{Cells: []interface{}{"unlimited", "Exempt", "<none>", "<none>", "<none>", "<none>", "0s"}}},
  4845. },
  4846. {
  4847. pl: flowcontrol.PriorityLevelConfiguration{
  4848. ObjectMeta: metav1.ObjectMeta{
  4849. Name: "unqueued",
  4850. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  4851. },
  4852. Spec: flowcontrol.PriorityLevelConfigurationSpec{
  4853. Type: flowcontrol.PriorityLevelEnablementLimited,
  4854. Limited: &flowcontrol.LimitedPriorityLevelConfiguration{
  4855. AssuredConcurrencyShares: 47,
  4856. LimitResponse: flowcontrol.LimitResponse{
  4857. Type: flowcontrol.LimitResponseTypeReject,
  4858. },
  4859. },
  4860. },
  4861. },
  4862. // Columns: Name, Type, AssuredConcurrencyShares, Queues, HandSize, QueueLengthLimit, Age
  4863. expected: []metav1.TableRow{{Cells: []interface{}{"unqueued", "Limited", int32(47), "<none>", "<none>", "<none>", "0s"}}},
  4864. },
  4865. {
  4866. pl: flowcontrol.PriorityLevelConfiguration{
  4867. ObjectMeta: metav1.ObjectMeta{
  4868. Name: "queued",
  4869. CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
  4870. },
  4871. Spec: flowcontrol.PriorityLevelConfigurationSpec{
  4872. Type: flowcontrol.PriorityLevelEnablementLimited,
  4873. Limited: &flowcontrol.LimitedPriorityLevelConfiguration{
  4874. AssuredConcurrencyShares: 42,
  4875. LimitResponse: flowcontrol.LimitResponse{
  4876. Type: flowcontrol.LimitResponseTypeQueue,
  4877. Queuing: &flowcontrol.QueuingConfiguration{
  4878. Queues: 8,
  4879. HandSize: 3,
  4880. QueueLengthLimit: 4,
  4881. },
  4882. },
  4883. },
  4884. },
  4885. },
  4886. // Columns: Name, Type, AssuredConcurrencyShares, Queues, HandSize, QueueLengthLimit, Age
  4887. expected: []metav1.TableRow{{Cells: []interface{}{"queued", "Limited", int32(42), int32(8), int32(3), int32(4), "0s"}}},
  4888. },
  4889. }
  4890. for i, test := range tests {
  4891. rows, err := printPriorityLevelConfiguration(&test.pl, printers.GenerateOptions{})
  4892. if err != nil {
  4893. t.Fatal(err)
  4894. }
  4895. for i := range rows {
  4896. rows[i].Object.Object = nil
  4897. }
  4898. if !reflect.DeepEqual(test.expected, rows) {
  4899. t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
  4900. }
  4901. }
  4902. }