2021-12-03 22:50:58 -05:00
|
|
|
//go:build extended
|
2020-10-14 16:47:20 -05:00
|
|
|
// +build extended
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
package cli //nolint:testpackage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-07-14 12:11:01 -05:00
|
|
|
"path"
|
2020-06-16 20:52:40 -05:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
godigest "github.com/opencontainers/go-digest"
|
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2021-11-10 09:31:03 -05:00
|
|
|
"gopkg.in/resty.v1"
|
2021-12-03 22:50:58 -05:00
|
|
|
zotErrors "zotregistry.io/zot/errors"
|
|
|
|
"zotregistry.io/zot/pkg/api"
|
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
|
|
|
extconf "zotregistry.io/zot/pkg/extensions/config"
|
2022-01-19 14:54:17 -05:00
|
|
|
"zotregistry.io/zot/pkg/test"
|
2020-06-16 20:52:40 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSearchImageCmd(t *testing.T) {
|
|
|
|
Convey("Test image help", t, func() {
|
|
|
|
args := []string{"--help"}
|
|
|
|
configPath := makeConfigFile("")
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewImageCommand(new(mockService))
|
2020-06-16 20:52:40 -05:00
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
2021-05-21 15:47:28 -05:00
|
|
|
cmd.SetErr(buff)
|
2020-06-16 20:52:40 -05:00
|
|
|
cmd.SetArgs(args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
So(buff.String(), ShouldContainSubstring, "Usage")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
Convey("with the shorthand", func() {
|
|
|
|
args[0] = "-h"
|
|
|
|
configPath := makeConfigFile("")
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewImageCommand(new(mockService))
|
2020-06-16 20:52:40 -05:00
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
2021-05-21 15:47:28 -05:00
|
|
|
cmd.SetErr(buff)
|
2020-06-16 20:52:40 -05:00
|
|
|
cmd.SetArgs(args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
So(buff.String(), ShouldContainSubstring, "Usage")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
Convey("Test image no url", t, func() {
|
|
|
|
args := []string{"imagetest", "--name", "dummyIdRandom"}
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"imagetest","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewImageCommand(new(mockService))
|
2020-06-16 20:52:40 -05:00
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
2021-05-21 15:47:28 -05:00
|
|
|
cmd.SetErr(buff)
|
2020-06-16 20:52:40 -05:00
|
|
|
cmd.SetArgs(args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
So(err, ShouldNotBeNil)
|
2020-07-06 17:44:32 -05:00
|
|
|
So(err, ShouldEqual, zotErrors.ErrNoURLProvided)
|
2020-06-16 20:52:40 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test image no params", t, func() {
|
|
|
|
args := []string{"imagetest", "--url", "someUrl"}
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"imagetest","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewImageCommand(new(mockService))
|
2020-06-16 20:52:40 -05:00
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
2021-05-21 15:47:28 -05:00
|
|
|
cmd.SetErr(buff)
|
2020-06-16 20:52:40 -05:00
|
|
|
cmd.SetArgs(args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test image invalid url", t, func() {
|
|
|
|
args := []string{"imagetest", "--name", "dummyImageName", "--url", "invalidUrl"}
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"imagetest","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewImageCommand(new(searchService))
|
2020-06-16 20:52:40 -05:00
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
|
|
|
cmd.SetErr(buff)
|
|
|
|
cmd.SetArgs(args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(err, ShouldEqual, zotErrors.ErrInvalidURL)
|
|
|
|
So(buff.String(), ShouldContainSubstring, "invalid URL format")
|
|
|
|
})
|
|
|
|
Convey("Test image invalid url port", t, func() {
|
|
|
|
args := []string{"imagetest", "--name", "dummyImageName", "--url", "http://localhost:99999"}
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"imagetest","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewImageCommand(new(searchService))
|
2020-06-16 20:52:40 -05:00
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
|
|
|
cmd.SetErr(buff)
|
|
|
|
cmd.SetArgs(args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(buff.String(), ShouldContainSubstring, "invalid port")
|
|
|
|
|
|
|
|
Convey("without flags", func() {
|
|
|
|
args := []string{"imagetest", "--url", "http://localhost:99999"}
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"imagetest","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewImageCommand(new(searchService))
|
2020-06-16 20:52:40 -05:00
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
|
|
|
cmd.SetErr(buff)
|
|
|
|
cmd.SetArgs(args)
|
|
|
|
err = cmd.Execute()
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(buff.String(), ShouldContainSubstring, "invalid port")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
Convey("Test image unreachable", t, func() {
|
|
|
|
args := []string{"imagetest", "--name", "dummyImageName", "--url", "http://localhost:9999"}
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"imagetest","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewImageCommand(new(searchService))
|
2020-06-16 20:52:40 -05:00
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
|
|
|
cmd.SetErr(buff)
|
|
|
|
cmd.SetArgs(args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test image url from config", t, func() {
|
|
|
|
args := []string{"imagetest", "--name", "dummyImageName"}
|
|
|
|
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"imagetest","url":"https://test-url.com","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
|
|
|
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewImageCommand(new(mockService))
|
2020-06-16 20:52:40 -05:00
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
2021-05-21 15:47:28 -05:00
|
|
|
cmd.SetErr(buff)
|
2020-06-16 20:52:40 -05:00
|
|
|
cmd.SetArgs(args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
space := regexp.MustCompile(`\s+`)
|
|
|
|
str := space.ReplaceAllString(buff.String(), " ")
|
|
|
|
So(strings.TrimSpace(str), ShouldEqual, "IMAGE NAME TAG DIGEST SIZE dummyImageName tag DigestsA 123kB")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test image by name", t, func() {
|
|
|
|
args := []string{"imagetest", "--name", "dummyImageName", "--url", "someUrlImage"}
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"imagetest","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
imageCmd := NewImageCommand(new(mockService))
|
2020-06-16 20:52:40 -05:00
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
imageCmd.SetOut(buff)
|
2021-05-21 15:47:28 -05:00
|
|
|
imageCmd.SetErr(buff)
|
2020-06-16 20:52:40 -05:00
|
|
|
imageCmd.SetArgs(args)
|
|
|
|
err := imageCmd.Execute()
|
|
|
|
space := regexp.MustCompile(`\s+`)
|
|
|
|
str := space.ReplaceAllString(buff.String(), " ")
|
|
|
|
So(strings.TrimSpace(str), ShouldEqual, "IMAGE NAME TAG DIGEST SIZE dummyImageName tag DigestsA 123kB")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
Convey("using shorthand", func() {
|
|
|
|
args := []string{"imagetest", "-n", "dummyImageName", "--url", "someUrlImage"}
|
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"imagetest","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
imageCmd := NewImageCommand(new(mockService))
|
2020-06-16 20:52:40 -05:00
|
|
|
imageCmd.SetOut(buff)
|
2021-05-21 15:47:28 -05:00
|
|
|
imageCmd.SetErr(buff)
|
2020-06-16 20:52:40 -05:00
|
|
|
imageCmd.SetArgs(args)
|
|
|
|
err := imageCmd.Execute()
|
|
|
|
|
|
|
|
space := regexp.MustCompile(`\s+`)
|
|
|
|
str := space.ReplaceAllString(buff.String(), " ")
|
|
|
|
So(strings.TrimSpace(str), ShouldEqual, "IMAGE NAME TAG DIGEST SIZE dummyImageName tag DigestsA 123kB")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOutputFormat(t *testing.T) {
|
|
|
|
Convey("Test text", t, func() {
|
|
|
|
args := []string{"imagetest", "--name", "dummyImageName", "-o", "text"}
|
|
|
|
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"imagetest","url":"https://test-url.com","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
|
|
|
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewImageCommand(new(mockService))
|
2020-06-16 20:52:40 -05:00
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
2021-05-21 15:47:28 -05:00
|
|
|
cmd.SetErr(buff)
|
2020-06-16 20:52:40 -05:00
|
|
|
cmd.SetArgs(args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
space := regexp.MustCompile(`\s+`)
|
|
|
|
str := space.ReplaceAllString(buff.String(), " ")
|
|
|
|
So(strings.TrimSpace(str), ShouldEqual, "IMAGE NAME TAG DIGEST SIZE dummyImageName tag DigestsA 123kB")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test json", t, func() {
|
|
|
|
args := []string{"imagetest", "--name", "dummyImageName", "-o", "json"}
|
|
|
|
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"imagetest","url":"https://test-url.com","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
|
|
|
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewImageCommand(new(mockService))
|
2020-06-16 20:52:40 -05:00
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
2021-05-21 15:47:28 -05:00
|
|
|
cmd.SetErr(buff)
|
2020-06-16 20:52:40 -05:00
|
|
|
cmd.SetArgs(args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
space := regexp.MustCompile(`\s+`)
|
|
|
|
str := space.ReplaceAllString(buff.String(), " ")
|
|
|
|
So(strings.TrimSpace(str), ShouldEqual, `{ "name": "dummyImageName", "tags": [ { "name":`+
|
2021-05-28 11:27:17 -05:00
|
|
|
` "tag", "size": 123445, "digest": "DigestsAreReallyLong", "configDigest": "", "layerDigests": null } ] }`)
|
2020-06-16 20:52:40 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test yaml", t, func() {
|
|
|
|
args := []string{"imagetest", "--name", "dummyImageName", "-o", "yaml"}
|
|
|
|
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"imagetest","url":"https://test-url.com","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
|
|
|
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewImageCommand(new(mockService))
|
2020-06-16 20:52:40 -05:00
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
2021-05-21 15:47:28 -05:00
|
|
|
cmd.SetErr(buff)
|
2020-06-16 20:52:40 -05:00
|
|
|
cmd.SetArgs(args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
space := regexp.MustCompile(`\s+`)
|
|
|
|
str := space.ReplaceAllString(buff.String(), " ")
|
|
|
|
So(strings.TrimSpace(str), ShouldEqual, `name: dummyImageName tags: -`+
|
2021-05-28 11:27:17 -05:00
|
|
|
` name: tag size: 123445 digest: DigestsAreReallyLong configdigest: "" layers: []`)
|
2020-06-16 20:52:40 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("Test yml", func() {
|
|
|
|
args := []string{"imagetest", "--name", "dummyImageName", "-o", "yml"}
|
|
|
|
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"imagetest","url":"https://test-url.com","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
|
|
|
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewImageCommand(new(mockService))
|
2020-06-16 20:52:40 -05:00
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
2021-05-21 15:47:28 -05:00
|
|
|
cmd.SetErr(buff)
|
2020-06-16 20:52:40 -05:00
|
|
|
cmd.SetArgs(args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
space := regexp.MustCompile(`\s+`)
|
|
|
|
str := space.ReplaceAllString(buff.String(), " ")
|
2021-05-28 11:27:17 -05:00
|
|
|
So(strings.TrimSpace(str), ShouldEqual, `name: dummyImageName tags: -`+
|
|
|
|
` name: tag size: 123445 digest: DigestsAreReallyLong configdigest: "" layers: []`)
|
2020-06-16 20:52:40 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test invalid", t, func() {
|
|
|
|
args := []string{"imagetest", "--name", "dummyImageName", "-o", "random"}
|
|
|
|
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"imagetest","url":"https://test-url.com","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
|
|
|
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewImageCommand(new(mockService))
|
2020-06-16 20:52:40 -05:00
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
|
|
|
cmd.SetErr(buff)
|
|
|
|
cmd.SetArgs(args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(buff.String(), ShouldContainSubstring, "invalid output format")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServerResponse(t *testing.T) {
|
|
|
|
Convey("Test from real server", t, func() {
|
2022-01-19 14:54:17 -05:00
|
|
|
port := test.GetFreePort()
|
|
|
|
url := test.GetBaseURL(port)
|
2021-06-08 15:11:18 -05:00
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
2021-12-28 08:29:30 -05:00
|
|
|
defaultVal := true
|
2021-06-08 15:11:18 -05:00
|
|
|
conf.Extensions = &extconf.ExtensionConfig{
|
2021-12-28 08:29:30 -05:00
|
|
|
Search: &extconf.SearchConfig{Enable: &defaultVal},
|
2021-05-26 12:22:31 -05:00
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
ctlr := api.NewController(conf)
|
2022-03-07 03:55:12 -05:00
|
|
|
ctlr.Config.Storage.RootDirectory = t.TempDir()
|
2020-06-16 20:52:40 -05:00
|
|
|
go func(controller *api.Controller) {
|
|
|
|
// this blocks
|
|
|
|
if err := controller.Run(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
}(ctlr)
|
2020-06-16 20:52:40 -05:00
|
|
|
// wait till ready
|
|
|
|
for {
|
|
|
|
_, err := resty.R().Get(url)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
defer func(controller *api.Controller) {
|
|
|
|
ctx := context.Background()
|
|
|
|
_ = controller.Server.Shutdown(ctx)
|
2021-12-13 14:23:31 -05:00
|
|
|
}(ctlr)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
uploadManifest(url)
|
|
|
|
|
|
|
|
Convey("Test all images config url", func() {
|
|
|
|
args := []string{"imagetest"}
|
|
|
|
configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s","showspinner":false}]}`, url))
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewImageCommand(new(searchService))
|
2020-06-16 20:52:40 -05:00
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
|
|
|
cmd.SetErr(buff)
|
|
|
|
cmd.SetArgs(args)
|
2022-03-07 03:55:12 -05:00
|
|
|
err := cmd.Execute()
|
2020-06-16 20:52:40 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
space := regexp.MustCompile(`\s+`)
|
|
|
|
str := space.ReplaceAllString(buff.String(), " ")
|
|
|
|
actual := strings.TrimSpace(str)
|
|
|
|
So(actual, ShouldContainSubstring, "IMAGE NAME TAG DIGEST SIZE")
|
2022-01-19 14:54:17 -05:00
|
|
|
So(actual, ShouldContainSubstring, "repo7 test:2.0 883fc0c5 15B")
|
|
|
|
So(actual, ShouldContainSubstring, "repo7 test:1.0 883fc0c5 15B")
|
2020-06-16 20:52:40 -05:00
|
|
|
})
|
|
|
|
|
2021-05-28 11:27:17 -05:00
|
|
|
Convey("Test all images verbose", func() {
|
|
|
|
args := []string{"imagetest", "--verbose"}
|
|
|
|
configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s","showspinner":false}]}`, url))
|
|
|
|
defer os.Remove(configPath)
|
|
|
|
cmd := NewImageCommand(new(searchService))
|
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
|
|
|
cmd.SetErr(buff)
|
|
|
|
cmd.SetArgs(args)
|
2022-03-07 03:55:12 -05:00
|
|
|
err := cmd.Execute()
|
2021-05-28 11:27:17 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
space := regexp.MustCompile(`\s+`)
|
|
|
|
str := space.ReplaceAllString(buff.String(), " ")
|
|
|
|
actual := strings.TrimSpace(str)
|
|
|
|
// Actual cli output should be something similar to (order of images may differ):
|
|
|
|
// IMAGE NAME TAG DIGEST CONFIG LAYERS SIZE
|
|
|
|
// repo7 test:2.0 a0ca253b b8781e88 15B
|
|
|
|
// b8781e88 15B
|
|
|
|
// repo7 test:1.0 a0ca253b b8781e88 15B
|
|
|
|
// b8781e88 15B
|
|
|
|
So(actual, ShouldContainSubstring, "IMAGE NAME TAG DIGEST CONFIG LAYERS SIZE")
|
2022-01-19 14:54:17 -05:00
|
|
|
So(actual, ShouldContainSubstring, "repo7 test:2.0 883fc0c5 3a1d2d0c 15B b8781e88 15B")
|
|
|
|
So(actual, ShouldContainSubstring, "repo7 test:1.0 883fc0c5 3a1d2d0c 15B b8781e88 15B")
|
2021-05-28 11:27:17 -05:00
|
|
|
})
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
Convey("Test image by name config url", func() {
|
|
|
|
args := []string{"imagetest", "--name", "repo7"}
|
|
|
|
configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s","showspinner":false}]}`, url))
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewImageCommand(new(searchService))
|
2020-06-16 20:52:40 -05:00
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
|
|
|
cmd.SetErr(buff)
|
|
|
|
cmd.SetArgs(args)
|
2022-03-07 03:55:12 -05:00
|
|
|
err := cmd.Execute()
|
2020-06-16 20:52:40 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
space := regexp.MustCompile(`\s+`)
|
|
|
|
str := space.ReplaceAllString(buff.String(), " ")
|
|
|
|
actual := strings.TrimSpace(str)
|
|
|
|
So(actual, ShouldContainSubstring, "IMAGE NAME TAG DIGEST SIZE")
|
2022-01-19 14:54:17 -05:00
|
|
|
So(actual, ShouldContainSubstring, "repo7 test:2.0 883fc0c5 15B")
|
|
|
|
So(actual, ShouldContainSubstring, "repo7 test:1.0 883fc0c5 15B")
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
Convey("with shorthand", func() {
|
|
|
|
args := []string{"imagetest", "-n", "repo7"}
|
|
|
|
configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s","showspinner":false}]}`, url))
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewImageCommand(new(searchService))
|
2020-06-16 20:52:40 -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, "IMAGE NAME TAG DIGEST SIZE")
|
2022-01-19 14:54:17 -05:00
|
|
|
So(actual, ShouldContainSubstring, "repo7 test:2.0 883fc0c5 15B")
|
|
|
|
So(actual, ShouldContainSubstring, "repo7 test:1.0 883fc0c5 15B")
|
2020-06-16 20:52:40 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-05-26 12:22:31 -05:00
|
|
|
Convey("Test image by digest", func() {
|
2022-01-19 14:54:17 -05:00
|
|
|
args := []string{"imagetest", "--digest", "883fc0c5"}
|
2021-05-26 12:22:31 -05:00
|
|
|
configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s","showspinner":false}]}`, url))
|
|
|
|
defer os.Remove(configPath)
|
|
|
|
cmd := NewImageCommand(new(searchService))
|
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
|
|
|
cmd.SetErr(buff)
|
|
|
|
cmd.SetArgs(args)
|
2022-03-07 03:55:12 -05:00
|
|
|
err := cmd.Execute()
|
2021-05-26 12:22:31 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
space := regexp.MustCompile(`\s+`)
|
|
|
|
str := space.ReplaceAllString(buff.String(), " ")
|
|
|
|
actual := strings.TrimSpace(str)
|
|
|
|
// Actual cli output should be something similar to (order of images may differ):
|
|
|
|
// IMAGE NAME TAG DIGEST SIZE
|
|
|
|
// repo7 test:2.0 a0ca253b 15B
|
|
|
|
// repo7 test:1.0 a0ca253b 15B
|
|
|
|
So(actual, ShouldContainSubstring, "IMAGE NAME TAG DIGEST SIZE")
|
2022-01-19 14:54:17 -05:00
|
|
|
So(actual, ShouldContainSubstring, "repo7 test:2.0 883fc0c5 15B")
|
|
|
|
So(actual, ShouldContainSubstring, "repo7 test:1.0 883fc0c5 15B")
|
2021-05-26 12:22:31 -05:00
|
|
|
Convey("with shorthand", func() {
|
2022-01-19 14:54:17 -05:00
|
|
|
args := []string{"imagetest", "-d", "883fc0c5"}
|
2021-05-26 12:22:31 -05:00
|
|
|
configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s","showspinner":false}]}`, url))
|
|
|
|
defer os.Remove(configPath)
|
|
|
|
cmd := NewImageCommand(new(searchService))
|
|
|
|
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, "IMAGE NAME TAG DIGEST SIZE")
|
2022-01-19 14:54:17 -05:00
|
|
|
So(actual, ShouldContainSubstring, "repo7 test:2.0 883fc0c5 15B")
|
|
|
|
So(actual, ShouldContainSubstring, "repo7 test:1.0 883fc0c5 15B")
|
2021-05-26 12:22:31 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
Convey("Test image by name invalid name", func() {
|
|
|
|
args := []string{"imagetest", "--name", "repo777"}
|
|
|
|
configPath := makeConfigFile(fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s","showspinner":false}]}`, url))
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewImageCommand(new(searchService))
|
2020-06-16 20:52:40 -05:00
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
|
|
|
cmd.SetErr(buff)
|
|
|
|
cmd.SetArgs(args)
|
2022-03-07 03:55:12 -05:00
|
|
|
err := cmd.Execute()
|
2020-06-16 20:52:40 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
actual := buff.String()
|
|
|
|
So(actual, ShouldContainSubstring, "unknown")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func uploadManifest(url string) {
|
|
|
|
// create a blob/layer
|
|
|
|
resp, _ := resty.R().Post(url + "/v2/repo7/blobs/uploads/")
|
2022-01-19 14:54:17 -05:00
|
|
|
loc := test.Location(url, resp)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
content := []byte("this is a blob5")
|
|
|
|
digest := godigest.FromBytes(content)
|
|
|
|
_, _ = resty.R().SetQueryParam("digest", digest.String()).
|
|
|
|
SetHeader("Content-Type", "application/octet-stream").SetBody(content).Put(loc)
|
|
|
|
|
2022-01-19 14:54:17 -05:00
|
|
|
// upload image config blob
|
|
|
|
resp, _ = resty.R().Post(url + "/v2/repo7/blobs/uploads/")
|
|
|
|
loc = test.Location(url, resp)
|
|
|
|
cblob, cdigest := test.GetImageConfig()
|
|
|
|
|
|
|
|
_, _ = resty.R().
|
|
|
|
SetContentLength(true).
|
|
|
|
SetHeader("Content-Length", fmt.Sprintf("%d", len(cblob))).
|
|
|
|
SetHeader("Content-Type", "application/octet-stream").
|
|
|
|
SetQueryParam("digest", cdigest.String()).
|
|
|
|
SetBody(cblob).
|
|
|
|
Put(loc)
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
// create a manifest
|
2021-12-13 14:23:31 -05:00
|
|
|
manifest := ispec.Manifest{
|
2020-06-16 20:52:40 -05:00
|
|
|
Config: ispec.Descriptor{
|
2022-01-19 14:54:17 -05:00
|
|
|
MediaType: "application/vnd.oci.image.config.v1+json",
|
|
|
|
Digest: cdigest,
|
|
|
|
Size: int64(len(cblob)),
|
2020-06-16 20:52:40 -05:00
|
|
|
},
|
|
|
|
Layers: []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
MediaType: "application/vnd.oci.image.layer.v1.tar",
|
|
|
|
Digest: digest,
|
|
|
|
Size: int64(len(content)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
manifest.SchemaVersion = 2
|
|
|
|
content, _ = json.Marshal(manifest)
|
2020-06-16 20:52:40 -05:00
|
|
|
_, _ = resty.R().SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json").
|
|
|
|
SetBody(content).Put(url + "/v2/repo7/manifests/test:1.0")
|
|
|
|
|
|
|
|
content = []byte("this is a blob5")
|
|
|
|
digest = godigest.FromBytes(content)
|
|
|
|
// create a manifest with same blob but a different tag
|
2021-12-13 14:23:31 -05:00
|
|
|
manifest = ispec.Manifest{
|
2020-06-16 20:52:40 -05:00
|
|
|
Config: ispec.Descriptor{
|
2022-01-19 14:54:17 -05:00
|
|
|
MediaType: "application/vnd.oci.image.config.v1+json",
|
|
|
|
Digest: cdigest,
|
|
|
|
Size: int64(len(cblob)),
|
2020-06-16 20:52:40 -05:00
|
|
|
},
|
|
|
|
Layers: []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
MediaType: "application/vnd.oci.image.layer.v1.tar",
|
|
|
|
Digest: digest,
|
|
|
|
Size: int64(len(content)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
manifest.SchemaVersion = 2
|
|
|
|
content, _ = json.Marshal(manifest)
|
2020-06-16 20:52:40 -05:00
|
|
|
_, _ = resty.R().SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json").
|
|
|
|
SetBody(content).Put(url + "/v2/repo7/manifests/test:2.0")
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockService struct{}
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
func (service mockService) getAllImages(ctx context.Context, config searchConfig, username, password string,
|
2021-12-13 14:23:31 -05:00
|
|
|
channel chan stringResult, wtgrp *sync.WaitGroup) {
|
|
|
|
defer wtgrp.Done()
|
2020-07-06 17:44:32 -05:00
|
|
|
defer close(channel)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
image := &imageStruct{}
|
|
|
|
image.Name = "randomimageName"
|
|
|
|
image.Tags = []tags{
|
|
|
|
{
|
|
|
|
Name: "tag",
|
|
|
|
Digest: "DigestsAreReallyLong",
|
|
|
|
Size: 123445,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
str, err := image.string(*config.outputFormat)
|
2020-06-16 20:52:40 -05:00
|
|
|
if err != nil {
|
2020-07-06 17:44:32 -05:00
|
|
|
channel <- stringResult{"", err}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
channel <- stringResult{str, nil}
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
func (service mockService) getImageByName(ctx context.Context, config searchConfig,
|
2021-12-13 14:23:31 -05:00
|
|
|
username, password, imageName string, channel chan stringResult, wtgrp *sync.WaitGroup) {
|
|
|
|
defer wtgrp.Done()
|
2020-07-06 17:44:32 -05:00
|
|
|
defer close(channel)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
image := &imageStruct{}
|
|
|
|
image.Name = imageName
|
|
|
|
image.Tags = []tags{
|
|
|
|
{
|
|
|
|
Name: "tag",
|
|
|
|
Digest: "DigestsAreReallyLong",
|
|
|
|
Size: 123445,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
str, err := image.string(*config.outputFormat)
|
2020-06-16 20:52:40 -05:00
|
|
|
if err != nil {
|
2020-07-06 17:44:32 -05:00
|
|
|
channel <- stringResult{"", err}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
channel <- stringResult{str, nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (service mockService) getCveByImage(ctx context.Context, config searchConfig, username, password,
|
2021-12-13 14:23:31 -05:00
|
|
|
imageName string, rch chan stringResult, wtgrp *sync.WaitGroup) {
|
|
|
|
defer wtgrp.Done()
|
|
|
|
defer close(rch)
|
2020-07-06 17:44:32 -05:00
|
|
|
|
|
|
|
cveRes := &cveResult{}
|
|
|
|
cveRes.Data = cveData{
|
|
|
|
CVEListForImage: cveListForImage{
|
|
|
|
Tag: imageName,
|
|
|
|
CVEList: []cve{
|
|
|
|
{
|
|
|
|
ID: "dummyCVEID",
|
|
|
|
Description: "Description of the CVE",
|
|
|
|
Title: "Title of that CVE",
|
|
|
|
Severity: "HIGH",
|
|
|
|
PackageList: []packageList{
|
|
|
|
{
|
|
|
|
Name: "packagename",
|
|
|
|
FixedVersion: "fixedver",
|
|
|
|
InstalledVersion: "installedver",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
str, err := cveRes.string(*config.outputFormat)
|
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
rch <- stringResult{"", err}
|
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
rch <- stringResult{str, nil}
|
2020-07-06 17:44:32 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
func (service mockService) getImagesByCveID(ctx context.Context, config searchConfig, username, password, cvid string,
|
|
|
|
rch chan stringResult, wtgrp *sync.WaitGroup) {
|
|
|
|
service.getImageByName(ctx, config, username, password, "anImage", rch, wtgrp)
|
2020-07-06 17:44:32 -05:00
|
|
|
}
|
|
|
|
|
2021-05-26 12:22:31 -05:00
|
|
|
func (service mockService) getImagesByDigest(ctx context.Context, config searchConfig, username,
|
2021-12-13 14:23:31 -05:00
|
|
|
password, digest string, rch chan stringResult, wtgrp *sync.WaitGroup) {
|
|
|
|
service.getImageByName(ctx, config, username, password, "anImage", rch, wtgrp)
|
2021-05-26 12:22:31 -05:00
|
|
|
}
|
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
func (service mockService) getImageByNameAndCVEID(ctx context.Context, config searchConfig, username,
|
2021-12-13 14:23:31 -05:00
|
|
|
password, imageName, cvid string, rch chan stringResult, wtgrp *sync.WaitGroup) {
|
|
|
|
service.getImageByName(ctx, config, username, password, imageName, rch, wtgrp)
|
2020-07-06 17:44:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (service mockService) getFixedTagsForCVE(ctx context.Context, config searchConfig,
|
2021-12-13 14:23:31 -05:00
|
|
|
username, password, imageName, cvid string, rch chan stringResult, wtgrp *sync.WaitGroup) {
|
|
|
|
service.getImageByName(ctx, config, username, password, imageName, rch, wtgrp)
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func makeConfigFile(content string) string {
|
2020-07-14 12:11:01 -05:00
|
|
|
os.Setenv("HOME", os.TempDir())
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
home, err := os.UserHomeDir()
|
2020-06-16 20:52:40 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-07-14 12:11:01 -05:00
|
|
|
configPath := path.Join(home + "/.zot")
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
if err := ioutil.WriteFile(configPath, []byte(content), 0o600); err != nil {
|
2020-06-16 20:52:40 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-07-14 12:11:01 -05:00
|
|
|
return configPath
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|