Commit a6e1067a authored by jackgr's avatar jackgr

Make github registry provider and repository service mockable.

parent 4ffe3422
...@@ -17,7 +17,6 @@ limitations under the License. ...@@ -17,7 +17,6 @@ limitations under the License.
package registry package registry
import ( import (
"github.com/google/go-github/github"
"github.com/kubernetes/deployment-manager/common" "github.com/kubernetes/deployment-manager/common"
"fmt" "fmt"
...@@ -41,9 +40,9 @@ type GithubPackageRegistry struct { ...@@ -41,9 +40,9 @@ type GithubPackageRegistry struct {
} }
// NewGithubPackageRegistry creates a GithubPackageRegistry. // NewGithubPackageRegistry creates a GithubPackageRegistry.
func NewGithubPackageRegistry(name, shortURL string, client *github.Client) (GithubPackageRegistry, error) { func NewGithubPackageRegistry(name, shortURL string, service RepositoryService) (GithubPackageRegistry, error) {
format := fmt.Sprintf("%s;%s", common.UnversionedRegistry, common.OneLevelRegistry) format := fmt.Sprintf("%s;%s", common.UnversionedRegistry, common.OneLevelRegistry)
gr, err := newGithubRegistry(name, shortURL, common.RegistryFormat(format), client) gr, err := newGithubRegistry(name, shortURL, common.RegistryFormat(format), service)
if err != nil { if err != nil {
return GithubPackageRegistry{}, err return GithubPackageRegistry{}, err
} }
...@@ -64,7 +63,7 @@ func (g GithubPackageRegistry) ListTypes(regex *regexp.Regexp) ([]Type, error) { ...@@ -64,7 +63,7 @@ func (g GithubPackageRegistry) ListTypes(regex *regexp.Regexp) ([]Type, error) {
var retTypes []Type var retTypes []Type
for _, t := range types { for _, t := range types {
// Check to see if there's a Chart.yaml file in the directory // Check to see if there's a Chart.yaml file in the directory
_, dc, _, err := g.client.Repositories.GetContents(g.owner, g.repository, t, nil) _, dc, _, err := g.service.GetContents(g.owner, g.repository, t, nil)
if err != nil { if err != nil {
log.Printf("Failed to list package files at path: %s: %v", t, err) log.Printf("Failed to list package files at path: %s: %v", t, err)
return nil, err return nil, err
...@@ -87,7 +86,7 @@ func (g GithubPackageRegistry) GetDownloadURLs(t Type) ([]*url.URL, error) { ...@@ -87,7 +86,7 @@ func (g GithubPackageRegistry) GetDownloadURLs(t Type) ([]*url.URL, error) {
return nil, err return nil, err
} }
_, dc, _, err := g.client.Repositories.GetContents(g.owner, g.repository, path, nil) _, dc, _, err := g.service.GetContents(g.owner, g.repository, path, nil)
if err != nil { if err != nil {
log.Printf("Failed to list package files at path: %s: %v", path, err) log.Printf("Failed to list package files at path: %s: %v", path, err)
return nil, err return nil, err
...@@ -111,7 +110,7 @@ func (g GithubPackageRegistry) GetDownloadURLs(t Type) ([]*url.URL, error) { ...@@ -111,7 +110,7 @@ func (g GithubPackageRegistry) GetDownloadURLs(t Type) ([]*url.URL, error) {
} }
func (g GithubPackageRegistry) getDirs(dir string) ([]string, error) { func (g GithubPackageRegistry) getDirs(dir string) ([]string, error) {
_, dc, _, err := g.client.Repositories.GetContents(g.owner, g.repository, dir, nil) _, dc, _, err := g.service.GetContents(g.owner, g.repository, dir, nil)
if err != nil { if err != nil {
log.Printf("Failed to get contents at path: %s: %v", dir, err) log.Printf("Failed to get contents at path: %s: %v", dir, err)
return nil, err return nil, err
......
...@@ -38,17 +38,34 @@ type githubRegistry struct { ...@@ -38,17 +38,34 @@ type githubRegistry struct {
repository string repository string
path string path string
format common.RegistryFormat format common.RegistryFormat
client *github.Client service RepositoryService
}
type RepositoryService interface {
GetContents(
owner, repo, path string,
opt *github.RepositoryContentGetOptions,
) (
fileContent *github.RepositoryContent,
directoryContent []*github.RepositoryContent,
resp *github.Response,
err error,
)
} }
// newGithubRegistry creates a githubRegistry. // newGithubRegistry creates a githubRegistry.
func newGithubRegistry(name, shortURL string, format common.RegistryFormat, client *github.Client) (githubRegistry, error) { func newGithubRegistry(name, shortURL string, format common.RegistryFormat, service RepositoryService) (githubRegistry, error) {
trimmed := util.TrimURLScheme(shortURL) trimmed := util.TrimURLScheme(shortURL)
owner, repository, path, err := parseGithubShortURL(trimmed) owner, repository, path, err := parseGithubShortURL(trimmed)
if err != nil { if err != nil {
return githubRegistry{}, fmt.Errorf("cannot create Github template registry %s: %s", name, err) return githubRegistry{}, 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, name: name,
shortURL: trimmed, shortURL: trimmed,
...@@ -56,7 +73,7 @@ func newGithubRegistry(name, shortURL string, format common.RegistryFormat, clie ...@@ -56,7 +73,7 @@ func newGithubRegistry(name, shortURL string, format common.RegistryFormat, clie
repository: repository, repository: repository,
path: path, path: path,
format: format, format: format,
client: client, service: service,
}, nil }, nil
} }
...@@ -165,7 +182,7 @@ func (g githubRegistry) GetDownloadURLs(t Type) ([]*url.URL, error) { ...@@ -165,7 +182,7 @@ func (g githubRegistry) GetDownloadURLs(t Type) ([]*url.URL, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
_, dc, _, err := g.client.Repositories.GetContents(g.owner, g.repository, path, nil) _, dc, _, err := g.service.GetContents(g.owner, g.repository, path, nil)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot list versions at path %s: %v", path, err) return nil, fmt.Errorf("cannot list versions at path %s: %v", path, err)
} }
...@@ -205,7 +222,7 @@ func (g githubRegistry) getDirs(dir string) ([]string, error) { ...@@ -205,7 +222,7 @@ func (g githubRegistry) getDirs(dir string) ([]string, error) {
path = g.path + "/" + dir path = g.path + "/" + dir
} }
_, dc, _, err := g.client.Repositories.GetContents(g.owner, g.repository, path, nil) _, dc, _, err := g.service.GetContents(g.owner, g.repository, path, nil)
if err != nil { if err != nil {
log.Printf("Failed to get contents at path: %s: %v", path, err) log.Printf("Failed to get contents at path: %s: %v", path, err)
return nil, err return nil, err
......
...@@ -17,7 +17,6 @@ limitations under the License. ...@@ -17,7 +17,6 @@ limitations under the License.
package registry package registry
import ( import (
"github.com/google/go-github/github"
"github.com/kubernetes/deployment-manager/common" "github.com/kubernetes/deployment-manager/common"
"fmt" "fmt"
...@@ -54,9 +53,9 @@ type GithubTemplateRegistry struct { ...@@ -54,9 +53,9 @@ type GithubTemplateRegistry struct {
} }
// NewGithubTemplateRegistry creates a GithubTemplateRegistry. // NewGithubTemplateRegistry creates a GithubTemplateRegistry.
func NewGithubTemplateRegistry(name, shortURL string, client *github.Client) (GithubTemplateRegistry, error) { func NewGithubTemplateRegistry(name, shortURL string, service RepositoryService) (GithubTemplateRegistry, error) {
format := fmt.Sprintf("%s;%s", common.VersionedRegistry, common.CollectionRegistry) format := fmt.Sprintf("%s;%s", common.VersionedRegistry, common.CollectionRegistry)
gr, err := newGithubRegistry(name, shortURL, common.RegistryFormat(format), client) gr, err := newGithubRegistry(name, shortURL, common.RegistryFormat(format), service)
if err != nil { if err != nil {
return GithubTemplateRegistry{}, err return GithubTemplateRegistry{}, err
} }
...@@ -113,7 +112,7 @@ func (g GithubTemplateRegistry) GetDownloadURLs(t Type) ([]*url.URL, error) { ...@@ -113,7 +112,7 @@ func (g GithubTemplateRegistry) GetDownloadURLs(t Type) ([]*url.URL, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
_, dc, _, err := g.client.Repositories.GetContents(g.owner, g.repository, path, nil) _, dc, _, err := g.service.GetContents(g.owner, g.repository, path, nil)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot list versions at path %s: %v", path, err) return nil, fmt.Errorf("cannot list versions at path %s: %v", path, err)
} }
...@@ -153,7 +152,7 @@ func (g GithubTemplateRegistry) getDirs(dir string) ([]string, error) { ...@@ -153,7 +152,7 @@ func (g GithubTemplateRegistry) getDirs(dir string) ([]string, error) {
path = g.path + "/" + dir path = g.path + "/" + dir
} }
_, dc, _, err := g.client.Repositories.GetContents(g.owner, g.repository, path, nil) _, dc, _, err := g.service.GetContents(g.owner, g.repository, path, nil)
if err != nil { if err != nil {
log.Printf("Failed to get contents at path: %s: %v", path, err) log.Printf("Failed to get contents at path: %s: %v", path, err)
return nil, err return nil, err
......
...@@ -17,7 +17,6 @@ limitations under the License. ...@@ -17,7 +17,6 @@ limitations under the License.
package registry package registry
import ( import (
"github.com/google/go-github/github"
"github.com/kubernetes/deployment-manager/common" "github.com/kubernetes/deployment-manager/common"
"github.com/kubernetes/deployment-manager/util" "github.com/kubernetes/deployment-manager/util"
...@@ -28,55 +27,71 @@ import ( ...@@ -28,55 +27,71 @@ import (
"sync" "sync"
) )
// RegistryProvider returns factories for creating registry clients. // RegistryProvider is a factory for Registry instances.
type RegistryProvider interface { type RegistryProvider interface {
GetRegistryByShortURL(URL string) (Registry, error) GetRegistryByShortURL(URL string) (Registry, error)
GetRegistryByName(registryName string) (Registry, error) GetRegistryByName(registryName string) (Registry, error)
}
// GithubRegistryProvider is a factory for GithubRegistry instances.
type GithubRegistryProvider interface {
GetGithubRegistry(cr common.Registry) (GithubRegistry, error) GetGithubRegistry(cr common.Registry) (GithubRegistry, error)
} }
func NewDefaultRegistryProvider() RegistryProvider { func NewDefaultRegistryProvider() RegistryProvider {
return NewRegistryProvider(nil, nil)
}
func NewRegistryProvider(rs common.RegistryService, grp GithubRegistryProvider) RegistryProvider {
if rs == nil {
rs = NewInmemRegistryService()
}
registries := make(map[string]Registry) registries := make(map[string]Registry)
rs := NewInmemRegistryService() rp := registryProvider{rs: rs, registries: registries}
return &DefaultRegistryProvider{registries: registries, rs: rs} if grp == nil {
grp = rp
}
rp.grp = grp
return rp
} }
type DefaultRegistryProvider struct { type registryProvider struct {
sync.RWMutex sync.RWMutex
registries map[string]Registry
rs common.RegistryService rs common.RegistryService
grp GithubRegistryProvider
registries map[string]Registry
} }
// Deprecated: Use GetRegistryByShortURL instead. func (rp registryProvider) GetRegistryByShortURL(URL string) (Registry, error) {
func (drp DefaultRegistryProvider) GetRegistryByURL(URL string) (Registry, error) { rp.RLock()
return drp.GetRegistryByShortURL(URL) defer rp.RUnlock()
}
func (drp DefaultRegistryProvider) GetRegistryByShortURL(URL string) (Registry, error) {
drp.RLock()
defer drp.RUnlock()
r := drp.findRegistryByShortURL(URL) r := rp.findRegistryByShortURL(URL)
if r == nil { if r == nil {
cr, err := drp.rs.GetByURL(URL) cr, err := rp.rs.GetByURL(URL)
if err != nil { if err != nil {
return nil, err return nil, err
} }
r, err := drp.GetGithubRegistry(*cr) r, err := rp.GetGithubRegistry(*cr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
drp.registries[r.GetRegistryName()] = r rp.registries[r.GetRegistryName()] = r
} }
return r, nil return r, nil
} }
func (drp DefaultRegistryProvider) findRegistryByShortURL(URL string) Registry { // findRegistryByShortURL trims the scheme from both the supplied URL
for _, r := range drp.registries { // and the short URL returned by GetRegistryShortURL.
if strings.HasPrefix(URL, r.GetRegistryShortURL()) { func (rp registryProvider) findRegistryByShortURL(URL string) Registry {
trimmed := util.TrimURLScheme(URL)
for _, r := range rp.registries {
if strings.HasPrefix(trimmed, util.TrimURLScheme(r.GetRegistryShortURL())) {
return r return r
} }
} }
...@@ -84,23 +99,23 @@ func (drp DefaultRegistryProvider) findRegistryByShortURL(URL string) Registry { ...@@ -84,23 +99,23 @@ func (drp DefaultRegistryProvider) findRegistryByShortURL(URL string) Registry {
return nil return nil
} }
func (drp DefaultRegistryProvider) GetRegistryByName(registryName string) (Registry, error) { func (rp registryProvider) GetRegistryByName(registryName string) (Registry, error) {
drp.RLock() rp.RLock()
defer drp.RUnlock() defer rp.RUnlock()
r, ok := drp.registries[registryName] r, ok := rp.registries[registryName]
if !ok { if !ok {
cr, err := drp.rs.Get(registryName) cr, err := rp.rs.Get(registryName)
if err != nil { if err != nil {
return nil, err return nil, err
} }
r, err := drp.GetGithubRegistry(*cr) r, err := rp.GetGithubRegistry(*cr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
drp.registries[r.GetRegistryName()] = r rp.registries[r.GetRegistryName()] = r
} }
return r, nil return r, nil
...@@ -116,15 +131,15 @@ func ParseRegistryFormat(rf common.RegistryFormat) map[common.RegistryFormat]boo ...@@ -116,15 +131,15 @@ func ParseRegistryFormat(rf common.RegistryFormat) map[common.RegistryFormat]boo
return result return result
} }
func (drp DefaultRegistryProvider) GetGithubRegistry(cr common.Registry) (GithubRegistry, error) { func (rp registryProvider) GetGithubRegistry(cr common.Registry) (GithubRegistry, error) {
if cr.Type == common.GithubRegistryType { if cr.Type == common.GithubRegistryType {
fMap := ParseRegistryFormat(cr.Format) fMap := ParseRegistryFormat(cr.Format)
if fMap[common.UnversionedRegistry] && fMap[common.OneLevelRegistry] { if fMap[common.UnversionedRegistry] && fMap[common.OneLevelRegistry] {
return NewGithubPackageRegistry(cr.Name, cr.URL, github.NewClient(nil)) return NewGithubPackageRegistry(cr.Name, cr.URL, nil)
} }
if fMap[common.VersionedRegistry] && fMap[common.CollectionRegistry] { if fMap[common.VersionedRegistry] && fMap[common.CollectionRegistry] {
return NewGithubTemplateRegistry(cr.Name, cr.URL, github.NewClient(nil)) return NewGithubTemplateRegistry(cr.Name, cr.URL, nil)
} }
return nil, fmt.Errorf("unknown registry format: %s", cr.Format) return nil, fmt.Errorf("unknown registry format: %s", cr.Format)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment