2014-02-25 16:17:48 +00:00
package main
import (
2023-07-14 18:02:38 +00:00
"context"
2016-06-01 20:38:14 +00:00
"fmt"
2014-02-25 16:17:48 +00:00
"strings"
2019-09-09 21:06:12 +00:00
"testing"
2015-04-18 16:46:47 +00:00
2023-07-27 19:52:44 +00:00
"github.com/docker/docker/integration-cli/cli"
2020-02-07 13:39:24 +00:00
"gotest.tools/v3/assert"
2014-02-25 16:17:48 +00:00
)
2022-06-16 21:32:10 +00:00
type DockerCLISearchSuite struct {
ds * DockerSuite
}
2023-07-14 18:02:38 +00:00
func ( s * DockerCLISearchSuite ) TearDownTest ( ctx context . Context , c * testing . T ) {
s . ds . TearDownTest ( ctx , c )
2022-06-16 21:32:10 +00:00
}
func ( s * DockerCLISearchSuite ) OnTimeout ( c * testing . T ) {
s . ds . OnTimeout ( c )
}
2014-02-25 16:17:48 +00:00
// search for repos named "registry" on the central registry
2022-06-16 21:32:10 +00:00
func ( s * DockerCLISearchSuite ) TestSearchOnCentralRegistry ( c * testing . T ) {
2023-07-27 19:52:44 +00:00
out := cli . DockerCmd ( c , "search" , "busybox" ) . Stdout ( )
2019-04-04 13:23:19 +00:00
assert . Assert ( c , strings . Contains ( out , "Busybox base image." ) , "couldn't find any repository named (or containing) 'Busybox base image.'" )
2014-02-25 16:17:48 +00:00
}
2015-04-03 00:55:08 +00:00
2022-06-16 21:32:10 +00:00
func ( s * DockerCLISearchSuite ) TestSearchStarsOptionWithWrongParameter ( c * testing . T ) {
2016-05-20 11:41:28 +00:00
out , _ , err := dockerCmdWithError ( "search" , "--filter" , "stars=a" , "busybox" )
2019-04-04 13:23:19 +00:00
assert . ErrorContains ( c , err , "" , out )
2022-03-02 17:56:06 +00:00
assert . Assert ( c , strings . Contains ( out , "invalid filter" ) , "couldn't find the invalid filter warning" )
2016-05-20 11:41:28 +00:00
out , _ , err = dockerCmdWithError ( "search" , "-f" , "stars=a" , "busybox" )
2019-04-04 13:23:19 +00:00
assert . ErrorContains ( c , err , "" , out )
2022-03-02 17:56:06 +00:00
assert . Assert ( c , strings . Contains ( out , "invalid filter" ) , "couldn't find the invalid filter warning" )
2016-05-20 11:41:28 +00:00
out , _ , err = dockerCmdWithError ( "search" , "-f" , "is-automated=a" , "busybox" )
2019-04-04 13:23:19 +00:00
assert . ErrorContains ( c , err , "" , out )
2022-03-02 17:56:06 +00:00
assert . Assert ( c , strings . Contains ( out , "invalid filter" ) , "couldn't find the invalid filter warning" )
2016-05-20 11:41:28 +00:00
out , _ , err = dockerCmdWithError ( "search" , "-f" , "is-official=a" , "busybox" )
2019-04-04 13:23:19 +00:00
assert . ErrorContains ( c , err , "" , out )
2022-03-02 17:56:06 +00:00
assert . Assert ( c , strings . Contains ( out , "invalid filter" ) , "couldn't find the invalid filter warning" )
2015-04-03 00:55:08 +00:00
}
2015-04-14 01:23:26 +00:00
2022-06-16 21:32:10 +00:00
func ( s * DockerCLISearchSuite ) TestSearchCmdOptions ( c * testing . T ) {
2023-07-27 19:52:44 +00:00
outSearchCmd := cli . DockerCmd ( c , "search" , "busybox" ) . Combined ( )
2019-05-22 12:55:14 +00:00
assert . Assert ( c , strings . Count ( outSearchCmd , "\n" ) > 3 , outSearchCmd )
2023-07-27 19:52:44 +00:00
outSearchCmdautomated := cli . DockerCmd ( c , "search" , "--filter" , "is-automated=true" , "busybox" ) . Combined ( ) // The busybox is a busybox base image, not an AUTOMATED image.
2019-05-22 12:55:14 +00:00
outSearchCmdautomatedSlice := strings . Split ( outSearchCmdautomated , "\n" )
for i := range outSearchCmdautomatedSlice {
assert . Assert ( c , ! strings . HasPrefix ( outSearchCmdautomatedSlice [ i ] , "busybox " ) , "The busybox is not an AUTOMATED image: %s" , outSearchCmdautomated )
}
2023-07-27 19:52:44 +00:00
outSearchCmdNotOfficial := cli . DockerCmd ( c , "search" , "--filter" , "is-official=false" , "busybox" ) . Combined ( ) // The busybox is a busybox base image, official image.
2019-05-22 12:55:14 +00:00
outSearchCmdNotOfficialSlice := strings . Split ( outSearchCmdNotOfficial , "\n" )
for i := range outSearchCmdNotOfficialSlice {
assert . Assert ( c , ! strings . HasPrefix ( outSearchCmdNotOfficialSlice [ i ] , "busybox " ) , "The busybox is not an OFFICIAL image: %s" , outSearchCmdNotOfficial )
}
2023-07-27 19:52:44 +00:00
outSearchCmdOfficial := cli . DockerCmd ( c , "search" , "--filter" , "is-official=true" , "busybox" ) . Combined ( ) // The busybox is a busybox base image, official image.
2019-05-22 12:55:14 +00:00
outSearchCmdOfficialSlice := strings . Split ( outSearchCmdOfficial , "\n" )
assert . Equal ( c , len ( outSearchCmdOfficialSlice ) , 3 ) // 1 header, 1 line, 1 carriage return
assert . Assert ( c , strings . HasPrefix ( outSearchCmdOfficialSlice [ 1 ] , "busybox " ) , "The busybox is an OFFICIAL image: %s" , outSearchCmdOfficial )
2023-07-27 19:52:44 +00:00
outSearchCmdStars := cli . DockerCmd ( c , "search" , "--filter" , "stars=10" , "busybox" ) . Combined ( )
2019-05-22 12:55:14 +00:00
assert . Assert ( c , strings . Count ( outSearchCmdStars , "\n" ) <= strings . Count ( outSearchCmd , "\n" ) , "Number of images with 10+ stars should be less than that of all images:\noutSearchCmdStars: %s\noutSearch: %s\n" , outSearchCmdStars , outSearchCmd )
2023-07-27 19:52:44 +00:00
cli . DockerCmd ( c , "search" , "--filter" , "is-automated=true" , "--filter" , "stars=2" , "--no-trunc=true" , "busybox" )
2019-05-22 12:55:14 +00:00
}
2015-09-22 11:44:40 +00:00
// search for repos which start with "ubuntu-" on the central registry
2022-06-16 21:32:10 +00:00
func ( s * DockerCLISearchSuite ) TestSearchOnCentralRegistryWithDash ( c * testing . T ) {
2023-07-27 19:52:44 +00:00
cli . DockerCmd ( c , "search" , "ubuntu-" )
2015-09-22 11:44:40 +00:00
}
2016-06-01 20:38:14 +00:00
// test case for #23055
2022-06-16 21:32:10 +00:00
func ( s * DockerCLISearchSuite ) TestSearchWithLimit ( c * testing . T ) {
2019-07-17 11:09:17 +00:00
for _ , limit := range [ ] int { 10 , 50 , 100 } {
out , _ , err := dockerCmdWithError ( "search" , fmt . Sprintf ( "--limit=%d" , limit ) , "docker" )
assert . NilError ( c , err )
outSlice := strings . Split ( out , "\n" )
assert . Equal ( c , len ( outSlice ) , limit + 2 ) // 1 header, 1 carriage return
}
2022-03-02 12:29:47 +00:00
for _ , limit := range [ ] int { - 1 , 101 } {
2019-07-17 11:09:17 +00:00
_ , _ , err := dockerCmdWithError ( "search" , fmt . Sprintf ( "--limit=%d" , limit ) , "docker" )
assert . ErrorContains ( c , err , "" )
}
2016-06-01 20:38:14 +00:00
}