Skip to content

Commit

Permalink
Merge pull request #459 from subutai-io/404
Browse files Browse the repository at this point in the history
added fix for #404
  • Loading branch information
crioto authored Jun 11, 2018
2 parents b86363a + f3ef449 commit 6439491
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 205 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ script:
- go test -coverprofile=template.out -covermode=atomic github.com/subutai-io/cdn/template
- go test -coverprofile=torrent.out -covermode=atomic github.com/subutai-io/cdn/torrent
- go test -coverprofile=upload.out -covermode=atomic github.com/subutai-io/cdn/upload
- go test -coverprofile=utils.out -covermode=atomic github.com/subutai-io/cdn/utils
- touch main.out cdn.out apt.out auth.out config.out db.out download.out pgp.out raw.out template.out torrent.out upload.out
#- go test -coverprofile=libgorjun.out -covermode=atomic github.com/subutai-io/cdn/libgorjun
#- cd /home/travis/gopath/src/github.com/subutai-io/cdn/libgorjun/; ./register.sh
Expand All @@ -39,6 +40,7 @@ after_success:
- cat template.out >> coverage.txt
- cat torrent.out >> coverage.txt
- cat upload.out >> coverage.txt
- cat utils.out >> coverage.txt
- bash <(curl -s https://codecov.io/bash)
- sudo systemctl start gorjun.service
- sudo systemctl status gorjun.service
35 changes: 35 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package config

import (
"testing"
"strconv"
)

func TestDefaultQuota(t *testing.T) {
tests := []struct {
name string
want int
}{
{name: "TestDefaultQuota-"},
// TODO: Add test cases.
}
for i := 1; i <= 2; i++ {
test := tests[0]
test.name += strconv.Itoa(i)
tests = append(tests, test)
}
tests[0].name += "0"
wants := []int{1 << 31, 1 << 21, 1 << 11}
for i := 0; i <= 2; i++ {
tests[i].want = wants[i]
}
quotas := []string{"2G", "2M", "2K"}
for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config.Storage.Userquota = quotas[i]
if got := DefaultQuota(); got != tt.want {
t.Errorf("DefaultQuota() = %v, want %v", got, tt.want)
}
})
}
}
3 changes: 2 additions & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/boltdb/bolt"
"github.com/subutai-io/agent/log"
"github.com/subutai-io/cdn/config"
"github.com/subutai-io/cdn/utils"
)

var (
Expand Down Expand Up @@ -1065,7 +1066,7 @@ func UserFile(owner, file string) (list []string) {
if b := tx.Bucket(Users).Bucket([]byte(owner)); b != nil {
if files := b.Bucket([]byte("files")); files != nil {
files.ForEach(func(k, v []byte) error {
if NameByHash(string(k)) == file {
if NameByHash(string(k)) == file && utils.In([]string{owner}, FileField(string(k), "owner")) {
list = append(list, string(k))
}
return nil
Expand Down
115 changes: 35 additions & 80 deletions download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/subutai-io/agent/log"
"github.com/subutai-io/cdn/config"
"github.com/subutai-io/cdn/db"
"github.com/subutai-io/cdn/utils"
)

// ListItem describes Gorjun entity. It can be APT package, Subutai template or Raw file.
Expand Down Expand Up @@ -152,7 +153,7 @@ func Info(repo string, r *http.Request) []byte {
token := strings.ToLower(r.URL.Query().Get("token"))
version := r.URL.Query().Get("version")
verified := r.URL.Query().Get("verified")
version = processVersion(version)
version = utils.ProcessVersion(version)
if name == "" {
name = subname
}
Expand All @@ -173,19 +174,19 @@ func Info(repo string, r *http.Request) []byte {
list = []string{id}
} else if owner == "" && token != "" {
log.Info("Case 2")
list = intersect([]string{id}, intersect(db.SearchName(name), db.OwnerFilesByRepo(db.TokenOwner(token), repo)))
list = utils.Intersect([]string{id}, utils.Intersect(db.SearchName(name), db.OwnerFilesByRepo(db.TokenOwner(token), repo)))
if len(list) == 0 {
list = intersect([]string{id}, intersect(db.SearchName(name), db.TokenFilesByRepo(token, repo)))
list = utils.Intersect([]string{id}, utils.Intersect(db.SearchName(name), db.TokenFilesByRepo(token, repo)))
if len(list) == 0 {
list = intersect([]string{id}, db.SearchName(name))
list = utils.Intersect([]string{id}, db.SearchName(name))
}
}
} else if owner != "" && token == "" {
log.Info("Case 3")
list = intersect([]string{id}, db.OwnerFilesByRepo(owner, repo))
list = utils.Intersect([]string{id}, db.OwnerFilesByRepo(owner, repo))
} else {
log.Info("Case 4")
list = intersect([]string{id}, union(db.OwnerFilesByRepo(owner, repo), intersect(db.TokenFilesByRepo(db.GetUserToken(owner), repo), db.TokenFilesByRepo(token, repo))))
list = utils.Intersect([]string{id}, utils.Union(db.OwnerFilesByRepo(owner, repo), utils.Intersect(db.TokenFilesByRepo(db.GetUserToken(owner), repo), db.TokenFilesByRepo(token, repo))))
}
} else {
list = []string{id}
Expand All @@ -206,7 +207,7 @@ func Info(repo string, r *http.Request) []byte {
verified = "true"
} else if owner == "" && token != "" {
log.Info("Case 2")
list = intersect(db.SearchName(name), db.TokenFilesByRepo(token, repo))
list = utils.Intersect(db.SearchName(name), db.TokenFilesByRepo(token, repo))
onlyTokenOwner := make([]string, 0)
for _, k := range list {
if db.FileField(k, "owner")[0] == db.TokenOwner(token) {
Expand All @@ -215,7 +216,7 @@ func Info(repo string, r *http.Request) []byte {
}
list = onlyTokenOwner
if len(list) == 0 {
list = intersect(db.SearchName(name), db.TokenFilesByRepo(token, repo))
list = utils.Intersect(db.SearchName(name), db.TokenFilesByRepo(token, repo))
if len(list) == 0 {
list = db.SearchName(name)
verified = "true"
Expand All @@ -226,16 +227,16 @@ func Info(repo string, r *http.Request) []byte {
list = db.OwnerFilesByRepo(owner, repo)
} else {
log.Info("Case 4")
list = intersect(db.SearchName(name), union(db.OwnerFilesByRepo(owner, repo), intersect(db.TokenFilesByRepo(db.GetUserToken(owner), repo), db.TokenFilesByRepo(token, repo))))
list = utils.Intersect(db.SearchName(name), utils.Union(db.OwnerFilesByRepo(owner, repo), utils.Intersect(db.TokenFilesByRepo(db.GetUserToken(owner), repo), db.TokenFilesByRepo(token, repo))))
}
} else {
list = db.SearchName(name)
}
}
list = unique(list)
list = utils.Unique(list)
if tag != "" {
listByTag, _ := db.Tag(tag)
list = intersect(list, listByTag)
list = utils.Intersect(list, listByTag)
}
if verified == "true" {
itemLatestVersion = GetVerified(list, name, repo, version)
Expand Down Expand Up @@ -326,19 +327,19 @@ func List(repo string, r *http.Request) []byte {
log.Info("Case 2")
} else if owner != "" && token == "" {
log.Info("Case 3")
list = intersect(list, db.OwnerFilesByRepo(owner, repo))
list = utils.Intersect(list, db.OwnerFilesByRepo(owner, repo))
} else {
log.Info("Case 4")
list = union(db.OwnerFilesByRepo(owner, repo),
intersect(
list = utils.Union(db.OwnerFilesByRepo(owner, repo),
utils.Intersect(
db.TokenFilesByRepo(db.GetUserToken(owner), repo),
db.TokenFilesByRepo(token, repo)))
}
list = unique(list)
list = utils.Unique(list)
if tag != "" {
listByTag, err := db.Tag(tag)
log.Check(log.DebugLevel, "Looking for artifacts with tag "+tag, err)
list = intersect(list, listByTag)
list = utils.Intersect(list, listByTag)
}
pstr := strings.Split(page, ",")
p[0], _ = strconv.Atoi(pstr[0])
Expand All @@ -364,7 +365,7 @@ func List(repo string, r *http.Request) []byte {
log.Debug(fmt.Sprintf("File #%+v (hash: %+v) in formatted way: %+v", i, k, item))
if (name == "" || (name != "" && ((subname != "" && strings.Contains(item.Name, subname)) || name == item.Name || strings.HasPrefix(name, item.Name+"-subutai-template")))) &&
(version == "" || (version != "" && (item.Version == version || (version == "latest" && checkVersion(items, item) != -1)))) &&
(verified != "true" || In(item.Owner, []string{"subutai", "jenkins", "docker", "travis", "appveyor", "devops"})) {
(verified != "true" || utils.In(item.Owner, []string{"subutai", "jenkins", "docker", "travis", "appveyor", "devops"})) {
if version == "latest" {
positionOlderItem := checkVersion(items, item)
if positionOlderItem != len(items) {
Expand Down Expand Up @@ -392,43 +393,6 @@ func List(repo string, r *http.Request) []byte {
return output
}

func checkVersion(items []ListItem, item ListItem) int {
exists := false
for i, v := range items {
if v.Name == item.Name && (len(v.Owner) > 0 && len(item.Owner) > 0 && v.Owner[0] == item.Owner[0]) {
exists = true
vVersion, _ := semver.Make(v.Version)
itemVersion, _ := semver.Make(item.Version)
if itemVersion.GTE(vVersion) {
log.Info(fmt.Sprintf("i = %d, vVersion: %+v, itemVersion: %+v, v: %+v <---> item: %+v", i, vVersion, itemVersion, v, item))
return i
}
}
}
if !exists {
return len(items)
}
return -1
}

func processVersion(version string) string {
if version == "latest" {
return ""
}
return version
}

func In(str []string, list []string) bool {
for _, s := range list {
for _, t := range str {
if s == t {
return true
}
}
}
return false
}

func GetVerified(list []string, name, repo, versionTemplate string) ListItem {
log.Debug(fmt.Sprintf("Getting file \"%+v\" from verified users", name))
latestVersion, _ := semver.Make("")
Expand All @@ -444,7 +408,7 @@ func GetVerified(list []string, name, repo, versionTemplate string) ListItem {
if info["name"] == name || (strings.HasPrefix(info["name"], name+"-subutai-template") && repo == "template") {
for _, owner := range db.FileField(info["id"], "owner") {
itemVersion, _ := semver.Make(info["version"])
if In([]string{owner}, []string{"subutai", "jenkins", "docker", "travis", "appveyor", "devops"}) {
if utils.In([]string{owner}, []string{"subutai", "jenkins", "docker", "travis", "appveyor", "devops"}) {
if itemVersion.GTE(latestVersion) && len(versionTemplate) == 0 {
log.Debug(fmt.Sprintf("First if %+v", k))
latestVersion = itemVersion
Expand All @@ -461,6 +425,7 @@ func GetVerified(list []string, name, repo, versionTemplate string) ListItem {
return itemLatestVersion
}


func FormatItem(info map[string]string, repo string) ListItem {
log.Debug(fmt.Sprintf("Repo: %+v, formatting item %+v", repo, info))
if len(info["prefsize"]) == 0 && repo == "template" {
Expand Down Expand Up @@ -503,32 +468,22 @@ func FormatItem(info map[string]string, repo string) ListItem {
return item
}

func intersect(listA, listB []string) (list []string) {
mapA := make(map[string]int)
for _, item := range listA {
mapA[item]++
}
for _, item := range listB {
if mapA[item] > 0 {
mapA[item]--
list = append(list, item)
}
}
return
}

func unique(list []string) (result []string) {
was := make(map[string]bool)
for _, v := range list {
if !was[v] {
was[v] = true
result = append(result, v)
func checkVersion(items []ListItem, item ListItem) int {
exists := false
for i, v := range items {
if v.Name == item.Name && (len(v.Owner) > 0 && len(item.Owner) > 0 && v.Owner[0] == item.Owner[0]) {
exists = true
vVersion, _ := semver.Make(v.Version)
itemVersion, _ := semver.Make(item.Version)
if itemVersion.GTE(vVersion) {
log.Info(fmt.Sprintf("i = %d, vVersion: %+v, itemVersion: %+v, v: %+v <---> item: %+v", i, vVersion, itemVersion, v, item))
return i
}
}
}
return
}

func union(listA []string, listB []string) []string {
listA = append(listA, listB[:]...)
return unique(listA)
if !exists {
return len(items)
}
return -1
}
Loading

0 comments on commit 6439491

Please sign in to comment.