feat(cli): add sort-by flag to sub commands (#1768)

Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com>
This commit is contained in:
LaurentiuNiculae
2023-09-14 20:51:17 +03:00
committed by GitHub
parent c210e3f377
commit aae8b7b4e3
23 changed files with 714 additions and 98 deletions
+76 -1
View File
@@ -131,7 +131,7 @@ func TestSearchImageCmd(t *testing.T) {
cmd.SetArgs(args)
err := cmd.Execute()
So(err, ShouldNotBeNil)
So(err, ShouldEqual, zerr.ErrInvalidURL)
So(strings.Contains(err.Error(), zerr.ErrInvalidURL.Error()), ShouldBeTrue)
So(buff.String(), ShouldContainSubstring, "invalid URL format")
})
@@ -1357,6 +1357,80 @@ func runDisplayIndexTests(baseURL string) {
})
}
func TestImagesSortFlag(t *testing.T) {
rootDir := t.TempDir()
port := test.GetFreePort()
baseURL := test.GetBaseURL(port)
conf := config.New()
conf.HTTP.Port = port
defaultVal := true
conf.Extensions = &extconf.ExtensionConfig{
Search: &extconf.SearchConfig{
BaseConfig: extconf.BaseConfig{Enable: &defaultVal},
CVE: nil,
},
}
ctlr := api.NewController(conf)
ctlr.Config.Storage.RootDirectory = rootDir
image1 := test.CreateImageWith().DefaultLayers().
ImageConfig(ispec.Image{Created: test.DateRef(2010, 1, 1, 1, 1, 1, 0, time.UTC)}).Build()
image2 := test.CreateImageWith().DefaultLayers().
ImageConfig(ispec.Image{Created: test.DateRef(2020, 1, 1, 1, 1, 1, 0, time.UTC)}).Build()
storeController := test.GetDefaultStoreController(rootDir, ctlr.Log)
err := test.WriteImageToFileSystem(image1, "a-repo", "tag1", storeController)
if err != nil {
t.FailNow()
}
err = test.WriteImageToFileSystem(image2, "b-repo", "tag2", storeController)
if err != nil {
t.FailNow()
}
cm := test.NewControllerManager(ctlr)
cm.StartAndWait(conf.HTTP.Port)
defer cm.StopServer()
Convey("Sorting", t, func() {
args := []string{"list", "--sort-by", "alpha-asc", "--url", baseURL}
cmd := NewImageCommand(new(searchService))
buff := bytes.NewBufferString("")
cmd.SetOut(buff)
cmd.SetErr(buff)
cmd.SetArgs(args)
err := cmd.Execute()
So(err, ShouldBeNil)
str := buff.String()
So(strings.Index(str, "a-repo"), ShouldBeLessThan, strings.Index(str, "b-repo"))
args = []string{"list", "--sort-by", "alpha-dsc", "--url", baseURL}
buff = bytes.NewBufferString("")
cmd.SetOut(buff)
cmd.SetErr(buff)
cmd.SetArgs(args)
err = cmd.Execute()
So(err, ShouldBeNil)
str = buff.String()
So(strings.Index(str, "b-repo"), ShouldBeLessThan, strings.Index(str, "a-repo"))
args = []string{"list", "--sort-by", "update-time", "--url", baseURL}
buff = bytes.NewBufferString("")
cmd.SetOut(buff)
cmd.SetErr(buff)
cmd.SetArgs(args)
err = cmd.Execute()
str = buff.String()
So(err, ShouldBeNil)
So(strings.Index(str, "b-repo"), ShouldBeLessThan, strings.Index(str, "a-repo"))
})
}
func TestImagesCommandGQL(t *testing.T) {
port := test.GetFreePort()
baseURL := test.GetBaseURL(port)
@@ -2620,6 +2694,7 @@ func getTestSearchConfig(url string, searchService SearchService) searchConfig {
return searchConfig{
searchService: searchService,
sortBy: "alpha-asc",
servURL: url,
user: user,
outputFormat: outputFormat,