resource_printer_test.go 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package kubectl
  14. import (
  15. "bytes"
  16. "encoding/json"
  17. "fmt"
  18. "io"
  19. "reflect"
  20. "strings"
  21. "testing"
  22. "time"
  23. "k8s.io/kubernetes/pkg/api"
  24. "k8s.io/kubernetes/pkg/api/testapi"
  25. "k8s.io/kubernetes/pkg/api/unversioned"
  26. "k8s.io/kubernetes/pkg/api/v1"
  27. "k8s.io/kubernetes/pkg/apis/batch"
  28. "k8s.io/kubernetes/pkg/apis/extensions"
  29. kubectltesting "k8s.io/kubernetes/pkg/kubectl/testing"
  30. "k8s.io/kubernetes/pkg/runtime"
  31. yamlserializer "k8s.io/kubernetes/pkg/runtime/serializer/yaml"
  32. "k8s.io/kubernetes/pkg/util/diff"
  33. "k8s.io/kubernetes/pkg/util/intstr"
  34. "k8s.io/kubernetes/pkg/util/sets"
  35. "github.com/ghodss/yaml"
  36. )
  37. func init() {
  38. api.Scheme.AddKnownTypes(testapi.Default.InternalGroupVersion(), &kubectltesting.TestStruct{})
  39. api.Scheme.AddKnownTypes(*testapi.Default.GroupVersion(), &kubectltesting.TestStruct{})
  40. }
  41. var testData = kubectltesting.TestStruct{
  42. Key: "testValue",
  43. Map: map[string]int{"TestSubkey": 1},
  44. StringList: []string{"a", "b", "c"},
  45. IntList: []int{1, 2, 3},
  46. }
  47. func TestVersionedPrinter(t *testing.T) {
  48. original := &kubectltesting.TestStruct{Key: "value"}
  49. p := NewVersionedPrinter(
  50. ResourcePrinterFunc(func(obj runtime.Object, w io.Writer) error {
  51. if obj == original {
  52. t.Fatalf("object should not be identical: %#v", obj)
  53. }
  54. if obj.(*kubectltesting.TestStruct).Key != "value" {
  55. t.Fatalf("object was not converted: %#v", obj)
  56. }
  57. return nil
  58. }),
  59. api.Scheme,
  60. *testapi.Default.GroupVersion(),
  61. )
  62. if err := p.PrintObj(original, nil); err != nil {
  63. t.Errorf("unexpected error: %v", err)
  64. }
  65. }
  66. func TestPrintDefault(t *testing.T) {
  67. printer, found, err := GetPrinter("", "", false)
  68. if err != nil {
  69. t.Fatalf("unexpected error: %#v", err)
  70. }
  71. if found {
  72. t.Errorf("no printer should have been found: %#v / %v", printer, err)
  73. }
  74. }
  75. type TestPrintType struct {
  76. Data string
  77. }
  78. func (obj *TestPrintType) GetObjectKind() unversioned.ObjectKind { return unversioned.EmptyObjectKind }
  79. type TestUnknownType struct{}
  80. func (obj *TestUnknownType) GetObjectKind() unversioned.ObjectKind { return unversioned.EmptyObjectKind }
  81. func TestPrinter(t *testing.T) {
  82. //test inputs
  83. simpleTest := &TestPrintType{"foo"}
  84. podTest := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
  85. podListTest := &api.PodList{
  86. Items: []api.Pod{
  87. {ObjectMeta: api.ObjectMeta{Name: "foo"}},
  88. {ObjectMeta: api.ObjectMeta{Name: "bar"}},
  89. },
  90. }
  91. emptyListTest := &api.PodList{}
  92. testapi, err := api.Scheme.ConvertToVersion(podTest, *testapi.Default.GroupVersion())
  93. if err != nil {
  94. t.Fatalf("unexpected error: %v", err)
  95. }
  96. printerTests := []struct {
  97. Name string
  98. Format string
  99. FormatArgument string
  100. Input runtime.Object
  101. Expect string
  102. }{
  103. {"test json", "json", "", simpleTest, "{\n \"Data\": \"foo\"\n}\n"},
  104. {"test yaml", "yaml", "", simpleTest, "Data: foo\n"},
  105. {"test template", "template", "{{if .id}}{{.id}}{{end}}{{if .metadata.name}}{{.metadata.name}}{{end}}",
  106. podTest, "foo"},
  107. {"test jsonpath", "jsonpath", "{.metadata.name}", podTest, "foo"},
  108. {"test jsonpath list", "jsonpath", "{.items[*].metadata.name}", podListTest, "foo bar"},
  109. {"test jsonpath empty list", "jsonpath", "{.items[*].metadata.name}", emptyListTest, ""},
  110. {"test name", "name", "", podTest, "pod/foo\n"},
  111. {"emits versioned objects", "template", "{{.kind}}", testapi, "Pod"},
  112. }
  113. for _, test := range printerTests {
  114. buf := bytes.NewBuffer([]byte{})
  115. printer, found, err := GetPrinter(test.Format, test.FormatArgument, false)
  116. if err != nil || !found {
  117. t.Errorf("in %s, unexpected error: %#v", test.Name, err)
  118. }
  119. if err := printer.PrintObj(test.Input, buf); err != nil {
  120. t.Errorf("in %s, unexpected error: %#v", test.Name, err)
  121. }
  122. if buf.String() != test.Expect {
  123. t.Errorf("in %s, expect %q, got %q", test.Name, test.Expect, buf.String())
  124. }
  125. }
  126. }
  127. func TestBadPrinter(t *testing.T) {
  128. badPrinterTests := []struct {
  129. Name string
  130. Format string
  131. FormatArgument string
  132. Error error
  133. }{
  134. {"empty template", "template", "", fmt.Errorf("template format specified but no template given")},
  135. {"bad template", "template", "{{ .Name", fmt.Errorf("error parsing template {{ .Name, template: output:1: unclosed action\n")},
  136. {"bad templatefile", "templatefile", "", fmt.Errorf("templatefile format specified but no template file given")},
  137. {"bad jsonpath", "jsonpath", "{.Name", fmt.Errorf("error parsing jsonpath {.Name, unclosed action\n")},
  138. }
  139. for _, test := range badPrinterTests {
  140. _, _, err := GetPrinter(test.Format, test.FormatArgument, false)
  141. if err == nil || err.Error() != test.Error.Error() {
  142. t.Errorf("in %s, expect %s, got %s", test.Name, test.Error, err)
  143. }
  144. }
  145. }
  146. func testPrinter(t *testing.T, printer ResourcePrinter, unmarshalFunc func(data []byte, v interface{}) error) {
  147. buf := bytes.NewBuffer([]byte{})
  148. err := printer.PrintObj(&testData, buf)
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. var poutput kubectltesting.TestStruct
  153. // Verify that given function runs without error.
  154. err = unmarshalFunc(buf.Bytes(), &poutput)
  155. if err != nil {
  156. t.Fatal(err)
  157. }
  158. // Use real decode function to undo the versioning process.
  159. poutput = kubectltesting.TestStruct{}
  160. s := yamlserializer.NewDecodingSerializer(testapi.Default.Codec())
  161. if err := runtime.DecodeInto(s, buf.Bytes(), &poutput); err != nil {
  162. t.Fatal(err)
  163. }
  164. if !reflect.DeepEqual(testData, poutput) {
  165. t.Errorf("Test data and unmarshaled data are not equal: %v", diff.ObjectDiff(poutput, testData))
  166. }
  167. obj := &api.Pod{
  168. ObjectMeta: api.ObjectMeta{Name: "foo"},
  169. }
  170. buf.Reset()
  171. printer.PrintObj(obj, buf)
  172. var objOut api.Pod
  173. // Verify that given function runs without error.
  174. err = unmarshalFunc(buf.Bytes(), &objOut)
  175. if err != nil {
  176. t.Fatalf("unexpected error: %#v", err)
  177. }
  178. // Use real decode function to undo the versioning process.
  179. objOut = api.Pod{}
  180. if err := runtime.DecodeInto(s, buf.Bytes(), &objOut); err != nil {
  181. t.Fatal(err)
  182. }
  183. if !reflect.DeepEqual(obj, &objOut) {
  184. t.Errorf("Unexpected inequality:\n%v", diff.ObjectDiff(obj, &objOut))
  185. }
  186. }
  187. func TestYAMLPrinter(t *testing.T) {
  188. testPrinter(t, &YAMLPrinter{}, yaml.Unmarshal)
  189. }
  190. func TestJSONPrinter(t *testing.T) {
  191. testPrinter(t, &JSONPrinter{}, json.Unmarshal)
  192. }
  193. func TestFormatResourceName(t *testing.T) {
  194. tests := []struct {
  195. kind, name string
  196. want string
  197. }{
  198. {"", "", ""},
  199. {"", "name", "name"},
  200. {"kind", "", "kind/"}, // should not happen in practice
  201. {"kind", "name", "kind/name"},
  202. }
  203. for _, tt := range tests {
  204. if got := formatResourceName(tt.kind, tt.name, true); got != tt.want {
  205. t.Errorf("formatResourceName(%q, %q) = %q, want %q", tt.kind, tt.name, got, tt.want)
  206. }
  207. }
  208. }
  209. func PrintCustomType(obj *TestPrintType, w io.Writer, options PrintOptions) error {
  210. data := obj.Data
  211. kind := options.Kind
  212. if options.WithKind {
  213. data = kind + "/" + data
  214. }
  215. _, err := fmt.Fprintf(w, "%s", data)
  216. return err
  217. }
  218. func ErrorPrintHandler(obj *TestPrintType, w io.Writer, options PrintOptions) error {
  219. return fmt.Errorf("ErrorPrintHandler error")
  220. }
  221. func TestCustomTypePrinting(t *testing.T) {
  222. columns := []string{"Data"}
  223. printer := NewHumanReadablePrinter(PrintOptions{
  224. ColumnLabels: []string{},
  225. })
  226. printer.Handler(columns, PrintCustomType)
  227. obj := TestPrintType{"test object"}
  228. buffer := &bytes.Buffer{}
  229. err := printer.PrintObj(&obj, buffer)
  230. if err != nil {
  231. t.Fatalf("An error occurred printing the custom type: %#v", err)
  232. }
  233. expectedOutput := "Data\ntest object"
  234. if buffer.String() != expectedOutput {
  235. t.Errorf("The data was not printed as expected. Expected:\n%s\nGot:\n%s", expectedOutput, buffer.String())
  236. }
  237. }
  238. func TestCustomTypePrintingWithKind(t *testing.T) {
  239. columns := []string{"Data"}
  240. printer := NewHumanReadablePrinter(PrintOptions{
  241. ColumnLabels: []string{},
  242. })
  243. printer.Handler(columns, PrintCustomType)
  244. printer.EnsurePrintWithKind("test")
  245. obj := TestPrintType{"test object"}
  246. buffer := &bytes.Buffer{}
  247. err := printer.PrintObj(&obj, buffer)
  248. if err != nil {
  249. t.Fatalf("An error occurred printing the custom type: %#v", err)
  250. }
  251. expectedOutput := "Data\ntest/test object"
  252. if buffer.String() != expectedOutput {
  253. t.Errorf("The data was not printed as expected. Expected:\n%s\nGot:\n%s", expectedOutput, buffer.String())
  254. }
  255. }
  256. func TestPrintHandlerError(t *testing.T) {
  257. columns := []string{"Data"}
  258. printer := NewHumanReadablePrinter(PrintOptions{
  259. ColumnLabels: []string{},
  260. })
  261. printer.Handler(columns, ErrorPrintHandler)
  262. obj := TestPrintType{"test object"}
  263. buffer := &bytes.Buffer{}
  264. err := printer.PrintObj(&obj, buffer)
  265. if err == nil || err.Error() != "ErrorPrintHandler error" {
  266. t.Errorf("Did not get the expected error: %#v", err)
  267. }
  268. }
  269. func TestUnknownTypePrinting(t *testing.T) {
  270. printer := NewHumanReadablePrinter(PrintOptions{
  271. ColumnLabels: []string{},
  272. })
  273. buffer := &bytes.Buffer{}
  274. err := printer.PrintObj(&TestUnknownType{}, buffer)
  275. if err == nil {
  276. t.Errorf("An error was expected from printing unknown type")
  277. }
  278. }
  279. func TestTemplatePanic(t *testing.T) {
  280. tmpl := `{{and ((index .currentState.info "foo").state.running.startedAt) .currentState.info.net.state.running.startedAt}}`
  281. printer, err := NewTemplatePrinter([]byte(tmpl))
  282. if err != nil {
  283. t.Fatalf("tmpl fail: %v", err)
  284. }
  285. buffer := &bytes.Buffer{}
  286. err = printer.PrintObj(&api.Pod{}, buffer)
  287. if err == nil {
  288. t.Fatalf("expected that template to crash")
  289. }
  290. if buffer.String() == "" {
  291. t.Errorf("no debugging info was printed")
  292. }
  293. }
  294. func TestNamePrinter(t *testing.T) {
  295. tests := map[string]struct {
  296. obj runtime.Object
  297. expect string
  298. }{
  299. "singleObject": {
  300. &api.Pod{
  301. TypeMeta: unversioned.TypeMeta{
  302. Kind: "Pod",
  303. },
  304. ObjectMeta: api.ObjectMeta{
  305. Name: "foo",
  306. },
  307. },
  308. "pod/foo\n"},
  309. "List": {
  310. &v1.List{
  311. TypeMeta: unversioned.TypeMeta{
  312. Kind: "List",
  313. },
  314. Items: []runtime.RawExtension{
  315. {
  316. Raw: []byte(`{"kind": "Pod", "apiVersion": "v1", "metadata": { "name": "foo"}}`),
  317. },
  318. {
  319. Raw: []byte(`{"kind": "Pod", "apiVersion": "v1", "metadata": { "name": "bar"}}`),
  320. },
  321. },
  322. },
  323. "pod/foo\npod/bar\n"},
  324. }
  325. printer, _, _ := GetPrinter("name", "", false)
  326. for name, item := range tests {
  327. buff := &bytes.Buffer{}
  328. err := printer.PrintObj(item.obj, buff)
  329. if err != nil {
  330. t.Errorf("%v: unexpected err: %v", name, err)
  331. continue
  332. }
  333. got := buff.String()
  334. if item.expect != got {
  335. t.Errorf("%v: expected %v, got %v", name, item.expect, got)
  336. }
  337. }
  338. }
  339. func TestTemplateStrings(t *testing.T) {
  340. // This unit tests the "exists" function as well as the template from update.sh
  341. table := map[string]struct {
  342. pod api.Pod
  343. expect string
  344. }{
  345. "nilInfo": {api.Pod{}, "false"},
  346. "emptyInfo": {api.Pod{Status: api.PodStatus{ContainerStatuses: []api.ContainerStatus{}}}, "false"},
  347. "fooExists": {
  348. api.Pod{
  349. Status: api.PodStatus{
  350. ContainerStatuses: []api.ContainerStatus{
  351. {
  352. Name: "foo",
  353. },
  354. },
  355. },
  356. },
  357. "false",
  358. },
  359. "barExists": {
  360. api.Pod{
  361. Status: api.PodStatus{
  362. ContainerStatuses: []api.ContainerStatus{
  363. {
  364. Name: "bar",
  365. },
  366. },
  367. },
  368. },
  369. "false",
  370. },
  371. "bothExist": {
  372. api.Pod{
  373. Status: api.PodStatus{
  374. ContainerStatuses: []api.ContainerStatus{
  375. {
  376. Name: "foo",
  377. },
  378. {
  379. Name: "bar",
  380. },
  381. },
  382. },
  383. },
  384. "false",
  385. },
  386. "barValid": {
  387. api.Pod{
  388. Status: api.PodStatus{
  389. ContainerStatuses: []api.ContainerStatus{
  390. {
  391. Name: "foo",
  392. },
  393. {
  394. Name: "bar",
  395. State: api.ContainerState{
  396. Running: &api.ContainerStateRunning{
  397. StartedAt: unversioned.Time{},
  398. },
  399. },
  400. },
  401. },
  402. },
  403. },
  404. "false",
  405. },
  406. "bothValid": {
  407. api.Pod{
  408. Status: api.PodStatus{
  409. ContainerStatuses: []api.ContainerStatus{
  410. {
  411. Name: "foo",
  412. State: api.ContainerState{
  413. Running: &api.ContainerStateRunning{
  414. StartedAt: unversioned.Time{},
  415. },
  416. },
  417. },
  418. {
  419. Name: "bar",
  420. State: api.ContainerState{
  421. Running: &api.ContainerStateRunning{
  422. StartedAt: unversioned.Time{},
  423. },
  424. },
  425. },
  426. },
  427. },
  428. },
  429. "true",
  430. },
  431. }
  432. // The point of this test is to verify that the below template works.
  433. tmpl := `{{if (exists . "status" "containerStatuses")}}{{range .status.containerStatuses}}{{if (and (eq .name "foo") (exists . "state" "running"))}}true{{end}}{{end}}{{end}}`
  434. p, err := NewTemplatePrinter([]byte(tmpl))
  435. if err != nil {
  436. t.Fatalf("tmpl fail: %v", err)
  437. }
  438. printer := NewVersionedPrinter(p, api.Scheme, *testapi.Default.GroupVersion())
  439. for name, item := range table {
  440. buffer := &bytes.Buffer{}
  441. err = printer.PrintObj(&item.pod, buffer)
  442. if err != nil {
  443. t.Errorf("%v: unexpected err: %v", name, err)
  444. continue
  445. }
  446. actual := buffer.String()
  447. if len(actual) == 0 {
  448. actual = "false"
  449. }
  450. if e := item.expect; e != actual {
  451. t.Errorf("%v: expected %v, got %v", name, e, actual)
  452. }
  453. }
  454. }
  455. func TestPrinters(t *testing.T) {
  456. om := func(name string) api.ObjectMeta { return api.ObjectMeta{Name: name} }
  457. templatePrinter, err := NewTemplatePrinter([]byte("{{.name}}"))
  458. if err != nil {
  459. t.Fatal(err)
  460. }
  461. templatePrinter2, err := NewTemplatePrinter([]byte("{{len .items}}"))
  462. if err != nil {
  463. t.Fatal(err)
  464. }
  465. jsonpathPrinter, err := NewJSONPathPrinter("{.metadata.name}")
  466. if err != nil {
  467. t.Fatal(err)
  468. }
  469. printers := map[string]ResourcePrinter{
  470. "humanReadable": NewHumanReadablePrinter(PrintOptions{
  471. NoHeaders: true,
  472. ColumnLabels: []string{},
  473. }),
  474. "humanReadableHeaders": NewHumanReadablePrinter(PrintOptions{
  475. ColumnLabels: []string{},
  476. }),
  477. "json": &JSONPrinter{},
  478. "yaml": &YAMLPrinter{},
  479. "template": templatePrinter,
  480. "template2": templatePrinter2,
  481. "jsonpath": jsonpathPrinter,
  482. "name": &NamePrinter{
  483. Typer: api.Scheme,
  484. Decoder: api.Codecs.UniversalDecoder(),
  485. },
  486. }
  487. objects := map[string]runtime.Object{
  488. "pod": &api.Pod{ObjectMeta: om("pod")},
  489. "emptyPodList": &api.PodList{},
  490. "nonEmptyPodList": &api.PodList{Items: []api.Pod{{}}},
  491. "endpoints": &api.Endpoints{
  492. Subsets: []api.EndpointSubset{{
  493. Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}, {IP: "localhost"}},
  494. Ports: []api.EndpointPort{{Port: 8080}},
  495. }}},
  496. }
  497. // map of printer name to set of objects it should fail on.
  498. expectedErrors := map[string]sets.String{
  499. "template2": sets.NewString("pod", "emptyPodList", "endpoints"),
  500. "jsonpath": sets.NewString("emptyPodList", "nonEmptyPodList", "endpoints"),
  501. }
  502. for pName, p := range printers {
  503. for oName, obj := range objects {
  504. b := &bytes.Buffer{}
  505. if err := p.PrintObj(obj, b); err != nil {
  506. if set, found := expectedErrors[pName]; found && set.Has(oName) {
  507. // expected error
  508. continue
  509. }
  510. t.Errorf("printer '%v', object '%v'; error: '%v'", pName, oName, err)
  511. }
  512. }
  513. }
  514. }
  515. func TestPrintEventsResultSorted(t *testing.T) {
  516. // Arrange
  517. printer := NewHumanReadablePrinter(PrintOptions{
  518. ColumnLabels: []string{},
  519. })
  520. obj := api.EventList{
  521. Items: []api.Event{
  522. {
  523. Source: api.EventSource{Component: "kubelet"},
  524. Message: "Item 1",
  525. FirstTimestamp: unversioned.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
  526. LastTimestamp: unversioned.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
  527. Count: 1,
  528. Type: api.EventTypeNormal,
  529. },
  530. {
  531. Source: api.EventSource{Component: "scheduler"},
  532. Message: "Item 2",
  533. FirstTimestamp: unversioned.NewTime(time.Date(1987, time.June, 17, 0, 0, 0, 0, time.UTC)),
  534. LastTimestamp: unversioned.NewTime(time.Date(1987, time.June, 17, 0, 0, 0, 0, time.UTC)),
  535. Count: 1,
  536. Type: api.EventTypeNormal,
  537. },
  538. {
  539. Source: api.EventSource{Component: "kubelet"},
  540. Message: "Item 3",
  541. FirstTimestamp: unversioned.NewTime(time.Date(2002, time.December, 25, 0, 0, 0, 0, time.UTC)),
  542. LastTimestamp: unversioned.NewTime(time.Date(2002, time.December, 25, 0, 0, 0, 0, time.UTC)),
  543. Count: 1,
  544. Type: api.EventTypeNormal,
  545. },
  546. },
  547. }
  548. buffer := &bytes.Buffer{}
  549. // Act
  550. err := printer.PrintObj(&obj, buffer)
  551. // Assert
  552. if err != nil {
  553. t.Fatalf("An error occurred printing the EventList: %#v", err)
  554. }
  555. out := buffer.String()
  556. VerifyDatesInOrder(out, "\n" /* rowDelimiter */, " " /* columnDelimiter */, t)
  557. }
  558. func TestPrintNodeStatus(t *testing.T) {
  559. printer := NewHumanReadablePrinter(PrintOptions{
  560. ColumnLabels: []string{},
  561. })
  562. table := []struct {
  563. node api.Node
  564. status string
  565. }{
  566. {
  567. node: api.Node{
  568. ObjectMeta: api.ObjectMeta{Name: "foo1"},
  569. Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: api.NodeReady, Status: api.ConditionTrue}}},
  570. },
  571. status: "Ready",
  572. },
  573. {
  574. node: api.Node{
  575. ObjectMeta: api.ObjectMeta{Name: "foo2"},
  576. Spec: api.NodeSpec{Unschedulable: true},
  577. Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: api.NodeReady, Status: api.ConditionTrue}}},
  578. },
  579. status: "Ready,SchedulingDisabled",
  580. },
  581. {
  582. node: api.Node{
  583. ObjectMeta: api.ObjectMeta{Name: "foo3"},
  584. Status: api.NodeStatus{Conditions: []api.NodeCondition{
  585. {Type: api.NodeReady, Status: api.ConditionTrue},
  586. {Type: api.NodeReady, Status: api.ConditionTrue}}},
  587. },
  588. status: "Ready",
  589. },
  590. {
  591. node: api.Node{
  592. ObjectMeta: api.ObjectMeta{Name: "foo4"},
  593. Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: api.NodeReady, Status: api.ConditionFalse}}},
  594. },
  595. status: "NotReady",
  596. },
  597. {
  598. node: api.Node{
  599. ObjectMeta: api.ObjectMeta{Name: "foo5"},
  600. Spec: api.NodeSpec{Unschedulable: true},
  601. Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: api.NodeReady, Status: api.ConditionFalse}}},
  602. },
  603. status: "NotReady,SchedulingDisabled",
  604. },
  605. {
  606. node: api.Node{
  607. ObjectMeta: api.ObjectMeta{Name: "foo6"},
  608. Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: "InvalidValue", Status: api.ConditionTrue}}},
  609. },
  610. status: "Unknown",
  611. },
  612. {
  613. node: api.Node{
  614. ObjectMeta: api.ObjectMeta{Name: "foo7"},
  615. Status: api.NodeStatus{Conditions: []api.NodeCondition{{}}},
  616. },
  617. status: "Unknown",
  618. },
  619. {
  620. node: api.Node{
  621. ObjectMeta: api.ObjectMeta{Name: "foo8"},
  622. Spec: api.NodeSpec{Unschedulable: true},
  623. Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: "InvalidValue", Status: api.ConditionTrue}}},
  624. },
  625. status: "Unknown,SchedulingDisabled",
  626. },
  627. {
  628. node: api.Node{
  629. ObjectMeta: api.ObjectMeta{Name: "foo9"},
  630. Spec: api.NodeSpec{Unschedulable: true},
  631. Status: api.NodeStatus{Conditions: []api.NodeCondition{{}}},
  632. },
  633. status: "Unknown,SchedulingDisabled",
  634. },
  635. }
  636. for _, test := range table {
  637. buffer := &bytes.Buffer{}
  638. err := printer.PrintObj(&test.node, buffer)
  639. if err != nil {
  640. t.Fatalf("An error occurred printing Node: %#v", err)
  641. }
  642. if !contains(strings.Fields(buffer.String()), test.status) {
  643. t.Fatalf("Expect printing node %s with status %#v, got: %#v", test.node.Name, test.status, buffer.String())
  644. }
  645. }
  646. }
  647. func contains(fields []string, field string) bool {
  648. for _, v := range fields {
  649. if v == field {
  650. return true
  651. }
  652. }
  653. return false
  654. }
  655. func TestPrintHunmanReadableIngressWithColumnLabels(t *testing.T) {
  656. ingress := extensions.Ingress{
  657. ObjectMeta: api.ObjectMeta{
  658. Name: "test1",
  659. CreationTimestamp: unversioned.Time{Time: time.Now().AddDate(-10, 0, 0)},
  660. Labels: map[string]string{
  661. "app_name": "kubectl_test_ingress",
  662. },
  663. },
  664. Spec: extensions.IngressSpec{
  665. Backend: &extensions.IngressBackend{
  666. ServiceName: "svc",
  667. ServicePort: intstr.FromInt(93),
  668. },
  669. },
  670. Status: extensions.IngressStatus{
  671. LoadBalancer: api.LoadBalancerStatus{
  672. Ingress: []api.LoadBalancerIngress{
  673. {
  674. IP: "2.3.4.5",
  675. Hostname: "localhost.localdomain",
  676. },
  677. },
  678. },
  679. },
  680. }
  681. buff := bytes.Buffer{}
  682. printIngress(&ingress, &buff, PrintOptions{
  683. ColumnLabels: []string{"app_name"},
  684. })
  685. output := string(buff.Bytes())
  686. appName := ingress.ObjectMeta.Labels["app_name"]
  687. if !strings.Contains(output, appName) {
  688. t.Errorf("expected to container app_name label value %s, but doesn't %s", appName, output)
  689. }
  690. }
  691. func TestPrintHumanReadableService(t *testing.T) {
  692. tests := []api.Service{
  693. {
  694. Spec: api.ServiceSpec{
  695. ClusterIP: "1.2.3.4",
  696. Type: "LoadBalancer",
  697. Ports: []api.ServicePort{
  698. {
  699. Port: 80,
  700. Protocol: "TCP",
  701. },
  702. },
  703. },
  704. Status: api.ServiceStatus{
  705. LoadBalancer: api.LoadBalancerStatus{
  706. Ingress: []api.LoadBalancerIngress{
  707. {
  708. IP: "2.3.4.5",
  709. },
  710. {
  711. IP: "3.4.5.6",
  712. },
  713. },
  714. },
  715. },
  716. },
  717. {
  718. Spec: api.ServiceSpec{
  719. ClusterIP: "1.3.4.5",
  720. Ports: []api.ServicePort{
  721. {
  722. Port: 80,
  723. Protocol: "TCP",
  724. },
  725. {
  726. Port: 8090,
  727. Protocol: "UDP",
  728. },
  729. {
  730. Port: 8000,
  731. Protocol: "TCP",
  732. },
  733. },
  734. },
  735. },
  736. {
  737. Spec: api.ServiceSpec{
  738. ClusterIP: "1.4.5.6",
  739. Type: "LoadBalancer",
  740. Ports: []api.ServicePort{
  741. {
  742. Port: 80,
  743. Protocol: "TCP",
  744. },
  745. {
  746. Port: 8090,
  747. Protocol: "UDP",
  748. },
  749. {
  750. Port: 8000,
  751. Protocol: "TCP",
  752. },
  753. },
  754. },
  755. Status: api.ServiceStatus{
  756. LoadBalancer: api.LoadBalancerStatus{
  757. Ingress: []api.LoadBalancerIngress{
  758. {
  759. IP: "2.3.4.5",
  760. },
  761. },
  762. },
  763. },
  764. },
  765. {
  766. Spec: api.ServiceSpec{
  767. ClusterIP: "1.5.6.7",
  768. Type: "LoadBalancer",
  769. Ports: []api.ServicePort{
  770. {
  771. Port: 80,
  772. Protocol: "TCP",
  773. },
  774. {
  775. Port: 8090,
  776. Protocol: "UDP",
  777. },
  778. {
  779. Port: 8000,
  780. Protocol: "TCP",
  781. },
  782. },
  783. },
  784. Status: api.ServiceStatus{
  785. LoadBalancer: api.LoadBalancerStatus{
  786. Ingress: []api.LoadBalancerIngress{
  787. {
  788. IP: "2.3.4.5",
  789. },
  790. {
  791. IP: "3.4.5.6",
  792. },
  793. {
  794. IP: "5.6.7.8",
  795. Hostname: "host5678",
  796. },
  797. },
  798. },
  799. },
  800. },
  801. }
  802. for _, svc := range tests {
  803. for _, wide := range []bool{false, true} {
  804. buff := bytes.Buffer{}
  805. printService(&svc, &buff, PrintOptions{false, false, false, wide, false, false, false, "", []string{}})
  806. output := string(buff.Bytes())
  807. ip := svc.Spec.ClusterIP
  808. if !strings.Contains(output, ip) {
  809. t.Errorf("expected to contain ClusterIP %s, but doesn't: %s", ip, output)
  810. }
  811. for n, ingress := range svc.Status.LoadBalancer.Ingress {
  812. ip = ingress.IP
  813. // For non-wide output, we only guarantee the first IP to be printed
  814. if (n == 0 || wide) && !strings.Contains(output, ip) {
  815. t.Errorf("expected to contain ingress ip %s with wide=%v, but doesn't: %s", ip, wide, output)
  816. }
  817. }
  818. for _, port := range svc.Spec.Ports {
  819. portSpec := fmt.Sprintf("%d/%s", port.Port, port.Protocol)
  820. if !strings.Contains(output, portSpec) {
  821. t.Errorf("expected to contain port: %s, but doesn't: %s", portSpec, output)
  822. }
  823. }
  824. // Each service should print on one line
  825. if 1 != strings.Count(output, "\n") {
  826. t.Errorf("expected a single newline, found %d", strings.Count(output, "\n"))
  827. }
  828. }
  829. }
  830. }
  831. func TestPrintHumanReadableWithNamespace(t *testing.T) {
  832. namespaceName := "testnamespace"
  833. name := "test"
  834. table := []struct {
  835. obj runtime.Object
  836. isNamespaced bool
  837. }{
  838. {
  839. obj: &api.Pod{
  840. ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
  841. },
  842. isNamespaced: true,
  843. },
  844. {
  845. obj: &api.ReplicationController{
  846. ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
  847. Spec: api.ReplicationControllerSpec{
  848. Replicas: 2,
  849. Template: &api.PodTemplateSpec{
  850. ObjectMeta: api.ObjectMeta{
  851. Labels: map[string]string{
  852. "name": "foo",
  853. "type": "production",
  854. },
  855. },
  856. Spec: api.PodSpec{
  857. Containers: []api.Container{
  858. {
  859. Image: "foo/bar",
  860. TerminationMessagePath: api.TerminationMessagePathDefault,
  861. ImagePullPolicy: api.PullIfNotPresent,
  862. },
  863. },
  864. RestartPolicy: api.RestartPolicyAlways,
  865. DNSPolicy: api.DNSDefault,
  866. NodeSelector: map[string]string{
  867. "baz": "blah",
  868. },
  869. },
  870. },
  871. },
  872. },
  873. isNamespaced: true,
  874. },
  875. {
  876. obj: &api.Service{
  877. ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
  878. Spec: api.ServiceSpec{
  879. ClusterIP: "1.2.3.4",
  880. Ports: []api.ServicePort{
  881. {
  882. Port: 80,
  883. Protocol: "TCP",
  884. },
  885. },
  886. },
  887. Status: api.ServiceStatus{
  888. LoadBalancer: api.LoadBalancerStatus{
  889. Ingress: []api.LoadBalancerIngress{
  890. {
  891. IP: "2.3.4.5",
  892. },
  893. },
  894. },
  895. },
  896. },
  897. isNamespaced: true,
  898. },
  899. {
  900. obj: &api.Endpoints{
  901. ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
  902. Subsets: []api.EndpointSubset{{
  903. Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}, {IP: "localhost"}},
  904. Ports: []api.EndpointPort{{Port: 8080}},
  905. },
  906. }},
  907. isNamespaced: true,
  908. },
  909. {
  910. obj: &api.Namespace{
  911. ObjectMeta: api.ObjectMeta{Name: name},
  912. },
  913. isNamespaced: false,
  914. },
  915. {
  916. obj: &api.Secret{
  917. ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
  918. },
  919. isNamespaced: true,
  920. },
  921. {
  922. obj: &api.ServiceAccount{
  923. ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
  924. Secrets: []api.ObjectReference{},
  925. },
  926. isNamespaced: true,
  927. },
  928. {
  929. obj: &api.Node{
  930. ObjectMeta: api.ObjectMeta{Name: name},
  931. Status: api.NodeStatus{},
  932. },
  933. isNamespaced: false,
  934. },
  935. {
  936. obj: &api.PersistentVolume{
  937. ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
  938. Spec: api.PersistentVolumeSpec{},
  939. },
  940. isNamespaced: false,
  941. },
  942. {
  943. obj: &api.PersistentVolumeClaim{
  944. ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
  945. Spec: api.PersistentVolumeClaimSpec{},
  946. },
  947. isNamespaced: true,
  948. },
  949. {
  950. obj: &api.Event{
  951. ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
  952. Source: api.EventSource{Component: "kubelet"},
  953. Message: "Item 1",
  954. FirstTimestamp: unversioned.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
  955. LastTimestamp: unversioned.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
  956. Count: 1,
  957. Type: api.EventTypeNormal,
  958. },
  959. isNamespaced: true,
  960. },
  961. {
  962. obj: &api.LimitRange{
  963. ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
  964. },
  965. isNamespaced: true,
  966. },
  967. {
  968. obj: &api.ResourceQuota{
  969. ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
  970. },
  971. isNamespaced: true,
  972. },
  973. {
  974. obj: &api.ComponentStatus{
  975. Conditions: []api.ComponentCondition{
  976. {Type: api.ComponentHealthy, Status: api.ConditionTrue, Message: "ok", Error: ""},
  977. },
  978. },
  979. isNamespaced: false,
  980. },
  981. }
  982. for _, test := range table {
  983. if test.isNamespaced {
  984. // Expect output to include namespace when requested.
  985. printer := NewHumanReadablePrinter(PrintOptions{
  986. WithNamespace: true,
  987. ColumnLabels: []string{},
  988. })
  989. buffer := &bytes.Buffer{}
  990. err := printer.PrintObj(test.obj, buffer)
  991. if err != nil {
  992. t.Fatalf("An error occurred printing object: %#v", err)
  993. }
  994. matched := contains(strings.Fields(buffer.String()), fmt.Sprintf("%s", namespaceName))
  995. if !matched {
  996. t.Errorf("Expect printing object to contain namespace: %#v", test.obj)
  997. }
  998. } else {
  999. // Expect error when trying to get all namespaces for un-namespaced object.
  1000. printer := NewHumanReadablePrinter(PrintOptions{
  1001. WithNamespace: true,
  1002. ColumnLabels: []string{},
  1003. })
  1004. buffer := &bytes.Buffer{}
  1005. err := printer.PrintObj(test.obj, buffer)
  1006. if err == nil {
  1007. t.Errorf("Expected error when printing un-namespaced type")
  1008. }
  1009. }
  1010. }
  1011. }
  1012. func TestPrintPod(t *testing.T) {
  1013. tests := []struct {
  1014. pod api.Pod
  1015. expect string
  1016. }{
  1017. {
  1018. // Test name, num of containers, restarts, container ready status
  1019. api.Pod{
  1020. ObjectMeta: api.ObjectMeta{Name: "test1"},
  1021. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1022. Status: api.PodStatus{
  1023. Phase: "podPhase",
  1024. ContainerStatuses: []api.ContainerStatus{
  1025. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1026. {RestartCount: 3},
  1027. },
  1028. },
  1029. },
  1030. "test1\t1/2\tpodPhase\t6\t",
  1031. },
  1032. {
  1033. // Test container error overwrites pod phase
  1034. api.Pod{
  1035. ObjectMeta: api.ObjectMeta{Name: "test2"},
  1036. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1037. Status: api.PodStatus{
  1038. Phase: "podPhase",
  1039. ContainerStatuses: []api.ContainerStatus{
  1040. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1041. {State: api.ContainerState{Waiting: &api.ContainerStateWaiting{Reason: "ContainerWaitingReason"}}, RestartCount: 3},
  1042. },
  1043. },
  1044. },
  1045. "test2\t1/2\tContainerWaitingReason\t6\t",
  1046. },
  1047. {
  1048. // Test the same as the above but with Terminated state and the first container overwrites the rest
  1049. api.Pod{
  1050. ObjectMeta: api.ObjectMeta{Name: "test3"},
  1051. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1052. Status: api.PodStatus{
  1053. Phase: "podPhase",
  1054. ContainerStatuses: []api.ContainerStatus{
  1055. {State: api.ContainerState{Waiting: &api.ContainerStateWaiting{Reason: "ContainerWaitingReason"}}, RestartCount: 3},
  1056. {State: api.ContainerState{Terminated: &api.ContainerStateTerminated{Reason: "ContainerTerminatedReason"}}, RestartCount: 3},
  1057. },
  1058. },
  1059. },
  1060. "test3\t0/2\tContainerWaitingReason\t6\t",
  1061. },
  1062. {
  1063. // Test ready is not enough for reporting running
  1064. api.Pod{
  1065. ObjectMeta: api.ObjectMeta{Name: "test4"},
  1066. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1067. Status: api.PodStatus{
  1068. Phase: "podPhase",
  1069. ContainerStatuses: []api.ContainerStatus{
  1070. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1071. {Ready: true, RestartCount: 3},
  1072. },
  1073. },
  1074. },
  1075. "test4\t1/2\tpodPhase\t6\t",
  1076. },
  1077. {
  1078. // Test ready is not enough for reporting running
  1079. api.Pod{
  1080. ObjectMeta: api.ObjectMeta{Name: "test5"},
  1081. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1082. Status: api.PodStatus{
  1083. Reason: "OutOfDisk",
  1084. Phase: "podPhase",
  1085. ContainerStatuses: []api.ContainerStatus{
  1086. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1087. {Ready: true, RestartCount: 3},
  1088. },
  1089. },
  1090. },
  1091. "test5\t1/2\tOutOfDisk\t6\t",
  1092. },
  1093. }
  1094. buf := bytes.NewBuffer([]byte{})
  1095. printer := HumanReadablePrinter{hiddenObjNum: 0}
  1096. for _, test := range tests {
  1097. printer.printPod(&test.pod, buf, PrintOptions{false, false, false, false, true, false, false, "", []string{}})
  1098. // We ignore time
  1099. if !strings.HasPrefix(buf.String(), test.expect) {
  1100. t.Fatalf("Expected: %s, got: %s", test.expect, buf.String())
  1101. }
  1102. buf.Reset()
  1103. }
  1104. if printer.hiddenObjNum > 0 {
  1105. t.Fatalf("Expected hidden pods: 0, got: %d", printer.hiddenObjNum)
  1106. }
  1107. }
  1108. func TestPrintNonTerminatedPod(t *testing.T) {
  1109. tests := []struct {
  1110. pod api.Pod
  1111. expect string
  1112. }{
  1113. {
  1114. // Test pod phase Running should be printed
  1115. api.Pod{
  1116. ObjectMeta: api.ObjectMeta{Name: "test1"},
  1117. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1118. Status: api.PodStatus{
  1119. Phase: api.PodRunning,
  1120. ContainerStatuses: []api.ContainerStatus{
  1121. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1122. {RestartCount: 3},
  1123. },
  1124. },
  1125. },
  1126. "test1\t1/2\tRunning\t6\t",
  1127. },
  1128. {
  1129. // Test pod phase Pending should be printed
  1130. api.Pod{
  1131. ObjectMeta: api.ObjectMeta{Name: "test2"},
  1132. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1133. Status: api.PodStatus{
  1134. Phase: api.PodPending,
  1135. ContainerStatuses: []api.ContainerStatus{
  1136. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1137. {RestartCount: 3},
  1138. },
  1139. },
  1140. },
  1141. "test2\t1/2\tPending\t6\t",
  1142. },
  1143. {
  1144. // Test pod phase Unknown should be printed
  1145. api.Pod{
  1146. ObjectMeta: api.ObjectMeta{Name: "test3"},
  1147. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1148. Status: api.PodStatus{
  1149. Phase: api.PodUnknown,
  1150. ContainerStatuses: []api.ContainerStatus{
  1151. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1152. {RestartCount: 3},
  1153. },
  1154. },
  1155. },
  1156. "test3\t1/2\tUnknown\t6\t",
  1157. },
  1158. {
  1159. // Test pod phase Succeeded shouldn't be printed
  1160. api.Pod{
  1161. ObjectMeta: api.ObjectMeta{Name: "test4"},
  1162. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1163. Status: api.PodStatus{
  1164. Phase: api.PodSucceeded,
  1165. ContainerStatuses: []api.ContainerStatus{
  1166. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1167. {RestartCount: 3},
  1168. },
  1169. },
  1170. },
  1171. "",
  1172. },
  1173. {
  1174. // Test pod phase Failed shouldn't be printed
  1175. api.Pod{
  1176. ObjectMeta: api.ObjectMeta{Name: "test5"},
  1177. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1178. Status: api.PodStatus{
  1179. Phase: api.PodFailed,
  1180. ContainerStatuses: []api.ContainerStatus{
  1181. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1182. {Ready: true, RestartCount: 3},
  1183. },
  1184. },
  1185. },
  1186. "",
  1187. },
  1188. }
  1189. buf := bytes.NewBuffer([]byte{})
  1190. printer := HumanReadablePrinter{hiddenObjNum: 0}
  1191. for _, test := range tests {
  1192. printer.printPod(&test.pod, buf, PrintOptions{false, false, false, false, false, false, false, "", []string{}})
  1193. // We ignore time
  1194. if !strings.HasPrefix(buf.String(), test.expect) {
  1195. t.Fatalf("Expected: %s, got: %s", test.expect, buf.String())
  1196. }
  1197. buf.Reset()
  1198. }
  1199. if printer.hiddenObjNum != 2 {
  1200. t.Fatalf("Expected hidden pods: 2, got: %d", printer.hiddenObjNum)
  1201. }
  1202. }
  1203. func TestPrintPodWithLabels(t *testing.T) {
  1204. tests := []struct {
  1205. pod api.Pod
  1206. labelColumns []string
  1207. startsWith string
  1208. endsWith string
  1209. }{
  1210. {
  1211. // Test name, num of containers, restarts, container ready status
  1212. api.Pod{
  1213. ObjectMeta: api.ObjectMeta{
  1214. Name: "test1",
  1215. Labels: map[string]string{"col1": "asd", "COL2": "zxc"},
  1216. },
  1217. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1218. Status: api.PodStatus{
  1219. Phase: "podPhase",
  1220. ContainerStatuses: []api.ContainerStatus{
  1221. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1222. {RestartCount: 3},
  1223. },
  1224. },
  1225. },
  1226. []string{"col1", "COL2"},
  1227. "test1\t1/2\tpodPhase\t6\t",
  1228. "\tasd\tzxc\n",
  1229. },
  1230. {
  1231. // Test name, num of containers, restarts, container ready status
  1232. api.Pod{
  1233. ObjectMeta: api.ObjectMeta{
  1234. Name: "test1",
  1235. Labels: map[string]string{"col1": "asd", "COL2": "zxc"},
  1236. },
  1237. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1238. Status: api.PodStatus{
  1239. Phase: "podPhase",
  1240. ContainerStatuses: []api.ContainerStatus{
  1241. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1242. {RestartCount: 3},
  1243. },
  1244. },
  1245. },
  1246. []string{},
  1247. "test1\t1/2\tpodPhase\t6\t",
  1248. "\n",
  1249. },
  1250. }
  1251. buf := bytes.NewBuffer([]byte{})
  1252. printer := HumanReadablePrinter{hiddenObjNum: 0}
  1253. for _, test := range tests {
  1254. printer.printPod(&test.pod, buf, PrintOptions{false, false, false, false, false, false, false, "", test.labelColumns})
  1255. // We ignore time
  1256. if !strings.HasPrefix(buf.String(), test.startsWith) || !strings.HasSuffix(buf.String(), test.endsWith) {
  1257. t.Fatalf("Expected to start with: %s and end with: %s, but got: %s", test.startsWith, test.endsWith, buf.String())
  1258. }
  1259. buf.Reset()
  1260. }
  1261. if printer.hiddenObjNum > 0 {
  1262. t.Fatalf("Expected hidden pods: 0, got: %d", printer.hiddenObjNum)
  1263. }
  1264. }
  1265. type stringTestList []struct {
  1266. name, got, exp string
  1267. }
  1268. func TestTranslateTimestamp(t *testing.T) {
  1269. tl := stringTestList{
  1270. {"a while from now", translateTimestamp(unversioned.Time{Time: time.Now().Add(2.1e9)}), "<invalid>"},
  1271. {"almost now", translateTimestamp(unversioned.Time{Time: time.Now().Add(1.9e9)}), "0s"},
  1272. {"now", translateTimestamp(unversioned.Time{Time: time.Now()}), "0s"},
  1273. {"unknown", translateTimestamp(unversioned.Time{}), "<unknown>"},
  1274. {"30 seconds ago", translateTimestamp(unversioned.Time{Time: time.Now().Add(-3e10)}), "30s"},
  1275. {"5 minutes ago", translateTimestamp(unversioned.Time{Time: time.Now().Add(-3e11)}), "5m"},
  1276. {"an hour ago", translateTimestamp(unversioned.Time{Time: time.Now().Add(-6e12)}), "1h"},
  1277. {"2 days ago", translateTimestamp(unversioned.Time{Time: time.Now().UTC().AddDate(0, 0, -2)}), "2d"},
  1278. {"months ago", translateTimestamp(unversioned.Time{Time: time.Now().UTC().AddDate(0, 0, -90)}), "90d"},
  1279. {"10 years ago", translateTimestamp(unversioned.Time{Time: time.Now().UTC().AddDate(-10, 0, 0)}), "10y"},
  1280. }
  1281. for _, test := range tl {
  1282. if test.got != test.exp {
  1283. t.Errorf("On %v, expected '%v', but got '%v'",
  1284. test.name, test.exp, test.got)
  1285. }
  1286. }
  1287. }
  1288. func TestPrintDeployment(t *testing.T) {
  1289. tests := []struct {
  1290. deployment extensions.Deployment
  1291. expect string
  1292. }{
  1293. {
  1294. extensions.Deployment{
  1295. ObjectMeta: api.ObjectMeta{
  1296. Name: "test1",
  1297. CreationTimestamp: unversioned.Time{Time: time.Now().Add(1.9e9)},
  1298. },
  1299. Spec: extensions.DeploymentSpec{
  1300. Replicas: 5,
  1301. Template: api.PodTemplateSpec{
  1302. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1303. },
  1304. },
  1305. Status: extensions.DeploymentStatus{
  1306. Replicas: 10,
  1307. UpdatedReplicas: 2,
  1308. AvailableReplicas: 1,
  1309. UnavailableReplicas: 4,
  1310. },
  1311. },
  1312. "test1\t5\t10\t2\t1\t0s\n",
  1313. },
  1314. }
  1315. buf := bytes.NewBuffer([]byte{})
  1316. for _, test := range tests {
  1317. printDeployment(&test.deployment, buf, PrintOptions{false, false, false, false, true, false, false, "", []string{}})
  1318. if buf.String() != test.expect {
  1319. t.Fatalf("Expected: %s, got: %s", test.expect, buf.String())
  1320. }
  1321. buf.Reset()
  1322. }
  1323. }
  1324. func TestPrintDaemonSet(t *testing.T) {
  1325. tests := []struct {
  1326. ds extensions.DaemonSet
  1327. startsWith string
  1328. }{
  1329. {
  1330. extensions.DaemonSet{
  1331. ObjectMeta: api.ObjectMeta{
  1332. Name: "test1",
  1333. CreationTimestamp: unversioned.Time{Time: time.Now().Add(1.9e9)},
  1334. },
  1335. Spec: extensions.DaemonSetSpec{
  1336. Template: api.PodTemplateSpec{
  1337. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1338. },
  1339. },
  1340. Status: extensions.DaemonSetStatus{
  1341. CurrentNumberScheduled: 2,
  1342. DesiredNumberScheduled: 3,
  1343. },
  1344. },
  1345. "test1\t3\t2\t<none>\t0s\n",
  1346. },
  1347. }
  1348. buf := bytes.NewBuffer([]byte{})
  1349. for _, test := range tests {
  1350. printDaemonSet(&test.ds, buf, PrintOptions{false, false, false, false, false, false, false, "", []string{}})
  1351. if !strings.HasPrefix(buf.String(), test.startsWith) {
  1352. t.Fatalf("Expected to start with %s but got %s", test.startsWith, buf.String())
  1353. }
  1354. buf.Reset()
  1355. }
  1356. }
  1357. func TestPrintJob(t *testing.T) {
  1358. completions := int32(2)
  1359. tests := []struct {
  1360. job batch.Job
  1361. expect string
  1362. }{
  1363. {
  1364. batch.Job{
  1365. ObjectMeta: api.ObjectMeta{
  1366. Name: "job1",
  1367. CreationTimestamp: unversioned.Time{Time: time.Now().Add(1.9e9)},
  1368. },
  1369. Spec: batch.JobSpec{
  1370. Completions: &completions,
  1371. },
  1372. Status: batch.JobStatus{
  1373. Succeeded: 1,
  1374. },
  1375. },
  1376. "job1\t2\t1\t0s\n",
  1377. },
  1378. {
  1379. batch.Job{
  1380. ObjectMeta: api.ObjectMeta{
  1381. Name: "job2",
  1382. CreationTimestamp: unversioned.Time{Time: time.Now().AddDate(-10, 0, 0)},
  1383. },
  1384. Spec: batch.JobSpec{
  1385. Completions: nil,
  1386. },
  1387. Status: batch.JobStatus{
  1388. Succeeded: 0,
  1389. },
  1390. },
  1391. "job2\t<none>\t0\t10y\n",
  1392. },
  1393. }
  1394. buf := bytes.NewBuffer([]byte{})
  1395. for _, test := range tests {
  1396. printJob(&test.job, buf, PrintOptions{false, false, false, false, true, false, false, "", []string{}})
  1397. if buf.String() != test.expect {
  1398. t.Fatalf("Expected: %s, got: %s", test.expect, buf.String())
  1399. }
  1400. buf.Reset()
  1401. }
  1402. }
  1403. func TestPrintPodShowLabels(t *testing.T) {
  1404. tests := []struct {
  1405. pod api.Pod
  1406. startsWith string
  1407. endsWith string
  1408. showLabels bool
  1409. }{
  1410. {
  1411. // Test name, num of containers, restarts, container ready status
  1412. api.Pod{
  1413. ObjectMeta: api.ObjectMeta{
  1414. Name: "test1",
  1415. Labels: map[string]string{"col1": "asd", "COL2": "zxc"},
  1416. },
  1417. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1418. Status: api.PodStatus{
  1419. Phase: "podPhase",
  1420. ContainerStatuses: []api.ContainerStatus{
  1421. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1422. {RestartCount: 3},
  1423. },
  1424. },
  1425. },
  1426. "test1\t1/2\tpodPhase\t6\t",
  1427. "\tCOL2=zxc,col1=asd\n",
  1428. true,
  1429. },
  1430. {
  1431. // Test name, num of containers, restarts, container ready status
  1432. api.Pod{
  1433. ObjectMeta: api.ObjectMeta{
  1434. Name: "test1",
  1435. Labels: map[string]string{"col3": "asd", "COL4": "zxc"},
  1436. },
  1437. Spec: api.PodSpec{Containers: make([]api.Container, 2)},
  1438. Status: api.PodStatus{
  1439. Phase: "podPhase",
  1440. ContainerStatuses: []api.ContainerStatus{
  1441. {Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
  1442. {RestartCount: 3},
  1443. },
  1444. },
  1445. },
  1446. "test1\t1/2\tpodPhase\t6\t",
  1447. "\n",
  1448. false,
  1449. },
  1450. }
  1451. buf := bytes.NewBuffer([]byte{})
  1452. printer := HumanReadablePrinter{hiddenObjNum: 0}
  1453. for _, test := range tests {
  1454. printer.printPod(&test.pod, buf, PrintOptions{false, false, false, false, false, test.showLabels, false, "", []string{}})
  1455. // We ignore time
  1456. if !strings.HasPrefix(buf.String(), test.startsWith) || !strings.HasSuffix(buf.String(), test.endsWith) {
  1457. t.Fatalf("Expected to start with: %s and end with: %s, but got: %s", test.startsWith, test.endsWith, buf.String())
  1458. }
  1459. buf.Reset()
  1460. }
  1461. if printer.hiddenObjNum > 0 {
  1462. t.Fatalf("Expected hidden pods: 0, got: %d", printer.hiddenObjNum)
  1463. }
  1464. }