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

Add creation timestamp to receipts on install #695

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions integration_test/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strings"
"testing"

"sigs.k8s.io/krew/internal/environment"
"sigs.k8s.io/krew/pkg/constants"
)

Expand All @@ -44,6 +45,12 @@ func TestKrewInstall(t *testing.T) {
test.Krew("install", validPlugin).RunOrFailOutput()
test.AssertExecutableInPATH("kubectl-" + validPlugin)
test.AssertPluginFromIndex(validPlugin, "default")

receiptPath := environment.NewPaths(test.Root()).PluginInstallReceiptPath(validPlugin)
pluginReceipt := test.loadReceipt(receiptPath)
if pluginReceipt.CreationTimestamp.Time.IsZero() {
t.Fatal("expected receipt to have a valid creationTimestamp")
}
chriskim06 marked this conversation as resolved.
Show resolved Hide resolved
}

func TestKrewInstallReRun(t *testing.T) {
Expand Down
18 changes: 12 additions & 6 deletions integration_test/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"sigs.k8s.io/krew/internal/installation/receipt"
"sigs.k8s.io/krew/internal/testutil"
"sigs.k8s.io/krew/pkg/constants"
"sigs.k8s.io/krew/pkg/index"
)

const (
Expand Down Expand Up @@ -161,12 +162,9 @@ func (it *ITest) AssertPluginFromIndex(plugin, indexName string) {
it.t.Helper()

receiptPath := environment.NewPaths(it.Root()).PluginInstallReceiptPath(plugin)
r, err := receipt.Load(receiptPath)
if err != nil {
it.t.Fatalf("error loading receipt: %v", err)
}
if r.Status.Source.Name != indexName {
it.t.Errorf("wanted index '%s', got: '%s'", indexName, r.Status.Source.Name)
pluginReceipt := it.loadReceipt(receiptPath)
chriskim06 marked this conversation as resolved.
Show resolved Hide resolved
if pluginReceipt.Status.Source.Name != indexName {
it.t.Errorf("wanted index '%s', got: '%s'", indexName, pluginReceipt.Status.Source.Name)
}
}

Expand Down Expand Up @@ -268,6 +266,14 @@ func (it *ITest) TempDir() *testutil.TempDir {
return it.tempDir
}

func (it *ITest) loadReceipt(path string) index.Receipt {
pluginReceipt, err := receipt.Load(path)
if err != nil {
it.t.Fatalf("error loading receipt: %v", err)
}
return pluginReceipt
}

// InitializeIndex initializes the krew index in `$root/index` with the actual krew-index.
// It caches the index tree as in-memory tar after the first run.
func (it *ITest) initializeIndex() {
Expand Down
22 changes: 15 additions & 7 deletions integration_test/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,23 @@ func TestKrewUpgrade(t *testing.T) {

// plugins installed via manifest get the special "detached" index so this needs to
// be changed to default in order for it to be upgraded here
receipt := environment.NewPaths(test.Root()).PluginInstallReceiptPath(validPlugin)
modifyReceiptIndex(t, receipt, "default")
receiptPath := environment.NewPaths(test.Root()).PluginInstallReceiptPath(validPlugin)
chriskim06 marked this conversation as resolved.
Show resolved Hide resolved
modifyReceiptIndex(t, receiptPath, "default")
pluginReceipt := test.loadReceipt(receiptPath)
initialCreationTimestamp := pluginReceipt.CreationTimestamp
chriskim06 marked this conversation as resolved.
Show resolved Hide resolved

initialLocation := resolvePluginSymlink(test, validPlugin)
test.Krew("upgrade").RunOrFail()
eventualLocation := resolvePluginSymlink(test, validPlugin)
if initialLocation == eventualLocation {
t.Errorf("Expecting the plugin path to change but was the same.")
}

pluginReceipt = test.loadReceipt(receiptPath)
eventualCreationTimestamp := pluginReceipt.CreationTimestamp
if initialCreationTimestamp != eventualCreationTimestamp {
t.Errorf("expected the receipt creationTimestamp to remain unchanged after upgrade")
}
}

func TestKrewUpgradePluginsFromCustomIndex(t *testing.T) {
Expand All @@ -64,14 +72,14 @@ func TestKrewUpgradePluginsFromCustomIndex(t *testing.T) {
test.WithDefaultIndex().WithCustomIndexFromDefault("foo")
test.Krew("install", "foo/"+validPlugin).RunOrFail()

receipt := environment.NewPaths(test.Root()).PluginInstallReceiptPath(validPlugin)
modifyManifestVersion(t, receipt, "v0.0.0")
receiptPath := environment.NewPaths(test.Root()).PluginInstallReceiptPath(validPlugin)
modifyManifestVersion(t, receiptPath, "v0.0.0")
out := string(test.Krew("upgrade").RunOrFailOutput())
if !strings.Contains(out, "Upgrading plugin: foo/"+validPlugin) {
t.Errorf("expected plugin foo/%s to be upgraded", validPlugin)
}

modifyManifestVersion(t, receipt, "v0.0.0")
modifyManifestVersion(t, receiptPath, "v0.0.0")
out = string(test.Krew("upgrade", validPlugin).RunOrFailOutput())
if !strings.Contains(out, "Upgrading plugin: foo/"+validPlugin) {
t.Errorf("expected plugin foo/%s to be upgraded", validPlugin)
Expand Down Expand Up @@ -101,8 +109,8 @@ func TestKrewUpgradeNoSecurityWarningForCustomIndex(t *testing.T) {
test.WithDefaultIndex().WithCustomIndexFromDefault("foo")
test.Krew("install", "foo/"+validPlugin).RunOrFail()

receipt := environment.NewPaths(test.Root()).PluginInstallReceiptPath(validPlugin)
modifyManifestVersion(t, receipt, "v0.0.1")
pluginReceipt := environment.NewPaths(test.Root()).PluginInstallReceiptPath(validPlugin)
modifyManifestVersion(t, pluginReceipt, "v0.0.1")
out := string(test.Krew("upgrade").RunOrFailOutput())
if strings.Contains(out, "Run them at your own risk") {
t.Errorf("expected install of custom plugin to not show security warning: %v", out)
Expand Down
4 changes: 3 additions & 1 deletion internal/installation/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"

"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog"

"sigs.k8s.io/krew/internal/download"
Expand Down Expand Up @@ -84,8 +85,9 @@ func Install(p environment.Paths, plugin index.Plugin, indexName string, opts In
}, opts); err != nil {
return errors.Wrap(err, "install failed")
}

klog.V(3).Infof("Storing install receipt for plugin %s", plugin.Name)
err = receipt.Store(receipt.New(plugin, indexName), p.PluginInstallReceiptPath(plugin.Name))
err = receipt.Store(receipt.New(plugin, indexName, metav1.Now()), p.PluginInstallReceiptPath(plugin.Name))
return errors.Wrap(err, "installation receipt could not be stored, uninstall may fail")
}

Expand Down
4 changes: 3 additions & 1 deletion internal/installation/receipt/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"io/ioutil"

"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"

"sigs.k8s.io/krew/internal/index/indexscanner"
Expand All @@ -43,7 +44,8 @@ func Load(path string) (index.Receipt, error) {
}

// New returns a new receipt with the given plugin and index name.
func New(plugin index.Plugin, indexName string) index.Receipt {
func New(plugin index.Plugin, indexName string, timestamp metav1.Time) index.Receipt {
plugin.CreationTimestamp = timestamp
return index.Receipt{
Plugin: plugin,
Status: index.ReceiptStatus{
Expand Down
5 changes: 4 additions & 1 deletion internal/installation/receipt/receipt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"sigs.k8s.io/krew/internal/index/indexscanner"
"sigs.k8s.io/krew/internal/testutil"
Expand Down Expand Up @@ -72,10 +73,12 @@ func TestLoad_preservesNonExistsError(t *testing.T) {
}

func TestNew(t *testing.T) {
timestamp := metav1.Now()
testPlugin := testutil.NewPlugin().WithName("foo").WithPlatforms(testutil.NewPlatform().V()).V()
wantReceipt := testutil.NewReceipt().WithPlugin(testPlugin).V()
wantReceipt.CreationTimestamp = timestamp

gotReceipt := New(testPlugin, constants.DefaultIndexName)
gotReceipt := New(testPlugin, constants.DefaultIndexName, timestamp)
if diff := cmp.Diff(gotReceipt, wantReceipt); diff != "" {
t.Fatalf("expected receipts to match: %s", diff)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/installation/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func Upgrade(p environment.Paths, plugin index.Plugin, indexName string) error {
}

klog.V(2).Infof("Upgrading install receipt for plugin %s", plugin.Name)
if err = receipt.Store(receipt.New(plugin, indexName), p.PluginInstallReceiptPath(plugin.Name)); err != nil {
if err = receipt.Store(receipt.New(plugin, indexName, installReceipt.CreationTimestamp), p.PluginInstallReceiptPath(plugin.Name)); err != nil {
return errors.Wrap(err, "installation receipt could not be stored, uninstall may fail")
}

Expand Down