0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2024-12-16 21:56:37 -05:00
zot/pkg/cli/root_test.go

79 lines
1.9 KiB
Go
Raw Normal View History

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"
"path"
2019-06-20 18:36:40 -05:00
"testing"
"github.com/anuvu/zot/pkg/cli"
. "github.com/smartystreets/goconvey/convey"
)
func TestUsage(t *testing.T) {
oldArgs := os.Args
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"}
err := cli.NewRootCmd(os.TempDir()).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"}
err := cli.NewRootCmd(os.TempDir()).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-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"}
err := cli.NewRootCmd(os.TempDir()).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) {
os.Args = []string{"cli_test", "serve", path.Join(os.TempDir(), "/x")}
So(func() { _ = cli.NewRootCmd(os.TempDir()).Execute() }, ShouldPanic)
2019-09-17 17:45:28 -05:00
})
Convey("non-existent config", func(c C) {
os.Args = []string{"cli_test", "serve", path.Join(os.TempDir(), "/x.yaml")}
So(func() { _ = cli.NewRootCmd(os.TempDir()).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()}
So(func() { _ = cli.NewRootCmd(os.TempDir()).Execute() }, ShouldPanic)
2019-09-17 17:45:28 -05:00
})
})
2019-06-20 18:36:40 -05:00
}
func TestGC(t *testing.T) {
oldArgs := os.Args
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"}
err := cli.NewRootCmd(os.TempDir()).Execute()
2019-06-20 18:36:40 -05:00
So(err, ShouldBeNil)
})
}