fix: mock deployer method signature (#1389)

Updates the mock deployer to have the correct method signature
(match interface it is mocking). Usage is illustrated using a
refactored implementation of mock.NewDeployerWithResult.
This commit is contained in:
Luke Kingland 2022-10-31 23:04:13 +09:00 committed by GitHub
parent 55383b7e24
commit c9b8d8e87c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 26 deletions

View File

@ -441,14 +441,14 @@ func TestClient_New_Delegation(t *testing.T) {
return "", nil return "", nil
} }
deployer.DeployFn = func(f fn.Function) error { deployer.DeployFn = func(_ context.Context, f fn.Function) (res fn.DeploymentResult, err error) {
if f.Name != expectedName { if f.Name != expectedName {
t.Fatalf("deployer expected name '%v', got '%v'", expectedName, f.Name) t.Fatalf("deployer expected name '%v', got '%v'", expectedName, f.Name)
} }
if f.Image != expectedImage { if f.Image != expectedImage {
t.Fatalf("deployer expected image '%v', got '%v'", expectedImage, f.Image) t.Fatalf("deployer expected image '%v', got '%v'", expectedImage, f.Image)
} }
return nil return
} }
// Invocation // Invocation
@ -559,12 +559,12 @@ func TestClient_Update(t *testing.T) {
expectedImage = "example.com/alice/testUpdate:latest" expectedImage = "example.com/alice/testUpdate:latest"
builder = mock.NewBuilder() builder = mock.NewBuilder()
pusher = mock.NewPusher() pusher = mock.NewPusher()
deployer = mock.NewDeployerWithResult(&fn.DeploymentResult{ deployer = mock.NewDeployerWithResult(fn.DeploymentResult{
Status: fn.Deployed, Status: fn.Deployed,
URL: "example.com", URL: "example.com",
Namespace: "test-ns", Namespace: "test-ns",
}) })
deployerUpdated = mock.NewDeployerWithResult(&fn.DeploymentResult{ deployerUpdated = mock.NewDeployerWithResult(fn.DeploymentResult{
Status: fn.Updated, Status: fn.Updated,
URL: "example.com", URL: "example.com",
Namespace: "test-ns", Namespace: "test-ns",
@ -608,14 +608,14 @@ func TestClient_Update(t *testing.T) {
} }
// Update whose implementaiton verifed the expected name and image // Update whose implementaiton verifed the expected name and image
deployer.DeployFn = func(f fn.Function) error { deployer.DeployFn = func(_ context.Context, f fn.Function) (res fn.DeploymentResult, err error) {
if f.Name != expectedName { if f.Name != expectedName {
t.Fatalf("updater expected name '%v', got '%v'", expectedName, f.Name) t.Fatalf("updater expected name '%v', got '%v'", expectedName, f.Name)
} }
if f.Image != expectedImage { if f.Image != expectedImage {
t.Fatalf("updater expected image '%v', got '%v'", expectedImage, f.Image) t.Fatalf("updater expected image '%v', got '%v'", expectedImage, f.Image)
} }
return nil return
} }
// Invoke the creation, triggering the function delegates, and // Invoke the creation, triggering the function delegates, and

View File

@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"os" "os"
@ -114,11 +115,11 @@ func testImageAndRegistry(cmdFn commandConstructor, t *testing.T) {
// the resultant Function should have the registry populated and image // the resultant Function should have the registry populated and image
// derived from the name. // derived from the name.
cmd.SetArgs([]string{"--registry=example.com/alice"}) cmd.SetArgs([]string{"--registry=example.com/alice"})
deployer.DeployFn = func(f fn.Function) error { deployer.DeployFn = func(_ context.Context, f fn.Function) (res fn.DeploymentResult, err error) {
if f.Registry != "example.com/alice" { if f.Registry != "example.com/alice" {
t.Fatal("registry flag not provided to deployer") t.Fatal("registry flag not provided to deployer")
} }
return nil return
} }
if err := cmd.Execute(); err != nil { if err := cmd.Execute(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -129,11 +130,11 @@ func testImageAndRegistry(cmdFn commandConstructor, t *testing.T) {
// Image member set to what was explicitly provided via the --image flag // Image member set to what was explicitly provided via the --image flag
// (not a derived name) // (not a derived name)
cmd.SetArgs([]string{"--image=example.com/alice/myfunc"}) cmd.SetArgs([]string{"--image=example.com/alice/myfunc"})
deployer.DeployFn = func(f fn.Function) error { deployer.DeployFn = func(_ context.Context, f fn.Function) (res fn.DeploymentResult, err error) {
if f.Image != "example.com/alice/myfunc" { if f.Image != "example.com/alice/myfunc" {
t.Fatalf("deployer expected f.Image 'example.com/alice/myfunc', got '%v'", f.Image) t.Fatalf("deployer expected f.Image 'example.com/alice/myfunc', got '%v'", f.Image)
} }
return nil return
} }
if err := cmd.Execute(); err != nil { if err := cmd.Execute(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -143,14 +144,14 @@ func testImageAndRegistry(cmdFn commandConstructor, t *testing.T) {
// they should both be plumbed through such that downstream agents (deployer // they should both be plumbed through such that downstream agents (deployer
// in this case) see them set on the Function and can act accordingly. // in this case) see them set on the Function and can act accordingly.
cmd.SetArgs([]string{"--registry=example.com/alice", "--image=example.com/alice/subnamespace/myfunc"}) cmd.SetArgs([]string{"--registry=example.com/alice", "--image=example.com/alice/subnamespace/myfunc"})
deployer.DeployFn = func(f fn.Function) error { deployer.DeployFn = func(_ context.Context, f fn.Function) (res fn.DeploymentResult, err error) {
if f.Registry != "example.com/alice" { if f.Registry != "example.com/alice" {
t.Fatal("registry flag value not seen on the Function by the deployer") t.Fatal("registry flag value not seen on the Function by the deployer")
} }
if f.Image != "example.com/alice/subnamespace/myfunc" { if f.Image != "example.com/alice/subnamespace/myfunc" {
t.Fatal("image flag value not seen on the Function by deployer") t.Fatal("image flag value not seen on the Function by deployer")
} }
return nil return
} }
if err := cmd.Execute(); err != nil { if err := cmd.Execute(); err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -8,27 +8,25 @@ import (
type Deployer struct { type Deployer struct {
DeployInvoked bool DeployInvoked bool
DeployFn func(fn.Function) error DeployFn func(context.Context, fn.Function) (fn.DeploymentResult, error)
DeployResult *fn.DeploymentResult
} }
func NewDeployer() *Deployer { func NewDeployer() *Deployer {
return &Deployer{ return &Deployer{
DeployFn: func(fn.Function) error { return nil }, DeployFn: func(context.Context, fn.Function) (fn.DeploymentResult, error) { return fn.DeploymentResult{}, nil },
}
}
func NewDeployerWithResult(result *fn.DeploymentResult) *Deployer {
return &Deployer{
DeployFn: func(fn.Function) error { return nil },
DeployResult: result,
} }
} }
func (i *Deployer) Deploy(ctx context.Context, f fn.Function) (fn.DeploymentResult, error) { func (i *Deployer) Deploy(ctx context.Context, f fn.Function) (fn.DeploymentResult, error) {
i.DeployInvoked = true i.DeployInvoked = true
if i.DeployResult != nil { return i.DeployFn(ctx, f)
return *i.DeployResult, i.DeployFn(f) }
}
return fn.DeploymentResult{}, i.DeployFn(f) // NewDeployerWithResult is a convenience method for creating a mock deployer
// with a deploy function implementation which returns the given result
// and no error.
func NewDeployerWithResult(result fn.DeploymentResult) *Deployer {
return &Deployer{
DeployFn: func(context.Context, fn.Function) (fn.DeploymentResult, error) { return result, nil },
}
} }