docker_cli_search_test.go 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "testing"
  6. )
  7. // search for repos named "registry" on the central registry
  8. func TestSearchOnCentralRegistry(t *testing.T) {
  9. testRequires(t, Network)
  10. searchCmd := exec.Command(dockerBinary, "search", "busybox")
  11. out, exitCode, err := runCommandWithOutput(searchCmd)
  12. if err != nil || exitCode != 0 {
  13. t.Fatalf("failed to search on the central registry: %s, %v", out, err)
  14. }
  15. if !strings.Contains(out, "Busybox base image.") {
  16. t.Fatal("couldn't find any repository named (or containing) 'Busybox base image.'")
  17. }
  18. logDone("search - search for repositories named (or containing) 'Busybox base image.'")
  19. }
  20. func TestSearchStarsOptionWithWrongParameter(t *testing.T) {
  21. searchCmdStarsChars := exec.Command(dockerBinary, "search", "--stars=a", "busybox")
  22. out, exitCode, err := runCommandWithOutput(searchCmdStarsChars)
  23. if err == nil || exitCode == 0 {
  24. t.Fatalf("Should not get right information: %s, %v", out, err)
  25. }
  26. if !strings.Contains(out, "invalid value") {
  27. t.Fatal("couldn't find the invalid value warning")
  28. }
  29. searchCmdStarsNegativeNumber := exec.Command(dockerBinary, "search", "-s=-1", "busybox")
  30. out, exitCode, err = runCommandWithOutput(searchCmdStarsNegativeNumber)
  31. if err == nil || exitCode == 0 {
  32. t.Fatalf("Should not get right information: %s, %v", out, err)
  33. }
  34. if !strings.Contains(out, "invalid value") {
  35. t.Fatal("couldn't find the invalid value warning")
  36. }
  37. logDone("search - Verify search with wrong parameter.")
  38. }
  39. func TestSearchCmdOptions(t *testing.T) {
  40. testRequires(t, Network)
  41. searchCmdhelp := exec.Command(dockerBinary, "search", "--help")
  42. out, exitCode, err := runCommandWithOutput(searchCmdhelp)
  43. if err != nil || exitCode != 0 {
  44. t.Fatalf("failed to get search help information: %s, %v", out, err)
  45. }
  46. if !strings.Contains(out, "Usage: docker search [OPTIONS] TERM") {
  47. t.Fatalf("failed to show docker search usage: %s, %v", out, err)
  48. }
  49. searchCmd := exec.Command(dockerBinary, "search", "busybox")
  50. outSearchCmd, exitCode, err := runCommandWithOutput(searchCmd)
  51. if err != nil || exitCode != 0 {
  52. t.Fatalf("failed to search on the central registry: %s, %v", outSearchCmd, err)
  53. }
  54. searchCmdautomated := exec.Command(dockerBinary, "search", "--automated=true", "busybox")
  55. outSearchCmdautomated, exitCode, err := runCommandWithOutput(searchCmdautomated) //The busybox is a busybox base image, not an AUTOMATED image.
  56. if err != nil || exitCode != 0 {
  57. t.Fatalf("failed to search with automated=true on the central registry: %s, %v", outSearchCmdautomated, err)
  58. }
  59. outSearchCmdautomatedSlice := strings.Split(outSearchCmdautomated, "\n")
  60. for i := range outSearchCmdautomatedSlice {
  61. if strings.HasPrefix(outSearchCmdautomatedSlice[i], "busybox ") {
  62. t.Fatalf("The busybox is not an AUTOMATED image: %s, %v", out, err)
  63. }
  64. }
  65. searchCmdStars := exec.Command(dockerBinary, "search", "-s=2", "busybox")
  66. outSearchCmdStars, exitCode, err := runCommandWithOutput(searchCmdStars)
  67. if err != nil || exitCode != 0 {
  68. t.Fatalf("failed to search with stars=2 on the central registry: %s, %v", outSearchCmdStars, err)
  69. }
  70. if strings.Count(outSearchCmdStars, "[OK]") > strings.Count(outSearchCmd, "[OK]") {
  71. t.Fatalf("The quantity of images with stars should be less than that of all images: %s, %v", outSearchCmdStars, err)
  72. }
  73. searchCmdOptions := exec.Command(dockerBinary, "search", "--stars=2", "--automated=true", "--no-trunc=true", "busybox")
  74. out, exitCode, err = runCommandWithOutput(searchCmdOptions)
  75. if err != nil || exitCode != 0 {
  76. t.Fatalf("failed to search with stars&automated&no-trunc options on the central registry: %s, %v", out, err)
  77. }
  78. logDone("search - have a try for search options.")
  79. }