Skip to content

Commit

Permalink
return error message from using --untag with nonexistent tag
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhelfand committed Jun 8, 2020
1 parent 2a42ee1 commit 4e785cf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
9 changes: 9 additions & 0 deletions pkg/kn/commands/service/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,15 @@ func TestServiceUpdateDeletionTimestampNotNil(t *testing.T) {
assert.ErrorContains(t, err, "service")
}

func TestServiceUpdateTagDoesNotExist(t *testing.T) {
orig := newEmptyService()

_, _, _, err := fakeServiceUpdate(orig, []string{
"service", "update", "foo", "--untag", "foo", "--no-wait"})

assert.Error(t, err, "Error: tag foo does not exist")
}

func newEmptyService() *servingv1.Service {
ret := &servingv1.Service{
TypeMeta: metav1.TypeMeta{
Expand Down
17 changes: 14 additions & 3 deletions pkg/kn/traffic/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@ func (e ServiceTraffic) isTagPresent(tag string) bool {
return false
}

func (e ServiceTraffic) untagRevision(tag string) {
func (e ServiceTraffic) untagRevision(tag string) error {
for i, target := range e {
if target.Tag == tag {
e[i].Tag = ""
break
return nil
}
}

return fmt.Errorf("Error: tag %s does not exist", tag)
}

func (e ServiceTraffic) isRevisionPresent(revision string) bool {
Expand Down Expand Up @@ -276,8 +278,17 @@ func Compute(cmd *cobra.Command, targets []servingv1.TrafficTarget, trafficFlags
traffic := newServiceTraffic(targets)

// First precedence: Untag revisions
var errStrings []string
for _, tag := range trafficFlags.UntagRevisions {
traffic.untagRevision(tag)
err = traffic.untagRevision(tag)
if err != nil {
errStrings = append(errStrings, err.Error())
}
}

// Return all errors from untagging revisions
if len(errStrings) > 0 {
return nil, fmt.Errorf(strings.Join(errStrings, "\n"))
}

for _, each := range trafficFlags.RevisionsTags {
Expand Down
12 changes: 12 additions & 0 deletions pkg/kn/traffic/compute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,18 @@ func TestComputeErrMsg(t *testing.T) {
[]string{"--traffic", "echo-v1=40", "--traffic", "echo-v1=60"},
"repetition of revision reference echo-v1 is not allowed, use only once with --traffic flag",
},
{
"untag single tag that does not exist",
append(newServiceTraffic([]servingv1.TrafficTarget{}), newTarget("latest", "echo-v1", 100, false)),
[]string{"--untag", "foo"},
"Error: tag foo does not exist",
},
{
"untag multiple tags that do not exist",
append(newServiceTraffic([]servingv1.TrafficTarget{}), newTarget("latest", "echo-v1", 100, false)),
[]string{"--untag", "foo", "--untag", "bar"},
"Error: tag foo does not exist\nError: tag bar does not exist",
},
} {
t.Run(testCase.name, func(t *testing.T) {
testCmd, tFlags := newTestTrafficCommand()
Expand Down

0 comments on commit 4e785cf

Please sign in to comment.