separate deploy from create for the --local flag

This commit is contained in:
Luke K 2020-03-04 15:12:05 +00:00
parent 86cdedec91
commit c78be32b72
No known key found for this signature in database
GPG Key ID: 4896F75BAF2E1966
3 changed files with 89 additions and 85 deletions

View File

@ -149,19 +149,6 @@ func (c *Client) Create(language string) (err error) {
return return
} }
// Deploy the initialized service function, returning its publicly
// addressible name for possible registration.
address, err := c.deployer.Deploy(c.name, c.root)
if err != nil {
return
}
// TODO
// Dervive the cluster address of the service.
// Derive the public domain of the service from the directory path.
c.dnsProvider.Provide(c.name, address)
// Associate the public domain to the cluster-defined address.
return return
} }
@ -245,8 +232,22 @@ func pathToDomain(path string, maxLevels int) string {
return domain return domain
} }
func (c *Client) Deploy() error { // Deploy the code at root, using the derived name, using the configured deployer.
return errors.New("Not Implemented") func (c *Client) Deploy() (err error) {
// Deploy the initialized service function, returning its publicly
// addressible name for possible registration.
address, err := c.deployer.Deploy(c.name, c.root)
if err != nil {
return
}
// TODO
// Dervive the cluster address of the service.
// Derive the public domain of the service from the directory path.
c.dnsProvider.Provide(c.name, address)
// Associate the public domain to the cluster-defined address.
return
} }
// Manual implementations (noops) of required interfaces. // Manual implementations (noops) of required interfaces.

View File

@ -8,6 +8,20 @@ import (
"github.com/lkingland/faas/client/mock" "github.com/lkingland/faas/client/mock"
) )
// TestInvalidDomain ensures that creating from a directory strucutre
// where the domain can not be derived while simultaneously failing to provide
// an explicit name fails.
func TestInvalidDomain(t *testing.T) {
_, err := client.New(
// Ensure no domain is found even if we are actually running within a path
// from which a domain could be derived.
client.WithDomainSearchLimit(0),
)
if err == nil {
t.Fatal("no error generated for unspecified and underivable name")
}
}
// TestCreate ensures that instantiation completes without error when provided with a // TestCreate ensures that instantiation completes without error when provided with a
// language. A single client instance services a single Service Function instance // language. A single client instance services a single Service Function instance
// and as such requires the desired effective DNS for the function. This is an optional // and as such requires the desired effective DNS for the function. This is an optional
@ -28,72 +42,6 @@ func TestCreate(t *testing.T) {
} }
} }
// TestCreateInvalidName ensures that creating from a directory strucutre
// where the domain can not be derived while simultaneously failing to provide
// an explicit name fails.
func TestCreateDomainInvalid(t *testing.T) {
_, err := client.New(
// Ensure no domain is found even if we are actually running within a path
// from which a domain could be derived.
client.WithDomainSearchLimit(0),
)
if err == nil {
t.Fatal("no error generated for unspecified and underivable name")
}
}
// TestCreateDomain ensures that the effective domain is dervied from
// directory structure. See the unit tests for pathToDomain for details.
func TestCreateDomain(t *testing.T) {
// the mock dns provider does nothing but receive the caluclated
// domain name via it's Provide(domain) method, which is the value
// being tested here.
dnsProvider := mock.NewDNSProvider()
client, err := client.New(
client.WithRoot("./testdata/example.com"), // set function root
client.WithDomainSearchLimit(1), // Limit recursion to one level
client.WithDNSProvider(dnsProvider), // will receive the final value
)
if err != nil {
t.Fatal(err)
}
if err := client.Create("go"); err != nil {
t.Fatal(err)
}
if !dnsProvider.ProvideInvoked {
t.Fatal("dns provider was not invoked")
}
if dnsProvider.NameRequested != "example.com" {
t.Fatalf("expected 'example.com', got '%v'", dnsProvider.NameRequested)
}
}
// TestCreateSubdomain ensures that a subdirectory is interpreted as a subdomain
// when calculating final domain. See the unit tests for pathToDomain for the
// details and edge cases of this caluclation.
func TestCreateSubdomain(t *testing.T) {
dnsProvider := mock.NewDNSProvider()
client, err := client.New(
client.WithRoot("./testdata/example.com/admin"),
client.WithDomainSearchLimit(2),
client.WithDNSProvider(dnsProvider),
)
if err != nil {
t.Fatal(err)
}
if err := client.Create("go"); err != nil {
t.Fatal(err)
}
if !dnsProvider.ProvideInvoked {
t.Fatal("dns provider was not invoked")
}
if dnsProvider.NameRequested != "admin.example.com" {
t.Fatalf("expected 'admin.example.com', got '%v'", dnsProvider.NameRequested)
}
}
// TestCreateInitializes ensures that a call to Create invokes the Service // TestCreateInitializes ensures that a call to Create invokes the Service
// Function Initializer with correct parameters. // Function Initializer with correct parameters.
func TestCreateInitializes(t *testing.T) { func TestCreateInitializes(t *testing.T) {
@ -129,9 +77,9 @@ func TestCreateInitializes(t *testing.T) {
} }
} }
// TestCreateDeploys ensures that a call to Create invokes the Sevice Function // TestDeploy ensures that a call to Deploy invokes the Sevice Function
// Deployer with the correct parameters. // Deployer with the correct parameters.
func TestCreateDeploys(t *testing.T) { func TestDeploy(t *testing.T) {
deployer := mock.NewDeployer() deployer := mock.NewDeployer()
client, err := client.New( client, err := client.New(
client.WithRoot("./testdata/example.com/admin"), // set function root client.WithRoot("./testdata/example.com/admin"), // set function root
@ -153,10 +101,62 @@ func TestCreateDeploys(t *testing.T) {
} }
return return
} }
if err := client.Create("go"); err != nil { if err := client.Deploy(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !deployer.DeployInvoked { if !deployer.DeployInvoked {
t.Fatal("deployer was not invoked") t.Fatal("deployer was not invoked")
} }
} }
// TestDeployDomain ensures that the effective domain is dervied from
// directory structure. See the unit tests for pathToDomain for details.
func TestDeployDomain(t *testing.T) {
// the mock dns provider does nothing but receive the caluclated
// domain name via it's Provide(domain) method, which is the value
// being tested here.
dnsProvider := mock.NewDNSProvider()
client, err := client.New(
client.WithRoot("./testdata/example.com"), // set function root
client.WithDomainSearchLimit(1), // Limit recursion to one level
client.WithDNSProvider(dnsProvider), // will receive the final value
)
if err != nil {
t.Fatal(err)
}
if err := client.Deploy(); err != nil {
t.Fatal(err)
}
if !dnsProvider.ProvideInvoked {
t.Fatal("dns provider was not invoked")
}
if dnsProvider.NameRequested != "example.com" {
t.Fatalf("expected 'example.com', got '%v'", dnsProvider.NameRequested)
}
}
// TestDeploySubdomain ensures that a subdirectory is interpreted as a subdomain
// when calculating final domain. See the unit tests for pathToDomain for the
// details and edge cases of this caluclation.
func TestDeploySubdomain(t *testing.T) {
dnsProvider := mock.NewDNSProvider()
client, err := client.New(
client.WithRoot("./testdata/example.com/admin"),
client.WithDomainSearchLimit(2),
client.WithDNSProvider(dnsProvider),
)
if err != nil {
t.Fatal(err)
}
if err := client.Deploy(); err != nil {
t.Fatal(err)
}
if !dnsProvider.ProvideInvoked {
t.Fatal("dns provider was not invoked")
}
if dnsProvider.NameRequested != "admin.example.com" {
t.Fatalf("expected 'admin.example.com', got '%v'", dnsProvider.NameRequested)
}
}

View File

@ -37,7 +37,10 @@ func create(cmd *cobra.Command, args []string) (err error) {
) )
// Instantiate a client, specifying optional verbosity. // Instantiate a client, specifying optional verbosity.
client := client.New(client.WithVerbose(verbose)) client, err := client.New(client.WithVerbose(verbose))
if err != nil {
return
}
// Invoke Service Funcation creation. // Invoke Service Funcation creation.
if err = client.Create(language); err != nil { if err = client.Create(language); err != nil {