printers_test.go 155 KB

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