|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package search |
| 18 | + |
| 19 | +import ( |
| 20 | + "regexp" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/containerd/nerdctl/mod/tigron/expect" |
| 24 | + "github.com/containerd/nerdctl/mod/tigron/test" |
| 25 | + |
| 26 | + "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" |
| 27 | +) |
| 28 | + |
| 29 | +// All tests in this file are based on the output of `nerdctl search alpine`. |
| 30 | +// |
| 31 | +// Expected output format (default behavior with --limit 10): |
| 32 | +// |
| 33 | +// NAME DESCRIPTION STARS OFFICIAL |
| 34 | +// alpine A minimal Docker image based on Alpine Linux… 11437 [OK] |
| 35 | +// alpine/git A simple git container running in alpine li… 249 |
| 36 | +// alpine/socat Run socat command in alpine container 115 |
| 37 | +// alpine/helm Auto-trigger docker build for kubernetes hel… 69 |
| 38 | +// alpine/curl 11 |
| 39 | +// alpine/k8s Kubernetes toolbox for EKS (kubectl, helm, i… 64 |
| 40 | +// alpine/bombardier Auto-trigger docker build for bombardier whe… 28 |
| 41 | +// alpine/httpie Auto-trigger docker build for `httpie` when … 21 |
| 42 | +// alpine/terragrunt Auto-trigger docker build for terragrunt whe… 18 |
| 43 | +// alpine/openssl openssl 7 |
| 44 | + |
| 45 | +func TestSearch(t *testing.T) { |
| 46 | + testCase := nerdtest.Setup() |
| 47 | + |
| 48 | + testCase.Command = test.Command("search", "alpine", "--limit", "5") |
| 49 | + |
| 50 | + testCase.Expected = test.Expects(expect.ExitCodeSuccess, nil, expect.All( |
| 51 | + expect.Contains("NAME"), |
| 52 | + expect.Contains("DESCRIPTION"), |
| 53 | + expect.Contains("STARS"), |
| 54 | + expect.Contains("OFFICIAL"), |
| 55 | + expect.Match(regexp.MustCompile(`NAME\s+DESCRIPTION\s+STARS\s+OFFICIAL`)), |
| 56 | + expect.Contains("alpine"), |
| 57 | + expect.Match(regexp.MustCompile(`alpine\s+A minimal Docker image based on Alpine Linux`)), |
| 58 | + expect.Match(regexp.MustCompile(`alpine\s+.*\s+\d+\s+\[OK\]`)), |
| 59 | + expect.Contains("[OK]"), |
| 60 | + expect.Match(regexp.MustCompile(`alpine/\w+`)), |
| 61 | + )) |
| 62 | + |
| 63 | + testCase.Run(t) |
| 64 | +} |
| 65 | + |
| 66 | +func TestSearchWithFilter(t *testing.T) { |
| 67 | + testCase := nerdtest.Setup() |
| 68 | + |
| 69 | + testCase.Command = test.Command("search", "alpine", "--filter", "is-official=true", "--limit", "5") |
| 70 | + |
| 71 | + testCase.Expected = test.Expects(expect.ExitCodeSuccess, nil, expect.All( |
| 72 | + expect.Contains("NAME"), |
| 73 | + expect.Contains("OFFICIAL"), |
| 74 | + expect.Contains("alpine"), |
| 75 | + expect.Contains("[OK]"), |
| 76 | + expect.Match(regexp.MustCompile(`alpine\s+.*\s+\d+\s+\[OK\]`)), |
| 77 | + )) |
| 78 | + |
| 79 | + testCase.Run(t) |
| 80 | +} |
| 81 | + |
| 82 | +func TestSearchWithNoTrunc(t *testing.T) { |
| 83 | + testCase := nerdtest.Setup() |
| 84 | + |
| 85 | + testCase.Command = test.Command("search", "alpine", "--limit", "3", "--no-trunc") |
| 86 | + |
| 87 | + testCase.Expected = test.Expects(expect.ExitCodeSuccess, nil, expect.All( |
| 88 | + expect.Contains("NAME"), |
| 89 | + expect.Contains("DESCRIPTION"), |
| 90 | + expect.Contains("alpine"), |
| 91 | + // With --no-trunc, the full description should be visible (not truncated with …) |
| 92 | + // The alpine description is longer than 45 chars, so without truncation |
| 93 | + // we should see more complete text |
| 94 | + expect.Match(regexp.MustCompile(`alpine\s+A minimal Docker image based on Alpine Linux`)), |
| 95 | + )) |
| 96 | + |
| 97 | + testCase.Run(t) |
| 98 | +} |
| 99 | + |
| 100 | +func TestSearchWithFormat(t *testing.T) { |
| 101 | + testCase := nerdtest.Setup() |
| 102 | + |
| 103 | + testCase.Command = test.Command("search", "alpine", "--limit", "2", "--format", "{{.Name}}: {{.StarCount}}") |
| 104 | + |
| 105 | + testCase.Expected = test.Expects(expect.ExitCodeSuccess, nil, expect.All( |
| 106 | + expect.Match(regexp.MustCompile(`alpine:\s*\d+`)), |
| 107 | + expect.DoesNotContain("NAME"), |
| 108 | + expect.DoesNotContain("DESCRIPTION"), |
| 109 | + expect.DoesNotContain("OFFICIAL"), |
| 110 | + )) |
| 111 | + |
| 112 | + testCase.Run(t) |
| 113 | +} |
| 114 | + |
| 115 | +func TestSearchOutputFormat(t *testing.T) { |
| 116 | + testCase := nerdtest.Setup() |
| 117 | + |
| 118 | + testCase.Command = test.Command("search", "alpine", "--limit", "5") |
| 119 | + |
| 120 | + testCase.Expected = test.Expects(expect.ExitCodeSuccess, nil, expect.All( |
| 121 | + expect.Match(regexp.MustCompile(`NAME\s+DESCRIPTION\s+STARS\s+OFFICIAL`)), |
| 122 | + expect.Match(regexp.MustCompile(`(?m)^alpine\s+.*\s+\d+\s+\[OK\]\s*$`)), |
| 123 | + expect.Match(regexp.MustCompile(`(?m)^alpine/\w+\s+.*\s+\d+\s*$`)), |
| 124 | + expect.DoesNotMatch(regexp.MustCompile(`(?m)^\s+\d+\s*$`)), |
| 125 | + )) |
| 126 | + |
| 127 | + testCase.Run(t) |
| 128 | +} |
| 129 | + |
| 130 | +func TestSearchDescriptionFormatting(t *testing.T) { |
| 131 | + testCase := nerdtest.Setup() |
| 132 | + |
| 133 | + testCase.Command = test.Command("search", "alpine", "--limit", "10") |
| 134 | + |
| 135 | + testCase.Expected = test.Expects(expect.ExitCodeSuccess, nil, expect.All( |
| 136 | + expect.Match(regexp.MustCompile(`Alpine Linux…`)), |
| 137 | + expect.DoesNotMatch(regexp.MustCompile(`(?m)^\s+\d+\s+`)), |
| 138 | + expect.Match(regexp.MustCompile(`(?m)^[a-z0-9/_-]+\s+.*\s+\d+`)), |
| 139 | + )) |
| 140 | + |
| 141 | + testCase.Run(t) |
| 142 | +} |
0 commit comments