2019-06-20 18:36:40 -05:00
|
|
|
package cli_test
|
|
|
|
|
|
|
|
import (
|
2021-10-05 04:12:22 -05:00
|
|
|
"context"
|
|
|
|
"fmt"
|
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-10-05 04:12:22 -05:00
|
|
|
"time"
|
2019-06-20 18:36:40 -05:00
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2021-12-13 14:23:31 -05:00
|
|
|
"gopkg.in/resty.v1"
|
2021-10-05 04:12:22 -05:00
|
|
|
"zotregistry.io/zot/pkg/api"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
|
|
|
"zotregistry.io/zot/pkg/cli"
|
2021-10-05 04:12:22 -05:00
|
|
|
. "zotregistry.io/zot/test"
|
2019-06-20 18:36:40 -05:00
|
|
|
)
|
|
|
|
|
2022-01-10 20:15:35 -05:00
|
|
|
func TestServerUsage(t *testing.T) {
|
2019-06-20 18:36:40 -05:00
|
|
|
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"}
|
2022-01-10 20:15:35 -05:00
|
|
|
err := cli.NewServerRootCmd().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"}
|
2022-01-10 20:15:35 -05:00
|
|
|
err := cli.NewServerRootCmd().Execute()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCliUsage(t *testing.T) {
|
|
|
|
oldArgs := os.Args
|
|
|
|
|
|
|
|
defer func() { os.Args = oldArgs }()
|
|
|
|
|
|
|
|
Convey("Test usage", t, func(c C) {
|
|
|
|
os.Args = []string{"cli_test", "help"}
|
|
|
|
err := cli.NewCliRootCmd().Execute()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test version", t, func(c C) {
|
|
|
|
os.Args = []string{"cli_test", "--version"}
|
|
|
|
err := cli.NewCliRootCmd().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"}
|
2022-01-10 20:15:35 -05:00
|
|
|
err := cli.NewServerRootCmd().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")}
|
2022-01-10 20:15:35 -05:00
|
|
|
So(func() { _ = cli.NewServerRootCmd().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")}
|
2022-01-10 20:15:35 -05:00
|
|
|
So(func() { _ = cli.NewServerRootCmd().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()}
|
2022-01-10 20:15:35 -05:00
|
|
|
So(func() { _ = cli.NewServerRootCmd().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()}
|
2022-01-10 20:15:35 -05:00
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
2021-05-13 13:59:12 -05:00
|
|
|
})
|
|
|
|
|
2021-10-25 07:05:03 -05:00
|
|
|
Convey("Test verify storage driver different than s3", 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", "storageDriver": {"name": "gcs"}},
|
|
|
|
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
|
|
|
|
"auth":{"htpasswd":{"path":"test/data/htpasswd"},"failDelay":1}}}`)
|
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
2022-01-10 20:15:35 -05:00
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
2021-10-25 07:05:03 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test verify subpath storage driver different than s3", 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", "storageDriver": {"name": "s3"},
|
|
|
|
"subPaths": {"/a": {"rootDirectory": "/zot-a","storageDriver": {"name": "gcs"}}}},
|
|
|
|
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
|
|
|
|
"auth":{"htpasswd":{"path":"test/data/htpasswd"},"failDelay":1}}}`)
|
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
2022-01-10 20:15:35 -05:00
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
2021-10-25 07:05:03 -05:00
|
|
|
})
|
|
|
|
|
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()}
|
2022-01-10 20:15:35 -05:00
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
2021-09-01 04:15:00 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
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()}
|
2022-01-10 20:15:35 -05:00
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldNotPanic)
|
2021-09-01 04:15:00 -05:00
|
|
|
})
|
|
|
|
|
2021-10-28 04:10:01 -05:00
|
|
|
Convey("Test verify w/ sync and w/o filesystem storage", 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", "storageDriver": {"name": "s3"}},
|
|
|
|
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
|
|
|
|
"auth":{"htpasswd":{"path":"test/data/htpasswd"},"failDelay":1}},
|
|
|
|
"extensions":{"sync": {"registries": [{"url":"localhost:9999"}]}}}`)
|
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
2022-01-10 20:15:35 -05:00
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
2021-10-28 04:10:01 -05:00
|
|
|
})
|
|
|
|
|
2021-11-25 07:04:39 -05:00
|
|
|
Convey("Test verify with bad sync prefixes", 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}},
|
|
|
|
"extensions":{"sync": {"registries": [{"url":"localhost:9999",
|
|
|
|
"content": [{"prefix":"[repo%^&"}]}]}}}`)
|
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
2022-01-10 20:15:35 -05:00
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
2021-11-25 07:04:39 -05:00
|
|
|
})
|
|
|
|
|
2021-09-10 10:23:26 -05:00
|
|
|
Convey("Test verify with bad authorization repo patterns", 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":{"\|":{"policies":[],"defaultPolicy":[]}}}}`)
|
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
2022-01-10 20:15:35 -05:00
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
2021-09-10 10:23:26 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test verify sync config default tls value", 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}},
|
|
|
|
"extensions":{"sync": {"registries": [{"url":"localhost:9999",
|
|
|
|
"content": [{"prefix":"repo**"}]}]}}}`)
|
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
2022-01-10 20:15:35 -05:00
|
|
|
err = cli.NewServerRootCmd().Execute()
|
2021-09-10 10:23:26 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
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()}
|
2022-01-10 20:15:35 -05:00
|
|
|
err = cli.NewServerRootCmd().Execute()
|
2021-05-13 13:59:12 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadConfig(t *testing.T) {
|
|
|
|
Convey("Test viper load config", t, func(c C) {
|
2021-06-08 15:11:18 -05:00
|
|
|
config := config.New()
|
2021-05-13 13:59:12 -05:00
|
|
|
So(func() { cli.LoadConfiguration(config, "../../examples/config-policy.json") }, ShouldNotPanic)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-05 04:12:22 -05:00
|
|
|
func TestScrub(t *testing.T) {
|
|
|
|
oldArgs := os.Args
|
|
|
|
|
|
|
|
defer func() { os.Args = oldArgs }()
|
|
|
|
|
|
|
|
Convey("Test scrub help", t, func(c C) {
|
|
|
|
os.Args = []string{"cli_test", "scrub", "-h"}
|
2022-01-10 20:15:35 -05:00
|
|
|
err := cli.NewServerRootCmd().Execute()
|
2021-10-05 04:12:22 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test scrub config", t, func(c C) {
|
|
|
|
Convey("non-existent config", func(c C) {
|
|
|
|
os.Args = []string{"cli_test", "scrub", path.Join(os.TempDir(), "/x.yaml")}
|
2022-01-10 20:15:35 -05:00
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
2021-10-05 04:12:22 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("unknown config", func(c C) {
|
|
|
|
os.Args = []string{"cli_test", "scrub", path.Join(os.TempDir(), "/x")}
|
2022-01-10 20:15:35 -05:00
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
2021-10-05 04:12:22 -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", "scrub", tmpfile.Name()}
|
2022-01-10 20:15:35 -05:00
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
2021-10-05 04:12:22 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("server is running", func(c C) {
|
|
|
|
port := GetFreePort()
|
|
|
|
config := config.New()
|
|
|
|
config.HTTP.Port = port
|
|
|
|
controller := api.NewController(config)
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "scrub-test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
controller.Config.Storage.RootDirectory = dir
|
|
|
|
go func(controller *api.Controller) {
|
|
|
|
// this blocks
|
|
|
|
if err := controller.Run(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}(controller)
|
|
|
|
// wait till ready
|
|
|
|
for {
|
|
|
|
_, err := resty.R().Get(fmt.Sprintf("http://127.0.0.1:%s", port))
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpfile, err := ioutil.TempFile("", "zot-test*.json")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(tmpfile.Name()) // clean up
|
|
|
|
content := []byte(fmt.Sprintf(`{
|
|
|
|
"storage": {
|
|
|
|
"rootDirectory": "%s"
|
|
|
|
},
|
|
|
|
"http": {
|
|
|
|
"port": %s
|
|
|
|
},
|
|
|
|
"log": {
|
|
|
|
"level": "debug"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, dir, port))
|
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
os.Args = []string{"cli_test", "scrub", tmpfile.Name()}
|
2022-01-10 20:15:35 -05:00
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
2021-10-05 04:12:22 -05:00
|
|
|
|
|
|
|
defer func(controller *api.Controller) {
|
|
|
|
ctx := context.Background()
|
|
|
|
_ = controller.Server.Shutdown(ctx)
|
|
|
|
}(controller)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("no image store provided", func(c C) {
|
|
|
|
port := GetFreePort()
|
|
|
|
|
|
|
|
tmpfile, err := ioutil.TempFile("", "zot-test*.json")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(tmpfile.Name()) // clean up
|
|
|
|
content := []byte(fmt.Sprintf(`{
|
|
|
|
"storage": {
|
|
|
|
"rootDirectory": ""
|
|
|
|
},
|
|
|
|
"http": {
|
|
|
|
"port": %s
|
|
|
|
},
|
|
|
|
"log": {
|
|
|
|
"level": "debug"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, port))
|
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "scrub", tmpfile.Name()}
|
2022-01-10 20:15:35 -05:00
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
2021-10-05 04:12:22 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("bad index.json", func(c C) {
|
|
|
|
port := GetFreePort()
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "scrub-test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
repoName := "badIndex"
|
|
|
|
|
|
|
|
repo, err := ioutil.TempDir(dir, repoName)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
if err := os.MkdirAll(fmt.Sprintf("%s/blobs", repo), 0o755); err != nil {
|
2021-10-05 04:12:22 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = os.Stat(fmt.Sprintf("%s/oci-layout", repo)); err != nil {
|
|
|
|
content := []byte(`{"imageLayoutVersion": "1.0.0"}`)
|
2021-12-13 14:23:31 -05:00
|
|
|
if err = ioutil.WriteFile(fmt.Sprintf("%s/oci-layout", repo), content, 0o600); err != nil {
|
2021-10-05 04:12:22 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = os.Stat(fmt.Sprintf("%s/index.json", repo)); err != nil {
|
|
|
|
content := []byte(`not a JSON content`)
|
2021-12-13 14:23:31 -05:00
|
|
|
if err = ioutil.WriteFile(fmt.Sprintf("%s/index.json", repo), content, 0o600); err != nil {
|
2021-10-05 04:12:22 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpfile, err := ioutil.TempFile("", "zot-test*.json")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(tmpfile.Name()) // clean up
|
|
|
|
content := []byte(fmt.Sprintf(`{
|
|
|
|
"storage": {
|
|
|
|
"rootDirectory": "%s"
|
|
|
|
},
|
|
|
|
"http": {
|
|
|
|
"port": %s
|
|
|
|
},
|
|
|
|
"log": {
|
|
|
|
"level": "debug"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, dir, port))
|
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
os.Args = []string{"cli_test", "scrub", tmpfile.Name()}
|
2022-01-10 20:15:35 -05:00
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
2021-10-05 04:12:22 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|