From 8b67285c529403a470f650fb576a4cefcbcd999c Mon Sep 17 00:00:00 2001 From: jackgr Date: Mon, 11 Jan 2016 22:16:46 -0800 Subject: [PATCH] Additional test clean ups. --- manager/manager/manager_test.go | 2 +- manager/manager/registryprovider_test.go | 106 ---------------- manager/manager/typeresolver_test.go | 12 +- registry/github_package_registry.go | 12 +- registry/github_registry.go | 149 +---------------------- registry/github_template_registry.go | 12 +- registry/registryprovider.go | 24 ++-- registry/registryprovider_test.go | 122 ++++--------------- registry/testhelper.go | 82 +++++++++++++ 9 files changed, 150 insertions(+), 371 deletions(-) delete mode 100644 manager/manager/registryprovider_test.go create mode 100644 registry/testhelper.go diff --git a/manager/manager/manager_test.go b/manager/manager/manager_test.go index 9d80a438c..810c0cb14 100644 --- a/manager/manager/manager_test.go +++ b/manager/manager/manager_test.go @@ -254,7 +254,7 @@ var testExpander = &expanderStub{} var testRepository = newRepositoryStub() var testDeployer = newDeployerStub() var testRegistryService = registry.NewInmemRegistryService() -var testProvider = newTestRegistryProvider("", nil) +var testProvider = registry.NewRegistryProvider(nil, registry.NewTestGithubRegistryProvider("", nil)) var testManager = NewManager(testExpander, testDeployer, testRepository, testProvider, testRegistryService) func TestListDeployments(t *testing.T) { diff --git a/manager/manager/registryprovider_test.go b/manager/manager/registryprovider_test.go deleted file mode 100644 index 4a4cab423..000000000 --- a/manager/manager/registryprovider_test.go +++ /dev/null @@ -1,106 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package manager - -import ( - "fmt" - "net/url" - "regexp" - "strings" - - "github.com/kubernetes/deployment-manager/common" - "github.com/kubernetes/deployment-manager/registry" -) - -type urlAndError struct { - u string - e error -} - -type testRegistryProvider struct { - r map[string]registry.Registry -} - -func newTestRegistryProvider(shortURL string, tests map[registry.Type]urlAndError) registry.RegistryProvider { - r := make(map[string]registry.Registry) - r[shortURL] = &testGithubRegistry{tests} - return testRegistryProvider{r} -} - -func (trp testRegistryProvider) GetRegistryByShortURL(URL string) (registry.Registry, error) { - for key, r := range trp.r { - if strings.HasPrefix(URL, key) { - return r, nil - } - } - - return nil, fmt.Errorf("No registry found for %s", URL) -} - -func (trp testRegistryProvider) GetRegistryByName(registryName string) (registry.Registry, error) { - panic(fmt.Errorf("GetRegistryByName should not be called in the test")) -} - -func (trp testRegistryProvider) GetGithubRegistry(cr common.Registry) (registry.GithubRegistry, error) { - panic(fmt.Errorf("GetGithubRegistry should not be called in the test")) -} - -type testGithubRegistry struct { - responses map[registry.Type]urlAndError -} - -func (tgr testGithubRegistry) GetRegistryName() string { - panic(fmt.Errorf("GetRegistryName should not be called in the test")) -} - -func (tgr testGithubRegistry) GetRegistryType() common.RegistryType { - return common.GithubRegistryType -} - -func (tgr testGithubRegistry) GetRegistryShortURL() string { - panic(fmt.Errorf("GetRegistryShortURL should not be called in the test")) -} - -func (tgr testGithubRegistry) GetRegistryFormat() common.RegistryFormat { - panic(fmt.Errorf("GetRegistryFormat should not be called in the test")) -} - -func (tgr testGithubRegistry) GetRegistryOwner() string { - panic(fmt.Errorf("GetRegistryOwner should not be called in the test")) -} - -func (tgr testGithubRegistry) GetRegistryRepository() string { - panic(fmt.Errorf("GetRegistryRepository should not be called in the test")) -} - -func (tgr testGithubRegistry) GetRegistryPath() string { - panic(fmt.Errorf("GetRegistryPath should not be called in the test")) -} - -func (tgr testGithubRegistry) ListTypes(regex *regexp.Regexp) ([]registry.Type, error) { - panic(fmt.Errorf("ListTypes should not be called in the test")) -} - -func (tgr testGithubRegistry) GetDownloadURLs(t registry.Type) ([]*url.URL, error) { - ret := tgr.responses[t] - URL, err := url.Parse(ret.u) - if err != nil { - panic(err) - } - - return []*url.URL{URL}, ret.e -} diff --git a/manager/manager/typeresolver_test.go b/manager/manager/typeresolver_test.go index 321564c23..37b58396e 100644 --- a/manager/manager/typeresolver_test.go +++ b/manager/manager/typeresolver_test.go @@ -228,6 +228,7 @@ func TestTooManyImports(t *testing.T) { responses: responses, expectedErr: errors.New("Number of imports exceeds maximum of 5"), } + testDriver(test, t) } @@ -270,6 +271,7 @@ func TestSharedImport(t *testing.T) { responses: responses, importOut: finalImports, } + testDriver(test, t) } @@ -300,17 +302,19 @@ func TestShortGithubUrl(t *testing.T) { "https://raw.githubusercontent.com/kubernetes/application-dm-templates/master/common/replicatedservice/v2/replicatedservice.py.schema": responseAndError{nil, http.StatusNotFound, ""}, } - githubUrlMaps := map[registry.Type]urlAndError{ - registry.NewTypeOrDie("common", "replicatedservice", "v1"): urlAndError{"https://raw.githubusercontent.com/kubernetes/application-dm-templates/master/common/replicatedservice/v1/replicatedservice.py", nil}, - registry.NewTypeOrDie("common", "replicatedservice", "v2"): urlAndError{"https://raw.githubusercontent.com/kubernetes/application-dm-templates/master/common/replicatedservice/v2/replicatedservice.py", nil}, + githubUrlMaps := map[registry.Type]registry.TestURLAndError{ + registry.NewTypeOrDie("common", "replicatedservice", "v1"): registry.TestURLAndError{"https://raw.githubusercontent.com/kubernetes/application-dm-templates/master/common/replicatedservice/v1/replicatedservice.py", nil}, + registry.NewTypeOrDie("common", "replicatedservice", "v2"): registry.TestURLAndError{"https://raw.githubusercontent.com/kubernetes/application-dm-templates/master/common/replicatedservice/v2/replicatedservice.py", nil}, } + grp := registry.NewTestGithubRegistryProvider("github.com/kubernetes/application-dm-templates", githubUrlMaps) test := resolverTestCase{ config: templateShortGithubTemplate, importOut: finalImports, urlcount: 4, responses: responses, - registryProvider: newTestRegistryProvider("github.com/kubernetes/application-dm-templates", githubUrlMaps), + registryProvider: registry.NewRegistryProvider(nil, grp), } + testDriver(test, t) } diff --git a/registry/github_package_registry.go b/registry/github_package_registry.go index 5bea6613a..a7de76e16 100644 --- a/registry/github_package_registry.go +++ b/registry/github_package_registry.go @@ -17,6 +17,7 @@ limitations under the License. package registry import ( + "github.com/google/go-github/github" "github.com/kubernetes/deployment-manager/common" "fmt" @@ -40,14 +41,19 @@ type GithubPackageRegistry struct { } // NewGithubPackageRegistry creates a GithubPackageRegistry. -func NewGithubPackageRegistry(name, shortURL string, service RepositoryService) (GithubPackageRegistry, error) { +func NewGithubPackageRegistry(name, shortURL string, service RepositoryService) (*GithubPackageRegistry, error) { format := fmt.Sprintf("%s;%s", common.UnversionedRegistry, common.OneLevelRegistry) + if service == nil { + client := github.NewClient(nil) + service = client.Repositories + } + gr, err := newGithubRegistry(name, shortURL, common.RegistryFormat(format), service) if err != nil { - return GithubPackageRegistry{}, err + return nil, err } - return GithubPackageRegistry{githubRegistry: gr}, nil + return &GithubPackageRegistry{githubRegistry: *gr}, nil } // ListTypes lists types in this registry whose string values conform to the diff --git a/registry/github_registry.go b/registry/github_registry.go index fed57322c..e40f20d3d 100644 --- a/registry/github_registry.go +++ b/registry/github_registry.go @@ -22,9 +22,6 @@ import ( "github.com/kubernetes/deployment-manager/util" "fmt" - "log" - "net/url" - "regexp" "strings" ) @@ -54,19 +51,14 @@ type RepositoryService interface { } // newGithubRegistry creates a githubRegistry. -func newGithubRegistry(name, shortURL string, format common.RegistryFormat, service RepositoryService) (githubRegistry, error) { +func newGithubRegistry(name, shortURL string, format common.RegistryFormat, service RepositoryService) (*githubRegistry, error) { trimmed := util.TrimURLScheme(shortURL) owner, repository, path, err := parseGithubShortURL(trimmed) if err != nil { - return githubRegistry{}, fmt.Errorf("cannot create Github template registry %s: %s", name, err) + return nil, fmt.Errorf("cannot create Github template registry %s: %s", name, err) } - if service == nil { - client := github.NewClient(nil) - service = client.Repositories - } - - return githubRegistry{ + return &githubRegistry{ name: name, shortURL: trimmed, owner: owner, @@ -132,138 +124,3 @@ func (g githubRegistry) GetRegistryRepository() string { func (g githubRegistry) GetRegistryPath() string { return g.path } - -// ListTypes lists types in this registry whose string values conform to the -// supplied regular expression, or all types, if the regular expression is nil. -func (g githubRegistry) ListTypes(regex *regexp.Regexp) ([]Type, error) { - // First list all the collections at the top level. - collections, err := g.getDirs("") - if err != nil { - log.Printf("cannot list qualifiers: %v", err) - return nil, err - } - - var retTypes []Type - for _, c := range collections { - // Then we need to fetch the versions (directories for this type) - types, err := g.getDirs(c) - if err != nil { - log.Printf("cannot fetch types for collection: %s", c) - return nil, err - } - - for _, t := range types { - path := c + "/" + t - // Then we need to fetch the versions (directories for this type) - versions, err := g.getDirs(path) - if err != nil { - log.Printf("cannot fetch versions at path %s", path) - return nil, err - } - - for _, v := range versions { - tt, err := NewType(c, t, v) - if err != nil { - return nil, fmt.Errorf("malformed type at path %s", path) - } - - retTypes = append(retTypes, tt) - } - } - } - - // TODO(jackgr): Use the supplied regex to filter the results. - return retTypes, nil -} - -// GetDownloadURLs fetches the download URLs for a given Type and checks for existence of a schema file. -func (g githubRegistry) GetDownloadURLs(t Type) ([]*url.URL, error) { - path, err := g.MakeRepositoryPath(t) - if err != nil { - return nil, err - } - _, dc, _, err := g.service.GetContents(g.owner, g.repository, path, nil) - if err != nil { - return nil, fmt.Errorf("cannot list versions at path %s: %v", path, err) - } - - var downloadURL, typeName, schemaName string - for _, f := range dc { - if *f.Type == "file" { - if *f.Name == t.Name+".jinja" || *f.Name == t.Name+".py" { - typeName = *f.Name - downloadURL = *f.DownloadURL - } - if *f.Name == t.Name+".jinja.schema" || *f.Name == t.Name+".py.schema" { - schemaName = *f.Name - } - } - } - - if downloadURL == "" { - return nil, fmt.Errorf("cannot find type %s", t.String()) - } - - if schemaName != typeName+".schema" { - return nil, fmt.Errorf("cannot find schema for %s, expected %s", t.String(), typeName+".schema") - } - - result, err := url.Parse(downloadURL) - if err != nil { - return nil, fmt.Errorf("cannot parse URL from %s: %s", downloadURL, err) - } - - return []*url.URL{result}, nil -} - -func (g githubRegistry) getDirs(dir string) ([]string, error) { - var path = g.path - if dir != "" { - path = g.path + "/" + dir - } - - _, dc, _, err := g.service.GetContents(g.owner, g.repository, path, nil) - if err != nil { - log.Printf("Failed to get contents at path: %s: %v", path, err) - return nil, err - } - - var dirs []string - for _, entry := range dc { - if *entry.Type == "dir" { - dirs = append(dirs, *entry.Name) - } - } - - return dirs, nil -} - -func (g githubRegistry) mapCollection(collection string) (string, error) { - if strings.ContainsAny(collection, "/") { - return "", fmt.Errorf("collection must not contain slashes, got %s", collection) - } - // TODO(vaikas): Implement lookup from the root metadata file to map collection to a path - return collection, nil -} - -// MakeRepositoryPath constructs a github path to a given type based on a repository, and type name and version. -// The returned repository path will be of the form: -// [githubRegistry.path/][Type.Collection]/Type.Name/Type.Version -// Type.Collection will be mapped using mapCollection in the future, for now it's a straight -// 1:1 mapping (if given) -func (g githubRegistry) MakeRepositoryPath(t Type) (string, error) { - // First map the collection - collection, err := g.mapCollection(t.Collection) - if err != nil { - return "", err - } - // Construct the return path - p := "" - if len(g.path) > 0 { - p += g.path + "/" - } - if len(collection) > 0 { - p += collection + "/" - } - return p + t.Name + "/" + t.GetVersion(), nil -} diff --git a/registry/github_template_registry.go b/registry/github_template_registry.go index be704eae2..40f34968b 100644 --- a/registry/github_template_registry.go +++ b/registry/github_template_registry.go @@ -17,6 +17,7 @@ limitations under the License. package registry import ( + "github.com/google/go-github/github" "github.com/kubernetes/deployment-manager/common" "fmt" @@ -53,14 +54,19 @@ type GithubTemplateRegistry struct { } // NewGithubTemplateRegistry creates a GithubTemplateRegistry. -func NewGithubTemplateRegistry(name, shortURL string, service RepositoryService) (GithubTemplateRegistry, error) { +func NewGithubTemplateRegistry(name, shortURL string, service RepositoryService) (*GithubTemplateRegistry, error) { format := fmt.Sprintf("%s;%s", common.VersionedRegistry, common.CollectionRegistry) + if service == nil { + client := github.NewClient(nil) + service = client.Repositories + } + gr, err := newGithubRegistry(name, shortURL, common.RegistryFormat(format), service) if err != nil { - return GithubTemplateRegistry{}, err + return nil, err } - return GithubTemplateRegistry{githubRegistry: gr}, nil + return &GithubTemplateRegistry{githubRegistry: *gr}, nil } // ListTypes lists types in this registry whose string values conform to the diff --git a/registry/registryprovider.go b/registry/registryprovider.go index 2d499fa0e..b25102e84 100644 --- a/registry/registryprovider.go +++ b/registry/registryprovider.go @@ -48,7 +48,7 @@ func NewRegistryProvider(rs common.RegistryService, grp GithubRegistryProvider) } registries := make(map[string]Registry) - rp := registryProvider{rs: rs, registries: registries} + rp := ®istryProvider{rs: rs, registries: registries} if grp == nil { grp = rp } @@ -68,22 +68,23 @@ func (rp registryProvider) GetRegistryByShortURL(URL string) (Registry, error) { rp.RLock() defer rp.RUnlock() - r := rp.findRegistryByShortURL(URL) - if r == nil { + result := rp.findRegistryByShortURL(URL) + if result == nil { cr, err := rp.rs.GetByURL(URL) if err != nil { return nil, err } - r, err := rp.GetGithubRegistry(*cr) + r, err := rp.grp.GetGithubRegistry(*cr) if err != nil { return nil, err } rp.registries[r.GetRegistryName()] = r + result = r } - return r, nil + return result, nil } // findRegistryByShortURL trims the scheme from both the supplied URL @@ -103,27 +104,28 @@ func (rp registryProvider) GetRegistryByName(registryName string) (Registry, err rp.RLock() defer rp.RUnlock() - r, ok := rp.registries[registryName] + result, ok := rp.registries[registryName] if !ok { cr, err := rp.rs.Get(registryName) if err != nil { return nil, err } - r, err := rp.GetGithubRegistry(*cr) + r, err := rp.grp.GetGithubRegistry(*cr) if err != nil { return nil, err } rp.registries[r.GetRegistryName()] = r + result = r } - return r, nil + return result, nil } func ParseRegistryFormat(rf common.RegistryFormat) map[common.RegistryFormat]bool { split := strings.Split(string(rf), ";") - var result map[common.RegistryFormat]bool + var result = map[common.RegistryFormat]bool{} for _, format := range split { result[common.RegistryFormat(format)] = true } @@ -209,6 +211,10 @@ func ShortTypeToDownloadURLs(rp RegistryProvider, t string) ([]string, error) { return nil, err } + if r == nil { + panic(fmt.Errorf("cannot get github registry for %s", t)) + } + tt, err := NewType(m[3], m[4], m[5]) if err != nil { return nil, err diff --git a/registry/registryprovider_test.go b/registry/registryprovider_test.go index 708975089..28e24c45e 100644 --- a/registry/registryprovider_test.go +++ b/registry/registryprovider_test.go @@ -16,125 +16,49 @@ limitations under the License. package registry -// TODO(jackgr): Finish implementing registry provider tests. - import ( - "github.com/kubernetes/deployment-manager/common" - - "fmt" - "net/url" - "regexp" - "strings" "testing" ) -type urlAndError struct { - u string - e error -} - -type testRegistryProvider struct { - URLPrefix string - r map[string]Registry -} - -func newTestRegistryProvider(URLPrefix string, tests map[Type]urlAndError) RegistryProvider { - r := make(map[string]Registry) - r[URLPrefix] = testGithubRegistry{tests} - return testRegistryProvider{URLPrefix, r} -} - -func (trp testRegistryProvider) GetRegistryByShortURL(URL string) (Registry, error) { - for key, r := range trp.r { - if strings.HasPrefix(URL, key) { - return r, nil - } - } - - return nil, fmt.Errorf("No registry found for %s", URL) -} - -func (trp testRegistryProvider) GetRegistryByName(registryName string) (Registry, error) { - panic(fmt.Errorf("GetRegistryByName should not be called in the test")) -} - -func (trp testRegistryProvider) GetGithubRegistry(cr common.Registry) (GithubRegistry, error) { - panic(fmt.Errorf("GetGithubRegistry should not be called in the test")) -} - -type testGithubRegistry struct { - responses map[Type]urlAndError -} - -func (tgr testGithubRegistry) GetRegistryName() string { - panic(fmt.Errorf("GetRegistryName should not be called in the test")) -} - -func (tgr testGithubRegistry) GetRegistryType() common.RegistryType { - return common.GithubRegistryType -} - -func (tgr testGithubRegistry) GetRegistryShortURL() string { - panic(fmt.Errorf("GetRegistryShortURL should not be called in the test")) -} - -func (tgr testGithubRegistry) GetRegistryFormat() common.RegistryFormat { - panic(fmt.Errorf("GetRegistryFormat should not be called in the test")) -} - -func (tgr testGithubRegistry) GetDownloadURLs(t Type) ([]*url.URL, error) { - ret := tgr.responses[t] - URL, err := url.Parse(ret.u) - if err != nil { - panic(err) - } - - return []*url.URL{URL}, ret.e -} - -func (tgr testGithubRegistry) ListTypes(regex *regexp.Regexp) ([]Type, error) { - panic(fmt.Errorf("ListTypes should not be called in the test")) -} - -func testUrlConversionDriver(rp RegistryProvider, tests map[string]urlAndError, t *testing.T) { +func testUrlConversionDriver(rp RegistryProvider, tests map[string]TestURLAndError, t *testing.T) { for in, expected := range tests { actual, err := GetDownloadURLs(rp, in) - if err != expected.e { - t.Errorf("failed on: %s : expected error %v but got %v", in, expected.e, err) + if err != expected.Err { + t.Fatalf("failed on: %s : expected error %v but got %v", in, expected.Err, err) } - if actual[0] != expected.u { - t.Errorf("failed on: %s : expected %s but got %v", in, expected.u, actual) + if actual[0] != expected.URL { + t.Fatalf("failed on: %s : expected %s but got %v", in, expected.URL, actual) } } } -func TestShortGithubUrlMapping(t *testing.T) { - githubUrlMaps := map[Type]urlAndError{ - NewTypeOrDie("common", "replicatedservice", "v1"): urlAndError{"https://raw.githubusercontent.com/kubernetes/application-dm-templates/master/common/replicatedservice/v1/replicatedservice.py", nil}, - NewTypeOrDie("storage", "redis", "v1"): urlAndError{"https://raw.githubusercontent.com/kubernetes/application-dm-templates/master/storage/redis/v1/redis.jinja", nil}, +func TestShortGithubUrlTemplateMapping(t *testing.T) { + githubUrlMaps := map[Type]TestURLAndError{ + NewTypeOrDie("common", "replicatedservice", "v1"): TestURLAndError{"https://raw.githubusercontent.com/kubernetes/application-dm-templates/master/common/replicatedservice/v1/replicatedservice.py", nil}, + NewTypeOrDie("storage", "redis", "v1"): TestURLAndError{"https://raw.githubusercontent.com/kubernetes/application-dm-templates/master/storage/redis/v1/redis.jinja", nil}, } - tests := map[string]urlAndError{ - "github.com/kubernetes/application-dm-templates/common/replicatedservice:v1": urlAndError{"https://raw.githubusercontent.com/kubernetes/application-dm-templates/master/common/replicatedservice/v1/replicatedservice.py", nil}, - "github.com/kubernetes/application-dm-templates/storage/redis:v1": urlAndError{"https://raw.githubusercontent.com/kubernetes/application-dm-templates/master/storage/redis/v1/redis.jinja", nil}, + tests := map[string]TestURLAndError{ + "github.com/kubernetes/application-dm-templates/common/replicatedservice:v1": TestURLAndError{"https://raw.githubusercontent.com/kubernetes/application-dm-templates/master/common/replicatedservice/v1/replicatedservice.py", nil}, + "github.com/kubernetes/application-dm-templates/storage/redis:v1": TestURLAndError{"https://raw.githubusercontent.com/kubernetes/application-dm-templates/master/storage/redis/v1/redis.jinja", nil}, } - test := newTestRegistryProvider("github.com/kubernetes/application-dm-templates", githubUrlMaps) - testUrlConversionDriver(test, tests, t) + grp := NewTestGithubRegistryProvider("github.com/kubernetes/application-dm-templates", githubUrlMaps) + testUrlConversionDriver(NewRegistryProvider(nil, grp), tests, t) } -func TestShortGithubUrlMappingDifferentOwnerAndRepo(t *testing.T) { - githubUrlMaps := map[Type]urlAndError{ - NewTypeOrDie("common", "replicatedservice", "v1"): urlAndError{"https://raw.githubusercontent.com/example/mytemplates/master/common/replicatedservice/v1/replicatedservice.py", nil}, - NewTypeOrDie("storage", "redis", "v1"): urlAndError{"https://raw.githubusercontent.com/example/mytemplates/master/storage/redis/v1/redis.jinja", nil}, +func TestShortGithubUrlPackageMapping(t *testing.T) { + githubUrlMaps := map[Type]TestURLAndError{ + NewTypeOrDie("", "mongodb", ""): TestURLAndError{"https://raw.githubusercontent.com/helm/charts/master/mongodb/manifests/mongodb.yaml", nil}, + NewTypeOrDie("", "redis", ""): TestURLAndError{"https://raw.githubusercontent.com/helm/charts/master/redis/manifests/redis.yaml", nil}, } - tests := map[string]urlAndError{ - "github.com/example/mytemplates/common/replicatedservice:v1": urlAndError{"https://raw.githubusercontent.com/example/mytemplates/master/common/replicatedservice/v1/replicatedservice.py", nil}, - "github.com/example/mytemplates/storage/redis:v1": urlAndError{"https://raw.githubusercontent.com/example/mytemplates/master/storage/redis/v1/redis.jinja", nil}, + tests := map[string]TestURLAndError{ + "github.com/helm/charts/mongodb": TestURLAndError{"https://raw.githubusercontent.com/helm/charts/master/mongodb/manifests/mongodb.yaml", nil}, + "github.com/helm/charts/redis": TestURLAndError{"https://raw.githubusercontent.com/helm/charts/master/redis/manifests/redis.yaml", nil}, } - test := newTestRegistryProvider("github.com/example/mytemplates", githubUrlMaps) - testUrlConversionDriver(test, tests, t) + grp := NewTestGithubRegistryProvider("github.com/helm/charts", githubUrlMaps) + testUrlConversionDriver(NewRegistryProvider(nil, grp), tests, t) } diff --git a/registry/testhelper.go b/registry/testhelper.go new file mode 100644 index 000000000..7b7dfe937 --- /dev/null +++ b/registry/testhelper.go @@ -0,0 +1,82 @@ +/* +Copyright 2015 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package registry + +// TODO(jackgr): Mock github repository service to test package and template registry implementations. + +import ( + "github.com/kubernetes/deployment-manager/common" + "github.com/kubernetes/deployment-manager/util" + + "fmt" + "net/url" + "regexp" + "strings" +) + +type TestURLAndError struct { + URL string + Err error +} + +type testGithubRegistryProvider struct { + shortURL string + responses map[Type]TestURLAndError +} + +type testGithubRegistry struct { + githubRegistry + responses map[Type]TestURLAndError +} + +func NewTestGithubRegistryProvider(shortURL string, responses map[Type]TestURLAndError) GithubRegistryProvider { + return testGithubRegistryProvider{ + shortURL: util.TrimURLScheme(shortURL), + responses: responses, + } +} + +func (tgrp testGithubRegistryProvider) GetGithubRegistry(cr common.Registry) (GithubRegistry, error) { + trimmed := util.TrimURLScheme(cr.URL) + if strings.HasPrefix(trimmed, tgrp.shortURL) { + ghr, err := newGithubRegistry(cr.Name, trimmed, cr.Format, nil) + if err != nil { + panic(fmt.Errorf("cannot create a github registry: %s", err)) + } + + return &testGithubRegistry{ + githubRegistry: *ghr, + responses: tgrp.responses, + }, nil + } + + panic(fmt.Errorf("unknown registry: %v", cr)) +} + +func (tgr testGithubRegistry) ListTypes(regex *regexp.Regexp) ([]Type, error) { + panic(fmt.Errorf("ListTypes should not be called in the test")) +} + +func (tgr testGithubRegistry) GetDownloadURLs(t Type) ([]*url.URL, error) { + result := tgr.responses[t] + URL, err := url.Parse(result.URL) + if err != nil { + panic(err) + } + + return []*url.URL{URL}, result.Err +}