-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out a method to parse termination messages
This reduces duplication, and allows us to make future changes to how termination messages are written in lock-step with how they're parsed.
- Loading branch information
Showing
8 changed files
with
151 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
Copyright 2019 The Tekton 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 termination | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
v1alpha1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" | ||
) | ||
|
||
// ParseMessage parses a termination message as results. | ||
func ParseMessage(msg string) ([]v1alpha1.PipelineResourceResult, error) { | ||
if msg == "" { | ||
return nil, nil | ||
} | ||
var r []v1alpha1.PipelineResourceResult | ||
if err := json.Unmarshal([]byte(msg), &r); err != nil { | ||
return nil, fmt.Errorf("parsing message json: %v", err) | ||
} | ||
return r, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Copyright 2019 The Tekton 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 termination | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
v1alpha1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" | ||
) | ||
|
||
func TestParseMessage(t *testing.T) { | ||
for _, c := range []struct { | ||
desc, msg string | ||
want []v1alpha1.PipelineResourceResult | ||
}{{ | ||
desc: "valid message", | ||
msg: `[{"digest":"foo"},{"key":"foo","value":"bar"}]`, | ||
want: []v1alpha1.PipelineResourceResult{{ | ||
Digest: "foo", | ||
}, { | ||
Key: "foo", | ||
Value: "bar", | ||
}}, | ||
}, { | ||
desc: "empty message", | ||
msg: "", | ||
want: nil, | ||
}} { | ||
t.Run(c.desc, func(t *testing.T) { | ||
got, err := ParseMessage(c.msg) | ||
if err != nil { | ||
t.Fatalf("ParseMessage: %v", err) | ||
} | ||
if d := cmp.Diff(c.want, got); d != "" { | ||
t.Fatalf("ParseMessage(-want,+got): %s", d) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestParseMessage_Invalid(t *testing.T) { | ||
if _, err := ParseMessage("INVALID NOT JSON"); err == nil { | ||
t.Error("Expected error parsing invalid JSON, got nil") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.