Skip to content

Commit

Permalink
Merge pull request #444 from subutai-io/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
crioto authored Jun 4, 2018
2 parents b6ecb7d + d1c5112 commit f940fe8
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
14 changes: 11 additions & 3 deletions download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 == "" {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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)
}
Expand Down
72 changes: 71 additions & 1 deletion download/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package download
import (
"testing"
"reflect"
"github.com/subutai-io/agent/log"
"fmt"
)


Expand Down Expand Up @@ -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)
}
})
}
}

0 comments on commit f940fe8

Please sign in to comment.