get.porter.sh/porter@v1.3.0/pkg/porter/delete_test.go (about)

     1  package porter
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"get.porter.sh/porter/pkg/cnab"
     8  	"get.porter.sh/porter/pkg/storage"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestDeleteInstallation(t *testing.T) {
    14  	ctx := context.Background()
    15  
    16  	testcases := []struct {
    17  		name                string
    18  		lastAction          string
    19  		lastActionStatus    string
    20  		force               bool
    21  		installationRemains bool
    22  		wantError           string
    23  	}{
    24  		{
    25  			name:      "not yet installed",
    26  			wantError: "not found",
    27  		}, {
    28  			name:                "last action not uninstall; no --force",
    29  			lastAction:          "install",
    30  			lastActionStatus:    cnab.StatusSucceeded,
    31  			installationRemains: true,
    32  			wantError:           ErrUnsafeInstallationDeleteRetryForce.Error(),
    33  		}, {
    34  			name:                "last action failed uninstall; no --force",
    35  			lastAction:          "uninstall",
    36  			lastActionStatus:    cnab.StatusFailed,
    37  			installationRemains: true,
    38  			wantError:           ErrUnsafeInstallationDeleteRetryForce.Error(),
    39  		}, {
    40  			name:             "last action not uninstall; --force",
    41  			lastAction:       "install",
    42  			lastActionStatus: cnab.StatusSucceeded,
    43  			force:            true,
    44  		}, {
    45  			name:             "last action failed uninstall; --force",
    46  			lastAction:       "uninstall",
    47  			lastActionStatus: cnab.StatusFailed,
    48  			force:            true,
    49  		},
    50  	}
    51  
    52  	for _, tc := range testcases {
    53  		t.Run(tc.name, func(t *testing.T) {
    54  			p := NewTestPorter(t)
    55  			defer p.Close()
    56  
    57  			var err error
    58  
    59  			// Create test claim
    60  			if tc.lastAction != "" {
    61  				i := p.TestInstallations.CreateInstallation(storage.NewInstallation("", "test"))
    62  				c := p.TestInstallations.CreateRun(i.NewRun(tc.lastAction, cnab.ExtendedBundle{}))
    63  				_ = p.TestInstallations.CreateResult(c.NewResult(tc.lastActionStatus))
    64  			}
    65  
    66  			opts := DeleteOptions{}
    67  			opts.Name = "test"
    68  			opts.Force = tc.force
    69  
    70  			err = p.DeleteInstallation(ctx, opts)
    71  			if tc.wantError != "" {
    72  				require.Error(t, err)
    73  				assert.Contains(t, err.Error(), tc.wantError)
    74  			} else {
    75  				require.NoError(t, err, "expected DeleteInstallation to succeed")
    76  			}
    77  
    78  			_, err = p.Installations.GetInstallation(ctx, "", "test")
    79  			if tc.installationRemains {
    80  				require.NoError(t, err, "expected installation to exist")
    81  			} else {
    82  				require.ErrorIs(t, err, storage.ErrNotFound{})
    83  			}
    84  		})
    85  	}
    86  }