2023-08-30 12:12:24 -05:00
|
|
|
//go:build search
|
|
|
|
// +build search
|
|
|
|
|
2023-10-03 13:15:39 -05:00
|
|
|
package client_test
|
2023-08-30 12:12:24 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
|
2024-01-31 23:34:07 -05:00
|
|
|
"zotregistry.dev/zot/pkg/api"
|
|
|
|
"zotregistry.dev/zot/pkg/api/config"
|
|
|
|
"zotregistry.dev/zot/pkg/cli/client"
|
|
|
|
test "zotregistry.dev/zot/pkg/test/common"
|
|
|
|
. "zotregistry.dev/zot/pkg/test/image-utils"
|
2023-08-30 12:12:24 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestReposCommand(t *testing.T) {
|
|
|
|
Convey("repos", t, func() {
|
|
|
|
port := test.GetFreePort()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
ctlr.Config.Storage.RootDirectory = t.TempDir()
|
|
|
|
cm := test.NewControllerManager(ctlr)
|
|
|
|
|
|
|
|
cm.StartAndWait(conf.HTTP.Port)
|
|
|
|
defer cm.StopServer()
|
|
|
|
|
2023-09-15 11:53:15 -05:00
|
|
|
err := UploadImage(CreateRandomImage(), baseURL, "repo1", "tag1")
|
2023-09-14 12:51:17 -05:00
|
|
|
So(err, ShouldBeNil)
|
2023-09-15 11:53:15 -05:00
|
|
|
err = UploadImage(CreateRandomImage(), baseURL, "repo2", "tag2")
|
2023-09-14 12:51:17 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-08-30 12:12:24 -05:00
|
|
|
configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"repostest","url":"%s","showspinner":false}]}`,
|
|
|
|
baseURL))
|
|
|
|
defer os.Remove(configPath)
|
|
|
|
|
2023-09-08 07:12:47 -05:00
|
|
|
args := []string{"list", "--config", "repostest"}
|
2023-10-03 13:15:39 -05:00
|
|
|
cmd := client.NewRepoCommand(client.NewSearchService())
|
2023-08-30 12:12:24 -05:00
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
|
|
|
cmd.SetErr(buff)
|
|
|
|
cmd.SetArgs(args)
|
2023-09-14 12:51:17 -05:00
|
|
|
err = cmd.Execute()
|
2023-08-30 12:12:24 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
space := regexp.MustCompile(`\s+`)
|
|
|
|
str := space.ReplaceAllString(buff.String(), " ")
|
|
|
|
actual := strings.TrimSpace(str)
|
|
|
|
So(actual, ShouldContainSubstring, "repo1")
|
|
|
|
So(actual, ShouldContainSubstring, "repo2")
|
2023-09-14 12:51:17 -05:00
|
|
|
|
|
|
|
args = []string{"list", "--sort-by", "alpha-dsc", "--config", "repostest"}
|
2023-10-03 13:15:39 -05:00
|
|
|
cmd = client.NewRepoCommand(client.NewSearchService())
|
2023-09-14 12:51:17 -05:00
|
|
|
buff = bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
|
|
|
cmd.SetErr(buff)
|
|
|
|
cmd.SetArgs(args)
|
|
|
|
err = cmd.Execute()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
space = regexp.MustCompile(`\s+`)
|
|
|
|
str = space.ReplaceAllString(buff.String(), " ")
|
|
|
|
actual = strings.TrimSpace(str)
|
|
|
|
So(actual, ShouldContainSubstring, "repo1")
|
|
|
|
So(actual, ShouldContainSubstring, "repo2")
|
|
|
|
So(strings.Index(actual, "repo2"), ShouldBeLessThan, strings.Index(actual, "repo1"))
|
|
|
|
|
|
|
|
args = []string{"list", "--sort-by", "alpha-asc", "--config", "repostest"}
|
2023-10-03 13:15:39 -05:00
|
|
|
cmd = client.NewRepoCommand(client.NewSearchService())
|
2023-09-14 12:51:17 -05:00
|
|
|
buff = bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
|
|
|
cmd.SetErr(buff)
|
|
|
|
cmd.SetArgs(args)
|
|
|
|
err = cmd.Execute()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
space = regexp.MustCompile(`\s+`)
|
|
|
|
str = space.ReplaceAllString(buff.String(), " ")
|
|
|
|
actual = strings.TrimSpace(str)
|
|
|
|
So(actual, ShouldContainSubstring, "repo1")
|
|
|
|
So(actual, ShouldContainSubstring, "repo2")
|
|
|
|
So(strings.Index(actual, "repo1"), ShouldBeLessThan, strings.Index(actual, "repo2"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSuggestions(t *testing.T) {
|
|
|
|
Convey("Suggestions", t, func() {
|
|
|
|
space := regexp.MustCompile(`\s+`)
|
2023-10-03 13:15:39 -05:00
|
|
|
suggestion := client.ShowSuggestionsIfUnknownCommand(
|
|
|
|
client.NewRepoCommand(client.NewSearchService()), []string{"bad-command"})
|
2023-09-14 12:51:17 -05:00
|
|
|
str := space.ReplaceAllString(suggestion.Error(), " ")
|
2023-12-08 03:05:02 -05:00
|
|
|
So(str, ShouldContainSubstring, "unknown cli subcommand")
|
2023-09-14 12:51:17 -05:00
|
|
|
|
2023-10-03 13:15:39 -05:00
|
|
|
suggestion = client.ShowSuggestionsIfUnknownCommand(
|
|
|
|
client.NewRepoCommand(client.NewSearchService()), []string{"listt"})
|
2023-09-14 12:51:17 -05:00
|
|
|
str = space.ReplaceAllString(suggestion.Error(), " ")
|
|
|
|
So(str, ShouldContainSubstring, "Did you mean this? list")
|
2023-08-30 12:12:24 -05:00
|
|
|
})
|
|
|
|
}
|