Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display more installation information with show #1605

Merged
merged 3 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions docs/content/operators/logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,17 @@ ID** uniquely identifies a run, or execution of a bundle.
```console
$ porter show whalegap
Name: whalegap
Bundle: getporter/whalegap
Version: 0.1.0
Created: 2021-01-27
Modified: 2021-03-09

Outputs:
-------------------------------------------------------------------------------
Name Type Value
-------------------------------------------------------------------------------
io.cnab.outputs.invocationImageLogs string executing install action from
whalegap (installation: wha...
Parameters:
---------------------------------------
Name Type Value
---------------------------------------
msg string whale hello there!
release string whalegap

History:
--------------------------------------------------------------------------
Expand Down
16 changes: 5 additions & 11 deletions docs/content/quickstart/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,11 @@ To see information about an installation, use the `porter show` command with the

```console
$ porter show porter-hello
Name: porter-hello
Created: 2021-03-27
Modified: 2021-05-03

Outputs:
-------------------------------------------------------------------------------
Name Type Value
-------------------------------------------------------------------------------
io.cnab.outputs.invocationImageLogs string DEBUG: defaulting action
to CNAB_ACTION (install)
executi...
Name: hello
Bundle: getporter/porter-hello
Version: 0.1.1
Created: 2021-05-24
Modified: 2021-05-24

History:
------------------------------------------------------------------------
Expand Down
27 changes: 22 additions & 5 deletions pkg/claims/helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package claims

import (
"fmt"
"testing"
"time"

"github.com/cnabio/cnab-go/bundle"
"github.com/cnabio/cnab-go/claim"
Expand All @@ -13,35 +15,50 @@ var _ claim.Provider = TestClaimProvider{}
type TestClaimProvider struct {
claim.Store
t *testing.T

idCounter uint
}

func NewTestClaimProvider(t *testing.T) TestClaimProvider {
return TestClaimProvider{
func NewTestClaimProvider(t *testing.T) *TestClaimProvider {
return &TestClaimProvider{
t: t,
Store: claim.NewMockStore(nil, nil),
}
}

// CreateClaim creates a new claim and saves it.
func (p TestClaimProvider) CreateClaim(installation string, action string, bun bundle.Bundle, parameters map[string]interface{}) claim.Claim {
func (p *TestClaimProvider) CreateClaim(installation string, action string, bun bundle.Bundle, parameters map[string]interface{}) claim.Claim {
c, err := claim.New(installation, action, bun, parameters)
require.NoError(p.t, err, "New claim failed")

// Ensure that the claim data is consistent between tests
c.BundleReference = bun.Name + ":" + bun.Version
p.idCounter += 1
c.ID = fmt.Sprintf("%d", p.idCounter)
c.Created = time.Date(2020, time.April, 18, 1, 2, 3, 4, time.UTC)

err = p.SaveClaim(c)
require.NoError(p.t, err, "SaveClaim failed")
return c
}

// CreateResult creates a new result from the specified claim and saves it.
func (p TestClaimProvider) CreateResult(c claim.Claim, status string) claim.Result {
func (p *TestClaimProvider) CreateResult(c claim.Claim, status string) claim.Result {
r, err := c.NewResult(status)
require.NoError(p.t, err, "NewResult failed")

// Ensure that the claim data is consistent between tests
p.idCounter += 1
r.ID = fmt.Sprintf("%d", p.idCounter)
err = p.SaveResult(r)
r.Created = time.Date(2020, time.April, 18, 1, 2, 3, 4, time.UTC)

require.NoError(p.t, err, "SaveResult failed")
return r
}

// CreateOutput creates a new output from the specified claim and result and saves it.
func (p TestClaimProvider) CreateOutput(c claim.Claim, r claim.Result, name string, value []byte) claim.Output {
func (p *TestClaimProvider) CreateOutput(c claim.Claim, r claim.Result, name string, value []byte) claim.Output {
o := claim.NewOutput(c, r, name, value)
err := p.SaveOutput(o)
require.NoError(p.t, err, "SaveOutput failed")
Expand Down
5 changes: 5 additions & 0 deletions pkg/cnab/provider/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type ActionArguments struct {
// Either a filepath to the bundle or the name of the bundle.
BundlePath string

// BundleReference is the OCI reference of the bundle.
BundleReference string

// Additional files to copy into the bundle
// Target Path => File Contents
Files map[string]string
Expand Down Expand Up @@ -153,6 +156,8 @@ func (r *Runtime) Execute(args ActionArguments) error {
return err
}

c.BundleReference = args.BundleReference

// Validate the action we are about to perform
err = c.Validate()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cnab/provider/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var _ CNABProvider = &TestRuntime{}

type TestRuntime struct {
*Runtime
TestClaims claims.TestClaimProvider
TestClaims *claims.TestClaimProvider
TestCredentials credentials.TestCredentialProvider
TestParameters *parameters.TestParameterProvider
TestConfig *config.TestConfig
Expand All @@ -30,7 +30,7 @@ func NewTestRuntime(t *testing.T) *TestRuntime {
return NewTestRuntimeWithConfig(tc, claimStorage, credentialStorage, parameterStorage)
}

func NewTestRuntimeWithConfig(tc *config.TestConfig, testClaims claims.TestClaimProvider, testCredentials credentials.TestCredentialProvider, testParameters parameters.TestParameterProvider) *TestRuntime {
func NewTestRuntimeWithConfig(tc *config.TestConfig, testClaims *claims.TestClaimProvider, testCredentials credentials.TestCredentialProvider, testParameters parameters.TestParameterProvider) *TestRuntime {
return &TestRuntime{
TestConfig: tc,
TestClaims: testClaims,
Expand Down
1 change: 1 addition & 0 deletions pkg/porter/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ func (e *dependencyExecutioner) prepareDependency(dep *queuedDependency) error {

func (e *dependencyExecutioner) executeDependency(dep *queuedDependency) error {
depArgs := cnabprovider.ActionArguments{
BundleReference: dep.Reference,
Action: e.parentArgs.Action,
BundlePath: dep.CNABFile,
Installation: extensions.BuildPrerequisiteInstallationName(e.parentArgs.Installation, dep.Alias),
Expand Down
21 changes: 20 additions & 1 deletion pkg/porter/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"strings"
"testing"
Expand All @@ -22,13 +23,14 @@ import (
"get.porter.sh/porter/pkg/yaml"
"github.com/cnabio/cnab-go/bundle"
cnabcreds "github.com/cnabio/cnab-go/credentials"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type TestPorter struct {
*Porter
TestConfig *config.TestConfig
TestClaims claims.TestClaimProvider
TestClaims *claims.TestClaimProvider
TestCredentials *credentials.TestCredentialProvider
TestParameters *parameters.TestParameterProvider
TestCache *cache.TestCache
Expand Down Expand Up @@ -207,6 +209,23 @@ func (p *TestPorter) AddTestBundleDir(bundleDir string, generateUniqueName bool)
return uniqueName
}

// CompareGoldenFile checks if the specified string matches the content of a golden test file.
// When they are different and PORTER_UPDATE_TEST_FILES is true, the file is updated to match
// the new test output.
func (p *TestPorter) CompareGoldenFile(goldenFile string, got string) {
t := p.T()

wantSchema, err := ioutil.ReadFile(goldenFile)
require.NoError(t, err)

if os.Getenv("PORTER_UPDATE_TEST_FILES") == "true" {
t.Logf("Updated test file %s to match latest test output", goldenFile)
require.NoError(t, ioutil.WriteFile(goldenFile, []byte(got), 0755), "could not update golden file %s", goldenFile)
} else {
assert.Equal(t, string(wantSchema), got, "The test output doesn't match the expected output in %s. If this was intentional, run mage updateTestfiles to fix the tests.", goldenFile)
}
}

type TestBuildProvider struct {
}

Expand Down
1 change: 1 addition & 0 deletions pkg/porter/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (o *BundleActionOptions) GetOptions() *BundleActionOptions {
func (p *Porter) BuildActionArgs(action BundleAction) (cnabprovider.ActionArguments, error) {
opts := action.GetOptions()
args := cnabprovider.ActionArguments{
BundleReference: opts.Reference,
Action: action.GetAction(),
Installation: opts.Name,
BundlePath: opts.CNABFile,
Expand Down
72 changes: 46 additions & 26 deletions pkg/porter/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"strconv"
"time"

"github.com/cnabio/cnab-go/claim"

"get.porter.sh/porter/pkg/printer"
dtprinter "github.com/carolynvs/datetime-printer"
"github.com/cnabio/cnab-go/claim"
"github.com/docker/distribution/reference"
"github.com/pkg/errors"
)

Expand All @@ -21,14 +21,17 @@ type ListOptions struct {
// DisplayInstallation holds a subset of pertinent values to be listed from installation data
// originating from its claims, results and outputs records
type DisplayInstallation struct {
Name string
Created time.Time
Modified time.Time
Action string
Status string

Outputs DisplayOutputs
History []InstallationAction
Name string `json:"name" yaml:"name"`
Created time.Time `json:"created" yaml:"created"`
Modified time.Time `json:"modified" yaml:"modified"`
Bundle string `json:"bundle,omitempty" yaml:"bundle,omitempty"`
Version string `json:"version" yaml:"version"`
Action string `json:"action" yaml:"action"`
Status string `json:"status" yaml:"status"`

Parameters DisplayValues `json:"parameters,omitempty" yaml:"parameters,omitempty"`
Outputs DisplayValues `json:"outputs,omitempty" yaml:"outputs,omitempty"`
History []InstallationAction `json:"history,omitempty" yaml:"history,omitempty"`
}

func NewDisplayInstallation(installation claim.Installation) (DisplayInstallation, error) {
Expand All @@ -52,22 +55,29 @@ func NewDisplayInstallation(installation claim.Installation) (DisplayInstallatio
if ok {
hasLogsS = strconv.FormatBool(hasLogs)
}

history[i] = InstallationAction{
ClaimID: hc.ID,
Action: hc.Action,
Timestamp: hc.Created,
Status: hc.GetStatus(),
HasLogs: hasLogsS,
ClaimID: hc.ID,
Action: hc.Action,
Parameters: hc.Parameters,
Timestamp: hc.Created,
Bundle: tryParseBundleRepository(hc.BundleReference),
Version: hc.Bundle.Version,
Status: hc.GetStatus(),
HasLogs: hasLogsS,
}
}

return DisplayInstallation{
Name: installation.Name,
Created: installTime,
Modified: c.Created,
Action: c.Action,
Status: installation.GetLastStatus(),
History: history,
Name: installation.Name,
Bundle: tryParseBundleRepository(c.BundleReference),
Version: c.Bundle.Version,
Created: installTime,
Modified: c.Created,
Action: c.Action,
Parameters: NewDisplayValuesFromParameters(c.Bundle, c.Parameters),
Status: installation.GetLastStatus(),
History: history,
}, nil
}

Expand All @@ -86,11 +96,14 @@ func (l DisplayInstallations) Less(i, j int) bool {
}

type InstallationAction struct {
ClaimID string
Action string
Timestamp time.Time
Status string
HasLogs string
ClaimID string `json:"claimID" yaml:"claimID"`
Bundle string `json:"bundle,omitempty" yaml:"bundle,omitempty"`
Version string `json:"version" yaml:"version"`
Action string `json:"action" yaml:"action"`
Parameters map[string]interface{} `json:"parameters,omitempty" yaml:"parameters,omitempty"`
Timestamp time.Time `json:"timestamp" yaml:"timestamp"`
Status string `json:"status" yaml:"status"`
HasLogs string `json:"hasLogs" yaml:"hasLogs"`
}

// ListInstallations lists installed bundles.
Expand Down Expand Up @@ -146,3 +159,10 @@ func (p *Porter) PrintInstallations(opts ListOptions) error {
return fmt.Errorf("invalid format: %s", opts.Format)
}
}

func tryParseBundleRepository(bundleReference string) string {
if ref, err := reference.ParseNormalizedNamed(bundleReference); err == nil {
return reference.FamiliarName(ref)
}
return bundleReference
}
Loading