12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084 |
- /*
- Copyright 2014 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package internalversion
- import (
- "math"
- "reflect"
- "testing"
- "time"
- "k8s.io/apimachinery/pkg/api/resource"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/runtime/schema"
- "k8s.io/apimachinery/pkg/util/diff"
- "k8s.io/apimachinery/pkg/util/intstr"
- "k8s.io/kubernetes/pkg/apis/apps"
- "k8s.io/kubernetes/pkg/apis/autoscaling"
- "k8s.io/kubernetes/pkg/apis/batch"
- "k8s.io/kubernetes/pkg/apis/certificates"
- "k8s.io/kubernetes/pkg/apis/coordination"
- api "k8s.io/kubernetes/pkg/apis/core"
- "k8s.io/kubernetes/pkg/apis/discovery"
- "k8s.io/kubernetes/pkg/apis/flowcontrol"
- "k8s.io/kubernetes/pkg/apis/networking"
- nodeapi "k8s.io/kubernetes/pkg/apis/node"
- "k8s.io/kubernetes/pkg/apis/policy"
- "k8s.io/kubernetes/pkg/apis/rbac"
- "k8s.io/kubernetes/pkg/apis/scheduling"
- "k8s.io/kubernetes/pkg/apis/storage"
- "k8s.io/kubernetes/pkg/printers"
- utilpointer "k8s.io/utils/pointer"
- )
- func TestFormatResourceName(t *testing.T) {
- tests := []struct {
- kind schema.GroupKind
- name string
- want string
- }{
- {schema.GroupKind{}, "", ""},
- {schema.GroupKind{}, "name", "name"},
- {schema.GroupKind{Kind: "Kind"}, "", "kind/"}, // should not happen in practice
- {schema.GroupKind{Kind: "Kind"}, "name", "kind/name"},
- {schema.GroupKind{Group: "group", Kind: "Kind"}, "name", "kind.group/name"},
- }
- for _, tt := range tests {
- if got := formatResourceName(tt.kind, tt.name, true); got != tt.want {
- t.Errorf("formatResourceName(%q, %q) = %q, want %q", tt.kind, tt.name, got, tt.want)
- }
- }
- }
- type TestPrintHandler struct {
- numCalls int
- }
- func (t *TestPrintHandler) TableHandler(columnDefinitions []metav1.TableColumnDefinition, printFunc interface{}) error {
- t.numCalls++
- return nil
- }
- func (t *TestPrintHandler) getNumCalls() int {
- return t.numCalls
- }
- func TestAllHandlers(t *testing.T) {
- h := &TestPrintHandler{numCalls: 0}
- AddHandlers(h)
- if h.getNumCalls() == 0 {
- t.Error("TableHandler not called in AddHandlers")
- }
- }
- func TestPrintEvent(t *testing.T) {
- tests := []struct {
- event api.Event
- options printers.GenerateOptions
- expected []metav1.TableRow
- }{
- // Basic event; no generate options
- {
- event: api.Event{
- Source: api.EventSource{Component: "kubelet"},
- InvolvedObject: api.ObjectReference{
- Kind: "Pod",
- Name: "Pod Name",
- FieldPath: "spec.containers{foo}",
- },
- Reason: "Event Reason",
- Message: "Message Data",
- FirstTimestamp: metav1.Time{Time: time.Now().UTC().AddDate(0, 0, -3)},
- LastTimestamp: metav1.Time{Time: time.Now().UTC().AddDate(0, 0, -2)},
- Count: 6,
- Type: api.EventTypeNormal,
- ObjectMeta: metav1.ObjectMeta{Name: "event1"},
- },
- options: printers.GenerateOptions{},
- // Columns: Last Seen, Type, Reason, Object, Message
- expected: []metav1.TableRow{{Cells: []interface{}{"2d", "Normal", "Event Reason", "pod/Pod Name", "Message Data"}}},
- },
- // Basic event; generate options=Wide
- {
- event: api.Event{
- Source: api.EventSource{
- Component: "kubelet",
- Host: "Node1",
- },
- InvolvedObject: api.ObjectReference{
- Kind: "Deployment",
- Name: "Deployment Name",
- FieldPath: "spec.containers{foo}",
- },
- Reason: "Event Reason",
- Message: "Message Data",
- FirstTimestamp: metav1.Time{Time: time.Now().UTC().AddDate(0, 0, -3)},
- LastTimestamp: metav1.Time{Time: time.Now().UTC().AddDate(0, 0, -2)},
- Count: 6,
- Type: api.EventTypeWarning,
- ObjectMeta: metav1.ObjectMeta{Name: "event2"},
- },
- options: printers.GenerateOptions{Wide: true},
- // Columns: Last Seen, Type, Reason, Object, Subobject, Message, First Seen, Count, Name
- expected: []metav1.TableRow{{Cells: []interface{}{"2d", "Warning", "Event Reason", "deployment/Deployment Name", "spec.containers{foo}", "kubelet, Node1", "Message Data", "3d", int64(6), "event2"}}},
- },
- // Basic event, w/o LastTimestamp set
- {
- event: api.Event{
- Source: api.EventSource{
- Component: "kubelet",
- Host: "Node1",
- },
- InvolvedObject: api.ObjectReference{
- Kind: "Deployment",
- Name: "Deployment Name",
- FieldPath: "spec.containers{foo}",
- },
- Reason: "Event Reason",
- Message: "Message Data",
- FirstTimestamp: metav1.Time{Time: time.Now().UTC().AddDate(0, 0, -3)},
- Count: 1,
- Type: api.EventTypeWarning,
- ObjectMeta: metav1.ObjectMeta{Name: "event3"},
- },
- options: printers.GenerateOptions{Wide: true},
- // Columns: Last Seen, Type, Reason, Object, Subobject, Message, First Seen, Count, Name
- expected: []metav1.TableRow{{Cells: []interface{}{"3d", "Warning", "Event Reason", "deployment/Deployment Name", "spec.containers{foo}", "kubelet, Node1", "Message Data", "3d", int64(1), "event3"}}},
- },
- }
- for i, test := range tests {
- rows, err := printEvent(&test.event, test.options)
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintEventsResultSorted(t *testing.T) {
- eventList := api.EventList{
- Items: []api.Event{
- {
- Source: api.EventSource{Component: "kubelet"},
- Message: "Item 1",
- FirstTimestamp: metav1.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
- LastTimestamp: metav1.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
- Count: 1,
- Type: api.EventTypeNormal,
- },
- {
- Source: api.EventSource{Component: "scheduler"},
- Message: "Item 2",
- FirstTimestamp: metav1.NewTime(time.Date(1987, time.June, 17, 0, 0, 0, 0, time.UTC)),
- LastTimestamp: metav1.NewTime(time.Date(1987, time.June, 17, 0, 0, 0, 0, time.UTC)),
- Count: 1,
- Type: api.EventTypeNormal,
- },
- {
- Source: api.EventSource{Component: "kubelet"},
- Message: "Item 3",
- FirstTimestamp: metav1.NewTime(time.Date(2002, time.December, 25, 0, 0, 0, 0, time.UTC)),
- LastTimestamp: metav1.NewTime(time.Date(2002, time.December, 25, 0, 0, 0, 0, time.UTC)),
- Count: 1,
- Type: api.EventTypeNormal,
- },
- },
- }
- rows, err := printEventList(&eventList, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- if len(rows) != 3 {
- t.Errorf("Generate Event List: Wrong number of table rows returned. Expected 3, got (%d)", len(rows))
- }
- // Verify the watch event dates are in order.
- firstRow := rows[0]
- message1 := firstRow.Cells[4]
- if message1.(string) != "Item 1" {
- t.Errorf("Wrong event ordering: expecting (Item 1), got (%s)", message1)
- }
- secondRow := rows[1]
- message2 := secondRow.Cells[4]
- if message2 != "Item 2" {
- t.Errorf("Wrong event ordering: expecting (Item 2), got (%s)", message2)
- }
- thirdRow := rows[2]
- message3 := thirdRow.Cells[4]
- if message3 != "Item 3" {
- t.Errorf("Wrong event ordering: expecting (Item 3), got (%s)", message3)
- }
- }
- func TestPrintNamespace(t *testing.T) {
- tests := []struct {
- namespace api.Namespace
- expected []metav1.TableRow
- }{
- // Basic namespace with status and age.
- {
- namespace: api.Namespace{
- ObjectMeta: metav1.ObjectMeta{
- Name: "namespace1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Status: api.NamespaceStatus{
- Phase: "FooStatus",
- },
- },
- // Columns: Name, Status, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"namespace1", "FooStatus", "0s"}}},
- },
- // Basic namespace without status or age.
- {
- namespace: api.Namespace{
- ObjectMeta: metav1.ObjectMeta{
- Name: "namespace2",
- },
- },
- // Columns: Name, Status, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"namespace2", "", "<unknown>"}}},
- },
- }
- for i, test := range tests {
- rows, err := printNamespace(&test.namespace, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintSecret(t *testing.T) {
- tests := []struct {
- secret api.Secret
- expected []metav1.TableRow
- }{
- // Basic namespace with type, data, and age.
- {
- secret: api.Secret{
- ObjectMeta: metav1.ObjectMeta{
- Name: "secret1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Type: "kubernetes.io/service-account-token",
- Data: map[string][]byte{
- "token": []byte("secret data"),
- },
- },
- // Columns: Name, Type, Data, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"secret1", "kubernetes.io/service-account-token", int64(1), "0s"}}},
- },
- // Basic namespace with type and age; no data.
- {
- secret: api.Secret{
- ObjectMeta: metav1.ObjectMeta{
- Name: "secret1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Type: "kubernetes.io/service-account-token",
- },
- // Columns: Name, Type, Data, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"secret1", "kubernetes.io/service-account-token", int64(0), "0s"}}},
- },
- }
- for i, test := range tests {
- rows, err := printSecret(&test.secret, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintServiceAccount(t *testing.T) {
- tests := []struct {
- serviceAccount api.ServiceAccount
- expected []metav1.TableRow
- }{
- // Basic service account without secrets
- {
- serviceAccount: api.ServiceAccount{
- ObjectMeta: metav1.ObjectMeta{
- Name: "sa1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Secrets: []api.ObjectReference{},
- },
- // Columns: Name, (Num) Secrets, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"sa1", int64(0), "0s"}}},
- },
- // Basic service account with two secrets.
- {
- serviceAccount: api.ServiceAccount{
- ObjectMeta: metav1.ObjectMeta{
- Name: "sa1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Secrets: []api.ObjectReference{
- {Name: "Secret1"},
- {Name: "Secret2"},
- },
- },
- // Columns: Name, (Num) Secrets, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"sa1", int64(2), "0s"}}},
- },
- }
- for i, test := range tests {
- rows, err := printServiceAccount(&test.serviceAccount, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintNodeStatus(t *testing.T) {
- table := []struct {
- node api.Node
- expected []metav1.TableRow
- }{
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
- Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: api.NodeReady, Status: api.ConditionTrue}}},
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion
- expected: []metav1.TableRow{{Cells: []interface{}{"foo1", "Ready", "<none>", "<unknown>", ""}}},
- },
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo2"},
- Spec: api.NodeSpec{Unschedulable: true},
- Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: api.NodeReady, Status: api.ConditionTrue}}},
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion
- expected: []metav1.TableRow{{Cells: []interface{}{"foo2", "Ready,SchedulingDisabled", "<none>", "<unknown>", ""}}},
- },
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo3"},
- Status: api.NodeStatus{Conditions: []api.NodeCondition{
- {Type: api.NodeReady, Status: api.ConditionTrue},
- {Type: api.NodeReady, Status: api.ConditionTrue}}},
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion
- expected: []metav1.TableRow{{Cells: []interface{}{"foo3", "Ready", "<none>", "<unknown>", ""}}},
- },
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo4"},
- Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: api.NodeReady, Status: api.ConditionFalse}}},
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion
- expected: []metav1.TableRow{{Cells: []interface{}{"foo4", "NotReady", "<none>", "<unknown>", ""}}},
- },
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo5"},
- Spec: api.NodeSpec{Unschedulable: true},
- Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: api.NodeReady, Status: api.ConditionFalse}}},
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion
- expected: []metav1.TableRow{{Cells: []interface{}{"foo5", "NotReady,SchedulingDisabled", "<none>", "<unknown>", ""}}},
- },
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo6"},
- Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: "InvalidValue", Status: api.ConditionTrue}}},
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion
- expected: []metav1.TableRow{{Cells: []interface{}{"foo6", "Unknown", "<none>", "<unknown>", ""}}},
- },
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo7"},
- Status: api.NodeStatus{Conditions: []api.NodeCondition{{}}},
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion
- expected: []metav1.TableRow{{Cells: []interface{}{"foo7", "Unknown", "<none>", "<unknown>", ""}}},
- },
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo8"},
- Spec: api.NodeSpec{Unschedulable: true},
- Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: "InvalidValue", Status: api.ConditionTrue}}},
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion
- expected: []metav1.TableRow{{Cells: []interface{}{"foo8", "Unknown,SchedulingDisabled", "<none>", "<unknown>", ""}}},
- },
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo9"},
- Spec: api.NodeSpec{Unschedulable: true},
- Status: api.NodeStatus{Conditions: []api.NodeCondition{{}}},
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion
- expected: []metav1.TableRow{{Cells: []interface{}{"foo9", "Unknown,SchedulingDisabled", "<none>", "<unknown>", ""}}},
- },
- }
- for i, test := range table {
- rows, err := printNode(&test.node, printers.GenerateOptions{})
- if err != nil {
- t.Fatalf("Error generating table rows for Node: %#v", err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintNodeRole(t *testing.T) {
- table := []struct {
- node api.Node
- expected []metav1.TableRow
- }{
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo9"},
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion
- expected: []metav1.TableRow{{Cells: []interface{}{"foo9", "Unknown", "<none>", "<unknown>", ""}}},
- },
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo10",
- Labels: map[string]string{"node-role.kubernetes.io/master": "", "node-role.kubernetes.io/proxy": "", "kubernetes.io/role": "node"},
- },
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion
- expected: []metav1.TableRow{{Cells: []interface{}{"foo10", "Unknown", "master,node,proxy", "<unknown>", ""}}},
- },
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo11",
- Labels: map[string]string{"kubernetes.io/role": "node"},
- },
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion
- expected: []metav1.TableRow{{Cells: []interface{}{"foo11", "Unknown", "node", "<unknown>", ""}}},
- },
- }
- for i, test := range table {
- rows, err := printNode(&test.node, printers.GenerateOptions{})
- if err != nil {
- t.Fatalf("An error occurred generating table rows for Node: %#v", err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintNodeOSImage(t *testing.T) {
- table := []struct {
- node api.Node
- expected []metav1.TableRow
- }{
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
- Status: api.NodeStatus{
- NodeInfo: api.NodeSystemInfo{OSImage: "fake-os-image"},
- Addresses: []api.NodeAddress{{Type: api.NodeExternalIP, Address: "1.1.1.1"}},
- },
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
- expected: []metav1.TableRow{
- {
- Cells: []interface{}{"foo1", "Unknown", "<none>", "<unknown>", "", "<none>", "1.1.1.1", "fake-os-image", "<unknown>", "<unknown>"},
- },
- },
- },
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo2"},
- Status: api.NodeStatus{
- NodeInfo: api.NodeSystemInfo{KernelVersion: "fake-kernel-version"},
- Addresses: []api.NodeAddress{{Type: api.NodeExternalIP, Address: "1.1.1.1"}},
- },
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
- expected: []metav1.TableRow{
- {
- Cells: []interface{}{"foo2", "Unknown", "<none>", "<unknown>", "", "<none>", "1.1.1.1", "<unknown>", "fake-kernel-version", "<unknown>"},
- },
- },
- },
- }
- for i, test := range table {
- rows, err := printNode(&test.node, printers.GenerateOptions{Wide: true})
- if err != nil {
- t.Fatalf("An error occurred generating table for Node: %#v", err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintNodeKernelVersion(t *testing.T) {
- table := []struct {
- node api.Node
- expected []metav1.TableRow
- }{
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
- Status: api.NodeStatus{
- NodeInfo: api.NodeSystemInfo{KernelVersion: "fake-kernel-version"},
- Addresses: []api.NodeAddress{{Type: api.NodeExternalIP, Address: "1.1.1.1"}},
- },
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
- expected: []metav1.TableRow{
- {
- Cells: []interface{}{"foo1", "Unknown", "<none>", "<unknown>", "", "<none>", "1.1.1.1", "<unknown>", "fake-kernel-version", "<unknown>"},
- },
- },
- },
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo2"},
- Status: api.NodeStatus{
- NodeInfo: api.NodeSystemInfo{OSImage: "fake-os-image"},
- Addresses: []api.NodeAddress{{Type: api.NodeExternalIP, Address: "1.1.1.1"}},
- },
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
- expected: []metav1.TableRow{
- {
- Cells: []interface{}{"foo2", "Unknown", "<none>", "<unknown>", "", "<none>", "1.1.1.1", "fake-os-image", "<unknown>", "<unknown>"},
- },
- },
- },
- }
- for i, test := range table {
- rows, err := printNode(&test.node, printers.GenerateOptions{Wide: true})
- if err != nil {
- t.Fatalf("An error occurred generating table rows Node: %#v", err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintNodeContainerRuntimeVersion(t *testing.T) {
- table := []struct {
- node api.Node
- expected []metav1.TableRow
- }{
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
- Status: api.NodeStatus{
- NodeInfo: api.NodeSystemInfo{ContainerRuntimeVersion: "foo://1.2.3"},
- Addresses: []api.NodeAddress{{Type: api.NodeExternalIP, Address: "1.1.1.1"}},
- },
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
- expected: []metav1.TableRow{
- {
- Cells: []interface{}{"foo1", "Unknown", "<none>", "<unknown>", "", "<none>", "1.1.1.1", "<unknown>", "<unknown>", "foo://1.2.3"},
- },
- },
- },
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo2"},
- Status: api.NodeStatus{
- NodeInfo: api.NodeSystemInfo{},
- Addresses: []api.NodeAddress{{Type: api.NodeExternalIP, Address: "1.1.1.1"}},
- },
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
- expected: []metav1.TableRow{
- {
- Cells: []interface{}{"foo2", "Unknown", "<none>", "<unknown>", "", "<none>", "1.1.1.1", "<unknown>", "<unknown>", "<unknown>"},
- },
- },
- },
- }
- for i, test := range table {
- rows, err := printNode(&test.node, printers.GenerateOptions{Wide: true})
- if err != nil {
- t.Fatalf("An error occurred generating table rows Node: %#v", err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintNodeName(t *testing.T) {
- table := []struct {
- node api.Node
- expected []metav1.TableRow
- }{
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "127.0.0.1"},
- Status: api.NodeStatus{},
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion
- expected: []metav1.TableRow{{Cells: []interface{}{"127.0.0.1", "Unknown", "<none>", "<unknown>", ""}}}},
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: ""},
- Status: api.NodeStatus{},
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion
- expected: []metav1.TableRow{{Cells: []interface{}{"", "Unknown", "<none>", "<unknown>", ""}}},
- },
- }
- for i, test := range table {
- rows, err := printNode(&test.node, printers.GenerateOptions{})
- if err != nil {
- t.Fatalf("An error occurred generating table rows Node: %#v", err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintNodeExternalIP(t *testing.T) {
- table := []struct {
- node api.Node
- expected []metav1.TableRow
- }{
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
- Status: api.NodeStatus{Addresses: []api.NodeAddress{{Type: api.NodeExternalIP, Address: "1.1.1.1"}}},
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
- expected: []metav1.TableRow{
- {
- Cells: []interface{}{"foo1", "Unknown", "<none>", "<unknown>", "", "<none>", "1.1.1.1", "<unknown>", "<unknown>", "<unknown>"},
- },
- },
- },
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo2"},
- Status: api.NodeStatus{Addresses: []api.NodeAddress{{Type: api.NodeInternalIP, Address: "1.1.1.1"}}},
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
- expected: []metav1.TableRow{
- {
- Cells: []interface{}{"foo2", "Unknown", "<none>", "<unknown>", "", "1.1.1.1", "<none>", "<unknown>", "<unknown>", "<unknown>"},
- },
- },
- },
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo3"},
- Status: api.NodeStatus{Addresses: []api.NodeAddress{
- {Type: api.NodeExternalIP, Address: "2.2.2.2"},
- {Type: api.NodeInternalIP, Address: "3.3.3.3"},
- {Type: api.NodeExternalIP, Address: "4.4.4.4"},
- }},
- },
- expected: []metav1.TableRow{
- {
- Cells: []interface{}{"foo3", "Unknown", "<none>", "<unknown>", "", "3.3.3.3", "2.2.2.2", "<unknown>", "<unknown>", "<unknown>"},
- },
- },
- },
- }
- for i, test := range table {
- rows, err := printNode(&test.node, printers.GenerateOptions{Wide: true})
- if err != nil {
- t.Fatalf("An error occurred generating table rows Node: %#v", err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintNodeInternalIP(t *testing.T) {
- table := []struct {
- node api.Node
- expected []metav1.TableRow
- }{
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
- Status: api.NodeStatus{Addresses: []api.NodeAddress{{Type: api.NodeInternalIP, Address: "1.1.1.1"}}},
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
- expected: []metav1.TableRow{
- {
- Cells: []interface{}{"foo1", "Unknown", "<none>", "<unknown>", "", "1.1.1.1", "<none>", "<unknown>", "<unknown>", "<unknown>"},
- },
- },
- },
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo2"},
- Status: api.NodeStatus{Addresses: []api.NodeAddress{{Type: api.NodeExternalIP, Address: "1.1.1.1"}}},
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
- expected: []metav1.TableRow{
- {
- Cells: []interface{}{"foo2", "Unknown", "<none>", "<unknown>", "", "<none>", "1.1.1.1", "<unknown>", "<unknown>", "<unknown>"},
- },
- },
- },
- {
- node: api.Node{
- ObjectMeta: metav1.ObjectMeta{Name: "foo3"},
- Status: api.NodeStatus{Addresses: []api.NodeAddress{
- {Type: api.NodeInternalIP, Address: "2.2.2.2"},
- {Type: api.NodeExternalIP, Address: "3.3.3.3"},
- {Type: api.NodeInternalIP, Address: "4.4.4.4"},
- }},
- },
- // Columns: Name, Status, Roles, Age, KubeletVersion, NodeInternalIP, NodeExternalIP, OSImage, KernelVersion, ContainerRuntimeVersion
- expected: []metav1.TableRow{
- {
- Cells: []interface{}{"foo3", "Unknown", "<none>", "<unknown>", "", "2.2.2.2", "3.3.3.3", "<unknown>", "<unknown>", "<unknown>"},
- },
- },
- },
- }
- for i, test := range table {
- rows, err := printNode(&test.node, printers.GenerateOptions{Wide: true})
- if err != nil {
- t.Fatalf("An error occurred generating table rows Node: %#v", err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintIngress(t *testing.T) {
- ingress := networking.Ingress{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test1",
- CreationTimestamp: metav1.Time{Time: time.Now().AddDate(-10, 0, 0)},
- },
- Spec: networking.IngressSpec{
- Backend: &networking.IngressBackend{
- ServiceName: "svc",
- ServicePort: intstr.FromInt(93),
- },
- },
- Status: networking.IngressStatus{
- LoadBalancer: api.LoadBalancerStatus{
- Ingress: []api.LoadBalancerIngress{
- {
- IP: "2.3.4.5",
- Hostname: "localhost.localdomain",
- },
- },
- },
- },
- }
- // Columns: Name, Hosts, Address, Ports, Age
- expected := []metav1.TableRow{{Cells: []interface{}{"test1", "*", "2.3.4.5", "80", "10y"}}}
- rows, err := printIngress(&ingress, printers.GenerateOptions{})
- if err != nil {
- t.Fatalf("Error generating table rows for Ingress: %#v", err)
- }
- rows[0].Object.Object = nil
- if !reflect.DeepEqual(expected, rows) {
- t.Errorf("mismatch: %s", diff.ObjectReflectDiff(expected, rows))
- }
- }
- func TestPrintServiceLoadBalancer(t *testing.T) {
- tests := []struct {
- service api.Service
- options printers.GenerateOptions
- expected []metav1.TableRow
- }{
- // Test load balancer service with multiple external IP's
- {
- service: api.Service{
- ObjectMeta: metav1.ObjectMeta{Name: "service1"},
- Spec: api.ServiceSpec{
- ClusterIP: "1.2.3.4",
- Type: "LoadBalancer",
- Ports: []api.ServicePort{{Port: 80, Protocol: "TCP"}},
- },
- Status: api.ServiceStatus{
- LoadBalancer: api.LoadBalancerStatus{
- Ingress: []api.LoadBalancerIngress{{IP: "2.3.4.5"}, {IP: "3.4.5.6"}}},
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
- expected: []metav1.TableRow{{Cells: []interface{}{"service1", "LoadBalancer", "1.2.3.4", "2.3.4.5,3.4.5.6", "80/TCP", "<unknown>"}}},
- },
- // Test load balancer service with pending external IP.
- {
- service: api.Service{
- ObjectMeta: metav1.ObjectMeta{Name: "service2"},
- Spec: api.ServiceSpec{
- ClusterIP: "1.3.4.5",
- Type: "LoadBalancer",
- Ports: []api.ServicePort{{Port: 80, Protocol: "TCP"}, {Port: 8090, Protocol: "UDP"}, {Port: 8000, Protocol: "TCP"}, {Port: 7777, Protocol: "SCTP"}},
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
- expected: []metav1.TableRow{{Cells: []interface{}{"service2", "LoadBalancer", "1.3.4.5", "<pending>", "80/TCP,8090/UDP,8000/TCP,7777/SCTP", "<unknown>"}}},
- },
- // Test load balancer service with multiple ports.
- {
- service: api.Service{
- ObjectMeta: metav1.ObjectMeta{Name: "service3"},
- Spec: api.ServiceSpec{
- ClusterIP: "1.4.5.6",
- Type: "LoadBalancer",
- Ports: []api.ServicePort{{Port: 80, Protocol: "TCP"}, {Port: 8090, Protocol: "UDP"}, {Port: 8000, Protocol: "TCP"}},
- },
- Status: api.ServiceStatus{
- LoadBalancer: api.LoadBalancerStatus{
- Ingress: []api.LoadBalancerIngress{{IP: "2.3.4.5"}}},
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
- expected: []metav1.TableRow{{Cells: []interface{}{"service3", "LoadBalancer", "1.4.5.6", "2.3.4.5", "80/TCP,8090/UDP,8000/TCP", "<unknown>"}}},
- },
- // Long external IP's list gets elided.
- {
- service: api.Service{
- ObjectMeta: metav1.ObjectMeta{Name: "service4"},
- Spec: api.ServiceSpec{
- ClusterIP: "1.5.6.7",
- Type: "LoadBalancer",
- Ports: []api.ServicePort{{Port: 80, Protocol: "TCP"}, {Port: 8090, Protocol: "UDP"}, {Port: 8000, Protocol: "TCP"}},
- },
- Status: api.ServiceStatus{
- LoadBalancer: api.LoadBalancerStatus{
- Ingress: []api.LoadBalancerIngress{{IP: "2.3.4.5"}, {IP: "3.4.5.6"}, {IP: "5.6.7.8", Hostname: "host5678"}}},
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
- 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>"}}},
- },
- // Generate options: Wide, includes selectors.
- {
- service: api.Service{
- ObjectMeta: metav1.ObjectMeta{Name: "service4"},
- Spec: api.ServiceSpec{
- ClusterIP: "1.5.6.7",
- Type: "LoadBalancer",
- Ports: []api.ServicePort{{Port: 80, Protocol: "TCP"}, {Port: 8090, Protocol: "UDP"}, {Port: 8000, Protocol: "TCP"}},
- Selector: map[string]string{"foo": "bar"},
- },
- Status: api.ServiceStatus{
- LoadBalancer: api.LoadBalancerStatus{
- Ingress: []api.LoadBalancerIngress{{IP: "2.3.4.5"}, {IP: "3.4.5.6"}, {IP: "5.6.7.8", Hostname: "host5678"}}},
- },
- },
- options: printers.GenerateOptions{Wide: true},
- // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
- 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"}}},
- },
- }
- for i, test := range tests {
- rows, err := printService(&test.service, test.options)
- if err != nil {
- t.Fatalf("Error printing table rows for Service: %#v", err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintPod(t *testing.T) {
- tests := []struct {
- pod api.Pod
- expect []metav1.TableRow
- }{
- {
- // Test name, num of containers, restarts, container ready status
- api.Pod{
- ObjectMeta: metav1.ObjectMeta{Name: "test1"},
- Spec: api.PodSpec{Containers: make([]api.Container, 2)},
- Status: api.PodStatus{
- Phase: "podPhase",
- ContainerStatuses: []api.ContainerStatus{
- {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
- {RestartCount: 3},
- },
- },
- },
- []metav1.TableRow{{Cells: []interface{}{"test1", "1/2", "podPhase", int64(6), "<unknown>"}}},
- },
- {
- // Test container error overwrites pod phase
- api.Pod{
- ObjectMeta: metav1.ObjectMeta{Name: "test2"},
- Spec: api.PodSpec{Containers: make([]api.Container, 2)},
- Status: api.PodStatus{
- Phase: "podPhase",
- ContainerStatuses: []api.ContainerStatus{
- {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
- {State: api.ContainerState{Waiting: &api.ContainerStateWaiting{Reason: "ContainerWaitingReason"}}, RestartCount: 3},
- },
- },
- },
- []metav1.TableRow{{Cells: []interface{}{"test2", "1/2", "ContainerWaitingReason", int64(6), "<unknown>"}}},
- },
- {
- // Test the same as the above but with Terminated state and the first container overwrites the rest
- api.Pod{
- ObjectMeta: metav1.ObjectMeta{Name: "test3"},
- Spec: api.PodSpec{Containers: make([]api.Container, 2)},
- Status: api.PodStatus{
- Phase: "podPhase",
- ContainerStatuses: []api.ContainerStatus{
- {State: api.ContainerState{Waiting: &api.ContainerStateWaiting{Reason: "ContainerWaitingReason"}}, RestartCount: 3},
- {State: api.ContainerState{Terminated: &api.ContainerStateTerminated{Reason: "ContainerTerminatedReason"}}, RestartCount: 3},
- },
- },
- },
- []metav1.TableRow{{Cells: []interface{}{"test3", "0/2", "ContainerWaitingReason", int64(6), "<unknown>"}}},
- },
- {
- // Test ready is not enough for reporting running
- api.Pod{
- ObjectMeta: metav1.ObjectMeta{Name: "test4"},
- Spec: api.PodSpec{Containers: make([]api.Container, 2)},
- Status: api.PodStatus{
- Phase: "podPhase",
- ContainerStatuses: []api.ContainerStatus{
- {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
- {Ready: true, RestartCount: 3},
- },
- },
- },
- []metav1.TableRow{{Cells: []interface{}{"test4", "1/2", "podPhase", int64(6), "<unknown>"}}},
- },
- {
- // Test ready is not enough for reporting running
- api.Pod{
- ObjectMeta: metav1.ObjectMeta{Name: "test5"},
- Spec: api.PodSpec{Containers: make([]api.Container, 2)},
- Status: api.PodStatus{
- Reason: "podReason",
- Phase: "podPhase",
- ContainerStatuses: []api.ContainerStatus{
- {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
- {Ready: true, RestartCount: 3},
- },
- },
- },
- []metav1.TableRow{{Cells: []interface{}{"test5", "1/2", "podReason", int64(6), "<unknown>"}}},
- },
- {
- // Test pod has 2 containers, one is running and the other is completed.
- api.Pod{
- ObjectMeta: metav1.ObjectMeta{Name: "test6"},
- Spec: api.PodSpec{Containers: make([]api.Container, 2)},
- Status: api.PodStatus{
- Phase: "Running",
- Reason: "",
- ContainerStatuses: []api.ContainerStatus{
- {Ready: true, RestartCount: 3, State: api.ContainerState{Terminated: &api.ContainerStateTerminated{Reason: "Completed", ExitCode: 0}}},
- {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
- },
- },
- },
- []metav1.TableRow{{Cells: []interface{}{"test6", "1/2", "Running", int64(6), "<unknown>"}}},
- },
- }
- for i, test := range tests {
- rows, err := printPod(&test.pod, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expect, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expect, rows))
- }
- }
- }
- func TestPrintPodwide(t *testing.T) {
- condition1 := "condition1"
- condition2 := "condition2"
- condition3 := "condition3"
- tests := []struct {
- pod api.Pod
- expect []metav1.TableRow
- }{
- {
- // Test when the NodeName and PodIP are not none
- api.Pod{
- ObjectMeta: metav1.ObjectMeta{Name: "test1"},
- Spec: api.PodSpec{
- Containers: make([]api.Container, 2),
- NodeName: "test1",
- ReadinessGates: []api.PodReadinessGate{
- {
- ConditionType: api.PodConditionType(condition1),
- },
- {
- ConditionType: api.PodConditionType(condition2),
- },
- {
- ConditionType: api.PodConditionType(condition3),
- },
- },
- },
- Status: api.PodStatus{
- Conditions: []api.PodCondition{
- {
- Type: api.PodConditionType(condition1),
- Status: api.ConditionFalse,
- },
- {
- Type: api.PodConditionType(condition2),
- Status: api.ConditionTrue,
- },
- },
- Phase: "podPhase",
- PodIPs: []api.PodIP{{IP: "1.1.1.1"}},
- ContainerStatuses: []api.ContainerStatus{
- {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
- {RestartCount: 3},
- },
- NominatedNodeName: "node1",
- },
- },
- []metav1.TableRow{{Cells: []interface{}{"test1", "1/2", "podPhase", int64(6), "<unknown>", "1.1.1.1", "test1", "node1", "1/3"}}},
- },
- {
- // Test when the NodeName and PodIP are not none
- api.Pod{
- ObjectMeta: metav1.ObjectMeta{Name: "test1"},
- Spec: api.PodSpec{
- Containers: make([]api.Container, 2),
- NodeName: "test1",
- ReadinessGates: []api.PodReadinessGate{
- {
- ConditionType: api.PodConditionType(condition1),
- },
- {
- ConditionType: api.PodConditionType(condition2),
- },
- {
- ConditionType: api.PodConditionType(condition3),
- },
- },
- },
- Status: api.PodStatus{
- Conditions: []api.PodCondition{
- {
- Type: api.PodConditionType(condition1),
- Status: api.ConditionFalse,
- },
- {
- Type: api.PodConditionType(condition2),
- Status: api.ConditionTrue,
- },
- },
- Phase: "podPhase",
- PodIPs: []api.PodIP{{IP: "1.1.1.1"}, {IP: "2001:db8::"}},
- ContainerStatuses: []api.ContainerStatus{
- {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
- {RestartCount: 3},
- },
- NominatedNodeName: "node1",
- },
- },
- []metav1.TableRow{{Cells: []interface{}{"test1", "1/2", "podPhase", int64(6), "<unknown>", "1.1.1.1", "test1", "node1", "1/3"}}},
- },
- {
- // Test when the NodeName and PodIP are none
- api.Pod{
- ObjectMeta: metav1.ObjectMeta{Name: "test2"},
- Spec: api.PodSpec{
- Containers: make([]api.Container, 2),
- NodeName: "",
- },
- Status: api.PodStatus{
- Phase: "podPhase",
- ContainerStatuses: []api.ContainerStatus{
- {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
- {State: api.ContainerState{Waiting: &api.ContainerStateWaiting{Reason: "ContainerWaitingReason"}}, RestartCount: 3},
- },
- },
- },
- []metav1.TableRow{{Cells: []interface{}{"test2", "1/2", "ContainerWaitingReason", int64(6), "<unknown>", "<none>", "<none>", "<none>", "<none>"}}},
- },
- }
- for i, test := range tests {
- rows, err := printPod(&test.pod, printers.GenerateOptions{Wide: true})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expect, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expect, rows))
- }
- }
- }
- func TestPrintPodConditions(t *testing.T) {
- runningPod := &api.Pod{
- ObjectMeta: metav1.ObjectMeta{Name: "test1", Labels: map[string]string{"a": "1", "b": "2"}},
- Spec: api.PodSpec{Containers: make([]api.Container, 2)},
- Status: api.PodStatus{
- Phase: "Running",
- ContainerStatuses: []api.ContainerStatus{
- {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
- {RestartCount: 3},
- },
- },
- }
- succeededPod := &api.Pod{
- ObjectMeta: metav1.ObjectMeta{Name: "test1", Labels: map[string]string{"a": "1", "b": "2"}},
- Spec: api.PodSpec{Containers: make([]api.Container, 2)},
- Status: api.PodStatus{
- Phase: "Succeeded",
- ContainerStatuses: []api.ContainerStatus{
- {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
- {RestartCount: 3},
- },
- },
- }
- failedPod := &api.Pod{
- ObjectMeta: metav1.ObjectMeta{Name: "test2", Labels: map[string]string{"b": "2"}},
- Spec: api.PodSpec{Containers: make([]api.Container, 2)},
- Status: api.PodStatus{
- Phase: "Failed",
- ContainerStatuses: []api.ContainerStatus{
- {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
- {RestartCount: 3},
- },
- },
- }
- tests := []struct {
- pod *api.Pod
- expect []metav1.TableRow
- }{
- // Should not have TableRowCondition
- {
- pod: runningPod,
- // Columns: Name, Ready, Reason, Restarts, Age
- expect: []metav1.TableRow{{Cells: []interface{}{"test1", "1/2", "Running", int64(6), "<unknown>"}}},
- },
- // Should have TableRowCondition: podSuccessConditions
- {
- pod: succeededPod,
- expect: []metav1.TableRow{
- {
- // Columns: Name, Ready, Reason, Restarts, Age
- Cells: []interface{}{"test1", "1/2", "Succeeded", int64(6), "<unknown>"},
- Conditions: podSuccessConditions,
- },
- },
- },
- // Should have TableRowCondition: podFailedCondition
- {
- pod: failedPod,
- expect: []metav1.TableRow{
- {
- // Columns: Name, Ready, Reason, Restarts, Age
- Cells: []interface{}{"test2", "1/2", "Failed", int64(6), "<unknown>"},
- Conditions: podFailedConditions,
- },
- },
- },
- }
- for i, test := range tests {
- rows, err := printPod(test.pod, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expect, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expect, rows))
- }
- }
- }
- func TestPrintPodList(t *testing.T) {
- tests := []struct {
- pods api.PodList
- expect []metav1.TableRow
- }{
- // Test podList's pod: name, num of containers, restarts, container ready status
- {
- api.PodList{
- Items: []api.Pod{
- {
- ObjectMeta: metav1.ObjectMeta{Name: "test1"},
- Spec: api.PodSpec{Containers: make([]api.Container, 2)},
- Status: api.PodStatus{
- Phase: "podPhase",
- ContainerStatuses: []api.ContainerStatus{
- {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
- {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
- },
- },
- },
- {
- ObjectMeta: metav1.ObjectMeta{Name: "test2"},
- Spec: api.PodSpec{Containers: make([]api.Container, 1)},
- Status: api.PodStatus{
- Phase: "podPhase",
- ContainerStatuses: []api.ContainerStatus{
- {Ready: true, RestartCount: 1, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
- },
- },
- },
- },
- },
- []metav1.TableRow{{Cells: []interface{}{"test1", "2/2", "podPhase", int64(6), "<unknown>"}}, {Cells: []interface{}{"test2", "1/1", "podPhase", int64(1), "<unknown>"}}},
- },
- }
- for _, test := range tests {
- rows, err := printPodList(&test.pods, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expect, rows) {
- t.Errorf("mismatch: %s", diff.ObjectReflectDiff(test.expect, rows))
- }
- }
- }
- func TestPrintNonTerminatedPod(t *testing.T) {
- tests := []struct {
- pod api.Pod
- expect []metav1.TableRow
- }{
- {
- // Test pod phase Running should be printed
- api.Pod{
- ObjectMeta: metav1.ObjectMeta{Name: "test1"},
- Spec: api.PodSpec{Containers: make([]api.Container, 2)},
- Status: api.PodStatus{
- Phase: api.PodRunning,
- ContainerStatuses: []api.ContainerStatus{
- {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
- {RestartCount: 3},
- },
- },
- },
- // Columns: Name, Ready, Reason, Restarts, Age
- []metav1.TableRow{{Cells: []interface{}{"test1", "1/2", "Running", int64(6), "<unknown>"}}},
- },
- {
- // Test pod phase Pending should be printed
- api.Pod{
- ObjectMeta: metav1.ObjectMeta{Name: "test2"},
- Spec: api.PodSpec{Containers: make([]api.Container, 2)},
- Status: api.PodStatus{
- Phase: api.PodPending,
- ContainerStatuses: []api.ContainerStatus{
- {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
- {RestartCount: 3},
- },
- },
- },
- // Columns: Name, Ready, Reason, Restarts, Age
- []metav1.TableRow{{Cells: []interface{}{"test2", "1/2", "Pending", int64(6), "<unknown>"}}},
- },
- {
- // Test pod phase Unknown should be printed
- api.Pod{
- ObjectMeta: metav1.ObjectMeta{Name: "test3"},
- Spec: api.PodSpec{Containers: make([]api.Container, 2)},
- Status: api.PodStatus{
- Phase: api.PodUnknown,
- ContainerStatuses: []api.ContainerStatus{
- {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
- {RestartCount: 3},
- },
- },
- },
- // Columns: Name, Ready, Reason, Restarts, Age
- []metav1.TableRow{{Cells: []interface{}{"test3", "1/2", "Unknown", int64(6), "<unknown>"}}},
- },
- {
- // Test pod phase Succeeded should be printed
- api.Pod{
- ObjectMeta: metav1.ObjectMeta{Name: "test4"},
- Spec: api.PodSpec{Containers: make([]api.Container, 2)},
- Status: api.PodStatus{
- Phase: api.PodSucceeded,
- ContainerStatuses: []api.ContainerStatus{
- {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
- {RestartCount: 3},
- },
- },
- },
- // Columns: Name, Ready, Reason, Restarts, Age
- []metav1.TableRow{
- {
- Cells: []interface{}{"test4", "1/2", "Succeeded", int64(6), "<unknown>"},
- Conditions: podSuccessConditions,
- },
- },
- },
- {
- // Test pod phase Failed shouldn't be printed
- api.Pod{
- ObjectMeta: metav1.ObjectMeta{Name: "test5"},
- Spec: api.PodSpec{Containers: make([]api.Container, 2)},
- Status: api.PodStatus{
- Phase: api.PodFailed,
- ContainerStatuses: []api.ContainerStatus{
- {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
- {Ready: true, RestartCount: 3},
- },
- },
- },
- // Columns: Name, Ready, Reason, Restarts, Age
- []metav1.TableRow{
- {
- Cells: []interface{}{"test5", "1/2", "Failed", int64(6), "<unknown>"},
- Conditions: podFailedConditions,
- },
- },
- },
- }
- for i, test := range tests {
- rows, err := printPod(&test.pod, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expect, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expect, rows))
- }
- }
- }
- func TestPrintPodTemplate(t *testing.T) {
- tests := []struct {
- podTemplate api.PodTemplate
- options printers.GenerateOptions
- expected []metav1.TableRow
- }{
- // Test basic pod template with no containers.
- {
- podTemplate: api.PodTemplate{
- ObjectMeta: metav1.ObjectMeta{Name: "pod-template-1"},
- Template: api.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{Name: "pod-template-1"},
- Spec: api.PodSpec{
- Containers: []api.Container{},
- },
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Containers, Images, Pod Labels
- expected: []metav1.TableRow{{Cells: []interface{}{"pod-template-1", "", "", "<none>"}}},
- },
- // Test basic pod template with two containers.
- {
- podTemplate: api.PodTemplate{
- ObjectMeta: metav1.ObjectMeta{Name: "pod-template-2"},
- Template: api.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{Name: "pod-template-2"},
- Spec: api.PodSpec{
- Containers: []api.Container{
- {
- Name: "fake-container1",
- Image: "fake-image1",
- },
- {
- Name: "fake-container2",
- Image: "fake-image2",
- },
- },
- },
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Containers, Images, Pod Labels
- expected: []metav1.TableRow{{Cells: []interface{}{"pod-template-2", "fake-container1,fake-container2", "fake-image1,fake-image2", "<none>"}}},
- },
- // Test basic pod template with pod labels
- {
- podTemplate: api.PodTemplate{
- ObjectMeta: metav1.ObjectMeta{Name: "pod-template-3"},
- Template: api.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Name: "pod-template-3",
- Labels: map[string]string{"foo": "bar"},
- },
- Spec: api.PodSpec{
- Containers: []api.Container{},
- },
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Containers, Images, Pod Labels
- expected: []metav1.TableRow{{Cells: []interface{}{"pod-template-3", "", "", "foo=bar"}}},
- },
- }
- for i, test := range tests {
- rows, err := printPodTemplate(&test.podTemplate, test.options)
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintPodTemplateList(t *testing.T) {
- templateList := api.PodTemplateList{
- Items: []api.PodTemplate{
- {
- ObjectMeta: metav1.ObjectMeta{Name: "pod-template-1"},
- Template: api.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Name: "pod-template-2",
- Labels: map[string]string{"foo": "bar"},
- },
- Spec: api.PodSpec{
- Containers: []api.Container{},
- },
- },
- },
- {
- ObjectMeta: metav1.ObjectMeta{Name: "pod-template-2"},
- Template: api.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Name: "pod-template-2",
- Labels: map[string]string{"a": "b"},
- },
- Spec: api.PodSpec{
- Containers: []api.Container{},
- },
- },
- },
- },
- }
- // Columns: Name, Containers, Images, Pod Labels
- expectedRows := []metav1.TableRow{
- {Cells: []interface{}{"pod-template-1", "", "", "foo=bar"}},
- {Cells: []interface{}{"pod-template-2", "", "", "a=b"}},
- }
- rows, err := printPodTemplateList(&templateList, printers.GenerateOptions{})
- if err != nil {
- t.Fatalf("Error printing pod template list: %#v", err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(expectedRows, rows) {
- t.Errorf("mismatch: %s", diff.ObjectReflectDiff(expectedRows, rows))
- }
- }
- type stringTestList []struct {
- name, got, exp string
- }
- func TestTranslateTimestampSince(t *testing.T) {
- tl := stringTestList{
- {"a while from now", translateTimestampSince(metav1.Time{Time: time.Now().Add(2.1e9)}), "<invalid>"},
- {"almost now", translateTimestampSince(metav1.Time{Time: time.Now().Add(1.9e9)}), "0s"},
- {"now", translateTimestampSince(metav1.Time{Time: time.Now()}), "0s"},
- {"unknown", translateTimestampSince(metav1.Time{}), "<unknown>"},
- {"30 seconds ago", translateTimestampSince(metav1.Time{Time: time.Now().Add(-3e10)}), "30s"},
- {"5 minutes ago", translateTimestampSince(metav1.Time{Time: time.Now().Add(-3e11)}), "5m"},
- {"an hour ago", translateTimestampSince(metav1.Time{Time: time.Now().Add(-6e12)}), "100m"},
- {"2 days ago", translateTimestampSince(metav1.Time{Time: time.Now().UTC().AddDate(0, 0, -2)}), "2d"},
- {"months ago", translateTimestampSince(metav1.Time{Time: time.Now().UTC().AddDate(0, 0, -90)}), "90d"},
- {"10 years ago", translateTimestampSince(metav1.Time{Time: time.Now().UTC().AddDate(-10, 0, 0)}), "10y"},
- }
- for _, test := range tl {
- if test.got != test.exp {
- t.Errorf("On %v, expected '%v', but got '%v'",
- test.name, test.exp, test.got)
- }
- }
- }
- func TestTranslateTimestampUntil(t *testing.T) {
- // Since this method compares the time with time.Now() internally,
- // small buffers of 0.1 seconds are added on comparing times to consider method call overhead.
- // Otherwise, the output strings become shorter than expected.
- const buf = 1e8
- tl := stringTestList{
- {"a while ago", translateTimestampUntil(metav1.Time{Time: time.Now().Add(-2.1e9)}), "<invalid>"},
- {"almost now", translateTimestampUntil(metav1.Time{Time: time.Now().Add(-1.9e9)}), "0s"},
- {"now", translateTimestampUntil(metav1.Time{Time: time.Now()}), "0s"},
- {"unknown", translateTimestampUntil(metav1.Time{}), "<unknown>"},
- {"in 30 seconds", translateTimestampUntil(metav1.Time{Time: time.Now().Add(3e10 + buf)}), "30s"},
- {"in 5 minutes", translateTimestampUntil(metav1.Time{Time: time.Now().Add(3e11 + buf)}), "5m"},
- {"in an hour", translateTimestampUntil(metav1.Time{Time: time.Now().Add(6e12 + buf)}), "100m"},
- {"in 2 days", translateTimestampUntil(metav1.Time{Time: time.Now().UTC().AddDate(0, 0, 2).Add(buf)}), "2d"},
- {"in months", translateTimestampUntil(metav1.Time{Time: time.Now().UTC().AddDate(0, 0, 90).Add(buf)}), "90d"},
- {"in 10 years", translateTimestampUntil(metav1.Time{Time: time.Now().UTC().AddDate(10, 0, 0).Add(buf)}), "10y"},
- }
- for _, test := range tl {
- if test.got != test.exp {
- t.Errorf("On %v, expected '%v', but got '%v'",
- test.name, test.exp, test.got)
- }
- }
- }
- func TestPrintDeployment(t *testing.T) {
- testDeployment := apps.Deployment{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: apps.DeploymentSpec{
- Replicas: 5,
- Template: api.PodTemplateSpec{
- Spec: api.PodSpec{
- Containers: []api.Container{
- {
- Name: "fake-container1",
- Image: "fake-image1",
- },
- {
- Name: "fake-container2",
- Image: "fake-image2",
- },
- },
- },
- },
- Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
- },
- Status: apps.DeploymentStatus{
- Replicas: 10,
- UpdatedReplicas: 2,
- AvailableReplicas: 1,
- UnavailableReplicas: 4,
- },
- }
- tests := []struct {
- deployment apps.Deployment
- options printers.GenerateOptions
- expected []metav1.TableRow
- }{
- // Test Deployment with no generate options.
- {
- deployment: testDeployment,
- options: printers.GenerateOptions{},
- // Columns: Name, ReadyReplicas, UpdatedReplicas, AvailableReplicas, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"test1", "0/5", int64(2), int64(1), "0s"}}},
- },
- // Test generate options: Wide.
- {
- deployment: testDeployment,
- options: printers.GenerateOptions{Wide: true},
- // Columns: Name, ReadyReplicas, UpdatedReplicas, AvailableReplicas, Age, Containers, Images, Selectors
- expected: []metav1.TableRow{{Cells: []interface{}{"test1", "0/5", int64(2), int64(1), "0s", "fake-container1,fake-container2", "fake-image1,fake-image2", "foo=bar"}}},
- },
- }
- for i, test := range tests {
- rows, err := printDeployment(&test.deployment, test.options)
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintDaemonSet(t *testing.T) {
- testDaemonSet := apps.DaemonSet{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: apps.DaemonSetSpec{
- Template: api.PodTemplateSpec{
- Spec: api.PodSpec{
- Containers: []api.Container{
- {
- Name: "fake-container1",
- Image: "fake-image1",
- },
- {
- Name: "fake-container2",
- Image: "fake-image2",
- },
- },
- },
- },
- Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
- },
- Status: apps.DaemonSetStatus{
- CurrentNumberScheduled: 2,
- DesiredNumberScheduled: 3,
- NumberReady: 1,
- UpdatedNumberScheduled: 2,
- NumberAvailable: 0,
- },
- }
- tests := []struct {
- daemonSet apps.DaemonSet
- options printers.GenerateOptions
- expected []metav1.TableRow
- }{
- // Test generate daemon set with no generate options.
- {
- daemonSet: testDaemonSet,
- options: printers.GenerateOptions{},
- // Columns: Name, Num Desired, Num Current, Num Ready, Num Updated, Num Available, Selectors, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"test1", int64(3), int64(2), int64(1), int64(2), int64(0), "<none>", "0s"}}},
- },
- // Test generate daemon set with "Wide" generate options.
- {
- daemonSet: testDaemonSet,
- options: printers.GenerateOptions{Wide: true},
- // Columns: Name, Num Desired, Num Current, Num Ready, Num Updated, Num Available, Node Selectors, Age, Containers, Images, Labels
- 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"}}},
- },
- }
- for i, test := range tests {
- rows, err := printDaemonSet(&test.daemonSet, test.options)
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintDaemonSetList(t *testing.T) {
- daemonSetList := apps.DaemonSetList{
- Items: []apps.DaemonSet{
- {
- ObjectMeta: metav1.ObjectMeta{
- Name: "daemonset1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: apps.DaemonSetSpec{
- Template: api.PodTemplateSpec{
- Spec: api.PodSpec{
- Containers: []api.Container{
- {
- Name: "fake-container1",
- Image: "fake-image1",
- },
- {
- Name: "fake-container2",
- Image: "fake-image2",
- },
- },
- },
- },
- },
- Status: apps.DaemonSetStatus{
- CurrentNumberScheduled: 2,
- DesiredNumberScheduled: 3,
- NumberReady: 1,
- UpdatedNumberScheduled: 2,
- NumberAvailable: 0,
- },
- },
- {
- ObjectMeta: metav1.ObjectMeta{
- Name: "daemonset2",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: apps.DaemonSetSpec{
- Template: api.PodTemplateSpec{},
- Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
- },
- Status: apps.DaemonSetStatus{
- CurrentNumberScheduled: 4,
- DesiredNumberScheduled: 2,
- NumberReady: 9,
- UpdatedNumberScheduled: 3,
- NumberAvailable: 3,
- },
- },
- },
- }
- // Columns: Name, Num Desired, Num Current, Num Ready, Num Updated, Num Available, Selectors, Age
- expectedRows := []metav1.TableRow{
- {Cells: []interface{}{"daemonset1", int64(3), int64(2), int64(1), int64(2), int64(0), "<none>", "0s"}},
- {Cells: []interface{}{"daemonset2", int64(2), int64(4), int64(9), int64(3), int64(3), "<none>", "0s"}},
- }
- rows, err := printDaemonSetList(&daemonSetList, printers.GenerateOptions{})
- if err != nil {
- t.Fatalf("Error printing daemon set list: %#v", err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(expectedRows, rows) {
- t.Errorf("mismatch: %s", diff.ObjectReflectDiff(expectedRows, rows))
- }
- }
- func TestPrintJob(t *testing.T) {
- now := time.Now()
- completions := int32(2)
- tests := []struct {
- job batch.Job
- options printers.GenerateOptions
- expected []metav1.TableRow
- }{
- {
- // Generate table rows for Job with no generate options.
- job: batch.Job{
- ObjectMeta: metav1.ObjectMeta{
- Name: "job1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: batch.JobSpec{
- Completions: &completions,
- Template: api.PodTemplateSpec{
- Spec: api.PodSpec{
- Containers: []api.Container{
- {
- Name: "fake-job-container1",
- Image: "fake-job-image1",
- },
- {
- Name: "fake-job-container2",
- Image: "fake-job-image2",
- },
- },
- },
- },
- Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"job-label": "job-lable-value"}},
- },
- Status: batch.JobStatus{
- Succeeded: 1,
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Completions, Duration, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"job1", "1/2", "", "0s"}}},
- },
- // Generate table rows for Job with generate options "Wide".
- {
- job: batch.Job{
- ObjectMeta: metav1.ObjectMeta{
- Name: "job1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: batch.JobSpec{
- Completions: &completions,
- Template: api.PodTemplateSpec{
- Spec: api.PodSpec{
- Containers: []api.Container{
- {
- Name: "fake-job-container1",
- Image: "fake-job-image1",
- },
- {
- Name: "fake-job-container2",
- Image: "fake-job-image2",
- },
- },
- },
- },
- Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"job-label": "job-label-value"}},
- },
- Status: batch.JobStatus{
- Succeeded: 1,
- },
- },
- options: printers.GenerateOptions{Wide: true},
- // Columns: Name, Completions, Duration, Age, Containers, Images, Selectors
- expected: []metav1.TableRow{
- {
- Cells: []interface{}{"job1", "1/2", "", "0s", "fake-job-container1,fake-job-container2", "fake-job-image1,fake-job-image2", "job-label=job-label-value"},
- },
- },
- },
- // Job with ten-year age.
- {
- job: batch.Job{
- ObjectMeta: metav1.ObjectMeta{
- Name: "job2",
- CreationTimestamp: metav1.Time{Time: time.Now().AddDate(-10, 0, 0)},
- },
- Spec: batch.JobSpec{
- Completions: nil,
- },
- Status: batch.JobStatus{
- Succeeded: 0,
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Completions, Duration, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"job2", "0/1", "", "10y"}}},
- },
- // Job with duration.
- {
- job: batch.Job{
- ObjectMeta: metav1.ObjectMeta{
- Name: "job3",
- CreationTimestamp: metav1.Time{Time: time.Now().AddDate(-10, 0, 0)},
- },
- Spec: batch.JobSpec{
- Completions: nil,
- },
- Status: batch.JobStatus{
- Succeeded: 0,
- StartTime: &metav1.Time{Time: now.Add(time.Minute)},
- CompletionTime: &metav1.Time{Time: now.Add(31 * time.Minute)},
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Completions, Duration, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"job3", "0/1", "30m", "10y"}}},
- },
- {
- job: batch.Job{
- ObjectMeta: metav1.ObjectMeta{
- Name: "job4",
- CreationTimestamp: metav1.Time{Time: time.Now().AddDate(-10, 0, 0)},
- },
- Spec: batch.JobSpec{
- Completions: nil,
- },
- Status: batch.JobStatus{
- Succeeded: 0,
- StartTime: &metav1.Time{Time: time.Now().Add(-20 * time.Minute)},
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Completions, Duration, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"job4", "0/1", "20m", "10y"}}},
- },
- }
- for i, test := range tests {
- rows, err := printJob(&test.job, test.options)
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintJobList(t *testing.T) {
- completions := int32(2)
- jobList := batch.JobList{
- Items: []batch.Job{
- {
- ObjectMeta: metav1.ObjectMeta{
- Name: "job1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: batch.JobSpec{
- Completions: &completions,
- Template: api.PodTemplateSpec{
- Spec: api.PodSpec{
- Containers: []api.Container{
- {
- Name: "fake-job-container1",
- Image: "fake-job-image1",
- },
- {
- Name: "fake-job-container2",
- Image: "fake-job-image2",
- },
- },
- },
- },
- Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"job-label": "job-lable-value"}},
- },
- Status: batch.JobStatus{
- Succeeded: 1,
- },
- },
- {
- ObjectMeta: metav1.ObjectMeta{
- Name: "job2",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: batch.JobSpec{
- Completions: &completions,
- Template: api.PodTemplateSpec{
- Spec: api.PodSpec{
- Containers: []api.Container{
- {
- Name: "fake-job-container1",
- Image: "fake-job-image1",
- },
- {
- Name: "fake-job-container2",
- Image: "fake-job-image2",
- },
- },
- },
- },
- Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"job-label": "job-lable-value"}},
- },
- Status: batch.JobStatus{
- Succeeded: 2,
- StartTime: &metav1.Time{Time: time.Now().Add(-20 * time.Minute)},
- },
- },
- },
- }
- // Columns: Name, Completions, Duration, Age
- expectedRows := []metav1.TableRow{
- {Cells: []interface{}{"job1", "1/2", "", "0s"}},
- {Cells: []interface{}{"job2", "2/2", "20m", "0s"}},
- }
- rows, err := printJobList(&jobList, printers.GenerateOptions{})
- if err != nil {
- t.Fatalf("Error printing job list: %#v", err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(expectedRows, rows) {
- t.Errorf("mismatch: %s", diff.ObjectReflectDiff(expectedRows, rows))
- }
- }
- func TestPrintHPA(t *testing.T) {
- minReplicasVal := int32(2)
- targetUtilizationVal := int32(80)
- currentUtilizationVal := int32(50)
- metricLabelSelector, err := metav1.ParseToLabelSelector("label=value")
- if err != nil {
- t.Errorf("unable to parse label selector: %v", err)
- }
- tests := []struct {
- hpa autoscaling.HorizontalPodAutoscaler
- expected []metav1.TableRow
- }{
- // minReplicas unset
- {
- hpa: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Name: "some-rc",
- Kind: "ReplicationController",
- },
- MaxReplicas: 10,
- },
- Status: autoscaling.HorizontalPodAutoscalerStatus{
- CurrentReplicas: 4,
- DesiredReplicas: 5,
- },
- },
- // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "<none>", "<unset>", int64(10), int64(4), "<unknown>"}}},
- },
- // external source type, target average value (no current)
- {
- hpa: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Name: "some-rc",
- Kind: "ReplicationController",
- },
- MinReplicas: &minReplicasVal,
- MaxReplicas: 10,
- Metrics: []autoscaling.MetricSpec{
- {
- Type: autoscaling.ExternalMetricSourceType,
- External: &autoscaling.ExternalMetricSource{
- Metric: autoscaling.MetricIdentifier{
- Name: "some-external-metric",
- Selector: metricLabelSelector,
- },
- Target: autoscaling.MetricTarget{
- Type: autoscaling.AverageValueMetricType,
- AverageValue: resource.NewMilliQuantity(100, resource.DecimalSI),
- },
- },
- },
- },
- },
- Status: autoscaling.HorizontalPodAutoscalerStatus{
- CurrentReplicas: 4,
- DesiredReplicas: 5,
- },
- },
- // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "<unknown>/100m (avg)", "2", int64(10), int64(4), "<unknown>"}}},
- },
- // external source type, target average value
- {
- hpa: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Name: "some-rc",
- Kind: "ReplicationController",
- },
- MinReplicas: &minReplicasVal,
- MaxReplicas: 10,
- Metrics: []autoscaling.MetricSpec{
- {
- Type: autoscaling.ExternalMetricSourceType,
- External: &autoscaling.ExternalMetricSource{
- Metric: autoscaling.MetricIdentifier{
- Name: "some-external-metric",
- Selector: metricLabelSelector,
- },
- Target: autoscaling.MetricTarget{
- Type: autoscaling.AverageValueMetricType,
- AverageValue: resource.NewMilliQuantity(100, resource.DecimalSI),
- },
- },
- },
- },
- },
- Status: autoscaling.HorizontalPodAutoscalerStatus{
- CurrentReplicas: 4,
- DesiredReplicas: 5,
- CurrentMetrics: []autoscaling.MetricStatus{
- {
- Type: autoscaling.ExternalMetricSourceType,
- External: &autoscaling.ExternalMetricStatus{
- Metric: autoscaling.MetricIdentifier{
- Name: "some-external-metric",
- Selector: metricLabelSelector,
- },
- Current: autoscaling.MetricValueStatus{
- AverageValue: resource.NewMilliQuantity(50, resource.DecimalSI),
- },
- },
- },
- },
- },
- },
- // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "50m/100m (avg)", "2", int64(10), int64(4), "<unknown>"}}},
- },
- // external source type, target value (no current)
- {
- hpa: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Name: "some-rc",
- Kind: "ReplicationController",
- },
- MinReplicas: &minReplicasVal,
- MaxReplicas: 10,
- Metrics: []autoscaling.MetricSpec{
- {
- Type: autoscaling.ExternalMetricSourceType,
- External: &autoscaling.ExternalMetricSource{
- Metric: autoscaling.MetricIdentifier{
- Name: "some-service-metric",
- Selector: metricLabelSelector,
- },
- Target: autoscaling.MetricTarget{
- Type: autoscaling.ValueMetricType,
- Value: resource.NewMilliQuantity(100, resource.DecimalSI),
- },
- },
- },
- },
- },
- Status: autoscaling.HorizontalPodAutoscalerStatus{
- CurrentReplicas: 4,
- DesiredReplicas: 5,
- },
- },
- // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "<unknown>/100m", "2", int64(10), int64(4), "<unknown>"}}},
- },
- // external source type, target value
- {
- hpa: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Name: "some-rc",
- Kind: "ReplicationController",
- },
- MinReplicas: &minReplicasVal,
- MaxReplicas: 10,
- Metrics: []autoscaling.MetricSpec{
- {
- Type: autoscaling.ExternalMetricSourceType,
- External: &autoscaling.ExternalMetricSource{
- Metric: autoscaling.MetricIdentifier{
- Name: "some-external-metric",
- Selector: metricLabelSelector,
- },
- Target: autoscaling.MetricTarget{
- Type: autoscaling.ValueMetricType,
- Value: resource.NewMilliQuantity(100, resource.DecimalSI),
- },
- },
- },
- },
- },
- Status: autoscaling.HorizontalPodAutoscalerStatus{
- CurrentReplicas: 4,
- DesiredReplicas: 5,
- CurrentMetrics: []autoscaling.MetricStatus{
- {
- Type: autoscaling.ExternalMetricSourceType,
- External: &autoscaling.ExternalMetricStatus{
- Metric: autoscaling.MetricIdentifier{
- Name: "some-external-metric",
- },
- Current: autoscaling.MetricValueStatus{
- Value: resource.NewMilliQuantity(50, resource.DecimalSI),
- },
- },
- },
- },
- },
- },
- // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "50m/100m", "2", int64(10), int64(4), "<unknown>"}}},
- },
- // pods source type (no current)
- {
- hpa: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Name: "some-rc",
- Kind: "ReplicationController",
- },
- MinReplicas: &minReplicasVal,
- MaxReplicas: 10,
- Metrics: []autoscaling.MetricSpec{
- {
- Type: autoscaling.PodsMetricSourceType,
- Pods: &autoscaling.PodsMetricSource{
- Metric: autoscaling.MetricIdentifier{
- Name: "some-pods-metric",
- },
- Target: autoscaling.MetricTarget{
- Type: autoscaling.AverageValueMetricType,
- AverageValue: resource.NewMilliQuantity(100, resource.DecimalSI),
- },
- },
- },
- },
- },
- Status: autoscaling.HorizontalPodAutoscalerStatus{
- CurrentReplicas: 4,
- DesiredReplicas: 5,
- },
- },
- // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "<unknown>/100m", "2", int64(10), int64(4), "<unknown>"}}},
- },
- // pods source type
- {
- hpa: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Name: "some-rc",
- Kind: "ReplicationController",
- },
- MinReplicas: &minReplicasVal,
- MaxReplicas: 10,
- Metrics: []autoscaling.MetricSpec{
- {
- Type: autoscaling.PodsMetricSourceType,
- Pods: &autoscaling.PodsMetricSource{
- Metric: autoscaling.MetricIdentifier{
- Name: "some-pods-metric",
- },
- Target: autoscaling.MetricTarget{
- Type: autoscaling.AverageValueMetricType,
- AverageValue: resource.NewMilliQuantity(100, resource.DecimalSI),
- },
- },
- },
- },
- },
- Status: autoscaling.HorizontalPodAutoscalerStatus{
- CurrentReplicas: 4,
- DesiredReplicas: 5,
- CurrentMetrics: []autoscaling.MetricStatus{
- {
- Type: autoscaling.PodsMetricSourceType,
- Pods: &autoscaling.PodsMetricStatus{
- Metric: autoscaling.MetricIdentifier{
- Name: "some-pods-metric",
- },
- Current: autoscaling.MetricValueStatus{
- AverageValue: resource.NewMilliQuantity(50, resource.DecimalSI),
- },
- },
- },
- },
- },
- },
- // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "50m/100m", "2", int64(10), int64(4), "<unknown>"}}},
- },
- // object source type (no current)
- {
- hpa: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Name: "some-rc",
- Kind: "ReplicationController",
- },
- MinReplicas: &minReplicasVal,
- MaxReplicas: 10,
- Metrics: []autoscaling.MetricSpec{
- {
- Type: autoscaling.ObjectMetricSourceType,
- Object: &autoscaling.ObjectMetricSource{
- DescribedObject: autoscaling.CrossVersionObjectReference{
- Name: "some-service",
- Kind: "Service",
- },
- Metric: autoscaling.MetricIdentifier{
- Name: "some-service-metric",
- },
- Target: autoscaling.MetricTarget{
- Type: autoscaling.ValueMetricType,
- Value: resource.NewMilliQuantity(100, resource.DecimalSI),
- },
- },
- },
- },
- },
- Status: autoscaling.HorizontalPodAutoscalerStatus{
- CurrentReplicas: 4,
- DesiredReplicas: 5,
- },
- },
- // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "<unknown>/100m", "2", int64(10), int64(4), "<unknown>"}}},
- },
- // object source type
- {
- hpa: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Name: "some-rc",
- Kind: "ReplicationController",
- },
- MinReplicas: &minReplicasVal,
- MaxReplicas: 10,
- Metrics: []autoscaling.MetricSpec{
- {
- Type: autoscaling.ObjectMetricSourceType,
- Object: &autoscaling.ObjectMetricSource{
- DescribedObject: autoscaling.CrossVersionObjectReference{
- Name: "some-service",
- Kind: "Service",
- },
- Metric: autoscaling.MetricIdentifier{
- Name: "some-service-metric",
- },
- Target: autoscaling.MetricTarget{
- Type: autoscaling.ValueMetricType,
- Value: resource.NewMilliQuantity(100, resource.DecimalSI),
- },
- },
- },
- },
- },
- Status: autoscaling.HorizontalPodAutoscalerStatus{
- CurrentReplicas: 4,
- DesiredReplicas: 5,
- CurrentMetrics: []autoscaling.MetricStatus{
- {
- Type: autoscaling.ObjectMetricSourceType,
- Object: &autoscaling.ObjectMetricStatus{
- DescribedObject: autoscaling.CrossVersionObjectReference{
- Name: "some-service",
- Kind: "Service",
- },
- Metric: autoscaling.MetricIdentifier{
- Name: "some-service-metric",
- },
- Current: autoscaling.MetricValueStatus{
- Value: resource.NewMilliQuantity(50, resource.DecimalSI),
- },
- },
- },
- },
- },
- },
- // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "50m/100m", "2", int64(10), int64(4), "<unknown>"}}},
- },
- // resource source type, targetVal (no current)
- {
- hpa: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Name: "some-rc",
- Kind: "ReplicationController",
- },
- MinReplicas: &minReplicasVal,
- MaxReplicas: 10,
- Metrics: []autoscaling.MetricSpec{
- {
- Type: autoscaling.ResourceMetricSourceType,
- Resource: &autoscaling.ResourceMetricSource{
- Name: api.ResourceCPU,
- Target: autoscaling.MetricTarget{
- Type: autoscaling.AverageValueMetricType,
- AverageValue: resource.NewMilliQuantity(100, resource.DecimalSI),
- },
- },
- },
- },
- },
- Status: autoscaling.HorizontalPodAutoscalerStatus{
- CurrentReplicas: 4,
- DesiredReplicas: 5,
- },
- },
- // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "<unknown>/100m", "2", int64(10), int64(4), "<unknown>"}}},
- },
- // resource source type, targetVal
- {
- hpa: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Name: "some-rc",
- Kind: "ReplicationController",
- },
- MinReplicas: &minReplicasVal,
- MaxReplicas: 10,
- Metrics: []autoscaling.MetricSpec{
- {
- Type: autoscaling.ResourceMetricSourceType,
- Resource: &autoscaling.ResourceMetricSource{
- Name: api.ResourceCPU,
- Target: autoscaling.MetricTarget{
- Type: autoscaling.AverageValueMetricType,
- AverageValue: resource.NewMilliQuantity(100, resource.DecimalSI),
- },
- },
- },
- },
- },
- Status: autoscaling.HorizontalPodAutoscalerStatus{
- CurrentReplicas: 4,
- DesiredReplicas: 5,
- CurrentMetrics: []autoscaling.MetricStatus{
- {
- Type: autoscaling.ResourceMetricSourceType,
- Resource: &autoscaling.ResourceMetricStatus{
- Name: api.ResourceCPU,
- Current: autoscaling.MetricValueStatus{
- AverageValue: resource.NewMilliQuantity(50, resource.DecimalSI),
- },
- },
- },
- },
- },
- },
- // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "50m/100m", "2", int64(10), int64(4), "<unknown>"}}},
- },
- // resource source type, targetUtil (no current)
- {
- hpa: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Name: "some-rc",
- Kind: "ReplicationController",
- },
- MinReplicas: &minReplicasVal,
- MaxReplicas: 10,
- Metrics: []autoscaling.MetricSpec{
- {
- Type: autoscaling.ResourceMetricSourceType,
- Resource: &autoscaling.ResourceMetricSource{
- Name: api.ResourceCPU,
- Target: autoscaling.MetricTarget{
- Type: autoscaling.UtilizationMetricType,
- AverageUtilization: &targetUtilizationVal,
- },
- },
- },
- },
- },
- Status: autoscaling.HorizontalPodAutoscalerStatus{
- CurrentReplicas: 4,
- DesiredReplicas: 5,
- },
- },
- // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "<unknown>/80%", "2", int64(10), int64(4), "<unknown>"}}},
- },
- // resource source type, targetUtil
- {
- hpa: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Name: "some-rc",
- Kind: "ReplicationController",
- },
- MinReplicas: &minReplicasVal,
- MaxReplicas: 10,
- Metrics: []autoscaling.MetricSpec{
- {
- Type: autoscaling.ResourceMetricSourceType,
- Resource: &autoscaling.ResourceMetricSource{
- Name: api.ResourceCPU,
- Target: autoscaling.MetricTarget{
- Type: autoscaling.UtilizationMetricType,
- AverageUtilization: &targetUtilizationVal,
- },
- },
- },
- },
- },
- Status: autoscaling.HorizontalPodAutoscalerStatus{
- CurrentReplicas: 4,
- DesiredReplicas: 5,
- CurrentMetrics: []autoscaling.MetricStatus{
- {
- Type: autoscaling.ResourceMetricSourceType,
- Resource: &autoscaling.ResourceMetricStatus{
- Name: api.ResourceCPU,
- Current: autoscaling.MetricValueStatus{
- AverageUtilization: ¤tUtilizationVal,
- AverageValue: resource.NewMilliQuantity(40, resource.DecimalSI),
- },
- },
- },
- },
- },
- },
- // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "50%/80%", "2", int64(10), int64(4), "<unknown>"}}},
- },
- // multiple specs
- {
- hpa: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: metav1.ObjectMeta{Name: "some-hpa"},
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Name: "some-rc",
- Kind: "ReplicationController",
- },
- MinReplicas: &minReplicasVal,
- MaxReplicas: 10,
- Metrics: []autoscaling.MetricSpec{
- {
- Type: autoscaling.PodsMetricSourceType,
- Pods: &autoscaling.PodsMetricSource{
- Metric: autoscaling.MetricIdentifier{
- Name: "some-pods-metric",
- },
- Target: autoscaling.MetricTarget{
- Type: autoscaling.AverageValueMetricType,
- AverageValue: resource.NewMilliQuantity(100, resource.DecimalSI),
- },
- },
- },
- {
- Type: autoscaling.ResourceMetricSourceType,
- Resource: &autoscaling.ResourceMetricSource{
- Name: api.ResourceCPU,
- Target: autoscaling.MetricTarget{
- Type: autoscaling.UtilizationMetricType,
- AverageUtilization: &targetUtilizationVal,
- },
- },
- },
- {
- Type: autoscaling.PodsMetricSourceType,
- Pods: &autoscaling.PodsMetricSource{
- Metric: autoscaling.MetricIdentifier{
- Name: "other-pods-metric",
- },
- Target: autoscaling.MetricTarget{
- Type: autoscaling.AverageValueMetricType,
- AverageValue: resource.NewMilliQuantity(400, resource.DecimalSI),
- },
- },
- },
- },
- },
- Status: autoscaling.HorizontalPodAutoscalerStatus{
- CurrentReplicas: 4,
- DesiredReplicas: 5,
- CurrentMetrics: []autoscaling.MetricStatus{
- {
- Type: autoscaling.PodsMetricSourceType,
- Pods: &autoscaling.PodsMetricStatus{
- Metric: autoscaling.MetricIdentifier{
- Name: "some-pods-metric",
- },
- Current: autoscaling.MetricValueStatus{
- AverageValue: resource.NewMilliQuantity(50, resource.DecimalSI),
- },
- },
- },
- {
- Type: autoscaling.ResourceMetricSourceType,
- Resource: &autoscaling.ResourceMetricStatus{
- Name: api.ResourceCPU,
- Current: autoscaling.MetricValueStatus{
- AverageUtilization: ¤tUtilizationVal,
- AverageValue: resource.NewMilliQuantity(40, resource.DecimalSI),
- },
- },
- },
- },
- },
- },
- // Columns: Name, Reference, Targets, MinPods, MaxPods, Replicas, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"some-hpa", "ReplicationController/some-rc", "50m/100m, 50%/80% + 1 more...", "2", int64(10), int64(4), "<unknown>"}}},
- },
- }
- for i, test := range tests {
- rows, err := printHorizontalPodAutoscaler(&test.hpa, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintService(t *testing.T) {
- singleExternalIP := []string{"80.11.12.10"}
- mulExternalIP := []string{"80.11.12.10", "80.11.12.11"}
- tests := []struct {
- service api.Service
- options printers.GenerateOptions
- expected []metav1.TableRow
- }{
- {
- // Test name, cluster ip, port with protocol
- service: api.Service{
- ObjectMeta: metav1.ObjectMeta{Name: "test1"},
- Spec: api.ServiceSpec{
- Type: api.ServiceTypeClusterIP,
- Ports: []api.ServicePort{
- {
- Protocol: "tcp",
- Port: 2233,
- },
- },
- ClusterIP: "10.9.8.7",
- Selector: map[string]string{"foo": "bar"}, // Does NOT get printed.
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
- expected: []metav1.TableRow{{Cells: []interface{}{"test1", "ClusterIP", "10.9.8.7", "<none>", "2233/tcp", "<unknown>"}}},
- },
- {
- // Test generate options: Wide includes selectors.
- service: api.Service{
- ObjectMeta: metav1.ObjectMeta{Name: "test1"},
- Spec: api.ServiceSpec{
- Type: api.ServiceTypeClusterIP,
- Ports: []api.ServicePort{
- {
- Protocol: "tcp",
- Port: 2233,
- },
- },
- ClusterIP: "10.9.8.7",
- Selector: map[string]string{"foo": "bar"},
- },
- },
- options: printers.GenerateOptions{Wide: true},
- // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age, Selector
- expected: []metav1.TableRow{{Cells: []interface{}{"test1", "ClusterIP", "10.9.8.7", "<none>", "2233/tcp", "<unknown>", "foo=bar"}}},
- },
- {
- // Test NodePort service
- service: api.Service{
- ObjectMeta: metav1.ObjectMeta{Name: "test2"},
- Spec: api.ServiceSpec{
- Type: api.ServiceTypeNodePort,
- Ports: []api.ServicePort{
- {
- Protocol: "tcp",
- Port: 8888,
- NodePort: 9999,
- },
- },
- ClusterIP: "10.9.8.7",
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
- expected: []metav1.TableRow{{Cells: []interface{}{"test2", "NodePort", "10.9.8.7", "<none>", "8888:9999/tcp", "<unknown>"}}},
- },
- {
- // Test LoadBalancer service
- service: api.Service{
- ObjectMeta: metav1.ObjectMeta{Name: "test3"},
- Spec: api.ServiceSpec{
- Type: api.ServiceTypeLoadBalancer,
- Ports: []api.ServicePort{
- {
- Protocol: "tcp",
- Port: 8888,
- },
- },
- ClusterIP: "10.9.8.7",
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
- expected: []metav1.TableRow{{Cells: []interface{}{"test3", "LoadBalancer", "10.9.8.7", "<pending>", "8888/tcp", "<unknown>"}}},
- },
- {
- // Test LoadBalancer service with single ExternalIP and no LoadBalancerStatus
- service: api.Service{
- ObjectMeta: metav1.ObjectMeta{Name: "test4"},
- Spec: api.ServiceSpec{
- Type: api.ServiceTypeLoadBalancer,
- Ports: []api.ServicePort{
- {
- Protocol: "tcp",
- Port: 8888,
- },
- },
- ClusterIP: "10.9.8.7",
- ExternalIPs: singleExternalIP,
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
- expected: []metav1.TableRow{{Cells: []interface{}{"test4", "LoadBalancer", "10.9.8.7", "80.11.12.10", "8888/tcp", "<unknown>"}}},
- },
- {
- // Test LoadBalancer service with single ExternalIP
- service: api.Service{
- ObjectMeta: metav1.ObjectMeta{Name: "test5"},
- Spec: api.ServiceSpec{
- Type: api.ServiceTypeLoadBalancer,
- Ports: []api.ServicePort{
- {
- Protocol: "tcp",
- Port: 8888,
- },
- },
- ClusterIP: "10.9.8.7",
- ExternalIPs: singleExternalIP,
- },
- Status: api.ServiceStatus{
- LoadBalancer: api.LoadBalancerStatus{
- Ingress: []api.LoadBalancerIngress{
- {
- IP: "3.4.5.6",
- Hostname: "test.cluster.com",
- },
- },
- },
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
- expected: []metav1.TableRow{{Cells: []interface{}{"test5", "LoadBalancer", "10.9.8.7", "3.4.5.6,80.11.12.10", "8888/tcp", "<unknown>"}}},
- },
- {
- // Test LoadBalancer service with mul ExternalIPs
- service: api.Service{
- ObjectMeta: metav1.ObjectMeta{Name: "test6"},
- Spec: api.ServiceSpec{
- Type: api.ServiceTypeLoadBalancer,
- Ports: []api.ServicePort{
- {
- Protocol: "tcp",
- Port: 8888,
- },
- },
- ClusterIP: "10.9.8.7",
- ExternalIPs: mulExternalIP,
- },
- Status: api.ServiceStatus{
- LoadBalancer: api.LoadBalancerStatus{
- Ingress: []api.LoadBalancerIngress{
- {
- IP: "2.3.4.5",
- Hostname: "test.cluster.local",
- },
- {
- IP: "3.4.5.6",
- Hostname: "test.cluster.com",
- },
- },
- },
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
- 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>"}}},
- },
- {
- // Test ExternalName service
- service: api.Service{
- ObjectMeta: metav1.ObjectMeta{Name: "test7"},
- Spec: api.ServiceSpec{
- Type: api.ServiceTypeExternalName,
- ExternalName: "my.database.example.com",
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
- expected: []metav1.TableRow{{Cells: []interface{}{"test7", "ExternalName", "<none>", "my.database.example.com", "<none>", "<unknown>"}}},
- },
- }
- for i, test := range tests {
- rows, err := printService(&test.service, test.options)
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintServiceList(t *testing.T) {
- serviceList := api.ServiceList{
- Items: []api.Service{
- {
- ObjectMeta: metav1.ObjectMeta{Name: "service1"},
- Spec: api.ServiceSpec{
- Type: api.ServiceTypeClusterIP,
- Ports: []api.ServicePort{
- {
- Protocol: "tcp",
- Port: 2233,
- },
- },
- ClusterIP: "10.9.8.7",
- },
- },
- {
- ObjectMeta: metav1.ObjectMeta{Name: "service2"},
- Spec: api.ServiceSpec{
- Type: api.ServiceTypeNodePort,
- Ports: []api.ServicePort{
- {
- Protocol: "udp",
- Port: 5566,
- },
- },
- ClusterIP: "1.2.3.4",
- },
- },
- },
- }
- // Columns: Name, Type, Cluster-IP, External-IP, Port(s), Age
- expectedRows := []metav1.TableRow{
- {Cells: []interface{}{"service1", "ClusterIP", "10.9.8.7", "<none>", "2233/tcp", "<unknown>"}},
- {Cells: []interface{}{"service2", "NodePort", "1.2.3.4", "<none>", "5566/udp", "<unknown>"}},
- }
- rows, err := printServiceList(&serviceList, printers.GenerateOptions{})
- if err != nil {
- t.Fatalf("Error printing service list: %#v", err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(expectedRows, rows) {
- t.Errorf("mismatch: %s", diff.ObjectReflectDiff(expectedRows, rows))
- }
- }
- func TestPrintPodDisruptionBudget(t *testing.T) {
- minAvailable := intstr.FromInt(22)
- maxUnavailable := intstr.FromInt(11)
- tests := []struct {
- pdb policy.PodDisruptionBudget
- expected []metav1.TableRow
- }{
- // Min Available set, no Max Available.
- {
- pdb: policy.PodDisruptionBudget{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: "ns1",
- Name: "pdb1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: policy.PodDisruptionBudgetSpec{
- MinAvailable: &minAvailable,
- },
- Status: policy.PodDisruptionBudgetStatus{
- DisruptionsAllowed: 5,
- },
- },
- // Columns: Name, Min Available, Max Available, Allowed Disruptions, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"pdb1", "22", "N/A", int64(5), "0s"}}},
- },
- // Max Available set, no Min Available.
- {
- pdb: policy.PodDisruptionBudget{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: "ns2",
- Name: "pdb2",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: policy.PodDisruptionBudgetSpec{
- MaxUnavailable: &maxUnavailable,
- },
- Status: policy.PodDisruptionBudgetStatus{
- DisruptionsAllowed: 5,
- },
- },
- // Columns: Name, Min Available, Max Available, Allowed Disruptions, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"pdb2", "N/A", "11", int64(5), "0s"}}},
- }}
- for i, test := range tests {
- rows, err := printPodDisruptionBudget(&test.pdb, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintPodDisruptionBudgetList(t *testing.T) {
- minAvailable := intstr.FromInt(22)
- maxUnavailable := intstr.FromInt(11)
- pdbList := policy.PodDisruptionBudgetList{
- Items: []policy.PodDisruptionBudget{
- {
- ObjectMeta: metav1.ObjectMeta{
- Namespace: "ns1",
- Name: "pdb1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: policy.PodDisruptionBudgetSpec{
- MaxUnavailable: &maxUnavailable,
- },
- Status: policy.PodDisruptionBudgetStatus{
- DisruptionsAllowed: 5,
- },
- },
- {
- ObjectMeta: metav1.ObjectMeta{
- Namespace: "ns2",
- Name: "pdb2",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: policy.PodDisruptionBudgetSpec{
- MinAvailable: &minAvailable,
- },
- Status: policy.PodDisruptionBudgetStatus{
- DisruptionsAllowed: 3,
- },
- },
- },
- }
- // Columns: Name, Min Available, Max Available, Allowed Disruptions, Age
- expectedRows := []metav1.TableRow{
- {Cells: []interface{}{"pdb1", "N/A", "11", int64(5), "0s"}},
- {Cells: []interface{}{"pdb2", "22", "N/A", int64(3), "0s"}},
- }
- rows, err := printPodDisruptionBudgetList(&pdbList, printers.GenerateOptions{})
- if err != nil {
- t.Fatalf("Error printing pod template list: %#v", err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(expectedRows, rows) {
- t.Errorf("mismatch: %s", diff.ObjectReflectDiff(expectedRows, rows))
- }
- }
- func TestPrintControllerRevision(t *testing.T) {
- tests := []struct {
- history apps.ControllerRevision
- expected []metav1.TableRow
- }{
- {
- history: apps.ControllerRevision{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- OwnerReferences: []metav1.OwnerReference{
- {
- Controller: boolP(true),
- APIVersion: "apps/v1",
- Kind: "DaemonSet",
- Name: "foo",
- },
- },
- },
- Revision: 1,
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"test1", "daemonset.apps/foo", int64(1), "0s"}}},
- },
- {
- history: apps.ControllerRevision{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test2",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- OwnerReferences: []metav1.OwnerReference{
- {
- Controller: boolP(false),
- Kind: "ABC",
- Name: "foo",
- },
- },
- },
- Revision: 2,
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"test2", "<none>", int64(2), "0s"}}},
- },
- {
- history: apps.ControllerRevision{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test3",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- OwnerReferences: []metav1.OwnerReference{},
- },
- Revision: 3,
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"test3", "<none>", int64(3), "0s"}}},
- },
- {
- history: apps.ControllerRevision{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test4",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- OwnerReferences: nil,
- },
- Revision: 4,
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"test4", "<none>", int64(4), "0s"}}},
- },
- }
- for i, test := range tests {
- rows, err := printControllerRevision(&test.history, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func boolP(b bool) *bool {
- return &b
- }
- func TestPrintConfigMap(t *testing.T) {
- tests := []struct {
- configMap api.ConfigMap
- expected []metav1.TableRow
- }{
- // Basic config map with no data.
- {
- configMap: api.ConfigMap{
- ObjectMeta: metav1.ObjectMeta{
- Name: "configmap1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- },
- // Columns: Name, Data, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"configmap1", int64(0), "0s"}}},
- },
- // Basic config map with one data entry
- {
- configMap: api.ConfigMap{
- ObjectMeta: metav1.ObjectMeta{
- Name: "configmap2",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Data: map[string]string{
- "foo": "bar",
- },
- },
- // Columns: Name, (Num) Data, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"configmap2", int64(1), "0s"}}},
- },
- // Basic config map with one data and one binary data entry.
- {
- configMap: api.ConfigMap{
- ObjectMeta: metav1.ObjectMeta{
- Name: "configmap3",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Data: map[string]string{
- "foo": "bar",
- },
- BinaryData: map[string][]byte{
- "bin": []byte("binary data"),
- },
- },
- // Columns: Name, (Num) Data, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"configmap3", int64(2), "0s"}}},
- },
- }
- for i, test := range tests {
- rows, err := printConfigMap(&test.configMap, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintNetworkPolicy(t *testing.T) {
- tests := []struct {
- policy networking.NetworkPolicy
- expected []metav1.TableRow
- }{
- // Basic network policy with empty spec.
- {
- policy: networking.NetworkPolicy{
- ObjectMeta: metav1.ObjectMeta{
- Name: "policy1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: networking.NetworkPolicySpec{},
- },
- // Columns: Name, Pod-Selector, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"policy1", "<none>", "0s"}}},
- },
- // Basic network policy with pod selector.
- {
- policy: networking.NetworkPolicy{
- ObjectMeta: metav1.ObjectMeta{
- Name: "policy2",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: networking.NetworkPolicySpec{
- PodSelector: metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
- },
- },
- // Columns: Name, Pod-Selector, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"policy2", "foo=bar", "0s"}}},
- },
- }
- for i, test := range tests {
- rows, err := printNetworkPolicy(&test.policy, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintRoleBinding(t *testing.T) {
- tests := []struct {
- binding rbac.RoleBinding
- options printers.GenerateOptions
- expected []metav1.TableRow
- }{
- // Basic role binding
- {
- binding: rbac.RoleBinding{
- ObjectMeta: metav1.ObjectMeta{
- Name: "binding1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Subjects: []rbac.Subject{
- {
- Kind: "User",
- Name: "system:kube-controller-manager",
- },
- },
- RoleRef: rbac.RoleRef{
- Kind: "Role",
- Name: "extension-apiserver-authentication-reader",
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"binding1", "Role/extension-apiserver-authentication-reader", "0s"}}},
- },
- // Generate options=Wide; print subject and roles.
- {
- binding: rbac.RoleBinding{
- ObjectMeta: metav1.ObjectMeta{
- Name: "binding2",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Subjects: []rbac.Subject{
- {
- Kind: "User",
- Name: "user-name",
- },
- {
- Kind: "Group",
- Name: "group-name",
- },
- {
- Kind: "ServiceAccount",
- Name: "service-account-name",
- Namespace: "service-account-namespace",
- },
- },
- RoleRef: rbac.RoleRef{
- Kind: "Role",
- Name: "role-name",
- },
- },
- options: printers.GenerateOptions{Wide: true},
- // Columns: Name, Age, Role, Users, Groups, ServiceAccounts
- expected: []metav1.TableRow{{Cells: []interface{}{"binding2", "Role/role-name", "0s", "user-name", "group-name", "service-account-namespace/service-account-name"}}},
- },
- }
- for i, test := range tests {
- rows, err := printRoleBinding(&test.binding, test.options)
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintClusterRoleBinding(t *testing.T) {
- tests := []struct {
- binding rbac.ClusterRoleBinding
- options printers.GenerateOptions
- expected []metav1.TableRow
- }{
- // Basic cluster role binding
- {
- binding: rbac.ClusterRoleBinding{
- ObjectMeta: metav1.ObjectMeta{
- Name: "binding1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Subjects: []rbac.Subject{
- {
- Kind: "User",
- Name: "system:kube-controller-manager",
- },
- },
- RoleRef: rbac.RoleRef{
- Kind: "Role",
- Name: "extension-apiserver-authentication-reader",
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"binding1", "Role/extension-apiserver-authentication-reader", "0s"}}},
- },
- // Generate options=Wide; print subject and roles.
- {
- binding: rbac.ClusterRoleBinding{
- ObjectMeta: metav1.ObjectMeta{
- Name: "binding2",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Subjects: []rbac.Subject{
- {
- Kind: "User",
- Name: "user-name",
- },
- {
- Kind: "Group",
- Name: "group-name",
- },
- {
- Kind: "ServiceAccount",
- Name: "service-account-name",
- Namespace: "service-account-namespace",
- },
- },
- RoleRef: rbac.RoleRef{
- Kind: "Role",
- Name: "role-name",
- },
- },
- options: printers.GenerateOptions{Wide: true},
- // Columns: Name, Age, Role, Users, Groups, ServiceAccounts
- expected: []metav1.TableRow{{Cells: []interface{}{"binding2", "Role/role-name", "0s", "user-name", "group-name", "service-account-namespace/service-account-name"}}},
- },
- }
- for i, test := range tests {
- rows, err := printClusterRoleBinding(&test.binding, test.options)
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintCertificateSigningRequest(t *testing.T) {
- tests := []struct {
- csr certificates.CertificateSigningRequest
- expected []metav1.TableRow
- }{
- // Basic CSR with no spec or status; defaults to status: Pending.
- {
- csr: certificates.CertificateSigningRequest{
- ObjectMeta: metav1.ObjectMeta{
- Name: "csr1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: certificates.CertificateSigningRequestSpec{},
- Status: certificates.CertificateSigningRequestStatus{},
- },
- // Columns: Name, Age, Requestor, Condition
- expected: []metav1.TableRow{{Cells: []interface{}{"csr1", "0s", "", "Pending"}}},
- },
- // Basic CSR with Spec and Status=Approved.
- {
- csr: certificates.CertificateSigningRequest{
- ObjectMeta: metav1.ObjectMeta{
- Name: "csr2",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: certificates.CertificateSigningRequestSpec{
- Username: "CSR Requestor",
- },
- Status: certificates.CertificateSigningRequestStatus{
- Conditions: []certificates.CertificateSigningRequestCondition{
- {
- Type: certificates.CertificateApproved,
- },
- },
- },
- },
- // Columns: Name, Age, Requestor, Condition
- expected: []metav1.TableRow{{Cells: []interface{}{"csr2", "0s", "CSR Requestor", "Approved"}}},
- },
- // Basic CSR with Spec and Status=Approved; certificate issued.
- {
- csr: certificates.CertificateSigningRequest{
- ObjectMeta: metav1.ObjectMeta{
- Name: "csr2",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: certificates.CertificateSigningRequestSpec{
- Username: "CSR Requestor",
- },
- Status: certificates.CertificateSigningRequestStatus{
- Conditions: []certificates.CertificateSigningRequestCondition{
- {
- Type: certificates.CertificateApproved,
- },
- },
- Certificate: []byte("cert data"),
- },
- },
- // Columns: Name, Age, Requestor, Condition
- expected: []metav1.TableRow{{Cells: []interface{}{"csr2", "0s", "CSR Requestor", "Approved,Issued"}}},
- },
- // Basic CSR with Spec and Status=Denied.
- {
- csr: certificates.CertificateSigningRequest{
- ObjectMeta: metav1.ObjectMeta{
- Name: "csr3",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: certificates.CertificateSigningRequestSpec{
- Username: "CSR Requestor",
- },
- Status: certificates.CertificateSigningRequestStatus{
- Conditions: []certificates.CertificateSigningRequestCondition{
- {
- Type: certificates.CertificateDenied,
- },
- },
- },
- },
- // Columns: Name, Age, Requestor, Condition
- expected: []metav1.TableRow{{Cells: []interface{}{"csr3", "0s", "CSR Requestor", "Denied"}}},
- },
- }
- for i, test := range tests {
- rows, err := printCertificateSigningRequest(&test.csr, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintReplicationController(t *testing.T) {
- tests := []struct {
- rc api.ReplicationController
- options printers.GenerateOptions
- expected []metav1.TableRow
- }{
- // Basic print replication controller without replicas or status.
- {
- rc: api.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Name: "rc1",
- Namespace: "test-namespace",
- },
- Spec: api.ReplicationControllerSpec{
- Selector: map[string]string{"a": "b"},
- Template: &api.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{"a": "b"},
- },
- Spec: api.PodSpec{
- Containers: []api.Container{
- {
- Name: "test",
- Image: "test_image",
- ImagePullPolicy: api.PullIfNotPresent,
- TerminationMessagePolicy: api.TerminationMessageReadFile,
- },
- },
- RestartPolicy: api.RestartPolicyAlways,
- DNSPolicy: api.DNSClusterFirst,
- },
- },
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Desired, Current, Ready, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"rc1", int64(0), int64(0), int64(0), "<unknown>"}}},
- },
- // Basic print replication controller with replicas; does not print containers or labels
- {
- rc: api.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Name: "rc1",
- Namespace: "test-namespace",
- },
- Spec: api.ReplicationControllerSpec{
- Replicas: 5,
- Selector: map[string]string{"a": "b"},
- Template: &api.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{"a": "b"},
- },
- Spec: api.PodSpec{
- Containers: []api.Container{
- {
- Name: "test",
- Image: "test_image",
- ImagePullPolicy: api.PullIfNotPresent,
- TerminationMessagePolicy: api.TerminationMessageReadFile,
- },
- },
- RestartPolicy: api.RestartPolicyAlways,
- DNSPolicy: api.DNSClusterFirst,
- },
- },
- },
- Status: api.ReplicationControllerStatus{
- Replicas: 3,
- ReadyReplicas: 1,
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Desired, Current, Ready, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"rc1", int64(5), int64(3), int64(1), "<unknown>"}}},
- },
- // Generate options: Wide; print containers and labels.
- {
- rc: api.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Name: "rc1",
- },
- Spec: api.ReplicationControllerSpec{
- Replicas: 5,
- Selector: map[string]string{"a": "b"},
- Template: &api.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{"a": "b"},
- },
- Spec: api.PodSpec{
- Containers: []api.Container{
- {
- Name: "test",
- Image: "test_image",
- ImagePullPolicy: api.PullIfNotPresent,
- TerminationMessagePolicy: api.TerminationMessageReadFile,
- },
- },
- RestartPolicy: api.RestartPolicyAlways,
- DNSPolicy: api.DNSClusterFirst,
- },
- },
- },
- Status: api.ReplicationControllerStatus{
- Replicas: 3,
- ReadyReplicas: 1,
- },
- },
- options: printers.GenerateOptions{Wide: true},
- // Columns: Name, Desired, Current, Ready, Age, Containers, Images, Selector
- expected: []metav1.TableRow{{Cells: []interface{}{"rc1", int64(5), int64(3), int64(1), "<unknown>", "test", "test_image", "a=b"}}},
- },
- }
- for i, test := range tests {
- rows, err := printReplicationController(&test.rc, test.options)
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintReplicaSet(t *testing.T) {
- tests := []struct {
- replicaSet apps.ReplicaSet
- options printers.GenerateOptions
- expected []metav1.TableRow
- }{
- // Generate options empty
- {
- replicaSet: apps.ReplicaSet{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: apps.ReplicaSetSpec{
- Replicas: 5,
- Template: api.PodTemplateSpec{
- Spec: api.PodSpec{
- Containers: []api.Container{
- {
- Name: "fake-container1",
- Image: "fake-image1",
- },
- {
- Name: "fake-container2",
- Image: "fake-image2",
- },
- },
- },
- },
- Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
- },
- Status: apps.ReplicaSetStatus{
- Replicas: 5,
- ReadyReplicas: 2,
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Desired, Current, Ready, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"test1", int64(5), int64(5), int64(2), "0s"}}},
- },
- // Generate options "Wide"
- {
- replicaSet: apps.ReplicaSet{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: apps.ReplicaSetSpec{
- Replicas: 5,
- Template: api.PodTemplateSpec{
- Spec: api.PodSpec{
- Containers: []api.Container{
- {
- Name: "fake-container1",
- Image: "fake-image1",
- },
- {
- Name: "fake-container2",
- Image: "fake-image2",
- },
- },
- },
- },
- Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
- },
- Status: apps.ReplicaSetStatus{
- Replicas: 5,
- ReadyReplicas: 2,
- },
- },
- options: printers.GenerateOptions{Wide: true},
- // Columns: Name, Desired, Current, Ready, Age, Containers, Images, Selector
- expected: []metav1.TableRow{{Cells: []interface{}{"test1", int64(5), int64(5), int64(2), "0s", "fake-container1,fake-container2", "fake-image1,fake-image2", "foo=bar"}}},
- },
- }
- for i, test := range tests {
- rows, err := printReplicaSet(&test.replicaSet, test.options)
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintReplicaSetList(t *testing.T) {
- replicaSetList := apps.ReplicaSetList{
- Items: []apps.ReplicaSet{
- {
- ObjectMeta: metav1.ObjectMeta{
- Name: "replicaset1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: apps.ReplicaSetSpec{
- Replicas: 5,
- Template: api.PodTemplateSpec{
- Spec: api.PodSpec{
- Containers: []api.Container{
- {
- Name: "fake-container1",
- Image: "fake-image1",
- },
- {
- Name: "fake-container2",
- Image: "fake-image2",
- },
- },
- },
- },
- },
- Status: apps.ReplicaSetStatus{
- Replicas: 5,
- ReadyReplicas: 2,
- },
- },
- {
- ObjectMeta: metav1.ObjectMeta{
- Name: "replicaset2",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: apps.ReplicaSetSpec{
- Replicas: 4,
- Template: api.PodTemplateSpec{},
- Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
- },
- Status: apps.ReplicaSetStatus{
- Replicas: 3,
- ReadyReplicas: 1,
- },
- },
- },
- }
- // Columns: Name, Desired, Current, Ready, Age
- expectedRows := []metav1.TableRow{
- {Cells: []interface{}{"replicaset1", int64(5), int64(5), int64(2), "0s"}},
- {Cells: []interface{}{"replicaset2", int64(4), int64(3), int64(1), "0s"}},
- }
- rows, err := printReplicaSetList(&replicaSetList, printers.GenerateOptions{})
- if err != nil {
- t.Fatalf("Error printing replica set list: %#v", err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(expectedRows, rows) {
- t.Errorf("mismatch: %s", diff.ObjectReflectDiff(expectedRows, rows))
- }
- }
- func TestPrintStatefulSet(t *testing.T) {
- tests := []struct {
- statefulSet apps.StatefulSet
- options printers.GenerateOptions
- expected []metav1.TableRow
- }{
- // Basic stateful set; no generate options.
- {
- statefulSet: apps.StatefulSet{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: apps.StatefulSetSpec{
- Replicas: 5,
- Template: api.PodTemplateSpec{
- Spec: api.PodSpec{
- Containers: []api.Container{
- {
- Name: "fake-container1",
- Image: "fake-image1",
- },
- {
- Name: "fake-container2",
- Image: "fake-image2",
- },
- },
- },
- },
- },
- Status: apps.StatefulSetStatus{
- Replicas: 5,
- ReadyReplicas: 2,
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Ready, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"test1", "2/5", "0s"}}},
- },
- // Generate options "Wide"; includes containers and images.
- {
- statefulSet: apps.StatefulSet{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: apps.StatefulSetSpec{
- Replicas: 5,
- Template: api.PodTemplateSpec{
- Spec: api.PodSpec{
- Containers: []api.Container{
- {
- Name: "fake-container1",
- Image: "fake-image1",
- },
- {
- Name: "fake-container2",
- Image: "fake-image2",
- },
- },
- },
- },
- },
- Status: apps.StatefulSetStatus{
- Replicas: 5,
- ReadyReplicas: 2,
- },
- },
- options: printers.GenerateOptions{Wide: true},
- // Columns: Name, Ready, Age, Containers, Images
- expected: []metav1.TableRow{{Cells: []interface{}{"test1", "2/5", "0s", "fake-container1,fake-container2", "fake-image1,fake-image2"}}},
- },
- }
- for i, test := range tests {
- rows, err := printStatefulSet(&test.statefulSet, test.options)
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintPersistentVolume(t *testing.T) {
- myScn := "my-scn"
- claimRef := api.ObjectReference{
- Name: "test",
- Namespace: "default",
- }
- tests := []struct {
- pv api.PersistentVolume
- expected []metav1.TableRow
- }{
- {
- // Test bound
- pv: api.PersistentVolume{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test1",
- },
- Spec: api.PersistentVolumeSpec{
- ClaimRef: &claimRef,
- AccessModes: []api.PersistentVolumeAccessMode{api.ReadOnlyMany},
- Capacity: map[api.ResourceName]resource.Quantity{
- api.ResourceStorage: resource.MustParse("4Gi"),
- },
- },
- Status: api.PersistentVolumeStatus{
- Phase: api.VolumeBound,
- },
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"test1", "4Gi", "ROX", "", "Bound", "default/test", "", "", "<unknown>", "<unset>"}}},
- },
- {
- // Test failed
- pv: api.PersistentVolume{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test2",
- },
- Spec: api.PersistentVolumeSpec{
- ClaimRef: &claimRef,
- AccessModes: []api.PersistentVolumeAccessMode{api.ReadOnlyMany},
- Capacity: map[api.ResourceName]resource.Quantity{
- api.ResourceStorage: resource.MustParse("4Gi"),
- },
- },
- Status: api.PersistentVolumeStatus{
- Phase: api.VolumeFailed,
- },
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"test2", "4Gi", "ROX", "", "Failed", "default/test", "", "", "<unknown>", "<unset>"}}},
- },
- {
- // Test pending
- pv: api.PersistentVolume{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test3",
- },
- Spec: api.PersistentVolumeSpec{
- ClaimRef: &claimRef,
- AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteMany},
- Capacity: map[api.ResourceName]resource.Quantity{
- api.ResourceStorage: resource.MustParse("10Gi"),
- },
- },
- Status: api.PersistentVolumeStatus{
- Phase: api.VolumePending,
- },
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"test3", "10Gi", "RWX", "", "Pending", "default/test", "", "", "<unknown>", "<unset>"}}},
- },
- {
- // Test pending, storageClass
- pv: api.PersistentVolume{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test4",
- },
- Spec: api.PersistentVolumeSpec{
- ClaimRef: &claimRef,
- StorageClassName: myScn,
- AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce},
- Capacity: map[api.ResourceName]resource.Quantity{
- api.ResourceStorage: resource.MustParse("10Gi"),
- },
- },
- Status: api.PersistentVolumeStatus{
- Phase: api.VolumePending,
- },
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"test4", "10Gi", "RWO", "", "Pending", "default/test", "my-scn", "", "<unknown>", "<unset>"}}},
- },
- {
- // Test available
- pv: api.PersistentVolume{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test5",
- },
- Spec: api.PersistentVolumeSpec{
- ClaimRef: &claimRef,
- StorageClassName: myScn,
- AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce},
- Capacity: map[api.ResourceName]resource.Quantity{
- api.ResourceStorage: resource.MustParse("10Gi"),
- },
- },
- Status: api.PersistentVolumeStatus{
- Phase: api.VolumeAvailable,
- },
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"test5", "10Gi", "RWO", "", "Available", "default/test", "my-scn", "", "<unknown>", "<unset>"}}},
- },
- {
- // Test released
- pv: api.PersistentVolume{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test6",
- },
- Spec: api.PersistentVolumeSpec{
- ClaimRef: &claimRef,
- StorageClassName: myScn,
- AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce},
- Capacity: map[api.ResourceName]resource.Quantity{
- api.ResourceStorage: resource.MustParse("10Gi"),
- },
- },
- Status: api.PersistentVolumeStatus{
- Phase: api.VolumeReleased,
- },
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"test6", "10Gi", "RWO", "", "Released", "default/test", "my-scn", "", "<unknown>", "<unset>"}}},
- },
- }
- for i, test := range tests {
- rows, err := printPersistentVolume(&test.pv, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintPersistentVolumeClaim(t *testing.T) {
- volumeMode := api.PersistentVolumeFilesystem
- myScn := "my-scn"
- tests := []struct {
- pvc api.PersistentVolumeClaim
- expected []metav1.TableRow
- }{
- {
- // Test name, num of containers, restarts, container ready status
- pvc: api.PersistentVolumeClaim{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test1",
- },
- Spec: api.PersistentVolumeClaimSpec{
- VolumeName: "my-volume",
- VolumeMode: &volumeMode,
- },
- Status: api.PersistentVolumeClaimStatus{
- Phase: api.ClaimBound,
- AccessModes: []api.PersistentVolumeAccessMode{api.ReadOnlyMany},
- Capacity: map[api.ResourceName]resource.Quantity{
- api.ResourceStorage: resource.MustParse("4Gi"),
- },
- },
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"test1", "Bound", "my-volume", "4Gi", "ROX", "", "<unknown>", "Filesystem"}}},
- },
- {
- // Test name, num of containers, restarts, container ready status
- pvc: api.PersistentVolumeClaim{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test2",
- },
- Spec: api.PersistentVolumeClaimSpec{
- VolumeMode: &volumeMode,
- },
- Status: api.PersistentVolumeClaimStatus{
- Phase: api.ClaimLost,
- AccessModes: []api.PersistentVolumeAccessMode{api.ReadOnlyMany},
- Capacity: map[api.ResourceName]resource.Quantity{
- api.ResourceStorage: resource.MustParse("4Gi"),
- },
- },
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"test2", "Lost", "", "", "", "", "<unknown>", "Filesystem"}}},
- },
- {
- // Test name, num of containers, restarts, container ready status
- pvc: api.PersistentVolumeClaim{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test3",
- },
- Spec: api.PersistentVolumeClaimSpec{
- VolumeName: "my-volume",
- VolumeMode: &volumeMode,
- },
- Status: api.PersistentVolumeClaimStatus{
- Phase: api.ClaimPending,
- AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteMany},
- Capacity: map[api.ResourceName]resource.Quantity{
- api.ResourceStorage: resource.MustParse("10Gi"),
- },
- },
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"test3", "Pending", "my-volume", "10Gi", "RWX", "", "<unknown>", "Filesystem"}}},
- },
- {
- // Test name, num of containers, restarts, container ready status
- pvc: api.PersistentVolumeClaim{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test4",
- },
- Spec: api.PersistentVolumeClaimSpec{
- VolumeName: "my-volume",
- StorageClassName: &myScn,
- VolumeMode: &volumeMode,
- },
- Status: api.PersistentVolumeClaimStatus{
- Phase: api.ClaimPending,
- AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce},
- Capacity: map[api.ResourceName]resource.Quantity{
- api.ResourceStorage: resource.MustParse("10Gi"),
- },
- },
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"test4", "Pending", "my-volume", "10Gi", "RWO", "my-scn", "<unknown>", "Filesystem"}}},
- },
- {
- // Test name, num of containers, restarts, container ready status
- pvc: api.PersistentVolumeClaim{
- ObjectMeta: metav1.ObjectMeta{
- Name: "test5",
- },
- Spec: api.PersistentVolumeClaimSpec{
- VolumeName: "my-volume",
- StorageClassName: &myScn,
- },
- Status: api.PersistentVolumeClaimStatus{
- Phase: api.ClaimPending,
- AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce},
- Capacity: map[api.ResourceName]resource.Quantity{
- api.ResourceStorage: resource.MustParse("10Gi"),
- },
- },
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"test5", "Pending", "my-volume", "10Gi", "RWO", "my-scn", "<unknown>", "<unset>"}}},
- },
- }
- for i, test := range tests {
- rows, err := printPersistentVolumeClaim(&test.pvc, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintComponentStatus(t *testing.T) {
- tests := []struct {
- componentStatus api.ComponentStatus
- expected []metav1.TableRow
- }{
- // Basic component status without conditions
- {
- componentStatus: api.ComponentStatus{
- ObjectMeta: metav1.ObjectMeta{
- Name: "cs1",
- },
- Conditions: []api.ComponentCondition{},
- },
- // Columns: Name, Status, Message, Error
- expected: []metav1.TableRow{{Cells: []interface{}{"cs1", "Unknown", "", ""}}},
- },
- // Basic component status with healthy condition.
- {
- componentStatus: api.ComponentStatus{
- ObjectMeta: metav1.ObjectMeta{
- Name: "cs2",
- },
- Conditions: []api.ComponentCondition{
- {
- Type: "Healthy",
- Status: api.ConditionTrue,
- Message: "test message",
- Error: "test error",
- },
- },
- },
- // Columns: Name, Status, Message, Error
- expected: []metav1.TableRow{{Cells: []interface{}{"cs2", "Healthy", "test message", "test error"}}},
- },
- // Basic component status with healthy condition.
- {
- componentStatus: api.ComponentStatus{
- ObjectMeta: metav1.ObjectMeta{
- Name: "cs3",
- },
- Conditions: []api.ComponentCondition{
- {
- Type: "Healthy",
- Status: api.ConditionFalse,
- Message: "test message",
- Error: "test error",
- },
- },
- },
- // Columns: Name, Status, Message, Error
- expected: []metav1.TableRow{{Cells: []interface{}{"cs3", "Unhealthy", "test message", "test error"}}},
- },
- }
- for i, test := range tests {
- rows, err := printComponentStatus(&test.componentStatus, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintCronJob(t *testing.T) {
- completions := int32(2)
- suspend := false
- tests := []struct {
- cronjob batch.CronJob
- options printers.GenerateOptions
- expected []metav1.TableRow
- }{
- // Basic cron job; does not print containers, images, or labels.
- {
- cronjob: batch.CronJob{
- ObjectMeta: metav1.ObjectMeta{
- Name: "cronjob1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: batch.CronJobSpec{
- Schedule: "0/5 * * * ?",
- Suspend: &suspend,
- JobTemplate: batch.JobTemplateSpec{
- Spec: batch.JobSpec{
- Completions: &completions,
- Template: api.PodTemplateSpec{
- Spec: api.PodSpec{
- Containers: []api.Container{
- {
- Name: "fake-job-container1",
- Image: "fake-job-image1",
- },
- {
- Name: "fake-job-container2",
- Image: "fake-job-image2",
- },
- },
- },
- },
- Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"a": "b"}},
- },
- },
- },
- Status: batch.CronJobStatus{
- LastScheduleTime: &metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Schedule, Suspend, Active, Last Schedule, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"cronjob1", "0/5 * * * ?", "False", int64(0), "0s", "0s"}}},
- },
- // Generate options: Wide; prints containers, images, and labels.
- {
- cronjob: batch.CronJob{
- ObjectMeta: metav1.ObjectMeta{
- Name: "cronjob1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: batch.CronJobSpec{
- Schedule: "0/5 * * * ?",
- Suspend: &suspend,
- JobTemplate: batch.JobTemplateSpec{
- Spec: batch.JobSpec{
- Completions: &completions,
- Template: api.PodTemplateSpec{
- Spec: api.PodSpec{
- Containers: []api.Container{
- {
- Name: "fake-job-container1",
- Image: "fake-job-image1",
- },
- {
- Name: "fake-job-container2",
- Image: "fake-job-image2",
- },
- },
- },
- },
- Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"a": "b"}},
- },
- },
- },
- Status: batch.CronJobStatus{
- LastScheduleTime: &metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- },
- options: printers.GenerateOptions{Wide: true},
- // Columns: Name, Schedule, Suspend, Active, Last Schedule, Age
- 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"}}},
- },
- // CronJob with Last Schedule and Age
- {
- cronjob: batch.CronJob{
- ObjectMeta: metav1.ObjectMeta{
- Name: "cronjob2",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
- },
- Spec: batch.CronJobSpec{
- Schedule: "0/5 * * * ?",
- Suspend: &suspend,
- },
- Status: batch.CronJobStatus{
- LastScheduleTime: &metav1.Time{Time: time.Now().Add(-3e10)},
- },
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Schedule, Suspend, Active, Last Schedule, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"cronjob2", "0/5 * * * ?", "False", int64(0), "30s", "5m"}}},
- },
- // CronJob without Last Schedule
- {
- cronjob: batch.CronJob{
- ObjectMeta: metav1.ObjectMeta{
- Name: "cronjob3",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
- },
- Spec: batch.CronJobSpec{
- Schedule: "0/5 * * * ?",
- Suspend: &suspend,
- },
- Status: batch.CronJobStatus{},
- },
- options: printers.GenerateOptions{},
- // Columns: Name, Schedule, Suspend, Active, Last Schedule, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"cronjob3", "0/5 * * * ?", "False", int64(0), "<none>", "5m"}}},
- },
- }
- for i, test := range tests {
- rows, err := printCronJob(&test.cronjob, test.options)
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintCronJobList(t *testing.T) {
- completions := int32(2)
- suspend := false
- cronJobList := batch.CronJobList{
- Items: []batch.CronJob{
- {
- ObjectMeta: metav1.ObjectMeta{
- Name: "cronjob1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: batch.CronJobSpec{
- Schedule: "0/5 * * * ?",
- Suspend: &suspend,
- JobTemplate: batch.JobTemplateSpec{
- Spec: batch.JobSpec{
- Completions: &completions,
- Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"a": "b"}},
- },
- },
- },
- Status: batch.CronJobStatus{
- LastScheduleTime: &metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- },
- {
- ObjectMeta: metav1.ObjectMeta{
- Name: "cronjob2",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: batch.CronJobSpec{
- Schedule: "4/5 1 1 1 ?",
- Suspend: &suspend,
- JobTemplate: batch.JobTemplateSpec{
- Spec: batch.JobSpec{
- Completions: &completions,
- Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"a": "b"}},
- },
- },
- },
- Status: batch.CronJobStatus{
- LastScheduleTime: &metav1.Time{Time: time.Now().Add(-20 * time.Minute)},
- },
- },
- },
- }
- // Columns: Name, Schedule, Suspend, Active, Last Schedule, Age
- expectedRows := []metav1.TableRow{
- {Cells: []interface{}{"cronjob1", "0/5 * * * ?", "False", int64(0), "0s", "0s"}},
- {Cells: []interface{}{"cronjob2", "4/5 1 1 1 ?", "False", int64(0), "20m", "0s"}},
- }
- rows, err := printCronJobList(&cronJobList, printers.GenerateOptions{})
- if err != nil {
- t.Fatalf("Error printing job list: %#v", err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(expectedRows, rows) {
- t.Errorf("mismatch: %s", diff.ObjectReflectDiff(expectedRows, rows))
- }
- }
- func TestPrintStorageClass(t *testing.T) {
- policyDelte := api.PersistentVolumeReclaimDelete
- policyRetain := api.PersistentVolumeReclaimRetain
- bindModeImmediate := storage.VolumeBindingImmediate
- bindModeWait := storage.VolumeBindingWaitForFirstConsumer
- tests := []struct {
- sc storage.StorageClass
- expected []metav1.TableRow
- }{
- {
- sc: storage.StorageClass{
- ObjectMeta: metav1.ObjectMeta{
- Name: "sc1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Provisioner: "kubernetes.io/glusterfs",
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"sc1", "kubernetes.io/glusterfs", "Delete",
- "Immediate", false, "0s"}}},
- },
- {
- sc: storage.StorageClass{
- ObjectMeta: metav1.ObjectMeta{
- Name: "sc2",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
- },
- Provisioner: "kubernetes.io/nfs",
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"sc2", "kubernetes.io/nfs", "Delete",
- "Immediate", false, "5m"}}},
- },
- {
- sc: storage.StorageClass{
- ObjectMeta: metav1.ObjectMeta{
- Name: "sc3",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
- },
- Provisioner: "kubernetes.io/nfs",
- ReclaimPolicy: &policyDelte,
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"sc3", "kubernetes.io/nfs", "Delete",
- "Immediate", false, "5m"}}},
- },
- {
- sc: storage.StorageClass{
- ObjectMeta: metav1.ObjectMeta{
- Name: "sc4",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
- },
- Provisioner: "kubernetes.io/nfs",
- ReclaimPolicy: &policyRetain,
- VolumeBindingMode: &bindModeImmediate,
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"sc4", "kubernetes.io/nfs", "Retain",
- "Immediate", false, "5m"}}},
- },
- {
- sc: storage.StorageClass{
- ObjectMeta: metav1.ObjectMeta{
- Name: "sc5",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
- },
- Provisioner: "kubernetes.io/nfs",
- ReclaimPolicy: &policyRetain,
- VolumeBindingMode: &bindModeWait,
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"sc5", "kubernetes.io/nfs", "Retain",
- "WaitForFirstConsumer", false, "5m"}}},
- },
- {
- sc: storage.StorageClass{
- ObjectMeta: metav1.ObjectMeta{
- Name: "sc6",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
- },
- Provisioner: "kubernetes.io/nfs",
- ReclaimPolicy: &policyRetain,
- AllowVolumeExpansion: boolP(true),
- VolumeBindingMode: &bindModeWait,
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"sc6", "kubernetes.io/nfs", "Retain",
- "WaitForFirstConsumer", true, "5m"}}},
- },
- }
- for i, test := range tests {
- rows, err := printStorageClass(&test.sc, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintLease(t *testing.T) {
- holder1 := "holder1"
- holder2 := "holder2"
- tests := []struct {
- lease coordination.Lease
- expected []metav1.TableRow
- }{
- {
- lease: coordination.Lease{
- ObjectMeta: metav1.ObjectMeta{
- Name: "lease1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: coordination.LeaseSpec{
- HolderIdentity: &holder1,
- },
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"lease1", "holder1", "0s"}}},
- },
- {
- lease: coordination.Lease{
- ObjectMeta: metav1.ObjectMeta{
- Name: "lease2",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
- },
- Spec: coordination.LeaseSpec{
- HolderIdentity: &holder2,
- },
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"lease2", "holder2", "5m"}}},
- },
- }
- for i, test := range tests {
- rows, err := printLease(&test.lease, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintPriorityClass(t *testing.T) {
- tests := []struct {
- pc scheduling.PriorityClass
- expected []metav1.TableRow
- }{
- {
- pc: scheduling.PriorityClass{
- ObjectMeta: metav1.ObjectMeta{
- Name: "pc1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Value: 1,
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"pc1", int64(1), bool(false), "0s"}}},
- },
- {
- pc: scheduling.PriorityClass{
- ObjectMeta: metav1.ObjectMeta{
- Name: "pc2",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
- },
- Value: 1000000000,
- GlobalDefault: true,
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"pc2", int64(1000000000), bool(true), "5m"}}},
- },
- }
- for i, test := range tests {
- rows, err := printPriorityClass(&test.pc, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintRuntimeClass(t *testing.T) {
- tests := []struct {
- rc nodeapi.RuntimeClass
- expected []metav1.TableRow
- }{
- {
- rc: nodeapi.RuntimeClass{
- ObjectMeta: metav1.ObjectMeta{
- Name: "rc1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Handler: "h1",
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"rc1", "h1", "0s"}}},
- },
- {
- rc: nodeapi.RuntimeClass{
- ObjectMeta: metav1.ObjectMeta{
- Name: "rc2",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
- },
- Handler: "h2",
- },
- expected: []metav1.TableRow{{Cells: []interface{}{"rc2", "h2", "5m"}}},
- },
- }
- for i, test := range tests {
- rows, err := printRuntimeClass(&test.rc, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintEndpoint(t *testing.T) {
- tests := []struct {
- endpoint api.Endpoints
- expected []metav1.TableRow
- }{
- // Basic endpoint with no IP's
- {
- endpoint: api.Endpoints{
- ObjectMeta: metav1.ObjectMeta{
- Name: "endpoint1",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- },
- // Columns: Name, Endpoints, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"endpoint1", "<none>", "0s"}}},
- },
- // Endpoint with no ports
- {
- endpoint: api.Endpoints{
- ObjectMeta: metav1.ObjectMeta{
- Name: "endpoint3",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
- },
- Subsets: []api.EndpointSubset{
- {
- Addresses: []api.EndpointAddress{
- {
- IP: "1.2.3.4",
- },
- {
- IP: "5.6.7.8",
- },
- },
- },
- },
- },
- // Columns: Name, Endpoints, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"endpoint3", "1.2.3.4,5.6.7.8", "5m"}}},
- },
- // Basic endpoint with two IP's and one port
- {
- endpoint: api.Endpoints{
- ObjectMeta: metav1.ObjectMeta{
- Name: "endpoint2",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Subsets: []api.EndpointSubset{
- {
- Addresses: []api.EndpointAddress{
- {
- IP: "1.2.3.4",
- },
- {
- IP: "5.6.7.8",
- },
- },
- Ports: []api.EndpointPort{
- {
- Port: 8001,
- Protocol: "tcp",
- },
- },
- },
- },
- },
- // Columns: Name, Endpoints, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"endpoint2", "1.2.3.4:8001,5.6.7.8:8001", "0s"}}},
- },
- // Basic endpoint with greater than three IP's triggering "more" string
- {
- endpoint: api.Endpoints{
- ObjectMeta: metav1.ObjectMeta{
- Name: "endpoint2",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Subsets: []api.EndpointSubset{
- {
- Addresses: []api.EndpointAddress{
- {
- IP: "1.2.3.4",
- },
- {
- IP: "5.6.7.8",
- },
- {
- IP: "9.8.7.6",
- },
- {
- IP: "6.6.6.6",
- },
- },
- Ports: []api.EndpointPort{
- {
- Port: 8001,
- Protocol: "tcp",
- },
- },
- },
- },
- },
- // Columns: Name, Endpoints, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"endpoint2", "1.2.3.4:8001,5.6.7.8:8001,9.8.7.6:8001 + 1 more...", "0s"}}},
- },
- }
- for i, test := range tests {
- rows, err := printEndpoints(&test.endpoint, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintEndpointSlice(t *testing.T) {
- tcpProtocol := api.ProtocolTCP
- tests := []struct {
- endpointSlice discovery.EndpointSlice
- expected []metav1.TableRow
- }{
- {
- endpointSlice: discovery.EndpointSlice{
- ObjectMeta: metav1.ObjectMeta{
- Name: "abcslice.123",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- AddressType: discovery.AddressTypeIPv4,
- Ports: []discovery.EndpointPort{{
- Name: utilpointer.StringPtr("http"),
- Port: utilpointer.Int32Ptr(80),
- Protocol: &tcpProtocol,
- }},
- Endpoints: []discovery.Endpoint{{
- Addresses: []string{"10.1.2.3", "2001:db8::1234:5678"},
- }},
- },
- // Columns: Name, AddressType, Ports, Endpoints, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"abcslice.123", "IPv4", "80", "10.1.2.3,2001:db8::1234:5678", "0s"}}},
- }, {
- endpointSlice: discovery.EndpointSlice{
- ObjectMeta: metav1.ObjectMeta{
- Name: "longerslicename.123",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
- },
- AddressType: discovery.AddressTypeIPv6,
- Ports: []discovery.EndpointPort{{
- Name: utilpointer.StringPtr("http"),
- Port: utilpointer.Int32Ptr(80),
- Protocol: &tcpProtocol,
- }, {
- Name: utilpointer.StringPtr("https"),
- Port: utilpointer.Int32Ptr(443),
- Protocol: &tcpProtocol,
- }},
- Endpoints: []discovery.Endpoint{{
- Addresses: []string{"10.1.2.3", "2001:db8::1234:5678"},
- }, {
- Addresses: []string{"10.2.3.4", "2001:db8::2345:6789"},
- }},
- },
- // Columns: Name, AddressType, Ports, Endpoints, Age
- 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"}}},
- }, {
- endpointSlice: discovery.EndpointSlice{
- ObjectMeta: metav1.ObjectMeta{
- Name: "multiportslice.123",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
- },
- AddressType: discovery.AddressTypeIPv4,
- Ports: []discovery.EndpointPort{{
- Name: utilpointer.StringPtr("http"),
- Port: utilpointer.Int32Ptr(80),
- Protocol: &tcpProtocol,
- }, {
- Name: utilpointer.StringPtr("https"),
- Port: utilpointer.Int32Ptr(443),
- Protocol: &tcpProtocol,
- }, {
- Name: utilpointer.StringPtr("extra1"),
- Port: utilpointer.Int32Ptr(3000),
- Protocol: &tcpProtocol,
- }, {
- Name: utilpointer.StringPtr("extra2"),
- Port: utilpointer.Int32Ptr(3001),
- Protocol: &tcpProtocol,
- }},
- Endpoints: []discovery.Endpoint{{
- Addresses: []string{"10.1.2.3", "2001:db8::1234:5678"},
- }, {
- Addresses: []string{"10.2.3.4", "2001:db8::2345:6789"},
- }},
- },
- // Columns: Name, AddressType, Ports, Endpoints, Age
- 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"}}},
- },
- }
- for i, test := range tests {
- rows, err := printEndpointSlice(&test.endpointSlice, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintFlowSchema(t *testing.T) {
- all := []string{"*"}
- tests := []struct {
- fs flowcontrol.FlowSchema
- expected []metav1.TableRow
- }{
- {
- fs: flowcontrol.FlowSchema{
- ObjectMeta: metav1.ObjectMeta{
- Name: "all-matcher",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: flowcontrol.FlowSchemaSpec{
- PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{Name: "allee"},
- MatchingPrecedence: math.MaxInt32,
- DistinguisherMethod: &flowcontrol.FlowDistinguisherMethod{Type: flowcontrol.FlowDistinguisherMethodByUserType},
- Rules: []flowcontrol.PolicyRulesWithSubjects{{
- Subjects: []flowcontrol.Subject{{
- Kind: flowcontrol.SubjectKindGroup,
- Group: &flowcontrol.GroupSubject{Name: "system:authenticated"},
- }},
- ResourceRules: []flowcontrol.ResourcePolicyRule{{
- Verbs: all,
- APIGroups: all,
- Resources: all,
- ClusterScope: true,
- Namespaces: all,
- }},
- }, {
- Subjects: []flowcontrol.Subject{{
- Kind: flowcontrol.SubjectKindGroup,
- Group: &flowcontrol.GroupSubject{Name: "system:unauthenticated"},
- }},
- ResourceRules: []flowcontrol.ResourcePolicyRule{{
- Verbs: all,
- APIGroups: all,
- Resources: all,
- ClusterScope: true,
- Namespaces: all,
- }},
- }, {
- Subjects: []flowcontrol.Subject{{
- Kind: flowcontrol.SubjectKindGroup,
- Group: &flowcontrol.GroupSubject{Name: "system:authenticated"},
- }, {
- Kind: flowcontrol.SubjectKindGroup,
- Group: &flowcontrol.GroupSubject{Name: "system:unauthenticated"},
- }},
- NonResourceRules: []flowcontrol.NonResourcePolicyRule{{
- Verbs: all,
- NonResourceURLs: all,
- }},
- }},
- },
- },
- // Columns: Name, PriorityLevelName, MatchingPrecedence, DistinguisherMethod, Age, MissingPL
- expected: []metav1.TableRow{{Cells: []interface{}{"all-matcher", "allee", int32(math.MaxInt32), "ByUser", "0s", "?"}}},
- }, {
- fs: flowcontrol.FlowSchema{
- ObjectMeta: metav1.ObjectMeta{
- Name: "some-matcher",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
- },
- Spec: flowcontrol.FlowSchemaSpec{
- PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{Name: "allee"},
- MatchingPrecedence: 0,
- DistinguisherMethod: &flowcontrol.FlowDistinguisherMethod{Type: flowcontrol.FlowDistinguisherMethodByNamespaceType},
- Rules: []flowcontrol.PolicyRulesWithSubjects{{
- Subjects: []flowcontrol.Subject{{
- Kind: flowcontrol.SubjectKindGroup,
- Group: &flowcontrol.GroupSubject{Name: "system:unauthenticated"},
- }},
- ResourceRules: []flowcontrol.ResourcePolicyRule{{
- Verbs: all,
- APIGroups: all,
- Resources: all,
- ClusterScope: true,
- Namespaces: all,
- }},
- }, {
- Subjects: []flowcontrol.Subject{{
- Kind: flowcontrol.SubjectKindGroup,
- Group: &flowcontrol.GroupSubject{Name: "system:authenticated"},
- }, {
- Kind: flowcontrol.SubjectKindGroup,
- Group: &flowcontrol.GroupSubject{Name: "system:unauthenticated"},
- }},
- NonResourceRules: []flowcontrol.NonResourcePolicyRule{{
- Verbs: all,
- NonResourceURLs: all,
- }},
- }},
- },
- Status: flowcontrol.FlowSchemaStatus{
- Conditions: []flowcontrol.FlowSchemaCondition{{
- Type: flowcontrol.FlowSchemaConditionDangling,
- Status: "True",
- LastTransitionTime: metav1.Time{Time: time.Now().Add(-time.Hour)},
- }},
- },
- },
- // Columns: Name, PriorityLevelName, MatchingPrecedence, DistinguisherMethod, Age, MissingPL
- expected: []metav1.TableRow{{Cells: []interface{}{"some-matcher", "allee", int32(0), "ByNamespace", "5m", "True"}}},
- }, {
- fs: flowcontrol.FlowSchema{
- ObjectMeta: metav1.ObjectMeta{
- Name: "exempt",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
- },
- Spec: flowcontrol.FlowSchemaSpec{
- PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{Name: "allee"},
- MatchingPrecedence: 0,
- DistinguisherMethod: nil,
- Rules: []flowcontrol.PolicyRulesWithSubjects{{
- Subjects: []flowcontrol.Subject{{
- Kind: flowcontrol.SubjectKindGroup,
- Group: &flowcontrol.GroupSubject{Name: "system:masters"},
- }},
- ResourceRules: []flowcontrol.ResourcePolicyRule{{
- Verbs: all,
- APIGroups: all,
- Resources: all,
- ClusterScope: true,
- Namespaces: all,
- }},
- }},
- },
- },
- // Columns: Name, PriorityLevelName, MatchingPrecedence, DistinguisherMethod, Age, MissingPL
- expected: []metav1.TableRow{{Cells: []interface{}{"exempt", "allee", int32(0), "<none>", "5m", "?"}}},
- },
- }
- for i, test := range tests {
- rows, err := printFlowSchema(&test.fs, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
- func TestPrintPriorityLevelConfiguration(t *testing.T) {
- tests := []struct {
- pl flowcontrol.PriorityLevelConfiguration
- expected []metav1.TableRow
- }{
- {
- pl: flowcontrol.PriorityLevelConfiguration{
- ObjectMeta: metav1.ObjectMeta{
- Name: "unlimited",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: flowcontrol.PriorityLevelConfigurationSpec{
- Type: flowcontrol.PriorityLevelEnablementExempt,
- },
- },
- // Columns: Name, Type, AssuredConcurrencyShares, Queues, HandSize, QueueLengthLimit, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"unlimited", "Exempt", "<none>", "<none>", "<none>", "<none>", "0s"}}},
- },
- {
- pl: flowcontrol.PriorityLevelConfiguration{
- ObjectMeta: metav1.ObjectMeta{
- Name: "unqueued",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: flowcontrol.PriorityLevelConfigurationSpec{
- Type: flowcontrol.PriorityLevelEnablementLimited,
- Limited: &flowcontrol.LimitedPriorityLevelConfiguration{
- AssuredConcurrencyShares: 47,
- LimitResponse: flowcontrol.LimitResponse{
- Type: flowcontrol.LimitResponseTypeReject,
- },
- },
- },
- },
- // Columns: Name, Type, AssuredConcurrencyShares, Queues, HandSize, QueueLengthLimit, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"unqueued", "Limited", int32(47), "<none>", "<none>", "<none>", "0s"}}},
- },
- {
- pl: flowcontrol.PriorityLevelConfiguration{
- ObjectMeta: metav1.ObjectMeta{
- Name: "queued",
- CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
- },
- Spec: flowcontrol.PriorityLevelConfigurationSpec{
- Type: flowcontrol.PriorityLevelEnablementLimited,
- Limited: &flowcontrol.LimitedPriorityLevelConfiguration{
- AssuredConcurrencyShares: 42,
- LimitResponse: flowcontrol.LimitResponse{
- Type: flowcontrol.LimitResponseTypeQueue,
- Queuing: &flowcontrol.QueuingConfiguration{
- Queues: 8,
- HandSize: 3,
- QueueLengthLimit: 4,
- },
- },
- },
- },
- },
- // Columns: Name, Type, AssuredConcurrencyShares, Queues, HandSize, QueueLengthLimit, Age
- expected: []metav1.TableRow{{Cells: []interface{}{"queued", "Limited", int32(42), int32(8), int32(3), int32(4), "0s"}}},
- },
- }
- for i, test := range tests {
- rows, err := printPriorityLevelConfiguration(&test.pl, printers.GenerateOptions{})
- if err != nil {
- t.Fatal(err)
- }
- for i := range rows {
- rows[i].Object.Object = nil
- }
- if !reflect.DeepEqual(test.expected, rows) {
- t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expected, rows))
- }
- }
- }
|