zotregistry.io/zot@v1.4.4-0.20231124084042-02a8ed785457/pkg/cli/client/repo_test.go (about) 1 //go:build search 2 // +build search 3 4 package client_test 5 6 import ( 7 "bytes" 8 "fmt" 9 "os" 10 "regexp" 11 "strings" 12 "testing" 13 14 . "github.com/smartystreets/goconvey/convey" 15 16 "zotregistry.io/zot/pkg/api" 17 "zotregistry.io/zot/pkg/api/config" 18 "zotregistry.io/zot/pkg/cli/client" 19 test "zotregistry.io/zot/pkg/test/common" 20 . "zotregistry.io/zot/pkg/test/image-utils" 21 ) 22 23 func TestReposCommand(t *testing.T) { 24 Convey("repos", t, func() { 25 port := test.GetFreePort() 26 baseURL := test.GetBaseURL(port) 27 conf := config.New() 28 conf.HTTP.Port = port 29 30 ctlr := api.NewController(conf) 31 ctlr.Config.Storage.RootDirectory = t.TempDir() 32 cm := test.NewControllerManager(ctlr) 33 34 cm.StartAndWait(conf.HTTP.Port) 35 defer cm.StopServer() 36 37 err := UploadImage(CreateRandomImage(), baseURL, "repo1", "tag1") 38 So(err, ShouldBeNil) 39 err = UploadImage(CreateRandomImage(), baseURL, "repo2", "tag2") 40 So(err, ShouldBeNil) 41 42 configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"repostest","url":"%s","showspinner":false}]}`, 43 baseURL)) 44 defer os.Remove(configPath) 45 46 args := []string{"list", "--config", "repostest"} 47 cmd := client.NewRepoCommand(client.NewSearchService()) 48 buff := bytes.NewBufferString("") 49 cmd.SetOut(buff) 50 cmd.SetErr(buff) 51 cmd.SetArgs(args) 52 err = cmd.Execute() 53 So(err, ShouldBeNil) 54 space := regexp.MustCompile(`\s+`) 55 str := space.ReplaceAllString(buff.String(), " ") 56 actual := strings.TrimSpace(str) 57 So(actual, ShouldContainSubstring, "repo1") 58 So(actual, ShouldContainSubstring, "repo2") 59 60 args = []string{"list", "--sort-by", "alpha-dsc", "--config", "repostest"} 61 cmd = client.NewRepoCommand(client.NewSearchService()) 62 buff = bytes.NewBufferString("") 63 cmd.SetOut(buff) 64 cmd.SetErr(buff) 65 cmd.SetArgs(args) 66 err = cmd.Execute() 67 So(err, ShouldBeNil) 68 space = regexp.MustCompile(`\s+`) 69 str = space.ReplaceAllString(buff.String(), " ") 70 actual = strings.TrimSpace(str) 71 So(actual, ShouldContainSubstring, "repo1") 72 So(actual, ShouldContainSubstring, "repo2") 73 So(strings.Index(actual, "repo2"), ShouldBeLessThan, strings.Index(actual, "repo1")) 74 75 args = []string{"list", "--sort-by", "alpha-asc", "--config", "repostest"} 76 cmd = client.NewRepoCommand(client.NewSearchService()) 77 buff = bytes.NewBufferString("") 78 cmd.SetOut(buff) 79 cmd.SetErr(buff) 80 cmd.SetArgs(args) 81 err = cmd.Execute() 82 So(err, ShouldBeNil) 83 space = regexp.MustCompile(`\s+`) 84 str = space.ReplaceAllString(buff.String(), " ") 85 actual = strings.TrimSpace(str) 86 So(actual, ShouldContainSubstring, "repo1") 87 So(actual, ShouldContainSubstring, "repo2") 88 So(strings.Index(actual, "repo1"), ShouldBeLessThan, strings.Index(actual, "repo2")) 89 }) 90 } 91 92 func TestSuggestions(t *testing.T) { 93 Convey("Suggestions", t, func() { 94 space := regexp.MustCompile(`\s+`) 95 suggestion := client.ShowSuggestionsIfUnknownCommand( 96 client.NewRepoCommand(client.NewSearchService()), []string{"bad-command"}) 97 str := space.ReplaceAllString(suggestion.Error(), " ") 98 So(str, ShouldContainSubstring, "unknown subcommand") 99 100 suggestion = client.ShowSuggestionsIfUnknownCommand( 101 client.NewRepoCommand(client.NewSearchService()), []string{"listt"}) 102 str = space.ReplaceAllString(suggestion.Error(), " ") 103 So(str, ShouldContainSubstring, "Did you mean this? list") 104 }) 105 }