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 receipt status #526

Merged
merged 9 commits into from
Mar 5, 2020
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
3 changes: 3 additions & 0 deletions internal/index/indexscanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ func ReadPlugin(f io.ReadCloser) (index.Plugin, error) {
func ReadReceiptFromFile(path string) (index.Receipt, error) {
var receipt index.Receipt
err := readFromFile(path, &receipt)
if receipt.Status.Source.Name == "" {
receipt.Status.Source.Name = constants.DefaultIndexName
corneliusweig marked this conversation as resolved.
Show resolved Hide resolved
}
return receipt, err
}

Expand Down
61 changes: 61 additions & 0 deletions internal/index/indexscanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ import (
"path/filepath"
"testing"

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

"sigs.k8s.io/krew/internal/testutil"
"sigs.k8s.io/krew/pkg/constants"
"sigs.k8s.io/krew/pkg/index"
)

func TestReadPluginFile(t *testing.T) {
Expand Down Expand Up @@ -90,6 +95,62 @@ func TestReadPluginFile(t *testing.T) {
}
}

func TestReadReceiptFromFile(t *testing.T) {
type args struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for this time it's ok but in the future I recommend avoiding single-field type args

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crap sorry about that, you mentioned that before. I'll remember next time and change this in a follow up PR most likely.

status index.ReceiptStatus
}
tests := []struct {
name string
args args
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't read the previous review comments, but is there a reason why receiptFilePath is wrapped in this args struct? There could as well just be a single receiptFilePath field.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its left over from a previous version. I had some other things there before that I moved out, I'll make the change.

wantStatus index.ReceiptStatus
}{
{
name: "read receipt of plugin from index foo",
args: args{
status: index.ReceiptStatus{
Source: index.SourceIndex{
Name: "foo",
},
},
},
wantStatus: index.ReceiptStatus{
Source: index.SourceIndex{
Name: "foo",
},
},
},
{
name: "read receipt of plugin without status",
args: args{
status: index.ReceiptStatus{},
},
wantStatus: index.ReceiptStatus{
Source: index.SourceIndex{
Name: constants.DefaultIndexName,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir, cleanup := testutil.NewTempDir(t)
defer cleanup()

plugin := "plugin" + constants.ManifestExtension
testReceipt := testutil.NewReceipt().WithPlugin(testutil.NewPlugin().V()).WithStatus(tt.args.status).V()
tmpDir.WriteYAML(plugin, testReceipt)

gotReceipt, err := ReadReceiptFromFile(tmpDir.Path(plugin))
if err != nil {
t.Fatalf("ReadReceiptFromFile() error: %v", err)
}
if diff := cmp.Diff(tt.wantStatus, gotReceipt.Status); diff != "" {
t.Errorf("expected matching receipts: %s\n", diff)
}
})
}
}

func TestReadPluginFile_preservesNotFoundErr(t *testing.T) {
_, err := ReadPluginFromFile(filepath.Join(testdataPath(t), "does-not-exist.yaml"))
if err == nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/installation/receipt/receipt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ import (

"sigs.k8s.io/krew/internal/index/indexscanner"
"sigs.k8s.io/krew/internal/testutil"
"sigs.k8s.io/krew/pkg/index"
)

func TestStore(t *testing.T) {
tmpDir, cleanup := testutil.NewTempDir(t)
defer cleanup()

testPlugin := testutil.NewPlugin().WithName("some-plugin").WithPlatforms(testutil.NewPlatform().V()).V()
testReceipt := testutil.NewReceipt().WithPlugin(testPlugin).V()
dest := tmpDir.Path("some-plugin.yaml")

if err := Store(testPlugin, dest); err != nil {
t.Fatal(err)
}

actual, err := indexscanner.LoadPluginByName(tmpDir.Root(), "some-plugin")
actual, err := indexscanner.ReadReceiptFromFile(dest)
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(&testPlugin, &actual); diff != "" {
if diff := cmp.Diff(&testReceipt, &actual); diff != "" {
t.Fatal(diff)
}
}
Expand All @@ -59,7 +59,7 @@ func TestLoad(t *testing.T) {
if err != nil {
t.Fatal(err)
}
testPluginReceipt := index.Receipt{Plugin: testPlugin}
testPluginReceipt := testutil.NewReceipt().WithPlugin(testPlugin).V()
if diff := cmp.Diff(&gotPlugin, &testPluginReceipt); diff != "" {
t.Fatal(diff)
}
Expand Down
37 changes: 37 additions & 0 deletions internal/testutil/receipt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2019 The Kubernetes Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package testutil

import (
"sigs.k8s.io/krew/pkg/constants"
"sigs.k8s.io/krew/pkg/index"
)

type Receipt struct{ v index.Receipt }

// NewReceipt builds an index.Receipt that is valid.
func NewReceipt() *Receipt {
return &Receipt{v: index.Receipt{
Status: index.ReceiptStatus{
Source: index.SourceIndex{
Name: constants.DefaultIndexName,
},
},
}}
}

func (r *Receipt) WithPlugin(p index.Plugin) *Receipt { r.v.Plugin = p; return r }
func (r *Receipt) WithStatus(s index.ReceiptStatus) *Receipt { r.v.Status = s; return r }
func (r *Receipt) V() index.Receipt { return r.v }
13 changes: 13 additions & 0 deletions pkg/index/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,17 @@ type FileOperation struct {
// Receipt describes a plugin receipt file.
type Receipt struct {
Plugin `json:",inline" yaml:",inline"`

Status ReceiptStatus `json:"status"`
corneliusweig marked this conversation as resolved.
Show resolved Hide resolved
}

// ReceiptStatus contains information about the installed plugin.
type ReceiptStatus struct {
Source SourceIndex `json:"source"`
}

// SourceIndex contains information about the index a plugin was installed from.
type SourceIndex struct {
// Name is the configured name of an index a plugin was installed from.
Name string `json:"name"`
}