From 2995270f0b4789ad5b11f70b273c9b0bf1d0f49d Mon Sep 17 00:00:00 2001 From: Chris Kim Date: Sun, 14 Feb 2021 20:35:35 -0800 Subject: [PATCH 1/6] Add creation timestamp to receipts on install --- integration_test/install_test.go | 7 ++++++ integration_test/testutil_test.go | 18 ++++++++++----- integration_test/upgrade_test.go | 22 +++++++++++++------ internal/installation/install.go | 4 +++- internal/installation/receipt/receipt.go | 4 +++- internal/installation/receipt/receipt_test.go | 5 ++++- internal/installation/upgrade.go | 2 +- 7 files changed, 45 insertions(+), 17 deletions(-) diff --git a/integration_test/install_test.go b/integration_test/install_test.go index 8004603a..8653ad05 100644 --- a/integration_test/install_test.go +++ b/integration_test/install_test.go @@ -21,6 +21,7 @@ import ( "strings" "testing" + "sigs.k8s.io/krew/internal/environment" "sigs.k8s.io/krew/pkg/constants" ) @@ -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") + } } func TestKrewInstallReRun(t *testing.T) { diff --git a/integration_test/testutil_test.go b/integration_test/testutil_test.go index 102e3515..8fbec235 100644 --- a/integration_test/testutil_test.go +++ b/integration_test/testutil_test.go @@ -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 ( @@ -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) + if pluginReceipt.Status.Source.Name != indexName { + it.t.Errorf("wanted index '%s', got: '%s'", indexName, pluginReceipt.Status.Source.Name) } } @@ -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() { diff --git a/integration_test/upgrade_test.go b/integration_test/upgrade_test.go index b9cbffe9..edd9b192 100644 --- a/integration_test/upgrade_test.go +++ b/integration_test/upgrade_test.go @@ -45,8 +45,10 @@ 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) + modifyReceiptIndex(t, receiptPath, "default") + pluginReceipt := test.loadReceipt(receiptPath) + initialCreationTimestamp := pluginReceipt.CreationTimestamp initialLocation := resolvePluginSymlink(test, validPlugin) test.Krew("upgrade").RunOrFail() @@ -54,6 +56,12 @@ func TestKrewUpgrade(t *testing.T) { 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) { @@ -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) @@ -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) diff --git a/internal/installation/install.go b/internal/installation/install.go index 24615da2..71fbdaf0 100644 --- a/internal/installation/install.go +++ b/internal/installation/install.go @@ -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" @@ -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") } diff --git a/internal/installation/receipt/receipt.go b/internal/installation/receipt/receipt.go index 83a27351..3c1a0e7a 100644 --- a/internal/installation/receipt/receipt.go +++ b/internal/installation/receipt/receipt.go @@ -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" @@ -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{ diff --git a/internal/installation/receipt/receipt_test.go b/internal/installation/receipt/receipt_test.go index ecb7e8e9..4f0ac08a 100644 --- a/internal/installation/receipt/receipt_test.go +++ b/internal/installation/receipt/receipt_test.go @@ -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" @@ -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) } diff --git a/internal/installation/upgrade.go b/internal/installation/upgrade.go index bac6d32a..711e9778 100644 --- a/internal/installation/upgrade.go +++ b/internal/installation/upgrade.go @@ -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") } From f992649961aefbc510bca5711445ec8d611c4b65 Mon Sep 17 00:00:00 2001 From: Chris Kim Date: Tue, 9 Mar 2021 08:24:51 -0800 Subject: [PATCH 2/6] Update integration_test/install_test.go Co-authored-by: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com> --- integration_test/install_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/integration_test/install_test.go b/integration_test/install_test.go index 8653ad05..a5d6c03b 100644 --- a/integration_test/install_test.go +++ b/integration_test/install_test.go @@ -47,8 +47,7 @@ func TestKrewInstall(t *testing.T) { test.AssertPluginFromIndex(validPlugin, "default") receiptPath := environment.NewPaths(test.Root()).PluginInstallReceiptPath(validPlugin) - pluginReceipt := test.loadReceipt(receiptPath) - if pluginReceipt.CreationTimestamp.Time.IsZero() { + if r := test.loadReceipt(receiptPath); r.CreationTimestamp.Time.IsZero() { t.Fatal("expected receipt to have a valid creationTimestamp") } } From e0d69429907bbc571565716bc5550ed22e571de0 Mon Sep 17 00:00:00 2001 From: Chris Kim Date: Tue, 9 Mar 2021 08:24:57 -0800 Subject: [PATCH 3/6] Update integration_test/testutil_test.go Co-authored-by: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com> --- integration_test/testutil_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration_test/testutil_test.go b/integration_test/testutil_test.go index 8fbec235..4cca312c 100644 --- a/integration_test/testutil_test.go +++ b/integration_test/testutil_test.go @@ -162,7 +162,7 @@ func (it *ITest) AssertPluginFromIndex(plugin, indexName string) { it.t.Helper() receiptPath := environment.NewPaths(it.Root()).PluginInstallReceiptPath(plugin) - pluginReceipt := it.loadReceipt(receiptPath) + r := it.loadReceipt(receiptPath) if pluginReceipt.Status.Source.Name != indexName { it.t.Errorf("wanted index '%s', got: '%s'", indexName, pluginReceipt.Status.Source.Name) } From 28e9698668bdae3a8953c4aacebed16ab8322ac0 Mon Sep 17 00:00:00 2001 From: Chris Kim Date: Tue, 9 Mar 2021 08:25:11 -0800 Subject: [PATCH 4/6] Update integration_test/upgrade_test.go Co-authored-by: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com> --- integration_test/upgrade_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration_test/upgrade_test.go b/integration_test/upgrade_test.go index edd9b192..2290abdd 100644 --- a/integration_test/upgrade_test.go +++ b/integration_test/upgrade_test.go @@ -45,7 +45,7 @@ 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 - receiptPath := environment.NewPaths(test.Root()).PluginInstallReceiptPath(validPlugin) + receipt := environment.NewPaths(test.Root()).PluginInstallReceiptPath(validPlugin) modifyReceiptIndex(t, receiptPath, "default") pluginReceipt := test.loadReceipt(receiptPath) initialCreationTimestamp := pluginReceipt.CreationTimestamp From 7cce053401a0248659f6495cecf904403ee4f4aa Mon Sep 17 00:00:00 2001 From: Chris Kim Date: Tue, 9 Mar 2021 08:25:23 -0800 Subject: [PATCH 5/6] Update integration_test/upgrade_test.go Co-authored-by: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com> --- integration_test/upgrade_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/integration_test/upgrade_test.go b/integration_test/upgrade_test.go index 2290abdd..ba282f32 100644 --- a/integration_test/upgrade_test.go +++ b/integration_test/upgrade_test.go @@ -47,8 +47,7 @@ func TestKrewUpgrade(t *testing.T) { // be changed to default in order for it to be upgraded here receipt := environment.NewPaths(test.Root()).PluginInstallReceiptPath(validPlugin) modifyReceiptIndex(t, receiptPath, "default") - pluginReceipt := test.loadReceipt(receiptPath) - initialCreationTimestamp := pluginReceipt.CreationTimestamp + initialTimestamp := test.loadReceipt(receiptPath).CreationTimestamp initialLocation := resolvePluginSymlink(test, validPlugin) test.Krew("upgrade").RunOrFail() From 23a58aa356ecc07bd21f8371c7f3bbc740934017 Mon Sep 17 00:00:00 2001 From: Chris Kim Date: Tue, 9 Mar 2021 08:34:39 -0800 Subject: [PATCH 6/6] Cleanup some mistakes from github commit --- integration_test/testutil_test.go | 4 ++-- integration_test/upgrade_test.go | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/integration_test/testutil_test.go b/integration_test/testutil_test.go index 4cca312c..9bd1100d 100644 --- a/integration_test/testutil_test.go +++ b/integration_test/testutil_test.go @@ -163,8 +163,8 @@ func (it *ITest) AssertPluginFromIndex(plugin, indexName string) { receiptPath := environment.NewPaths(it.Root()).PluginInstallReceiptPath(plugin) r := it.loadReceipt(receiptPath) - if pluginReceipt.Status.Source.Name != indexName { - it.t.Errorf("wanted index '%s', got: '%s'", indexName, pluginReceipt.Status.Source.Name) + if r.Status.Source.Name != indexName { + it.t.Errorf("wanted index '%s', got: '%s'", indexName, r.Status.Source.Name) } } diff --git a/integration_test/upgrade_test.go b/integration_test/upgrade_test.go index ba282f32..c4274668 100644 --- a/integration_test/upgrade_test.go +++ b/integration_test/upgrade_test.go @@ -45,9 +45,9 @@ 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) + receiptPath := environment.NewPaths(test.Root()).PluginInstallReceiptPath(validPlugin) modifyReceiptIndex(t, receiptPath, "default") - initialTimestamp := test.loadReceipt(receiptPath).CreationTimestamp + initialCreationTimestamp := test.loadReceipt(receiptPath).CreationTimestamp initialLocation := resolvePluginSymlink(test, validPlugin) test.Krew("upgrade").RunOrFail() @@ -56,8 +56,7 @@ func TestKrewUpgrade(t *testing.T) { t.Errorf("Expecting the plugin path to change but was the same.") } - pluginReceipt = test.loadReceipt(receiptPath) - eventualCreationTimestamp := pluginReceipt.CreationTimestamp + eventualCreationTimestamp := test.loadReceipt(receiptPath).CreationTimestamp if initialCreationTimestamp != eventualCreationTimestamp { t.Errorf("expected the receipt creationTimestamp to remain unchanged after upgrade") }