diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2019-11-23 18:41:13 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2019-11-23 18:41:13 +0300 |
commit | bc61f03703dffb79c3d9d887385b968315362caa (patch) | |
tree | 04a160cdb5494784af9956400a1b4853c5a5a7f0 | |
parent | 08fb0bfc792db7c1b006ea066904d19a3456f99f (diff) |
Implement a transitional domains source
-rw-r--r-- | app.go | 2 | ||||
-rw-r--r-- | internal/source/disk/disk.go | 4 | ||||
-rw-r--r-- | internal/source/domains.go | 54 | ||||
-rw-r--r-- | internal/source/domains_test.go | 61 | ||||
-rw-r--r-- | internal/source/gitlab/gitlab.go | 16 | ||||
-rw-r--r-- | internal/source/source.go | 4 |
6 files changed, 119 insertions, 22 deletions
@@ -348,7 +348,7 @@ func (a *theApp) Run() { a.listenMetricsFD(&wg, a.ListenMetrics) } - a.domains.Start(a.Domain) + a.domains.Read(a.Domain) wg.Wait() } diff --git a/internal/source/disk/disk.go b/internal/source/disk/disk.go index 2283cad4..a3ae8901 100644 --- a/internal/source/disk/disk.go +++ b/internal/source/disk/disk.go @@ -52,9 +52,9 @@ func (d *Disk) IsReady() bool { return d.dm != nil } -// Start starts the domain source, in this case it is reading domains from +// Read starts the domain source, in this case it is reading domains from // groups on disk concurrently. -func (d *Disk) Start(rootDomain string) { +func (d *Disk) Read(rootDomain string) { go Watch(rootDomain, d.updateDomains, time.Second) } diff --git a/internal/source/domains.go b/internal/source/domains.go index df375883..92bab663 100644 --- a/internal/source/domains.go +++ b/internal/source/domains.go @@ -3,12 +3,21 @@ package source import ( "gitlab.com/gitlab-org/gitlab-pages/internal/domain" "gitlab.com/gitlab-org/gitlab-pages/internal/source/disk" + "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab" ) +var newSourceDomains = []string{ + "pages-project-poc.gitlab.io", + "pages-namespace-poc.gitlab.io", + "pages-custom-poc.grzegorz.co", +} + // Domains struct represents a map of all domains supported by pages. It is -// currently reading them from disk. +// currently using two sources during the transition to the new GitLab domains +// source. type Domains struct { - Source + gitlab Source + disk *disk.Disk // legacy disk source } // NewDomains is a factory method for domains initializing a mutex. It should @@ -16,13 +25,44 @@ type Domains struct { // nil value. func NewDomains() *Domains { return &Domains{ - Source: disk.New(), + disk: disk.New(), + gitlab: gitlab.New(), } } -// GetDomain overridden here allows us to switch behavior and the domains -// source for some subset of domains, to test / PoC the new GitLab Domains -// Source that we plan to introduce +// GetDomain retrieves a domain information from a source. We are using two +// sources here because it allows us to switch behavior and the domain source +// for some subset of domains, to test / PoC the new GitLab Domains Source that +// we plan to use to replace the disk source. func (d *Domains) GetDomain(name string) *domain.Domain { - return d.Source.GetDomain(name) + return d.source(name).GetDomain(name) +} + +// HasDomain checks if domain exists. It is using new and the legacy domains +// source. +func (d *Domains) HasDomain(name string) bool { + return d.source(name).HasDomain(name) +} + +// Start starts the disk domain source. It is DEPRECATED, because we want to +// remove it entirely when disk source gets removed. +func (d *Domains) Read(rootDomain string) { + d.disk.Read(rootDomain) +} + +// IsReady checks if the disk domain source managed to traverse entire pages +// filesystem and is ready for use. It is DEPRECATED, because we want to remove +// it entirely when disk source gets removed. +func (d *Domains) IsReady() bool { + return d.disk.IsReady() +} + +func (d *Domains) source(domain string) Source { + for _, name := range newSourceDomains { + if domain == name { + return d.gitlab + } + } + + return d.disk } diff --git a/internal/source/domains_test.go b/internal/source/domains_test.go new file mode 100644 index 00000000..1da50db8 --- /dev/null +++ b/internal/source/domains_test.go @@ -0,0 +1,61 @@ +package source + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + + "gitlab.com/gitlab-org/gitlab-pages/internal/domain" + "gitlab.com/gitlab-org/gitlab-pages/internal/source/disk" +) + +type mockSource struct { + mock.Mock +} + +func (m mockSource) GetDomain(name string) *domain.Domain { + args := m.Called(name) + + return args.Get(0).(*domain.Domain) +} + +func (m mockSource) HasDomain(name string) bool { + args := m.Called(name) + + return args.Bool(0) +} + +func TestSourceTransition(t *testing.T) { + testDomain := newSourceDomains[0] + + t.Run("when requesting a test domain", func(t *testing.T) { + newSource := new(mockSource) + newSource.On("GetDomain", testDomain). + Return(&domain.Domain{Name: testDomain}). + Once() + newSource.On("HasDomain", testDomain).Return(true).Once() + defer newSource.AssertExpectations(t) + + domains := &Domains{ + disk: disk.New(), + gitlab: newSource, + } + + domains.GetDomain(testDomain) + domains.HasDomain(testDomain) + }) + + t.Run("when requesting a non-test domain", func(t *testing.T) { + newSource := new(mockSource) + defer newSource.AssertExpectations(t) + + domains := &Domains{ + disk: disk.New(), + gitlab: newSource, + } + + assert.Nil(t, domains.GetDomain("some.test.io")) + assert.False(t, domains.HasDomain("some.test.io")) + }) +} diff --git a/internal/source/gitlab/gitlab.go b/internal/source/gitlab/gitlab.go index 8ac8fd85..b0efb01d 100644 --- a/internal/source/gitlab/gitlab.go +++ b/internal/source/gitlab/gitlab.go @@ -14,13 +14,20 @@ type Gitlab struct { cache Cache } +// New returns a new instance of gitlab domain source. +func New() *Gitlab { + return &Gitlab{} +} + // GetDomain return a representation of a domain that we have fetched from // GitLab +// It should return source.Lookup TODO func (g *Gitlab) GetDomain(name string) *domain.Domain { return nil } // HasDomain checks if a domain is known to GitLab +// TODO lookup status code etc. func (g *Gitlab) HasDomain(name string) bool { return g.GetDomain(name) != nil } @@ -30,12 +37,3 @@ func (g *Gitlab) HasDomain(name string) bool { func (g *Gitlab) Resolve(*http.Request) (*serving.LookupPath, string, error) { return nil, "", nil } - -// Watch starts Gitlab domains source TODO remove -func (g *Gitlab) Watch(rootDomain string) { -} - -// Ready checks if Gitlab domains source can be used TODO remove -func (g *Gitlab) Ready() bool { - return false -} diff --git a/internal/source/source.go b/internal/source/source.go index f7407283..da63cbcc 100644 --- a/internal/source/source.go +++ b/internal/source/source.go @@ -2,10 +2,8 @@ package source import "gitlab.com/gitlab-org/gitlab-pages/internal/domain" -// Source represents an abstract interface of a domains configuration source +// Source represents an abstract interface of a domains configuration source. type Source interface { GetDomain(string) *domain.Domain HasDomain(string) bool - Start(rootDomain string) // Deprecated - transitional interface - IsReady() bool // Deprecated - transitional interface } |