Skip to content
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
4 changes: 3 additions & 1 deletion pkg/porter/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package porter
import (
"context"
"testing"
"time"

"get.porter.sh/porter/pkg/cnab"
"get.porter.sh/porter/pkg/printer"
Expand Down Expand Up @@ -213,7 +214,8 @@ func TestPorter_getDisplayInstallationState(t *testing.T) {
run := p.TestInstallations.CreateRun(installation.NewRun(cnab.ActionInstall), p.TestInstallations.SetMutableRunValues)
result := p.TestInstallations.CreateResult(run.NewResult(cnab.StatusSucceeded), p.TestInstallations.SetMutableResultValues)
installation.ApplyResult(run, result)
installation.Status.Installed = &now
installTime := now.Add(-time.Second * 5)
installation.Status.Installed = &installTime
displayInstallationState = getDisplayInstallationState(installation)
require.Equal(t, StateInstalled, displayInstallationState)

Expand Down
4 changes: 3 additions & 1 deletion pkg/porter/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package porter
import (
"path/filepath"
"testing"
"time"

"get.porter.sh/porter/pkg/cnab"
"get.porter.sh/porter/pkg/portercontext"
Expand Down Expand Up @@ -147,10 +148,11 @@ func TestPorter_IsInstallationInSync(t *testing.T) {
p := NewTestPorter(t)
defer p.Close()

installTime := now.Add(-time.Second * 5)
i := storage.Installation{
Uninstalled: false,
Status: storage.InstallationStatus{
Installed: &now,
Installed: &installTime,
Uninstalled: &now,
},
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/storage/installation.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,17 @@ type InstallationStatus struct {

// IsInstalled checks if the installation is currently installed.
func (i Installation) IsInstalled() bool {
if i.Status.Uninstalled != nil && i.Status.Installed != nil {
return i.Status.Installed.After(*i.Status.Uninstalled)
}
return i.Status.Uninstalled == nil && i.Status.Installed != nil
}

// IsUninstalled checks if the installation has been uninstalled.
func (i Installation) IsUninstalled() bool {
if i.Status.Uninstalled != nil && i.Status.Installed != nil {
return i.Status.Uninstalled.After(*i.Status.Installed)
}
return i.Status.Uninstalled != nil
}

Expand Down
47 changes: 46 additions & 1 deletion pkg/storage/installation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package storage

import (
"testing"
"time"

"get.porter.sh/porter/pkg/cnab"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -89,6 +90,7 @@ func TestInstallation_ApplyResult(t *testing.T) {
t.Run("uninstall failed", func(t *testing.T) {
// Make an installed bundle
inst := NewInstallation("dev", "mybuns")
inst.Status.Created = now.Add(-time.Second * 10)
inst.Status.Installed = &inst.Status.Created

// try to uninstall it and fail
Expand All @@ -105,6 +107,7 @@ func TestInstallation_ApplyResult(t *testing.T) {
t.Run("uninstall succeeded", func(t *testing.T) {
// Make an installed bundle
inst := NewInstallation("dev", "mybuns")
inst.Status.Created = now.Add(-time.Second * 10)
inst.Status.Installed = &inst.Status.Created

// uninstall it
Expand All @@ -116,6 +119,48 @@ func TestInstallation_ApplyResult(t *testing.T) {
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")
assert.Equal(t, &result.Created, inst.Status.Uninstalled, "the uninstalled timestamp should be set")
})

t.Run("desired state after re-installation and re-unstallation", func(t *testing.T) {
// Make an installed bundle
inst := NewInstallation("dev", "mybuns")
inst.Status.Created = now.Add(-time.Second * 15)
inst.Status.Installed = &inst.Status.Created

// uninstall the bundle
run := inst.NewRun(cnab.ActionUninstall)
result := run.NewResult(cnab.StatusSucceeded)
result.Created = now.Add(-time.Second * 10)

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 be set")

// re-install the bundle
run = inst.NewRun(cnab.ActionInstall)
result = run.NewResult(cnab.StatusSucceeded)
result.Created = now.Add(-time.Second * 5)

inst.ApplyResult(run, result)

assert.True(t, inst.IsInstalled(), "the installation should be marked as installed")
assert.False(t, inst.IsUninstalled(), "the installation should not be marked as uninstalled")
assert.Equal(t, &result.Created, inst.Status.Installed, "the installed timestamp should be set to the new install time")
assert.NotEmpty(t, inst.Status.Uninstalled, "the uninstalled timestamp should still be be set")

// re-uninstall the bundle
run = inst.NewRun(cnab.ActionUninstall)
result = run.NewResult(cnab.StatusSucceeded)

inst.ApplyResult(run, result)

assert.False(t, inst.IsInstalled(), "the installation should not be marked as installed")
assert.True(t, inst.IsUninstalled(), "the installation should be marked as uninstalled")
assert.NotEmpty(t, inst.Status.Installed, "the installed timestamp should still be be set")
assert.Equal(t, &result.Created, inst.Status.Uninstalled, "the uninstalled timestamp should be set to the new uninstall time")
})
}