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"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2021-12-13 14:23:31 -05:00
|
|
|
zotErrors "zotregistry.io/zot/errors"
|
2020-06-16 20:52:40 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestConfigCmdBasics(t *testing.T) {
|
|
|
|
Convey("Test config help", t, func() {
|
|
|
|
args := []string{"--help"}
|
|
|
|
configPath := makeConfigFile("showspinner = false")
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewConfigCommand()
|
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("showspinner = false")
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewConfigCommand()
|
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 config no args", t, func() {
|
|
|
|
args := []string{}
|
|
|
|
configPath := makeConfigFile("showspinner = false")
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewConfigCommand()
|
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, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigCmdMain(t *testing.T) {
|
|
|
|
Convey("Test add config", t, func() {
|
|
|
|
args := []string{"add", "configtest1", "https://test-url.com"}
|
|
|
|
file := makeConfigFile("")
|
|
|
|
defer os.Remove(file)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewConfigCommand()
|
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)
|
|
|
|
_ = cmd.Execute()
|
|
|
|
|
|
|
|
actual, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
actualStr := string(actual)
|
|
|
|
So(actualStr, ShouldContainSubstring, "configtest1")
|
|
|
|
So(actualStr, ShouldContainSubstring, "https://test-url.com")
|
|
|
|
})
|
|
|
|
|
2022-03-21 12:37:23 -05:00
|
|
|
Convey("Test add config with invalid format", t, func() {
|
|
|
|
args := []string{"--list"}
|
|
|
|
configPath := makeConfigFile(`{"configs":{"_name":"configtest","url":"https://test-url.com","showspinner":false}}`)
|
|
|
|
defer os.Remove(configPath)
|
|
|
|
cmd := NewConfigCommand()
|
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
|
|
|
cmd.SetErr(buff)
|
|
|
|
cmd.SetArgs(args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
So(err, ShouldEqual, zotErrors.ErrCliBadConfig)
|
|
|
|
})
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
Convey("Test add config with invalid URL", t, func() {
|
|
|
|
args := []string{"add", "configtest1", "test..com"}
|
|
|
|
file := makeConfigFile("")
|
|
|
|
defer os.Remove(file)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewConfigCommand()
|
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)
|
|
|
|
So(err, ShouldEqual, zotErrors.ErrInvalidURL)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test fetch all config", t, func() {
|
|
|
|
args := []string{"--list"}
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"configtest","url":"https://test-url.com","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewConfigCommand()
|
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, "https://test-url.com")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("with the shorthand", func() {
|
|
|
|
args := []string{"-l"}
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"configtest","url":"https://test-url.com","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewConfigCommand()
|
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, "https://test-url.com")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("From empty file", func() {
|
|
|
|
args := []string{"-l"}
|
|
|
|
configPath := makeConfigFile(``)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewConfigCommand()
|
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)
|
|
|
|
So(strings.TrimSpace(buff.String()), ShouldEqual, "")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test fetch a config", t, func() {
|
|
|
|
args := []string{"configtest", "--list"}
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"configtest","url":"https://test-url.com","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewConfigCommand()
|
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, "url = https://test-url.com")
|
|
|
|
So(buff.String(), ShouldContainSubstring, "showspinner = false")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("with the shorthand", func() {
|
|
|
|
args := []string{"configtest", "-l"}
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"configtest","url":"https://test-url.com","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewConfigCommand()
|
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, "url = https://test-url.com")
|
|
|
|
So(buff.String(), ShouldContainSubstring, "showspinner = false")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("From empty file", func() {
|
|
|
|
args := []string{"configtest", "-l"}
|
|
|
|
configPath := makeConfigFile(``)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewConfigCommand()
|
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)
|
|
|
|
So(strings.TrimSpace(buff.String()), ShouldEqual, "")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test fetch a config val", t, func() {
|
|
|
|
args := []string{"configtest", "url"}
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"configtest","url":"https://test-url.com","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewConfigCommand()
|
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(), ShouldEqual, "https://test-url.com\n")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("From empty file", func() {
|
|
|
|
args := []string{"configtest", "url"}
|
|
|
|
configPath := makeConfigFile(``)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewConfigCommand()
|
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, "does not exist")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test add a config val", t, func() {
|
|
|
|
args := []string{"configtest", "showspinner", "false"}
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"configtest","url":"https://test-url.com"}]}`)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewConfigCommand()
|
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)
|
|
|
|
|
|
|
|
actual, err := ioutil.ReadFile(configPath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
actualStr := string(actual)
|
|
|
|
So(actualStr, ShouldContainSubstring, "https://test-url.com")
|
|
|
|
So(actualStr, ShouldContainSubstring, `"showspinner":false`)
|
|
|
|
So(buff.String(), ShouldEqual, "")
|
|
|
|
|
|
|
|
Convey("To an empty file", func() {
|
|
|
|
args := []string{"configtest", "showspinner", "false"}
|
|
|
|
configPath := makeConfigFile(``)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewConfigCommand()
|
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)
|
|
|
|
So(buff.String(), ShouldContainSubstring, "does not exist")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test overwrite a config", t, func() {
|
|
|
|
args := []string{"configtest", "url", "https://new-url.com"}
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"configtest","url":"https://test-url.com","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewConfigCommand()
|
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)
|
|
|
|
|
|
|
|
actual, err := ioutil.ReadFile(configPath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
actualStr := string(actual)
|
|
|
|
So(actualStr, ShouldContainSubstring, `https://new-url.com`)
|
|
|
|
So(actualStr, ShouldContainSubstring, `"showspinner":false`)
|
|
|
|
So(actualStr, ShouldNotContainSubstring, `https://test-url.com`)
|
|
|
|
So(buff.String(), ShouldEqual, "")
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test reset a config val", t, func() {
|
|
|
|
args := []string{"configtest", "showspinner", "--reset"}
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"configtest","url":"https://test-url.com","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewConfigCommand()
|
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)
|
|
|
|
|
|
|
|
actual, err := ioutil.ReadFile(configPath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
actualStr := string(actual)
|
|
|
|
So(actualStr, ShouldNotContainSubstring, "showspinner")
|
|
|
|
So(actualStr, ShouldContainSubstring, `"url":"https://test-url.com"`)
|
|
|
|
So(buff.String(), ShouldEqual, "")
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test reset a url", t, func() {
|
|
|
|
args := []string{"configtest", "url", "--reset"}
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"configtest","url":"https://test-url.com","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
2020-07-14 12:11:01 -05:00
|
|
|
cmd := NewConfigCommand()
|
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)
|
|
|
|
So(buff.String(), ShouldContainSubstring, "cannot reset")
|
|
|
|
})
|
2020-10-29 07:47:27 -05:00
|
|
|
|
|
|
|
Convey("Test add a config with an existing saved name", t, func() {
|
|
|
|
args := []string{"add", "configtest", "https://test-url.com/new"}
|
|
|
|
configPath := makeConfigFile(`{"configs":[{"_name":"configtest","url":"https://test-url.com","showspinner":false}]}`)
|
|
|
|
defer os.Remove(configPath)
|
|
|
|
cmd := NewConfigCommand()
|
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(buff)
|
2021-05-21 15:47:28 -05:00
|
|
|
cmd.SetErr(buff)
|
2020-10-29 07:47:27 -05:00
|
|
|
cmd.SetArgs(args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(buff.String(), ShouldContainSubstring, "cli config name already added")
|
|
|
|
})
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|