Skip to content

Commit abef8d6

Browse files
committed
Made context a mandatory parameter instead
Signed-off-by: David Gannon <19214156+dgannon991@users.noreply.github.com>
1 parent 1b18baa commit abef8d6

19 files changed

+75
-48
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ delete-test-cluster:
3434

3535
GOPATH := $(shell go env GOPATH)
3636
HAS_GOLANGCI := $(shell $(CHECK) golangci-lint)
37-
GOLANGCI_VERSION := v1.51.2
37+
GOLANGCI_VERSION := v1.63.4
3838
HAS_KIND := $(shell $(CHECK) kind)
3939
HAS_KUBECTL := $(shell $(CHECK) kubectl)
4040
HAS_GOCOV_XML := $(shell $(CHECK) gocov-xml;)

action/action.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package action
22

33
import (
4+
"context"
45
"crypto/sha256"
56
"encoding/hex"
67
"encoding/json"
@@ -53,7 +54,7 @@ func New(d driver.Driver) Action {
5354
// caller is responsible for persisting the claim records and outputs using the
5455
// SaveOperationResult function. An error is only returned when the operation could not
5556
// be executed, otherwise any error is returned in the OperationResult.
56-
func (a Action) Run(c claim.Claim, creds valuesource.Set, opCfgs ...OperationConfigFunc) (driver.OperationResult, claim.Result, error) {
57+
func (a Action) Run(ctx context.Context, c claim.Claim, creds valuesource.Set, opCfgs ...OperationConfigFunc) (driver.OperationResult, claim.Result, error) {
5758
if a.Driver == nil {
5859
return driver.OperationResult{}, claim.Result{}, errors.New("the action driver is not set")
5960
}
@@ -84,7 +85,7 @@ func (a Action) Run(c claim.Claim, creds valuesource.Set, opCfgs ...OperationCon
8485
}
8586

8687
var opErr *multierror.Error
87-
opResult, err := a.Driver.Run(op)
88+
opResult, err := a.Driver.Run(ctx, op)
8889
if err != nil {
8990
opErr = multierror.Append(opErr, err)
9091
}
@@ -279,7 +280,10 @@ func setOutputsOnClaimResult(c claim.Claim, result *claim.Result, opResult drive
279280

280281
for outputName, outputValue := range opResult.Outputs {
281282
outputDef, isDefined := c.Bundle.Outputs[outputName]
282-
result.OutputMetadata.SetGeneratedByBundle(outputName, isDefined)
283+
err := result.OutputMetadata.SetGeneratedByBundle(outputName, isDefined)
284+
if err != nil {
285+
outputErrors = append(outputErrors, err)
286+
}
283287
if isDefined {
284288
err := validateOutputType(c.Bundle, outputName, outputDef, outputValue)
285289
if err != nil {
@@ -288,7 +292,10 @@ func setOutputsOnClaimResult(c claim.Claim, result *claim.Result, opResult drive
288292
}
289293

290294
if outputValue != "" {
291-
result.OutputMetadata.SetContentDigest(outputName, buildOutputContentDigest(outputValue))
295+
err := result.OutputMetadata.SetContentDigest(outputName, buildOutputContentDigest(outputValue))
296+
if err != nil {
297+
outputErrors = append(outputErrors, err)
298+
}
292299
}
293300
}
294301

action/action_test.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package action
22

33
import (
4+
"context"
45
"encoding/json"
56
"errors"
67
"fmt"
@@ -31,7 +32,7 @@ type mockDriver struct {
3132
func (d *mockDriver) Handles(imageType string) bool {
3233
return d.shouldHandle
3334
}
34-
func (d *mockDriver) Run(op *driver.Operation) (driver.OperationResult, error) {
35+
func (d *mockDriver) Run(ctx context.Context, op *driver.Operation) (driver.OperationResult, error) {
3536
d.Operation = op
3637
fmt.Fprintln(op.Out, "mocked running the bundle")
3738
return d.Result, d.Error
@@ -720,7 +721,7 @@ func TestAction_RunAction(t *testing.T) {
720721
inst := New(d)
721722
inst.SaveLogs = true
722723

723-
opResult, claimResult, err := inst.Run(c, mockSet, out)
724+
opResult, claimResult, err := inst.Run(context.Background(), c, mockSet, out)
724725
require.NoError(t, err)
725726
require.NoError(t, opResult.Error)
726727
assert.Equal(t, claim.ActionInstall, c.Action)
@@ -748,7 +749,7 @@ func TestAction_RunAction(t *testing.T) {
748749
inst := New(d)
749750
inst.SaveLogs = false
750751

751-
opResult, _, err := inst.Run(c, mockSet, out)
752+
opResult, _, err := inst.Run(context.Background(), c, mockSet, out)
752753
require.NoError(t, err)
753754
require.NoError(t, opResult.Error)
754755

@@ -772,7 +773,7 @@ func TestAction_RunAction(t *testing.T) {
772773
op.Files["/tmp/another/path"] = "ANOTHER FILE"
773774
return nil
774775
}
775-
_, _, err := inst.Run(c, mockSet, out, addFile)
776+
_, _, err := inst.Run(context.Background(), c, mockSet, out, addFile)
776777
require.NoError(t, err)
777778
assert.Contains(t, d.Operation.Files, "/tmp/another/path")
778779
})
@@ -792,7 +793,7 @@ func TestAction_RunAction(t *testing.T) {
792793
sabotage := func(op *driver.Operation) error {
793794
return errors.New("oops")
794795
}
795-
_, _, err := inst.Run(c, mockSet, out, sabotage)
796+
_, _, err := inst.Run(context.Background(), c, mockSet, out, sabotage)
796797
require.EqualError(t, err, "oops")
797798
})
798799

@@ -805,7 +806,7 @@ func TestAction_RunAction(t *testing.T) {
805806
Error: nil,
806807
}
807808
inst := New(d)
808-
_, claimResult, err := inst.Run(c, mockSet, out)
809+
_, claimResult, err := inst.Run(context.Background(), c, mockSet, out)
809810
require.NoError(t, err)
810811
assert.Equal(t, claim.ActionInstall, c.Action)
811812
assert.Equal(t, claim.StatusSucceeded, claimResult.Status)
@@ -848,7 +849,7 @@ func TestAction_RunAction(t *testing.T) {
848849
Error: nil,
849850
}
850851
inst := New(d)
851-
opResult, _, err := inst.Run(c, mockSet, out)
852+
opResult, _, err := inst.Run(context.Background(), c, mockSet, out)
852853
require.NoError(t, err)
853854

854855
assert.Contains(t, opResult.Outputs, "hasDefault1", "the output always applies so an output value should have been set")
@@ -873,7 +874,7 @@ func TestAction_RunAction(t *testing.T) {
873874
Error: nil,
874875
}
875876
inst := New(d)
876-
opResult, _, err := inst.Run(c, mockSet, out)
877+
opResult, _, err := inst.Run(context.Background(), c, mockSet, out)
877878
require.NoError(t, err)
878879
require.Contains(t, opResult.Error.Error(), "required output noDefault is missing and has no default")
879880
})
@@ -885,7 +886,7 @@ func TestAction_RunAction(t *testing.T) {
885886
Error: errors.New("I always fail"),
886887
}
887888
inst := New(d)
888-
_, _, err := inst.Run(c, mockSet, out)
889+
_, _, err := inst.Run(context.Background(), c, mockSet, out)
889890
require.Error(t, err)
890891
})
891892

@@ -901,7 +902,7 @@ func TestAction_RunAction(t *testing.T) {
901902
Error: errors.New("I always fail"),
902903
}
903904
inst := New(d)
904-
opResult, claimResult, err := inst.Run(c, mockSet, out)
905+
opResult, claimResult, err := inst.Run(context.Background(), c, mockSet, out)
905906
require.NoError(t, err)
906907
require.Contains(t, opResult.Error.Error(), "I always fail")
907908
assert.Equal(t, claim.ActionInstall, c.Action)
@@ -922,7 +923,7 @@ func TestAction_RunAction(t *testing.T) {
922923
Error: errors.New("I always fail"),
923924
}
924925
inst := New(d)
925-
opResult, claimResult, err := inst.Run(c, mockSet, out)
926+
opResult, claimResult, err := inst.Run(context.Background(), c, mockSet, out)
926927
require.Error(t, err, "Unknown action should fail")
927928
require.NoError(t, opResult.Error)
928929
assert.Empty(t, claimResult)

action/example_install_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package action_test
22

33
import (
4+
"context"
45
"fmt"
56
"time"
67

@@ -61,7 +62,7 @@ func Example_install() {
6162
// Pass an empty set of credentials
6263
var creds valuesource.Set
6364

64-
opResult, claimResult, err := a.Run(c, creds)
65+
opResult, claimResult, err := a.Run(context.Background(), c, creds)
6566
if err != nil {
6667
// Something terrible has occurred and we could not even run the bundle
6768
panic(err)

action/example_invoke_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package action_test
22

33
import (
4+
"context"
45
"fmt"
56
"time"
67

@@ -23,9 +24,6 @@ func Example_invoke() {
2324

2425
// Load the previous claim for the installation
2526
existingClaim := createInstallClaim()
26-
if err != nil {
27-
panic(err)
28-
}
2927

3028
// Create a claim for running the custom logs action based on the previous claim
3129
c, err := existingClaim.NewClaim("logs", existingClaim.Bundle, parameters)
@@ -51,7 +49,7 @@ func Example_invoke() {
5149
// Pass an empty set of credentials
5250
var creds valuesource.Set
5351

54-
opResult, claimResult, err := a.Run(c, creds)
52+
opResult, claimResult, err := a.Run(context.Background(), c, creds)
5553
if err != nil {
5654
// Something terrible has occurred and we could not even run the bundle
5755
panic(err)

action/example_running_status_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package action_test
22

33
import (
4+
"context"
45
"fmt"
56
"time"
67

@@ -56,7 +57,7 @@ func Example_runningStatus() {
5657
// Save the upgrade claim in the Running Status
5758
saveResult(c, claim.StatusRunning)
5859

59-
opResult, claimResult, err := a.Run(c, creds)
60+
opResult, claimResult, err := a.Run(context.Background(), c, creds)
6061
if err != nil {
6162
// If the bundle isn't run due to an error preparing,
6263
// record a failure so we aren't left stuck in running

action/example_upgrade_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package action_test
22

33
import (
4+
"context"
45
"fmt"
56
"time"
67

@@ -44,7 +45,7 @@ func Example_upgrade() {
4445
// Pass an empty set of credentials
4546
var creds valuesource.Set
4647

47-
opResult, claimResult, err := a.Run(c, creds)
48+
opResult, claimResult, err := a.Run(context.Background(), c, creds)
4849
if err != nil {
4950
// Something terrible has occurred and we could not even run the bundle
5051
panic(err)

driver/command/command.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package command
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -26,7 +27,7 @@ type Driver struct {
2627
}
2728

2829
// Run executes the command
29-
func (d *Driver) Run(op *driver.Operation) (driver.OperationResult, error) {
30+
func (d *Driver) Run(ctx context.Context, op *driver.Operation) (driver.OperationResult, error) {
3031
return d.exec(op)
3132
}
3233

driver/command/command_nix_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package command
55

66
import (
7+
"context"
78
"os"
89
"testing"
910

@@ -64,7 +65,7 @@ func TestCommandDriverOutputs(t *testing.T) {
6465
t.Fatalf("Expected driver %s to exist Driver Name %s ", name, cmddriver.Name)
6566
}
6667
op := buildOp()
67-
opResult, err := cmddriver.Run(op)
68+
opResult, err := cmddriver.Run(context.Background(), op)
6869
if err != nil {
6970
t.Fatalf("Driver Run failed %v", err)
7071
}
@@ -89,7 +90,7 @@ func TestCommandDriverOutputs(t *testing.T) {
8990
t.Fatalf("Expected driver %s to exist Driver Name %s ", name, cmddriver.Name)
9091
}
9192
op := buildOp()
92-
opResult, err := cmddriver.Run(op)
93+
opResult, err := cmddriver.Run(context.Background(), op)
9394
if err != nil {
9495
t.Fatalf("Driver Run failed %v", err)
9596
}
@@ -114,7 +115,7 @@ func TestCommandDriverOutputs(t *testing.T) {
114115
t.Fatalf("Expected driver %s to exist Driver Name %s ", name, cmddriver.Name)
115116
}
116117
op := buildOp()
117-
_, err := cmddriver.Run(op)
118+
_, err := cmddriver.Run(context.Background(), op)
118119
assert.NoError(t, err)
119120
}
120121
CreateAndRunTestCommandDriver(t, name, false, content, testfunc)
@@ -133,7 +134,7 @@ func TestCommandDriverOutputs(t *testing.T) {
133134
}
134135
op := buildOp()
135136
op.Bundle.Definitions["output2"].Default = "DEFAULT OUTPUT 2"
136-
opResult, err := cmddriver.Run(op)
137+
opResult, err := cmddriver.Run(context.Background(), op)
137138
if err != nil {
138139
t.Fatalf("Driver Run failed %v", err)
139140
}

driver/debug/debug.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package debug
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67

@@ -15,7 +16,7 @@ type Driver struct {
1516
}
1617

1718
// Run executes the operation on the Debug driver
18-
func (d *Driver) Run(op *driver.Operation) (driver.OperationResult, error) {
19+
func (d *Driver) Run(ctx context.Context, op *driver.Operation) (driver.OperationResult, error) {
1920
data, err := json.MarshalIndent(op, "", " ")
2021
if err != nil {
2122
return driver.OperationResult{}, err

driver/debug/debug_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package debug
22

33
import (
4+
"context"
45
"io/ioutil"
56
"testing"
67

@@ -36,6 +37,6 @@ func TestDebugDriver_Run(t *testing.T) {
3637
Out: ioutil.Discard,
3738
}
3839

39-
_, err := d.Run(op)
40+
_, err := d.Run(context.Background(), op)
4041
is.NoError(err)
4142
}

driver/docker/docker.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ type Driver struct {
4949
}
5050

5151
// Run executes the Docker driver
52-
func (d *Driver) Run(op *driver.Operation) (driver.OperationResult, error) {
53-
return d.exec(op)
52+
func (d *Driver) Run(ctx context.Context, op *driver.Operation) (driver.OperationResult, error) {
53+
return d.exec(ctx, op)
5454
}
5555

5656
// Handles indicates that the Docker driver supports "docker" and "oci"
@@ -181,8 +181,7 @@ func (d *Driver) initializeDockerCli() (command.Cli, error) {
181181
return cli, nil
182182
}
183183

184-
func (d *Driver) exec(op *driver.Operation) (driver.OperationResult, error) {
185-
ctx := context.Background()
184+
func (d *Driver) exec(ctx context.Context, op *driver.Operation) (driver.OperationResult, error) {
186185

187186
cli, err := d.initializeDockerCli()
188187
if err != nil {

driver/docker/docker_integration_test.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
//go:build integration
2-
// +build integration
3-
41
package docker
52

63
import (
74
"bytes"
5+
"context"
86
"fmt"
97
"io"
108
"os"
@@ -92,7 +90,7 @@ func runDriverTest(t *testing.T, image bundle.InvocationImage, skipValidations b
9290
}
9391

9492
docker := &Driver{}
95-
opResult, err := docker.Run(op)
93+
opResult, err := docker.Run(context.Background(), op)
9694

9795
if skipValidations {
9896
return
@@ -155,7 +153,7 @@ func TestDriver_Run_CaptureOutput(t *testing.T) {
155153
}
156154

157155
docker := &Driver{}
158-
_, err := docker.Run(op)
156+
_, err := docker.Run(context.Background(), op)
159157

160158
assert.NoError(t, err)
161159
assert.Equal(t, "installing bundle...\n", stdout.String())
@@ -190,7 +188,7 @@ func TestDriver_ValidateImageDigestFail(t *testing.T) {
190188

191189
docker := &Driver{}
192190

193-
_, err := docker.Run(op)
191+
_, err := docker.Run(context.Background(), op)
194192
require.Error(t, err, "expected an error")
195193
// Not asserting actual image digests to support arbitrary integration test images
196194
assert.Contains(t, err.Error(),

0 commit comments

Comments
 (0)