2022-01-10 11:06:12 -05:00
|
|
|
package common_test
|
|
|
|
|
|
|
|
import (
|
2022-12-22 13:19:42 -05:00
|
|
|
"os"
|
|
|
|
"path"
|
2022-01-10 11:06:12 -05:00
|
|
|
"testing"
|
|
|
|
|
2023-09-06 11:58:00 -05:00
|
|
|
notreg "github.com/notaryproject/notation-go/registry"
|
2022-01-10 11:06:12 -05:00
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
2022-01-10 11:06:12 -05:00
|
|
|
"zotregistry.io/zot/pkg/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommon(t *testing.T) {
|
|
|
|
Convey("test Contains()", t, func() {
|
|
|
|
first := []string{"apple", "biscuit"}
|
|
|
|
So(common.Contains(first, "apple"), ShouldBeTrue)
|
|
|
|
So(common.Contains(first, "peach"), ShouldBeFalse)
|
|
|
|
So(common.Contains([]string{}, "apple"), ShouldBeFalse)
|
|
|
|
})
|
2022-12-22 13:19:42 -05:00
|
|
|
|
2023-03-09 13:43:26 -05:00
|
|
|
Convey("test MarshalThroughStruct()", t, func() {
|
|
|
|
cfg := config.New()
|
|
|
|
|
|
|
|
newCfg := struct {
|
|
|
|
DistSpecVersion string
|
|
|
|
}{}
|
|
|
|
|
|
|
|
_, err := common.MarshalThroughStruct(cfg, &newCfg)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(newCfg.DistSpecVersion, ShouldEqual, cfg.DistSpecVersion)
|
|
|
|
|
|
|
|
// negative
|
|
|
|
obj := make(chan int)
|
|
|
|
toObj := config.New()
|
|
|
|
|
|
|
|
_, err = common.MarshalThroughStruct(obj, &toObj)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
_, err = common.MarshalThroughStruct(toObj, &obj)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
Convey("test dirExists()", t, func() {
|
|
|
|
exists := common.DirExists("testdir")
|
|
|
|
So(exists, ShouldBeFalse)
|
2023-02-02 14:39:03 -05:00
|
|
|
tempDir := t.TempDir()
|
2022-12-22 13:19:42 -05:00
|
|
|
|
2023-02-02 14:39:03 -05:00
|
|
|
file, err := os.Create(path.Join(tempDir, "file.txt"))
|
2022-12-22 13:19:42 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
isDir := common.DirExists(file.Name())
|
|
|
|
So(isDir, ShouldBeFalse)
|
|
|
|
})
|
|
|
|
|
2023-04-24 13:13:15 -05:00
|
|
|
Convey("Index func", t, func() {
|
|
|
|
So(common.Index([]string{"a", "b"}, "b"), ShouldEqual, 1)
|
|
|
|
So(common.Index([]string{"a", "b"}, "c"), ShouldEqual, -1)
|
|
|
|
})
|
2023-09-06 11:58:00 -05:00
|
|
|
|
|
|
|
Convey("Test ArtifactTypeNotation const has same value as in notaryproject", t, func() {
|
|
|
|
So(common.ArtifactTypeNotation, ShouldEqual, notreg.ArtifactTypeNotation)
|
|
|
|
})
|
2022-12-22 13:19:42 -05:00
|
|
|
}
|