2019-06-20 18:36:40 -05:00
|
|
|
package cli_test
|
|
|
|
|
|
|
|
import (
|
2021-10-05 04:12:22 -05:00
|
|
|
"context"
|
2022-02-09 19:51:35 -05:00
|
|
|
"encoding/json"
|
2021-10-05 04:12:22 -05:00
|
|
|
"fmt"
|
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"
|
2022-02-09 19:51:35 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2022-01-19 14:54:17 -05:00
|
|
|
. "zotregistry.io/zot/pkg/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) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2019-09-17 17:45:28 -05:00
|
|
|
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) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2021-05-13 13:59:12 -05:00
|
|
|
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) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2021-10-25 07:05:03 -05:00
|
|
|
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) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2021-10-25 07:05:03 -05:00
|
|
|
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
|
|
|
})
|
|
|
|
|
2022-08-10 17:28:52 -05:00
|
|
|
Convey("Test verify subpath storage config", t, func(c C) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2022-08-10 17:28:52 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(tmpfile.Name()) // clean up
|
|
|
|
content := []byte(`{"storage":{"rootDirectory":"/tmp/zot",
|
|
|
|
"subPaths": {"/a": {"rootDirectory": "/zot-a"},"/b": {"rootDirectory": "/zot-a"}}},
|
|
|
|
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
|
|
|
|
"auth":{"htpasswd":{"path":"test/data/htpasswd"},"failDelay":1}}}`)
|
|
|
|
err = os.WriteFile(tmpfile.Name(), content, 0o0600)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
|
|
|
err = cli.NewServerRootCmd().Execute()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// sub paths that point to same directory should have same storage config.
|
|
|
|
content = []byte(`{"storage":{"rootDirectory":"/tmp/zot",
|
|
|
|
"subPaths": {"/a": {"rootDirectory": "/zot-a","dedupe":"true"},
|
|
|
|
"/b": {{"rootDirectory": "/zot-a","dedupe":"false"}}}},
|
|
|
|
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
|
|
|
|
"auth":{"htpasswd":{"path":"test/data/htpasswd"},"failDelay":1}}}`)
|
|
|
|
err = os.WriteFile(tmpfile.Name(), content, 0o0600)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
|
|
|
|
|
|
|
// sub paths that point to default root directory should not be allowed.
|
|
|
|
content = []byte(`{"storage":{"rootDirectory":"/tmp/zot",
|
|
|
|
"subPaths": {"/a": {"rootDirectory": "/tmp/zot","dedupe":"true"},"/b": {{"rootDirectory": "/zot-a"}}}},
|
|
|
|
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
|
|
|
|
"auth":{"htpasswd":{"path":"test/data/htpasswd"},"failDelay":1}}}`)
|
|
|
|
err = os.WriteFile(tmpfile.Name(), content, 0o0600)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
|
|
|
|
|
|
|
content = []byte(`{"storage":{"rootDirectory":"/tmp/zot",
|
|
|
|
"subPaths": {"/a": {"rootDirectory": "/zot-a","dedupe":"true","gc":"true"},
|
|
|
|
"/b": {"rootDirectory": "/zot-a","dedupe":"true","gc":"false"}}}},
|
|
|
|
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
|
|
|
|
"auth":{"htpasswd":{"path":"test/data/htpasswd"},"failDelay":1}}}`)
|
|
|
|
err = os.WriteFile(tmpfile.Name(), content, 0o0600)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
|
|
|
|
|
|
|
content = []byte(`{"storage":{"rootDirectory":"/tmp/zot",
|
|
|
|
"subPaths": {"/a": {"rootDirectory": "/zot-a","dedupe":"true","gc":"true"},
|
|
|
|
"/b": {"rootDirectory": "/zot-a","dedupe":"true","gc":"true","gcDelay":"1s"}}}},
|
|
|
|
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
|
|
|
|
"auth":{"htpasswd":{"path":"test/data/htpasswd"},"failDelay":1}}}`)
|
|
|
|
err = os.WriteFile(tmpfile.Name(), content, 0o0600)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
|
|
|
|
|
|
|
content = []byte(`{"storage":{"rootDirectory":"/tmp/zot",
|
|
|
|
"subPaths": {"/a": {"rootDirectory": "/zot-a","dedupe":"true","gc":"true","gcDelay":"1s","gcInterval":"1s"},
|
|
|
|
"/b": {"rootDirectory": "/zot-a","dedupe":"true","gc":"true","gcDelay":"1s"}}}},
|
|
|
|
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
|
|
|
|
"auth":{"htpasswd":{"path":"test/data/htpasswd"},"failDelay":1}}}`)
|
|
|
|
err = os.WriteFile(tmpfile.Name(), content, 0o0600)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
|
|
|
|
|
|
|
content = []byte(`{"storage":{"rootDirectory":"/tmp/zot",
|
|
|
|
"subPaths": {"/a": {"rootDirectory": "/tmp/zot","dedupe":"true","gc":"true","gcDelay":"1s","gcInterval":"1s"},
|
|
|
|
"/b": {"rootDirectory": "/zot-a","dedupe":"true","gc":"true","gcDelay":"1s"}}}},
|
|
|
|
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
|
|
|
|
"auth":{"htpasswd":{"path":"test/data/htpasswd"},"failDelay":1}}}`)
|
|
|
|
err = os.WriteFile(tmpfile.Name(), content, 0o0600)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
|
|
|
})
|
|
|
|
|
2021-09-01 04:15:00 -05:00
|
|
|
Convey("Test verify w/ authorization and w/o authentication", t, func(c C) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2021-09-01 04:15:00 -05:00
|
|
|
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) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2021-09-01 04:15:00 -05:00
|
|
|
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
|
|
|
})
|
|
|
|
|
2022-07-14 10:13:46 -05:00
|
|
|
Convey("Test verify anonymous authorization", t, func(c C) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2022-03-10 07:25:15 -05:00
|
|
|
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",
|
2022-07-14 10:13:46 -05:00
|
|
|
"accessControl":{"**":{"anonymousPolicy": ["read", "create"]},
|
|
|
|
"/repo":{"anonymousPolicy": ["read", "create"]}
|
2022-03-10 07:25:15 -05:00
|
|
|
}}}`)
|
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldNotPanic)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test verify default authorization fail", t, func(c C) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2022-03-10 07:25:15 -05:00
|
|
|
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":{"**":{"defaultPolicy": ["read", "create"]},
|
2022-07-14 10:13:46 -05:00
|
|
|
"/repo":{"anonymousPolicy": ["read", "create"]},
|
2022-03-10 07:25:15 -05:00
|
|
|
"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.NewServerRootCmd().Execute() }, ShouldPanic)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test verify default authorization fail", t, func(c C) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2022-03-10 07:25:15 -05:00
|
|
|
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":{"**":{"defaultPolicy": ["read", "create"]},
|
2022-07-14 10:13:46 -05:00
|
|
|
"/repo":{"anonymousPolicy": ["read", "create"]},
|
2022-03-10 07:25:15 -05:00
|
|
|
"/repo2":{"policies": [{
|
|
|
|
"users": ["charlie"],
|
|
|
|
"actions": ["read", "create", "update"]}]}
|
|
|
|
}}}`)
|
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
|
|
|
})
|
|
|
|
|
2021-10-28 04:10:01 -05:00
|
|
|
Convey("Test verify w/ sync and w/o filesystem storage", t, func(c C) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2021-10-28 04:10:01 -05:00
|
|
|
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}},
|
2022-01-10 11:06:12 -05:00
|
|
|
"extensions":{"sync": {"registries": [{"urls":["localhost:9999"],
|
|
|
|
"maxRetries": 1, "retryDelay": "10s"}]}}}`)
|
2021-10-28 04:10:01 -05:00
|
|
|
_, 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-12-07 13:26:26 -05:00
|
|
|
Convey("Test verify w/ sync and w/ filesystem storage", t, func(c C) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2021-12-07 13:26:26 -05:00
|
|
|
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}},
|
2022-01-10 11:06:12 -05:00
|
|
|
"extensions":{"sync": {"registries": [{"urls":["localhost:9999"],
|
|
|
|
"maxRetries": 1, "retryDelay": "10s"}]}}}`)
|
2021-12-07 13:26:26 -05:00
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldNotPanic)
|
|
|
|
})
|
|
|
|
|
2021-11-25 07:04:39 -05:00
|
|
|
Convey("Test verify with bad sync prefixes", t, func(c C) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2021-11-25 07:04:39 -05:00
|
|
|
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}},
|
2021-12-29 10:14:56 -05:00
|
|
|
"extensions":{"sync": {"registries": [{"urls":["localhost:9999"],
|
2022-01-10 11:06:12 -05:00
|
|
|
"maxRetries": 1, "retryDelay": "10s",
|
2021-11-25 07:04:39 -05:00
|
|
|
"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) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2021-09-10 10:23:26 -05:00
|
|
|
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},
|
2022-07-14 10:13:46 -05:00
|
|
|
"accessControl":{"[":{"policies":[],"anonymousPolicy":[]}}}}`)
|
2021-09-10 10:23:26 -05:00
|
|
|
_, 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) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2021-09-10 10:23:26 -05:00
|
|
|
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}},
|
2021-12-29 10:14:56 -05:00
|
|
|
"extensions":{"sync": {"registries": [{"urls":["localhost:9999"],
|
2022-01-10 11:06:12 -05:00
|
|
|
"maxRetries": 1, "retryDelay": "10s",
|
2021-09-10 10:23:26 -05:00
|
|
|
"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)
|
|
|
|
})
|
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
Convey("Test verify sync without retry options", t, func(c C) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2022-01-10 11:06:12 -05:00
|
|
|
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": [{"urls":["localhost:9999"],
|
2022-02-09 10:55:03 -05:00
|
|
|
"maxRetries": 10, "content": [{"prefix":"repo**"}]}]}}}`)
|
2022-01-10 11:06:12 -05:00
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
|
|
|
})
|
|
|
|
|
2022-05-06 05:15:03 -05:00
|
|
|
Convey("Test verify config with unknown keys", t, func(c C) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2022-05-06 05:15:03 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(tmpfile.Name()) // clean up
|
|
|
|
content := []byte(`{"distSpecVersion": "1.0.0", "storage": {"rootDirectory": "/tmp/zot"},
|
2022-07-14 10:13:46 -05:00
|
|
|
"http": {"url": "127.0.0.1", "port": "8080"},
|
2022-05-06 05:15:03 -05:00
|
|
|
"log": {"level": "debug"}}`)
|
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test verify config with missing basedn key", t, func(c C) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2022-05-06 05:15:03 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(tmpfile.Name()) // clean up
|
|
|
|
content := []byte(`{"distSpecVersion": "1.0.0", "storage": {"rootDirectory": "/tmp/zot"},
|
|
|
|
"http": {"auth": {"ldap": {"address": "ldap", "userattribute": "uid"}},
|
2022-07-14 10:13:46 -05:00
|
|
|
"address": "127.0.0.1", "port": "8080"},
|
2022-05-06 05:15:03 -05:00
|
|
|
"log": {"level": "debug"}}`)
|
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test verify config with missing address key", t, func(c C) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2022-05-06 05:15:03 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(tmpfile.Name()) // clean up
|
|
|
|
content := []byte(`{"distSpecVersion": "1.0.0", "storage": {"rootDirectory": "/tmp/zot"},
|
|
|
|
"http": {"auth": {"ldap": {"basedn": "ou=Users,dc=example,dc=org", "userattribute": "uid"}},
|
2022-07-14 10:13:46 -05:00
|
|
|
"address": "127.0.0.1", "port": "8080"},
|
2022-05-06 05:15:03 -05:00
|
|
|
"log": {"level": "debug"}}`)
|
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test verify config with missing userattribute key", t, func(c C) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2022-05-06 05:15:03 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(tmpfile.Name()) // clean up
|
|
|
|
content := []byte(`{"distSpecVersion": "1.0.0", "storage": {"rootDirectory": "/tmp/zot"},
|
|
|
|
"http": {"auth": {"ldap": {"basedn": "ou=Users,dc=example,dc=org", "address": "ldap"}},
|
2022-07-14 10:13:46 -05:00
|
|
|
"address": "127.0.0.1", "port": "8080"},
|
2022-05-06 05:15:03 -05:00
|
|
|
"log": {"level": "debug"}}`)
|
|
|
|
_, err = tmpfile.Write(content)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = tmpfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
|
|
|
|
So(func() { _ = cli.NewServerRootCmd().Execute() }, ShouldPanic)
|
|
|
|
})
|
|
|
|
|
2021-05-13 13:59:12 -05:00
|
|
|
Convey("Test verify good config", t, func(c C) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2021-05-13 13:59:12 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(tmpfile.Name()) // clean up
|
2022-03-07 07:50:15 -05:00
|
|
|
content := []byte(`{"distSpecVersion": "1.0.0", "storage": {"rootDirectory": "/tmp/zot"},
|
2022-07-14 10:13:46 -05:00
|
|
|
"http": {"address": "127.0.0.1", "port": "8080"},
|
2021-05-13 13:59:12 -05:00
|
|
|
"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()
|
2022-02-10 09:17:49 -05:00
|
|
|
err := cli.LoadConfiguration(config, "../../examples/config-policy.json")
|
|
|
|
So(err, ShouldBeNil)
|
2021-05-13 13:59:12 -05:00
|
|
|
})
|
2022-08-10 17:28:52 -05:00
|
|
|
Convey("Test subpath config combination", t, func(c C) {
|
|
|
|
config := config.New()
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2022-08-10 17:28:52 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
content := []byte(`{"storage":{"rootDirectory":"/tmp/zot",
|
|
|
|
"subPaths": {"/a": {"rootDirectory": "/tmp/zot","dedupe":"true","gc":"true","gcDelay":"1s","gcInterval":"1s"},
|
|
|
|
"/b": {"rootDirectory": "/zot-a","dedupe":"true","gc":"true","gcDelay":"1s"}}},
|
|
|
|
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
|
|
|
|
"auth":{"htpasswd":{"path":"test/data/htpasswd"},"failDelay":1}}}`)
|
|
|
|
err = os.WriteFile(tmpfile.Name(), content, 0o0600)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = cli.LoadConfiguration(config, tmpfile.Name())
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
content = []byte(`{"storage":{"rootDirectory":"/tmp/zot",
|
|
|
|
"subPaths": {"/a": {"rootDirectory": "/zot-a","dedupe":"true","gc":"true","gcDelay":"1s","gcInterval":"1s"},
|
|
|
|
"/b": {"rootDirectory": "/zot-a","dedupe":"true","gc":"true","gcDelay":"1s"}}},
|
|
|
|
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
|
|
|
|
"auth":{"htpasswd":{"path":"test/data/htpasswd"},"failDelay":1}}}`)
|
|
|
|
err = os.WriteFile(tmpfile.Name(), content, 0o0600)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = cli.LoadConfiguration(config, tmpfile.Name())
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
content = []byte(`{"storage":{"rootDirectory":"/tmp/zot",
|
|
|
|
"subPaths": {"/a": {"rootDirectory": "/zot-a","dedupe":"true"},
|
|
|
|
"/b": {"rootDirectory": "/zot-a","dedupe":"false"}}},
|
|
|
|
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
|
|
|
|
"auth":{"htpasswd":{"path":"test/data/htpasswd"},"failDelay":1}}}`)
|
|
|
|
err = os.WriteFile(tmpfile.Name(), content, 0o0600)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = cli.LoadConfiguration(config, tmpfile.Name())
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
content = []byte(`{"storage":{"rootDirectory":"/tmp/zot",
|
|
|
|
"subPaths": {"/a": {"rootDirectory": "/zot-a","dedupe":"true"},
|
|
|
|
"/b": {"rootDirectory": "/zot-a","dedupe":"true"}}},
|
|
|
|
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
|
|
|
|
"auth":{"htpasswd":{"path":"test/data/htpasswd"},"failDelay":1}}}`)
|
|
|
|
err = os.WriteFile(tmpfile.Name(), content, 0o0600)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = cli.LoadConfiguration(config, tmpfile.Name())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
2022-09-09 00:41:13 -05:00
|
|
|
|
|
|
|
Convey("Test HTTP port", t, func() {
|
|
|
|
config := config.New()
|
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
|
|
|
|
content := []byte(`{"storage":{"rootDirectory":"/tmp/zot",
|
|
|
|
"subPaths": {"/a": {"rootDirectory": "/zot-a","dedupe":"true"},
|
|
|
|
"/b": {"rootDirectory": "/zot-a","dedupe":"true"}}},
|
|
|
|
"http":{"address":"127.0.0.1","port":"8080","realm":"zot",
|
|
|
|
"auth":{"htpasswd":{"path":"test/data/htpasswd"},"failDelay":1}}}`)
|
|
|
|
err = os.WriteFile(tmpfile.Name(), content, 0o0600)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = cli.LoadConfiguration(config, tmpfile.Name())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
content = []byte(`{"storage":{"rootDirectory":"/tmp/zot",
|
|
|
|
"subPaths": {"/a": {"rootDirectory": "/zot-a","dedupe":"true"},
|
|
|
|
"/b": {"rootDirectory": "/zot-a","dedupe":"true"}}},
|
|
|
|
"http":{"address":"127.0.0.1","port":"-1","realm":"zot",
|
|
|
|
"auth":{"htpasswd":{"path":"test/data/htpasswd"},"failDelay":1}}}`)
|
|
|
|
err = os.WriteFile(tmpfile.Name(), content, 0o0600)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = cli.LoadConfiguration(config, tmpfile.Name())
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
content = []byte(`{"storage":{"rootDirectory":"/tmp/zot",
|
|
|
|
"subPaths": {"/a": {"rootDirectory": "/zot-a","dedupe":"true"},
|
|
|
|
"/b": {"rootDirectory": "/zot-a","dedupe":"true"}}},
|
|
|
|
"http":{"address":"127.0.0.1","port":"65536","realm":"zot",
|
|
|
|
"auth":{"htpasswd":{"path":"test/data/htpasswd"},"failDelay":1}}}`)
|
|
|
|
err = os.WriteFile(tmpfile.Name(), content, 0o0600)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = cli.LoadConfiguration(config, tmpfile.Name())
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
2021-05-13 13:59:12 -05:00
|
|
|
}
|
|
|
|
|
2022-02-09 19:51:35 -05:00
|
|
|
func TestGC(t *testing.T) {
|
|
|
|
Convey("Test GC config", t, func(c C) {
|
|
|
|
config := config.New()
|
2022-02-10 09:17:49 -05:00
|
|
|
err := cli.LoadConfiguration(config, "../../examples/config-multiple.json")
|
|
|
|
So(err, ShouldBeNil)
|
2022-02-09 19:51:35 -05:00
|
|
|
So(config.Storage.GCDelay, ShouldEqual, storage.DefaultGCDelay)
|
2022-02-10 09:17:49 -05:00
|
|
|
err = cli.LoadConfiguration(config, "../../examples/config-gc.json")
|
|
|
|
So(err, ShouldBeNil)
|
2022-02-09 19:51:35 -05:00
|
|
|
So(config.Storage.GCDelay, ShouldNotEqual, storage.DefaultGCDelay)
|
2022-03-21 13:40:37 -05:00
|
|
|
err = cli.LoadConfiguration(config, "../../examples/config-gc-periodic.json")
|
|
|
|
So(err, ShouldBeNil)
|
2022-02-09 19:51:35 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test GC config corner cases", t, func(c C) {
|
2022-09-02 07:56:02 -05:00
|
|
|
contents, err := os.ReadFile("../../examples/config-gc.json")
|
2022-02-09 19:51:35 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("GC delay without GC", func() {
|
|
|
|
config := config.New()
|
|
|
|
err = json.Unmarshal(contents, config)
|
|
|
|
config.Storage.GC = false
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
file, err := os.CreateTemp("", "gc-config-*.json")
|
2022-02-09 19:51:35 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
contents, err = json.MarshalIndent(config, "", " ")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
err = os.WriteFile(file.Name(), contents, 0o600)
|
2022-02-09 19:51:35 -05:00
|
|
|
So(err, ShouldBeNil)
|
2022-02-10 09:17:49 -05:00
|
|
|
err = cli.LoadConfiguration(config, file.Name())
|
|
|
|
So(err, ShouldBeNil)
|
2022-02-09 19:51:35 -05:00
|
|
|
})
|
|
|
|
|
2022-03-21 13:40:37 -05:00
|
|
|
Convey("GC interval without GC", func() {
|
|
|
|
config := config.New()
|
|
|
|
err = json.Unmarshal(contents, config)
|
|
|
|
config.Storage.GC = false
|
|
|
|
config.Storage.GCDelay = 0
|
|
|
|
config.Storage.GCInterval = 24 * time.Hour
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
file, err := os.CreateTemp("", "gc-config-*.json")
|
2022-03-21 13:40:37 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
contents, err = json.MarshalIndent(config, "", " ")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
err = os.WriteFile(file.Name(), contents, 0o600)
|
2022-03-21 13:40:37 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = cli.LoadConfiguration(config, file.Name())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
2022-02-09 19:51:35 -05:00
|
|
|
Convey("Negative GC delay", func() {
|
|
|
|
config := config.New()
|
|
|
|
err = json.Unmarshal(contents, config)
|
|
|
|
config.Storage.GCDelay = -1 * time.Second
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
file, err := os.CreateTemp("", "gc-config-*.json")
|
2022-02-09 19:51:35 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
contents, err = json.MarshalIndent(config, "", " ")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
err = os.WriteFile(file.Name(), contents, 0o600)
|
2022-02-09 19:51:35 -05:00
|
|
|
So(err, ShouldBeNil)
|
2022-02-10 09:17:49 -05:00
|
|
|
err = cli.LoadConfiguration(config, file.Name())
|
|
|
|
So(err, ShouldNotBeNil)
|
2022-02-09 19:51:35 -05:00
|
|
|
})
|
2022-03-30 08:02:03 -05:00
|
|
|
|
|
|
|
Convey("GC delay when GC = false", func() {
|
|
|
|
config := config.New()
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
file, err := os.CreateTemp("", "gc-false-config-*.json")
|
2022-03-30 08:02:03 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
content := []byte(`{"distSpecVersion": "1.0.0", "storage": {"rootDirectory": "/tmp/zot",
|
2022-07-14 10:13:46 -05:00
|
|
|
"gc": false}, "http": {"address": "127.0.0.1", "port": "8080"},
|
2022-03-30 08:02:03 -05:00
|
|
|
"log": {"level": "debug"}}`)
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
err = os.WriteFile(file.Name(), content, 0o600)
|
2022-03-30 08:02:03 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = cli.LoadConfiguration(config, file.Name())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(config.Storage.GCDelay, ShouldEqual, 0)
|
|
|
|
})
|
2022-03-21 13:40:37 -05:00
|
|
|
|
|
|
|
Convey("Negative GC interval", func() {
|
|
|
|
config := config.New()
|
|
|
|
err = json.Unmarshal(contents, config)
|
|
|
|
config.Storage.GCInterval = -1 * time.Second
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
file, err := os.CreateTemp("", "gc-config-*.json")
|
2022-03-21 13:40:37 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
contents, err = json.MarshalIndent(config, "", " ")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
err = os.WriteFile(file.Name(), contents, 0o600)
|
2022-03-21 13:40:37 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = cli.LoadConfiguration(config, file.Name())
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
2022-02-09 19:51:35 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2021-12-07 13:26:26 -05:00
|
|
|
Convey("Test scrub no args", t, func(c C) {
|
|
|
|
os.Args = []string{"cli_test", "scrub"}
|
|
|
|
err := cli.NewServerRootCmd().Execute()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
2021-10-05 04:12:22 -05:00
|
|
|
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) {
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2021-10-05 04:12:22 -05:00
|
|
|
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)
|
|
|
|
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2021-10-05 04:12:22 -05:00
|
|
|
|
|
|
|
controller.Config.Storage.RootDirectory = dir
|
|
|
|
go func(controller *api.Controller) {
|
|
|
|
// this blocks
|
2022-03-24 07:49:51 -05:00
|
|
|
if err := controller.Run(context.Background()); err != nil {
|
2021-10-05 04:12:22 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2021-10-05 04:12:22 -05:00
|
|
|
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()
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2021-10-05 04:12:22 -05:00
|
|
|
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()
|
|
|
|
|
2022-03-07 03:55:12 -05:00
|
|
|
dir := t.TempDir()
|
2021-10-05 04:12:22 -05:00
|
|
|
|
|
|
|
repoName := "badIndex"
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
repo, err := os.MkdirTemp(dir, repoName)
|
2021-10-05 04:12:22 -05:00
|
|
|
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"}`)
|
2022-09-02 07:56:02 -05:00
|
|
|
if err = os.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`)
|
2022-09-02 07:56:02 -05:00
|
|
|
if err = os.WriteFile(fmt.Sprintf("%s/index.json", repo), content, 0o600); err != nil {
|
2021-10-05 04:12:22 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
tmpfile, err := os.CreateTemp("", "zot-test*.json")
|
2021-10-05 04:12:22 -05:00
|
|
|
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
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|