From a71d74cfd5e98ddf68b03ec2ea257d2d04b0f857 Mon Sep 17 00:00:00 2001 From: Guillaume Lours Date: Mon, 28 Oct 2019 10:54:48 +0100 Subject: [PATCH] Add all well known actions Rename Status action to io.cnab.status Remove Downgrade action Signed-off-by: Guillaume Lours --- action/action.go | 2 - action/status.go | 34 ---------------- action/status_test.go | 85 --------------------------------------- action/well_known.go | 66 ++++++++++++++++++++++++++++++ action/well_known_test.go | 8 ++++ claim/claim.go | 6 +-- 6 files changed, 76 insertions(+), 125 deletions(-) delete mode 100644 action/status.go delete mode 100644 action/status_test.go create mode 100644 action/well_known.go create mode 100644 action/well_known_test.go diff --git a/action/action.go b/action/action.go index 9fd8f692..4bc9d7bf 100644 --- a/action/action.go +++ b/action/action.go @@ -23,8 +23,6 @@ const stateful = false // - install // - upgrade // - uninstall -// - downgrade -// - status type Action interface { // Run an action, and record the status in the given claim Run(*claim.Claim, credentials.Set, ...OperationConfigFunc) error diff --git a/action/status.go b/action/status.go deleted file mode 100644 index d05829f3..00000000 --- a/action/status.go +++ /dev/null @@ -1,34 +0,0 @@ -package action - -import ( - "github.com/deislabs/cnab-go/claim" - "github.com/deislabs/cnab-go/credentials" - "github.com/deislabs/cnab-go/driver" -) - -// Status runs a status action on a CNAB bundle. -type Status struct { - Driver driver.Driver -} - -// Run executes a status action in an image -func (i *Status) Run(c *claim.Claim, creds credentials.Set, opCfgs ...OperationConfigFunc) error { - invocImage, err := selectInvocationImage(i.Driver, c) - if err != nil { - return err - } - - op, err := opFromClaim(claim.ActionStatus, stateful, c, invocImage, creds) - if err != nil { - return err - } - - err = OperationConfigs(opCfgs).ApplyConfig(op) - if err != nil { - return err - } - - // Ignore OperationResult because non-modifying actions don't have outputs to save. - _, err = i.Driver.Run(op) - return err -} diff --git a/action/status_test.go b/action/status_test.go deleted file mode 100644 index c7f6742c..00000000 --- a/action/status_test.go +++ /dev/null @@ -1,85 +0,0 @@ -package action - -import ( - "errors" - "io/ioutil" - "testing" - - "github.com/deislabs/cnab-go/driver" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -// makes sure Status implements Action interface -var _ Action = &Status{} - -func TestStatus_Run(t *testing.T) { - out := func(op *driver.Operation) error { - op.Out = ioutil.Discard - return nil - } - - t.Run("happy-path", func(t *testing.T) { - st := &Status{ - Driver: &mockDriver{ - shouldHandle: true, - Result: driver.OperationResult{ - Outputs: map[string]string{ - "/tmp/some/path": "SOME CONTENT", - }, - }, - Error: nil, - }, - } - c := newClaim() - err := st.Run(c, mockSet, out) - assert.NoError(t, err) - // Status is not a modifying action - assert.Empty(t, c.Outputs) - }) - - t.Run("configure operation", func(t *testing.T) { - c := newClaim() - d := &mockDriver{ - shouldHandle: true, - Result: driver.OperationResult{ - Outputs: map[string]string{ - "/tmp/some/path": "SOME CONTENT", - }, - }, - Error: nil, - } - inst := &Status{Driver: d} - addFile := func(op *driver.Operation) error { - op.Files["/tmp/another/path"] = "ANOTHER FILE" - return nil - } - require.NoError(t, inst.Run(c, mockSet, out, addFile)) - assert.Contains(t, d.Operation.Files, "/tmp/another/path") - }) - - t.Run("error case: configure operation", func(t *testing.T) { - c := newClaim() - d := &mockDriver{ - shouldHandle: true, - Result: driver.OperationResult{ - Outputs: map[string]string{ - "/tmp/some/path": "SOME CONTENT", - }, - }, - Error: nil, - } - inst := &Status{Driver: d} - sabotage := func(op *driver.Operation) error { - return errors.New("oops") - } - require.EqualError(t, inst.Run(c, mockSet, out, sabotage), "oops") - }) - - t.Run("error case: driver doesn't handle image", func(t *testing.T) { - c := newClaim() - st := &Status{Driver: &mockDriver{Error: errors.New("I always fail")}} - err := st.Run(c, mockSet, out) - assert.Error(t, err) - }) -} diff --git a/action/well_known.go b/action/well_known.go new file mode 100644 index 00000000..c4f4e030 --- /dev/null +++ b/action/well_known.go @@ -0,0 +1,66 @@ +package action + +import ( + "github.com/deislabs/cnab-go/claim" + "github.com/deislabs/cnab-go/credentials" + "github.com/deislabs/cnab-go/driver" +) + +// Well known constants define the Well Know CNAB action to be taken +const ( + ActionDryRun = "io.cnab.dry-run" + ActionHelp = "io.cnab.help" + ActionLog = "io.cnab.log" + ActionStatus = "io.cnab.status" + ActionStatusJSON = "io.cnab.status+json" +) + +// DryRun runs a dry-run action on a CNAB bundle. +type DryRun struct { + Driver driver.Driver +} + +// Help runs a help action on a CNAB bundle. +type Help struct { + Driver driver.Driver +} + +// Log runs a log action on a CNAB bundle. +type Log struct { + Driver driver.Driver +} + +// Status runs a status action on a CNAB bundle. +type Status struct { + Driver driver.Driver +} + +// StatusJSON runs a status+JSON action on a CNAB bundle. +type StatusJSON struct { + Driver driver.Driver +} + +// Run executes a dry-run action in an image +func (i *DryRun) Run(c *claim.Claim, creds credentials.Set, opCfgs ...OperationConfigFunc) error { + return (&RunCustom{Driver: i.Driver, Action: ActionDryRun}).Run(c, creds, opCfgs...) +} + +// Run executes a help action in an image +func (i *Help) Run(c *claim.Claim, creds credentials.Set, opCfgs ...OperationConfigFunc) error { + return (&RunCustom{Driver: i.Driver, Action: ActionHelp}).Run(c, creds, opCfgs...) +} + +// Run executes a status action in an image +func (i *Log) Run(c *claim.Claim, creds credentials.Set, opCfgs ...OperationConfigFunc) error { + return (&RunCustom{Driver: i.Driver, Action: ActionLog}).Run(c, creds, opCfgs...) +} + +// Run executes a status action in an image +func (i *Status) Run(c *claim.Claim, creds credentials.Set, opCfgs ...OperationConfigFunc) error { + return (&RunCustom{Driver: i.Driver, Action: ActionStatus}).Run(c, creds, opCfgs...) +} + +// Run executes a status+JSON action in an image +func (i *StatusJSON) Run(c *claim.Claim, creds credentials.Set, opCfgs ...OperationConfigFunc) error { + return (&RunCustom{Driver: i.Driver, Action: ActionStatusJSON}).Run(c, creds, opCfgs...) +} diff --git a/action/well_known_test.go b/action/well_known_test.go new file mode 100644 index 00000000..a5400d1e --- /dev/null +++ b/action/well_known_test.go @@ -0,0 +1,8 @@ +package action + +// makes sure DryRun, Help, Log, Status, StatusJSON implements Action interface +var _ Action = &DryRun{} +var _ Action = &Help{} +var _ Action = &Log{} +var _ Action = &Status{} +var _ Action = &StatusJSON{} diff --git a/claim/claim.go b/claim/claim.go index 95c83202..22371b38 100644 --- a/claim/claim.go +++ b/claim/claim.go @@ -23,16 +23,14 @@ const ( const ( ActionInstall = "install" ActionUpgrade = "upgrade" - ActionDowngrade = "downgrade" ActionUninstall = "uninstall" - ActionStatus = "status" ActionUnknown = "unknown" ) // Claim is an installation claim receipt. // -// Claims reprsent information about a particular installation, and -// provide the necessary data to upgrade, uninstall, and downgrade +// Claims represent information about a particular installation, and +// provide the necessary data to upgrade and uninstall // a CNAB package. type Claim struct { Name string `json:"name"`