diff --git a/download/download.go b/download/download.go index 8d1cf6c..e2dc464 100644 --- a/download/download.go +++ b/download/download.go @@ -146,12 +146,16 @@ func Info(repo string, r *http.Request) []byte { id := r.URL.Query().Get("id") tag := r.URL.Query().Get("tag") name := r.URL.Query().Get("name") + subname := r.URL.Query().Get("subname") page := r.URL.Query().Get("page") owner := strings.ToLower(r.URL.Query().Get("owner")) token := strings.ToLower(r.URL.Query().Get("token")) version := r.URL.Query().Get("version") verified := r.URL.Query().Get("verified") version = processVersion(version) + if name == "" { + name = subname + } if id != "" && name != "" && db.NameByHash(id) != name { return nil } @@ -161,7 +165,7 @@ func Info(repo string, r *http.Request) []byte { } list := make([]string, 0) if id != "" { - // log.Debug(fmt.Sprintf("id was provided")) + // log.Debug(fmt.Sprintf("id was provided")) name = db.NameByHash(id) if verified != "true" { if owner == "" && token == "" { @@ -266,7 +270,7 @@ func Info(repo string, r *http.Request) []byte { } item := FormatItem(db.Info(k), repo) if (id == "" || id == item.ID) && - (name == item.Name || strings.HasPrefix(name, item.Name+"-subutai-template")) && + ((subname != "" && strings.Contains(item.Name, subname)) || name == item.Name || strings.HasPrefix(name, item.Name + "-subutai-template")) && (version == "" || (version != "" && item.Version == version)) { items = []ListItem{item} itemVersion, _ := semver.Make(item.Version) @@ -303,9 +307,13 @@ func List(repo string, r *http.Request) []byte { tag := r.URL.Query().Get("tag") name := r.URL.Query().Get("name") page := r.URL.Query().Get("page") + subname := r.URL.Query().Get("subname") owner := strings.ToLower(r.URL.Query().Get("owner")) token := strings.ToLower(r.URL.Query().Get("token")) version := r.URL.Query().Get("version") + if name == "" { + name = subname + } if token != "" && db.TokenOwner(token) == "" { token = "" log.Debug(fmt.Sprintf("List: provided token is invalid")) @@ -353,7 +361,7 @@ func List(repo string, r *http.Request) []byte { } item := FormatItem(db.Info(k), repo) log.Debug(fmt.Sprintf("File #%+v (hash: %+v) in formatted way: %+v", i, k, item)) - if (name == "" || (name != "" && (name == item.Name || strings.HasPrefix(name, item.Name+"-subutai-template")))) && + if (name == "" || (name != "" && ((subname != "" && strings.Contains(item.Name, subname)) || name == item.Name || strings.HasPrefix(name, item.Name+"-subutai-template")))) && (version == "" || (version != "" && item.Version == version)) { items = append(items, item) } diff --git a/download/download_test.go b/download/download_test.go index 05e31c5..023ee87 100644 --- a/download/download_test.go +++ b/download/download_test.go @@ -3,6 +3,8 @@ package download import ( "testing" "reflect" + "github.com/subutai-io/agent/log" + "fmt" ) @@ -127,4 +129,72 @@ func Test_processVersion(t *testing.T) { } } - +func TestFormatItem(t *testing.T) { + type args struct { + info map[string]string + repo string + } + tests := []struct { + name string + args args + want ListItem + }{ + {name: "TestFormatItem-0"}, + {name: "TestFormatItem-1"}, + // TODO: Add test cases. + } + { + tests[0].args.info = make(map[string]string) + tests[0].args.info["id"] = "some-id" + tests[0].args.info["md5"] = "some-md5" + tests[0].args.info["sha256"] = "some-sha256" + tests[0].args.info["arch"] = "some-arch" + tests[0].args.info["name"] = "some-name-subutai-template" + tests[0].args.info["size"] = "123" + tests[0].args.info["parent"] = "some-parent" + tests[0].args.info["parent-owner"] = "some-parent-owner" + tests[0].args.info["parent-version"] = "some-parent-version" + tests[0].args.info["version"] = "some-version" + tests[0].args.info["Description"] = "some-Description" + tests[0].args.repo = "template" + } + { + tests[1].args.info = make(map[string]string) + tests[1].args.info["id"] = "some-id" + tests[1].args.info["sha256"] = "some-sha256" + tests[1].args.info["SHA256"] = "some-sha256" + tests[1].args.info["arch"] = "some-arch" + tests[1].args.info["Architecture"] = "some-arch" + tests[1].args.info["name"] = "some-name-apt" + tests[1].args.info["size"] = "123" + tests[1].args.info["Size"] = "123" + tests[1].args.info["parent"] = "some-parent" + tests[1].args.info["parent-owner"] = "some-parent-owner" + tests[1].args.info["parent-version"] = "some-parent-version" + tests[1].args.info["version"] = "some-version" + tests[1].args.info["Version"] = "some-version" + tests[1].args.info["Description"] = "some-Description" + tests[1].args.repo = "apt" + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := FormatItem(tt.args.info, tt.args.repo) + if got.ID == tt.args.info["id"] && + got.Filename == tt.args.info["name"] && + ((tt.name == "TestFormatItem-0" && got.Name == "some-name") || (tt.name == "TestFormatItem-1" && got.Name == "some-name-apt")) && + ((tt.name == "TestFormatItem-0" && got.Hash.Md5 == "some-md5") || (tt.name == "TestFormatItem-1" && got.Hash.Md5 == tt.args.info["id"])) && + got.Hash.Sha256 == tt.args.info["sha256"] && + got.Version == tt.args.info["version"] && + got.Parent == tt.args.info["parent"] && + got.ParentOwner == tt.args.info["parent-owner"] && + got.ParentVersion == tt.args.info["parent-version"] && + ((tt.name == "TestFormatItem-0" && got.Prefsize == "tiny") || (tt.name == "TestFormatItem-1" && got.Prefsize == "")) && + got.Description == tt.args.info["Description"] && + ((tt.name == "TestFormatItem-0" && got.Architecture == "SOME-ARCH") || (tt.name == "TestFormatItem-1" && got.Architecture == "some-arch")) { + log.Info(fmt.Sprintf("Test %s passes", tt.name)) + } else { + t.Errorf("Test %s didn't pass: check of %+v vs %+v didn't pass", tt.name, got, tt.args.info) + } + }) + } +}