From f115a7a87198c4fcbaac83766af7aa7abd80a9de Mon Sep 17 00:00:00 2001 From: Luke Kingland Date: Wed, 10 Mar 2021 19:57:49 +0900 Subject: [PATCH] client: remove implicit call to Build on Deploy --- client.go | 10 +++++++--- client_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index afac4f7f..6649f240 100644 --- a/client.go +++ b/client.go @@ -33,6 +33,9 @@ type Client struct { progressListener ProgressListener // progress listener } +// ErrNotBuilt indicates the Function has not yet been built. +var ErrNotBuilt = errors.New("not built") + // Builder of Function source to runnable image. type Builder interface { // Build a Function project with source located at path. @@ -406,9 +409,10 @@ func (c *Client) Deploy(path string) (err error) { return } - // Build the Function - if err = c.Build(f.Root); err != nil { - return + // Functions must be built (have an associated image) before being deployed. + // Note that externally built images may be specified in the func.yaml + if !f.Built() { + return ErrNotBuilt } // Push the image for the named service to the configured registry diff --git a/client_test.go b/client_test.go index 70e31936..ab6d3954 100644 --- a/client_test.go +++ b/client_test.go @@ -674,6 +674,36 @@ func TestListOutsideRoot(t *testing.T) { } } +// TestDeployUnbuilt ensures that a call to deploy a Function which was not +// fully created (ie. was only initialized, not actually built and deploys) +// yields an expected, and informative, error. +func TestDeployUnbuilt(t *testing.T) { + root := "testdata/example.com/testDeploy" // Root from which to run the test + if err := os.MkdirAll(root, 0700); err != nil { + t.Fatal(err) + } + defer os.RemoveAll(root) + + // New Client + client := bosonFunc.New(bosonFunc.WithRegistry(TestRegistry)) + + // Initialize (half-create) a new Function at root + if err := client.Create(bosonFunc.Function{Root: root}); err != nil { + t.Fatal(err) + } + + // Now try to deploy it. Ie. without having run the necessary build step. + err := client.Deploy(root) + if err == nil { + t.Fatal("did not receive an error attempting to deploy an unbuilt Function") + } + + if !errors.Is(err, bosonFunc.ErrNotBuilt) { + t.Fatalf("did not receive expected error type. Expected ErrNotBuilt, got %T", err) + } + +} + // TODO: The tests which confirm an error is generated do not currently test // that the expected error is received; just that any error is generated. // This should be replaced with typed errors or at a minimum code prefixes