-
Notifications
You must be signed in to change notification settings - Fork 28
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
Adds support for img labels and anno. in build create cmd #68
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package flags | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// MapValue implements pflag.Value interface, in order to store key-value | ||
// pairs used on Shipwright's BuildSpec which have map[string]string as field type. | ||
type MapValue struct { | ||
kvMap map[string]string | ||
} | ||
|
||
// String prints out the string representation of the map. | ||
func (m *MapValue) String() string { | ||
slice := []string{} | ||
for k, v := range m.kvMap { | ||
slice = append(slice, fmt.Sprintf("%s=%s", k, v)) | ||
} | ||
csv, _ := writeAsCSV(slice) | ||
return fmt.Sprintf("[%s]", csv) | ||
} | ||
|
||
// Set receives a key-value entry separated by equal sign ("="). | ||
func (m *MapValue) Set(value string) error { | ||
k, v, err := splitKeyValue(value) | ||
if err != nil { | ||
return err | ||
} | ||
m.kvMap[k] = v | ||
return nil | ||
} | ||
|
||
// Type analogous to the pflag "stringArray" type, where each flag entry will be translated to a | ||
// single array (slice) entry, therefore the comma (",") is accepted as part of the value, as any | ||
// other special character. | ||
func (c *MapValue) Type() string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: rename |
||
return "stringArray" | ||
} | ||
|
||
// NewMapValue instantiate a MapValue sharing the map. | ||
func NewMapValue(m map[string]string) *MapValue { | ||
return &MapValue{kvMap: m} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package flags | ||
|
||
import ( | ||
"testing" | ||
|
||
buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1" | ||
|
||
o "github.com/onsi/gomega" | ||
) | ||
|
||
func TestNewMapValue(t *testing.T) { | ||
g := o.NewGomegaWithT(t) | ||
|
||
spec := &buildv1alpha1.BuildSpec{Output: buildv1alpha1.Image{ | ||
Labels: map[string]string{}, | ||
}} | ||
c := NewMapValue(spec.Output.Labels) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: rename this variable to |
||
|
||
// expect error when key-value is not split by equal sign | ||
err := c.Set("a") | ||
g.Expect(err).NotTo(o.BeNil()) | ||
|
||
// setting a simple key-value entry | ||
err = c.Set("a=b") | ||
g.Expect(err).To(o.BeNil()) | ||
g.Expect(len(spec.Output.Labels)).To(o.Equal(1)) | ||
g.Expect(spec.Output.Labels["a"]).To(o.Equal("b")) | ||
|
||
// setting a key-value entry with special characters | ||
err = c.Set("b=c,d,e=f") | ||
g.Expect(err).To(o.BeNil()) | ||
g.Expect(len(spec.Output.Labels)).To(o.Equal(2)) | ||
g.Expect(spec.Output.Labels["b"]).To(o.Equal("c,d,e=f")) | ||
|
||
// setting a key-value entry with space on it | ||
err = c.Set("c=d e") | ||
g.Expect(err).To(o.BeNil()) | ||
g.Expect(len(spec.Output.Labels)).To(o.Equal(3)) | ||
g.Expect(spec.Output.Labels["c"]).To(o.Equal("d e")) | ||
|
||
// making sure the string representation produced is as expected | ||
s := c.String() | ||
g.Expect(s).To(o.Equal("[a=b,\"b=c,d,e=f\",c=d e]")) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env bats | ||
|
||
source test/e2e/helpers.sh | ||
|
||
setup() { | ||
load 'bats/support/load' | ||
load 'bats/assert/load' | ||
load 'bats/file/load' | ||
} | ||
|
||
teardown() { | ||
run kubectl delete builds.shipwright.io --all | ||
run kubectl delete buildruns.shipwright.io --all | ||
} | ||
|
||
@test "shp output image labels and annotation lifecycle" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great, thank you! |
||
# generate random names for our build and buildrun | ||
build_name=$(random_name) | ||
buildrun_name=$(random_name) | ||
|
||
# create a Build with a label and an annotation | ||
run shp build create ${build_name} --source-url=https://github.com/shipwright-io/sample-go --output-image=my-image --output-image-label=foo=bar --output-image-annotation=created-by=shipwright | ||
assert_success | ||
|
||
# ensure that the build was successfully created | ||
assert_output --partial "Created build \"${build_name}\"" | ||
|
||
# get the yaml for the Build object | ||
run kubectl get builds.shipwright.io/${build_name} -o yaml | ||
assert_success | ||
|
||
# ensure that the label and annotation were inserted into the Build object | ||
assert_output --partial "foo: bar" | ||
assert_output --partial "created-by: shipwright" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: remove empty line.