Skip to content

Commit

Permalink
add cnab.Provider interface, TestDuffle, TestClaimStore impls; add sh…
Browse files Browse the repository at this point in the history
…ow_test.go
  • Loading branch information
vdice committed Jul 10, 2019
1 parent 8f10692 commit 03d18f8
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 62 deletions.
8 changes: 5 additions & 3 deletions cmd/porter/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package main
import (
"strings"

"github.com/spf13/cobra"

cnab "github.com/deislabs/porter/pkg/cnab/provider"
"github.com/deislabs/porter/pkg/porter"
"github.com/deislabs/porter/pkg/printer"
"github.com/spf13/cobra"
)

func buildBundlesCommand(p *porter.Porter) *cobra.Command {
Expand Down Expand Up @@ -329,7 +331,6 @@ func buildPublishCommand(p *porter.Porter) *cobra.Command {
return cmd
}

// TODO: test!
func buildBundleShowCommand(p *porter.Porter) *cobra.Command {
opts := porter.ShowOptions{}

Expand All @@ -345,7 +346,8 @@ Optional output formats include json and yaml.
return opts.Validate(args)
},
RunE: func(cmd *cobra.Command, args []string) error {
return p.ShowBundle(opts)
cp := cnab.NewDuffle(p.Config)
return p.ShowBundle(opts, cp)
},
}

Expand Down
1 change: 0 additions & 1 deletion cmd/porter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func buildRootCommand() *cobra.Command {
cmd.AddCommand(buildBundlesCommand(p))
cmd.AddCommand(buildMixinsCommand(p))
cmd.AddCommand(buildCredentialsCommand(p))
// cmd.AddCommand(buildOutputsCommand(p))

for _, alias := range buildBundleAliasCommands(p) {
cmd.AddCommand(alias)
Expand Down
14 changes: 14 additions & 0 deletions pkg/cnab/provider/duffle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"path/filepath"

"github.com/deislabs/cnab-go/claim"
"github.com/deislabs/cnab-go/driver"
duffledriver "github.com/deislabs/duffle/pkg/driver"
"github.com/deislabs/porter/pkg/config"
Expand All @@ -12,6 +13,10 @@ import (
"github.com/pkg/errors"
)

type Provider interface {
FetchClaim(name string) (*claim.Claim, error)
}

type Duffle struct {
*config.Config
}
Expand Down Expand Up @@ -67,3 +72,12 @@ func (d *Duffle) newDriver(driverName, claimName string) (driver.Driver, error)

return driverImpl, err
}

func (d *Duffle) FetchClaim(name string) (*claim.Claim, error) {
claimStore := d.NewClaimStore()
claim, err := claimStore.Read(name)
if err != nil {
return nil, errors.Wrapf(err, "could not retrieve claim %s", name)
}
return &claim, nil
}
112 changes: 112 additions & 0 deletions pkg/cnab/provider/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package cnabprovider

import (
"encoding/json"
"fmt"

"github.com/deislabs/cnab-go/claim"
"github.com/deislabs/cnab-go/utils/crud"
"github.com/deislabs/porter/pkg/config"
"github.com/pkg/errors"
)

type TestDuffle struct {
*Duffle
*config.TestConfig
ClaimStore TestStore
}

type TestStore struct {
backingStore map[string][]byte
}

func NewTestDuffle(tc *config.TestConfig) *TestDuffle {
d := NewDuffle(tc.Config)

return &TestDuffle{
Duffle: d,
TestConfig: tc,
ClaimStore: NewTestStore(),
}
}

func (t *TestDuffle) FetchClaim(name string) (*claim.Claim, error) {
bytes, err := t.ClaimStore.Read(name)
if err != nil {
return nil, errors.Wrapf(err, "could not retrieve claim %s", name)
}

var claim claim.Claim
err = json.Unmarshal(bytes, &claim)
if err != nil {
return nil, errors.Wrapf(err, "error encountered unmarshaling claim %s", name)
}

return &claim, nil
}

func (t *TestDuffle) NewClaimStore() crud.Store {
return NewTestStore()
}

func NewTestStore() TestStore {
return TestStore{
backingStore: make(map[string][]byte),
}
}

// The following are the necessary methods for TestStore to implement
// to satisfy the crud.Store interface

func (ts TestStore) List() ([]string, error) {
claimList := []string{}
for name := range ts.backingStore {
claimList = append(claimList, name)
}
return claimList, nil
}

func (ts TestStore) Store(name string, bytes []byte) error {
ts.backingStore[name] = bytes
return nil
}

func (ts TestStore) Read(name string) ([]byte, error) {
bytes, exists := ts.backingStore[name]
if !exists {
return []byte{}, fmt.Errorf("claim %s not found", name)
}
return bytes, nil
}

func (ts TestStore) ReadAll() ([]claim.Claim, error) {
claims := make([]claim.Claim, 0)

list, err := ts.List()
if err != nil {
return claims, err
}

for _, c := range list {
bytes, err := ts.Read(c)
if err != nil {
return claims, err
}

var cl claim.Claim
err = json.Unmarshal(bytes, &cl)
if err != nil {
return nil, err
}
claims = append(claims, cl)
}
return claims, nil
}

func (ts TestStore) Delete(name string) error {
_, ok := ts.backingStore[name]
if ok {
delete(ts.backingStore, name)
}
return nil
}
7 changes: 2 additions & 5 deletions pkg/config/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ type Dependency struct {
// The manifest for the dependency
m *Manifest

Name string `yaml:"name"`
Parameters map[string]string `yaml:"parameters,omitempty"`
// TODO: can remove?
Name string `yaml:"name"`
Parameters map[string]string `yaml:"parameters,omitempty"`
Connections []BundleConnection `yaml:"connections",omitempty`
}

Expand Down Expand Up @@ -195,14 +194,12 @@ func (d *Dependency) resolve() (map[string]interface{}, []string, error) {
return depVals, sensitiveStuff, nil
}

// TODO: can remove?
type BundleOutput struct {
Name string `yaml:"name"`
Path string `yaml:"path"`
EnvironmentVariable string `yaml:"env"`
}

// TODO: can remove?
type BundleConnection struct {
Source string `yaml:source`
Destination string `yaml:destination`
Expand Down
2 changes: 2 additions & 0 deletions pkg/porter/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func (l CondensedClaimList) Less(i, j int) bool {

// ListBundles lists installed bundles using the printer.Format provided
func (p *Porter) ListBundles(opts printer.PrintOptions) error {
// TODO: supply cnab.Provider interface as second arg, use to access ClaimStore
// This will enable unit testing (see show_test.go)
cp := cnab.NewDuffle(p.Config)
claimStore := cp.NewClaimStore()
claims, err := claimStore.ReadAll()
Expand Down
2 changes: 1 addition & 1 deletion pkg/porter/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (p *Porter) ApplyBundleOutputs(opts RunOptions, outputs []string) error {
Value: outputValue,
}

data, err := json.Marshal(output)
data, err := json.MarshalIndent(output, "", " ")
if err != nil {
return err
}
Expand Down
54 changes: 16 additions & 38 deletions pkg/porter/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,26 @@ func (so *ShowOptions) Validate(args []string) error {

// ShowBundle shows a bundle, or more properly a bundle claim, along with any
// associated outputs
func (p *Porter) ShowBundle(opts ShowOptions) error {
cl, err := p.fetchClaim(opts.Name)
func (p *Porter) ShowBundle(opts ShowOptions, cp cnab.Provider) error {
claim, err := cp.FetchClaim(opts.Name)
if err != nil {
return err
}

outputs, err := p.listBundleOutputs(opts.Name)
if err != nil {
return err
}

cl := ClaimListing{
Name: claim.Name,
Created: claim.Created,
Modified: claim.Modified,
Action: claim.Result.Action,
Status: claim.Result.Status,
Outputs: *outputs,
}

switch opts.Format {
case printer.FormatJson:
return printer.PrintJson(p.Out, cl)
Expand Down Expand Up @@ -88,8 +102,6 @@ func (p *Porter) ShowBundle(opts ShowOptions) error {

// Iterate through all Bundle Outputs and add to rows
for _, o := range cl.Outputs {
// TODO: test sensitive
// TODO: test truncation
value := o.Value
// If output is sensitive, substitute local path
if o.Sensitive {
Expand Down Expand Up @@ -121,37 +133,3 @@ func (p *Porter) ShowBundle(opts ShowOptions) error {
return fmt.Errorf("invalid format: %s", opts.Format)
}
}

func (p *Porter) fetchClaim(name string) (*ClaimListing, error) {
cp := cnab.NewDuffle(p.Config)
claimStore := cp.NewClaimStore()
claim, err := claimStore.Read(name)
if err != nil {
return nil, errors.Wrapf(err, "could not retrieve claim %s", name)
}

var condensedClaims CondensedClaimList
condensedClaim := CondensedClaim{
Name: claim.Name,
Created: claim.Created,
Modified: claim.Modified,
Action: claim.Result.Action,
Status: claim.Result.Status,
}
condensedClaims = append(condensedClaims, condensedClaim)

outputList, err := p.listBundleOutputs(name)
if err != nil {
return nil, errors.Wrapf(err, "unable to list outputs for claim %s", name)
}

return &ClaimListing{
Name: claim.Name,
Created: claim.Created,
Modified: claim.Modified,
Action: claim.Result.Action,
Status: claim.Result.Status,
Outputs: *outputList,
}, nil

}
48 changes: 38 additions & 10 deletions pkg/porter/show_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package porter

import (
"encoding/json"
"path/filepath"
"testing"
"time"

"github.com/deislabs/porter/pkg/printer"
"github.com/stretchr/testify/require"

"github.com/deislabs/cnab-go/claim"
cnab "github.com/deislabs/porter/pkg/cnab/provider"
"github.com/deislabs/porter/pkg/printer"
)

func TestPorter_ShowBundle(t *testing.T) {
Expand All @@ -21,17 +26,40 @@ func TestPorter_ShowBundle(t *testing.T) {
Name: "test-bundle",
Format: printer.FormatTable,
}
err = p.ShowBundle(opts)
require.NoError(t, err)
d := cnab.NewTestDuffle(p.TestConfig)

wantContains := []string{
"NAME MODIFIED",
"foo now",
"bar now",
// Create test claim
claim := claim.Claim{
Name: "test-bundle",
Created: time.Date(1983, time.April, 18, 1, 2, 3, 4, time.UTC),
Modified: time.Date(1983, time.April, 18, 1, 2, 3, 4, time.UTC),
Result: claim.Result{
Action: "install",
Status: "success",
},
}
claimBytes, err := json.MarshalIndent(claim, "", " ")
require.NoError(t, err)
d.ClaimStore.Store("test-bundle", claimBytes)

err = p.ShowBundle(opts, d)
require.NoError(t, err)

wantOutput :=
`Name: test-bundle
Created: 1983-04-18
Modified: 1983-04-18
Last Action: install
Last Status: success
Outputs:
-----------------------------------------------------
Name Type Value (Path if sensitive)
-----------------------------------------------------
foo string /root/.porter/outputs/test-bundle/foo
bar string bar-value
`

gotOutput := p.TestConfig.TestContext.GetOutput()
for _, want := range wantContains {
require.Contains(t, gotOutput, want)
}
require.Equal(t, wantOutput, gotOutput)
}
9 changes: 5 additions & 4 deletions scripts/test/test-wordpress.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ if cat ${install_log} | grep -q "${sensitive_value}"; then
exit 1
fi

echo "Verifing bundle outputs..."
# TODO: porter bundle show wordpress to see outputs listing
${PORTER_HOME}/porter outputs list -b wordpress | grep "wordpress-password"
${PORTER_HOME}/porter output show -n wordpress-password -b wordpress | grep "${sensitive_value}"
echo "Verifing bundle outputs via 'porter bundle show'"
show_output=$(${PORTER_HOME}/porter show wordpress)
echo "${show_output}"
echo "${show_output}" | grep -q "wordpress-password"
# TODO: check for output path (since output is default sensitive)

cat ${PORTER_HOME}/claims/wordpress.json

Expand Down

0 comments on commit 03d18f8

Please sign in to comment.