Skip to content

Commit

Permalink
Merge pull request #456 from subutai-io/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
crioto authored Jun 6, 2018
2 parents f940fe8 + e4b0cb1 commit 3d9a7a6
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 129 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ script:
- go test -coverprofile=cdn.out -covermode=atomic github.com/subutai-io/cdn
- go test -coverprofile=apt.out -covermode=atomic github.com/subutai-io/cdn/apt
- go test -coverprofile=auth.out -covermode=atomic github.com/subutai-io/cdn/auth
- go test -coverprofile=auto.out -covermode=atomic github.com/subutai-io/cdn/auto
- go test -coverprofile=config.out -covermode=atomic github.com/subutai-io/cdn/config
- go test -coverprofile=db.out -covermode=atomic github.com/subutai-io/cdn/db
- go test -coverprofile=download.out -covermode=atomic github.com/subutai-io/cdn/download
Expand All @@ -21,7 +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
- touch main.out cdn.out apt.out auth.out auto.out config.out db.out download.out pgp.out raw.out template.out torrent.out upload.out
- 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
#- cd /home/travis/gopath/src/github.com/subutai-io/cdn/libgorjun; go get github.com/stretchr/testify/assert;
Expand All @@ -31,7 +30,6 @@ after_success:
- cat cdn.out >> coverage.txt
- cat apt.out >> coverage.txt
- cat auth.out >> coverage.txt
- cat auto.out >> coverage.txt
- cat config.out >> coverage.txt
- cat db.out >> coverage.txt
- cat download.out >> coverage.txt
Expand Down
63 changes: 0 additions & 63 deletions auto/auto.go

This file was deleted.

42 changes: 0 additions & 42 deletions auto/auto_test.go

This file was deleted.

40 changes: 35 additions & 5 deletions download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ func List(repo string, r *http.Request) []byte {
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")
if name == "" {
name = subname
}
Expand Down Expand Up @@ -362,13 +363,21 @@ 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 != "" && ((subname != "" && strings.Contains(item.Name, subname)) || name == item.Name || strings.HasPrefix(name, item.Name+"-subutai-template")))) &&
(version == "" || (version != "" && item.Version == version)) {
(version == "" || (version != "" && (item.Version == version || (version == "latest" && checkVersion(items, item) != -1)))) &&
(verified != "true" || In(item.Owner, []string{"subutai", "jenkins", "docker", "travis", "appveyor", "devops"})) {
if version == "latest" {
positionOlderItem := checkVersion(items, item)
if positionOlderItem != len(items) {
items = append(items[:positionOlderItem], items[positionOlderItem+1:]...)
}
}
items = append(items, item)
}
if len(items) >= p[1] {
break
}
}

log.Info(fmt.Sprintf("list: final list: "))
for i, k := range items {
log.Info(fmt.Sprintf("list: item %d: %s (filename: %s)", i, k.ID, db.NameByHash(k.ID)))
Expand All @@ -383,17 +392,38 @@ 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 {
func In(str []string, list []string) bool {
for _, s := range list {
if s == str {
return true
for _, t := range str {
if s == t {
return true
}
}
}
return false
Expand All @@ -414,7 +444,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(owner, []string{"subutai", "jenkins", "docker", "travis", "appveyor", "devops"}) {
if 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 Down
51 changes: 46 additions & 5 deletions download/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ import (

func TestIn(t *testing.T) {
type args struct {
str string
str []string
list []string
}
tests := []struct {
name string
args args
want bool
}{
{"TestIn-1", args{"in", []string{"string", "in", "slice"}}, true},
{"TestIn-2", args{"in", []string(nil)}, false},
{"TestIn-3", args{"", []string{"", "in", "slice"}}, true},
{"TestIn-4", args{"outside", []string{"", "in", "slice"}}, false},
{"TestIn-1", args{[]string{"in"}, []string{"string", "in", "slice"}}, true},
{"TestIn-2", args{[]string{"in"}, []string(nil)}, false},
{"TestIn-3", args{[]string{""}, []string{"", "in", "slice"}}, true},
{"TestIn-4", args{[]string{"outside"}, []string{"", "in", "slice"}}, false},
// TODO: Add test cases.
}
for _, tt := range tests {
Expand Down Expand Up @@ -198,3 +198,44 @@ func TestFormatItem(t *testing.T) {
})
}
}

func Test_checkVersion(t *testing.T) {
type args struct {
items []ListItem
item ListItem
}
tests := []struct {
name string
args args
want int
}{
{name: "Test_checkVersion-0"},
{name: "Test_checkVersion-1"},
{name: "Test_checkVersion-2"},
{name: "Test_checkVersion-3"},
// TODO: Add test cases.
}
tests[0].args.items = append(tests[0].args.items, ListItem{Name: "debian-stretch", Owner: []string{"subutai"}, Version: "0.4.1"})
tests[0].args.item = ListItem{Name: "debian-stretch", Owner: []string{"subutai"}, Version: "0.4.5"}
tests[0].want = 0
tests[1].args.items = append(tests[1].args.items, ListItem{Name: "debian-stretch", Owner: []string{"subutai"}, Version: "0.4.5"})
tests[1].args.item = ListItem{Name: "debian-stretch", Owner: []string{"subutai"}, Version: "0.4.1"}
tests[1].want = -1
tests[2].args.items = append(tests[2].args.items, ListItem{Name: "debian-stretch", Owner: []string{"somebody"}, Version: "0.4.1"})
tests[2].args.item = ListItem{Name: "debian-stretch", Owner: []string{"subutai"}, Version: "0.4.5"}
tests[2].want = 1
tests[3].args.items = append(tests[3].args.items, ListItem{Name: "debian-stretch", Owner: []string{"somebody"}, Version: "0.4.1"})
tests[3].args.items = append(tests[3].args.items, ListItem{Name: "debian-stretch", Owner: []string{"subutai"}, Version: "0.4.2"})
tests[3].args.item = ListItem{Name: "debian-stretch", Owner: []string{"subutai"}, Version: "0.4.3"}
tests[3].want = 1
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
log.Warn(fmt.Sprintf("Starting test"))
log.Warn(fmt.Sprintf("tt.args.items = %+v", tt.args.items))
log.Warn(fmt.Sprintf("tt.args.item = %+v", tt.args.item))
if got := checkVersion(tt.args.items, tt.args.item); got != tt.want {
t.Errorf("checkVersion() = %v, want %v", got, tt.want)
}
})
}
}
11 changes: 3 additions & 8 deletions garbage-clean-app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@ func CleanGarbage() {
list := db.SearchName("")
for _, k := range list {
info := db.Info(k)
if len(info["Description"]) > 0 {
whiteList = append(whiteList, info["name"])
} else {
whiteList = append(whiteList, info["md5"])
}
if info["md5"] == "" {
whiteList = append(whiteList, info["id"])
}
whiteList = append(whiteList, info["name"])
whiteList = append(whiteList, info["md5"])
whiteList = append(whiteList, info["id"])
}
files, _ := ioutil.ReadDir(config.Storage.Path)
for _, file := range files {
Expand Down
2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/jasonlvhit/gocron"
"github.com/subutai-io/cdn/apt"
"github.com/subutai-io/cdn/auth"
"github.com/subutai-io/cdn/auto"
"github.com/subutai-io/cdn/config"
"github.com/subutai-io/cdn/db"
"github.com/subutai-io/cdn/raw"
Expand All @@ -38,7 +37,6 @@ func main() {
// defer torrent.Close()
// go torrent.SeedLocal()
go RunTask()
go auto.CleanGarbage()
if len(config.CDN.Node) > 0 {
target := url.URL{Scheme: "https", Host: config.CDN.Node}
proxy := httputil.NewSingleHostReverseProxy(&target)
Expand Down
2 changes: 1 addition & 1 deletion template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ func allFieldsPresent(templateData *download.ListItem) (bool, string) {
fieldName := typeOfT.Field(i).Name
fieldValue := f.Interface()

if (download.In(fieldName, requiredFields) && fieldValue == "") ||
if (download.In([]string{fieldName}, requiredFields) && fieldValue == "") ||
(fieldName == "Owner" && len(templateData.Owner) == 0) {
message := fieldName + " field required"
return false, message
Expand Down

0 comments on commit 3d9a7a6

Please sign in to comment.