Skip to content

Commit

Permalink
Set the uninstalled flag on the installation (getporter#2189)
Browse files Browse the repository at this point in the history
I made a mistake and never set the uninstalled flag when I added this
field to installation status. It's now being set when a bundle run is
applied to the installation.

Signed-off-by: Carolyn Van Slyck <[email protected]>
  • Loading branch information
carolynvs authored Jun 22, 2022
2 parents d4ab1d6 + f366b0b commit ddefcea
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 23 deletions.
44 changes: 24 additions & 20 deletions pkg/porter/runs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,32 +71,36 @@ func TestPorter_PrintInstallationRunsOutput(t *testing.T) {
}

for _, tc := range outputTestcases {
p := NewTestPorter(t)
defer p.Close()
ctx := context.Background()
tc := tc
t.Run(tc.name, func(t *testing.T) {
p := NewTestPorter(t)
defer p.Close()
ctx := context.Background()

installation := p.TestInstallations.CreateInstallation(storage.NewInstallation("staging", "shared-k8s"), p.TestInstallations.SetMutableInstallationValues)
installation := p.TestInstallations.CreateInstallation(storage.NewInstallation("staging", "shared-k8s"), p.TestInstallations.SetMutableInstallationValues)

installRun := p.TestInstallations.CreateRun(installation.NewRun(cnab.ActionInstall), p.TestInstallations.SetMutableRunValues)
uninstallRun := p.TestInstallations.CreateRun(installation.NewRun(cnab.ActionUninstall), p.TestInstallations.SetMutableRunValues)
result := p.TestInstallations.CreateResult(installRun.NewResult(cnab.StatusSucceeded), p.TestInstallations.SetMutableResultValues)
result2 := p.TestInstallations.CreateResult(uninstallRun.NewResult(cnab.StatusSucceeded), p.TestInstallations.SetMutableResultValues)
installRun := p.TestInstallations.CreateRun(installation.NewRun(cnab.ActionInstall), p.TestInstallations.SetMutableRunValues)
uninstallRun := p.TestInstallations.CreateRun(installation.NewRun(cnab.ActionUninstall), p.TestInstallations.SetMutableRunValues)
result := p.TestInstallations.CreateResult(installRun.NewResult(cnab.StatusSucceeded), p.TestInstallations.SetMutableResultValues)
result2 := p.TestInstallations.CreateResult(uninstallRun.NewResult(cnab.StatusSucceeded), p.TestInstallations.SetMutableResultValues)

installation.ApplyResult(installRun, result)
installation.ApplyResult(uninstallRun, result2)
installation.Status.Installed = &now
installation.ApplyResult(installRun, result)
installation.ApplyResult(uninstallRun, result2)
installation.Status.Installed = &now

require.NoError(t, p.TestInstallations.UpdateInstallation(ctx, installation))
require.NoError(t, p.TestInstallations.UpdateInstallation(ctx, installation))

opts := RunListOptions{sharedOptions: sharedOptions{
Namespace: "staging",
Name: "shared-k8s",
}, PrintOptions: printer.PrintOptions{Format: tc.format},
}
opts := RunListOptions{sharedOptions: sharedOptions{
Namespace: "staging",
Name: "shared-k8s",
}, PrintOptions: printer.PrintOptions{Format: tc.format},
}

err := p.PrintInstallationRuns(context.Background(), opts)
require.NoError(t, err)
err := p.PrintInstallationRuns(context.Background(), opts)
require.NoError(t, err)

p.CompareGoldenFile(tc.outputFile, p.TestConfig.TestContext.GetOutput())
})

p.CompareGoldenFile(tc.outputFile, p.TestConfig.TestContext.GetOutput())
}
}
9 changes: 6 additions & 3 deletions pkg/storage/installation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Installation struct {
// SchemaVersion is the version of the installation state schema.
SchemaVersion schema.Version `json:"schemaVersion"`

// ID is the unique identifire for an installation record.
// ID is the unique identifier for an installation record.
ID string `json:"id"`

// Name of the installation. Immutable.
Expand Down Expand Up @@ -104,8 +104,11 @@ func (i *Installation) ApplyResult(run Run, result Result) {
}

if !i.IsInstalled() && run.Action == cnab.ActionInstall && result.Status == cnab.StatusSucceeded {
now := time.Now()
i.Status.Installed = &now
i.Status.Installed = &result.Created
}

if !i.IsUninstalled() && run.Action == cnab.ActionUninstall && result.Status == cnab.StatusSucceeded {
i.Status.Uninstalled = &result.Created
}
}

Expand Down
64 changes: 64 additions & 0 deletions pkg/storage/installation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package storage
import (
"testing"

"get.porter.sh/porter/pkg/cnab"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestInstallation_String(t *testing.T) {
t.Parallel()

i := Installation{Name: "mybun"}
assert.Equal(t, "/mybun", i.String())

Expand Down Expand Up @@ -55,3 +58,64 @@ func TestOCIReferenceParts_GetBundleReference(t *testing.T) {
})
}
}

func TestInstallation_ApplyResult(t *testing.T) {
t.Parallel()

t.Run("install failed", func(t *testing.T) {
// try to install a bundle and fail
inst := NewInstallation("dev", "mybuns")
run := inst.NewRun(cnab.ActionInstall)
result := run.NewResult(cnab.StatusFailed)

inst.ApplyResult(run, result)

assert.False(t, inst.IsInstalled(), "a failed install should not mark the installation as installed")
assert.Empty(t, inst.Status.Installed, "the installed timestamp should not be set")
})

t.Run("install succeeded", func(t *testing.T) {
// install a bundle
inst := NewInstallation("dev", "mybuns")
run := inst.NewRun(cnab.ActionInstall)
result := run.NewResult(cnab.StatusSucceeded)

inst.ApplyResult(run, result)

assert.True(t, inst.IsInstalled(), "a failed install should not mark the installation as installed")
assert.Equal(t, &result.Created, inst.Status.Installed, "the installed timestamp should be set to the result timestamp")
})

t.Run("uninstall failed", func(t *testing.T) {
// Make an installed bundle
inst := NewInstallation("dev", "mybuns")
inst.Status.Installed = &inst.Status.Created

// try to uninstall it and fail
run := inst.NewRun(cnab.ActionUninstall)
result := run.NewResult(cnab.StatusFailed)

inst.ApplyResult(run, result)

assert.True(t, inst.IsInstalled(), "the installation should still be marked as installed")
assert.False(t, inst.IsUninstalled(), "the installation should not be marked as uninstalled")
assert.Empty(t, inst.Status.Uninstalled, "the uninstalled timestamp should not be set")
})

t.Run("uninstall succeeded", func(t *testing.T) {
// Make an installed bundle
inst := NewInstallation("dev", "mybuns")
inst.Status.Installed = &inst.Status.Created

// uninstall it
run := inst.NewRun(cnab.ActionUninstall)
result := run.NewResult(cnab.StatusSucceeded)

inst.ApplyResult(run, result)

assert.False(t, inst.IsInstalled(), "the installation should no longer be considered installed")
assert.True(t, inst.IsUninstalled(), "the installation should be marked as uninstalled")
assert.Equal(t, &inst.Status.Created, inst.Status.Installed, "the installed timestamp should still be set")
assert.Equal(t, &result.Created, inst.Status.Uninstalled, "the uninstalled timestamp should not be set")
})
}
5 changes: 5 additions & 0 deletions tests/smoke/hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ func TestHelloBundle(t *testing.T) {

// Uninstall and remove the installation
test.RequirePorter("uninstall", testdata.MyBuns, "--namespace", test.CurrentNamespace(), "-c=mybuns")
displayInstallations, err := test.ListInstallations(false, test.CurrentNamespace(), testdata.MyBuns, nil)
require.NoError(t, err, "List installations failed")
require.Len(t, displayInstallations, 1, "expected the installation to still be returned by porter list even though it's uninstalled")
require.NotEmpty(t, displayInstallations[0].Status.Uninstalled, "expected the installation to be flagged as uninstalled")

test.RequirePorter("installation", "delete", testdata.MyBuns, "--namespace", test.CurrentNamespace())
test.RequireInstallationNotFound(test.CurrentNamespace(), testdata.MyBuns)

Expand Down

0 comments on commit ddefcea

Please sign in to comment.