2019-06-20 18:36:40 -05:00
|
|
|
package cli_test
|
|
|
|
|
|
|
|
import (
|
2019-09-17 17:45:28 -05:00
|
|
|
"io/ioutil"
|
2019-06-20 18:36:40 -05:00
|
|
|
"os"
|
2020-06-16 20:52:40 -05:00
|
|
|
"path"
|
2019-06-20 18:36:40 -05:00
|
|
|
"testing"
|
|
|
|
|
2021-05-13 13:59:12 -05:00
|
|
|
"github.com/anuvu/zot/pkg/api"
|
2019-06-20 18:36:40 -05:00
|
|
|
"github.com/anuvu/zot/pkg/cli"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2021-05-13 13:59:12 -05:00
|
|
|
"github.com/spf13/viper"
|
2019-06-20 18:36:40 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUsage(t *testing.T) {
|
|
|
|
oldArgs := os.Args
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
defer func() { os.Args = oldArgs }()
|
|
|
|
|
2019-09-17 17:45:28 -05:00
|
|
|
Convey("Test usage", t, func(c C) {
|
2019-06-20 18:36:40 -05:00
|
|
|
os.Args = []string{"cli_test", "help"}
|
2020-07-14 12:11:01 -05:00
|
|
|
err := cli.NewRootCmd().Execute()
|
2019-06-20 18:36:40 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
2019-09-17 17:45:28 -05:00
|
|
|
|
|
|
|
Convey("Test version", t, func(c C) {
|
|
|
|
os.Args = []string{"cli_test", "--version"}
|
2020-07-14 12:11:01 -05:00
|
|
|
err := cli.NewRootCmd().Execute()
|
2019-09-17 17:45:28 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServe(t *testing.T) {
|
|
|
|
oldArgs := os.Args
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
defer func() { os.Args = oldArgs }()
|
|
|
|
|
2019-09-17 17:45:28 -05:00
|
|
|
Convey("Test serve help", t, func(c C) {
|
2019-06-20 18:36:40 -05:00
|
|
|
os.Args = []string{"cli_test", "serve", "-h"}
|
2020-07-14 12:11:01 -05:00
|
|
|
err := cli.NewRootCmd().Execute()
|
2019-06-20 18:36:40 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
2019-09-17 17:45:28 -05:00
|
|
|
|
|
|
|
Convey("Test serve config", t, func(c C) {
|
|
|
|
Convey("unknown config", func(c C) {
|
2020-06-16 20:52:40 -05:00
|
|
|
os.Args = []string{"cli_test", "serve", path.Join(os.TempDir(), "/x")}
|
2020-07-14 12:11:01 -05:00
|
|
|
So(func() { _ = cli.NewRootCmd().Execute() }, ShouldPanic)
|
2019-09-17 17:45:28 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("non-existent config", func(c C) {
|
2020-06-16 20:52:40 -05:00
|
|
|
os.Args = []string{"cli_test", "serve", path.Join(os.TempDir(), "/x.yaml")}
|
2020-07-14 12:11:01 -05:00
|
|
|
So(func() { _ = cli.NewRootCmd().Execute() }, ShouldPanic)
|
2019-09-17 17:45:28 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("bad config", func(c C) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "zot-test*.json")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(tmpfile.Name()) // clean up
|
|
|
|
content := []byte(`{"log":{}}`)
|
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "serve", tmpfile.Name()}
|
2020-07-14 12:11:01 -05:00
|
|
|
So(func() { _ = cli.NewRootCmd().Execute() }, ShouldPanic)
|
2019-09-17 17:45:28 -05:00
|
|
|
})
|
|
|
|
})
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
2021-05-13 13:59:12 -05:00
|
|
|
func TestVerify(t *testing.T) {
|
|
|
|
oldArgs := os.Args
|
|
|
|
|
|
|
|
defer func() { os.Args = oldArgs }()
|
|
|
|
|
|
|
|
Convey("Test verify bad config", t, func(c C) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "zot-test*.json")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(tmpfile.Name()) // clean up
|
|
|
|
content := []byte(`{"log":{}}`)
|
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
|
|
|
So(func() { _ = cli.NewRootCmd().Execute() }, ShouldPanic)
|
|
|
|
})
|
|
|
|
|
2021-09-01 04:15:00 -05:00
|
|
|
Convey("Test verify w/ authorization and w/o authentication", t, func(c C) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "zot-test*.json")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(tmpfile.Name()) // clean up
|
|
|
|
content := []byte(`{"storage":{"rootDirectory":"/tmp/zot"},
|
|
|
|
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
|
|
|
|
"accessControl":{"adminPolicy":{"users":["admin"],
|
|
|
|
"actions":["read","create","update","delete"]}}}}`)
|
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
|
|
|
So(func() { _ = cli.NewRootCmd().Execute() }, ShouldPanic)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test verify w/ authorization and w/ authentication", t, func(c C) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "zot-test*.json")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(tmpfile.Name()) // clean up
|
|
|
|
content := []byte(`{"storage":{"rootDirectory":"/tmp/zot"},
|
|
|
|
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
|
|
|
|
"auth":{"htpasswd":{"path":"test/data/htpasswd"},"failDelay":1},
|
|
|
|
"accessControl":{"adminPolicy":{"users":["admin"],
|
|
|
|
"actions":["read","create","update","delete"]}}}}`)
|
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
|
|
|
So(func() { _ = cli.NewRootCmd().Execute() }, ShouldNotPanic)
|
|
|
|
})
|
|
|
|
|
2021-05-13 13:59:12 -05:00
|
|
|
Convey("Test verify good config", t, func(c C) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "zot-test*.json")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(tmpfile.Name()) // clean up
|
|
|
|
content := []byte(`{"version": "0.1.0-dev", "storage": {"rootDirectory": "/tmp/zot"},
|
|
|
|
"http": {"address": "127.0.0.1", "port": "8080", "ReadOnly": false},
|
|
|
|
"log": {"level": "debug"}}`)
|
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
|
|
|
err = cli.NewRootCmd().Execute()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadConfig(t *testing.T) {
|
|
|
|
Convey("Test viper load config", t, func(c C) {
|
|
|
|
config := api.NewConfig()
|
|
|
|
So(func() { cli.LoadConfiguration(config, "../../examples/config-policy.json") }, ShouldNotPanic)
|
|
|
|
adminPolicy := viper.GetStringMapStringSlice("http.accessControl.adminPolicy")
|
|
|
|
So(config.AccessControl.AdminPolicy.Actions, ShouldResemble, adminPolicy["actions"])
|
|
|
|
So(config.AccessControl.AdminPolicy.Users, ShouldResemble, adminPolicy["users"])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
func TestGC(t *testing.T) {
|
|
|
|
oldArgs := os.Args
|
2019-12-13 00:53:18 -05:00
|
|
|
|
2019-06-20 18:36:40 -05:00
|
|
|
defer func() { os.Args = oldArgs }()
|
|
|
|
|
2019-09-17 17:45:28 -05:00
|
|
|
Convey("Test gc", t, func(c C) {
|
2019-06-20 18:36:40 -05:00
|
|
|
os.Args = []string{"cli_test", "garbage-collect", "-h"}
|
2020-07-14 12:11:01 -05:00
|
|
|
err := cli.NewRootCmd().Execute()
|
2019-06-20 18:36:40 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
}
|