Skip to content

Commit

Permalink
Add context setting and test
Browse files Browse the repository at this point in the history
  • Loading branch information
chuangw6 committed May 1, 2022
1 parent 955f414 commit 136e04d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
11 changes: 10 additions & 1 deletion pkg/apis/pipeline/v1beta1/param_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,20 @@ func getContextParams(ctx context.Context, overlays ...Param) []Param {
Type: ParamTypeString,
StringVal: fmt.Sprintf("$(params.%s)", ps.Name),
}
} else {
} else if ps.Type == ParamTypeArray {
p.Value = ArrayOrString{
Type: ParamTypeArray,
ArrayVal: []string{fmt.Sprintf("$(params.%s[*])", ps.Name)},
}
} else {
objVal := make(map[string]string)
for k := range ps.Properties {
objVal[fmt.Sprintf("$(params.%s.%s)", p.Name, k)] = ""
}
p.Value = ArrayOrString{
Type: ParamTypeObject,
ObjectVal: objVal,
}
}
out = append(out, p)
}
Expand Down
85 changes: 85 additions & 0 deletions pkg/apis/pipeline/v1beta1/param_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,30 @@ func TestAddContextParams(t *testing.T) {
},
},
},
{
name: "add-object-param",
params: []Param{{Name: "c", Value: *NewObject(map[string]string{"key1": "val1", "key2": "val2"})}},
want: paramCtxVal{
"a": ParamSpec{
Name: "a",
Type: ParamTypeString,
},
"b": ParamSpec{
Name: "b",
Type: ParamTypeArray,
},
"c": ParamSpec{
Name: "c",
Type: ParamTypeObject,
},
},
},
{
name: "existing-param",
params: []Param{
{Name: "a", Value: *NewArrayOrString("foo1")},
{Name: "b", Value: *NewArrayOrString("bar1", "baz1")},
{Name: "c", Value: *NewObject(map[string]string{"key1": "val1", "key2": "val2"})},
},
want: paramCtxVal{
"a": ParamSpec{
Expand All @@ -71,6 +90,10 @@ func TestAddContextParams(t *testing.T) {
Name: "b",
Type: ParamTypeArray,
},
"c": ParamSpec{
Name: "c",
Type: ParamTypeObject,
},
},
},
{
Expand All @@ -91,6 +114,10 @@ func TestAddContextParams(t *testing.T) {
Name: "b",
Type: ParamTypeArray,
},
"c": ParamSpec{
Name: "c",
Type: ParamTypeObject,
},
},
},
} {
Expand Down Expand Up @@ -118,11 +145,18 @@ func TestAddContextParamSpec(t *testing.T) {
name: "add-paramspec",
params: []ParamSpec{{
Name: "a",
}, {
Name: "b",
Properties: map[string]PropertySpec{"key1": {}},
}},
want: paramCtxVal{
"a": ParamSpec{
Name: "a",
},
"b": ParamSpec{
Name: "b",
Properties: map[string]PropertySpec{"key1": {}},
},
},
},
{
Expand All @@ -132,6 +166,12 @@ func TestAddContextParamSpec(t *testing.T) {
Type: ParamTypeArray,
Default: NewArrayOrString("foo", "bar"),
Description: "tacocat",
}, {
Name: "b",
Type: ParamTypeObject,
Properties: map[string]PropertySpec{"key2": {}},
Default: NewObject(map[string]string{"key2": "val"}),
Description: "my object",
}},
want: paramCtxVal{
"a": ParamSpec{
Expand All @@ -140,6 +180,13 @@ func TestAddContextParamSpec(t *testing.T) {
Default: NewArrayOrString("foo", "bar"),
Description: "tacocat",
},
"b": ParamSpec{
Name: "b",
Type: ParamTypeObject,
Properties: map[string]PropertySpec{"key2": {}},
Default: NewObject(map[string]string{"key2": "val"}),
Description: "my object",
},
},
},
{
Expand All @@ -159,6 +206,13 @@ func TestAddContextParamSpec(t *testing.T) {
Default: NewArrayOrString("foo", "bar"),
Description: "tacocat",
},
"b": ParamSpec{
Name: "b",
Type: ParamTypeObject,
Properties: map[string]PropertySpec{"key2": {}},
Default: NewObject(map[string]string{"key2": "val"}),
Description: "my object",
},
},
},
} {
Expand Down Expand Up @@ -187,6 +241,13 @@ func TestGetContextParams(t *testing.T) {
Default: NewArrayOrString("bar"),
Description: "racecar",
},
{
Name: "c",
Type: ParamTypeObject,
Properties: map[string]PropertySpec{"key1": {}},
Default: NewObject(map[string]string{"key1": "val1"}),
Description: "my object",
},
}

ctx = addContextParamSpec(ctx, want)
Expand All @@ -210,13 +271,23 @@ func TestGetContextParams(t *testing.T) {
ArrayVal: []string{"$(params.b[*])"},
},
},
{
Name: "c",
Value: ArrayOrString{
Type: ParamTypeObject,
ObjectVal: map[string]string{"$(params.c.key1)": ""},
},
},
},
},
{
name: "with-overlay",
overlay: []Param{{
Name: "a",
Value: *NewArrayOrString("tacocat"),
}, {
Name: "c",
Value: *NewObject(map[string]string{"key2": "val2"}),
}},
want: []Param{
{
Expand All @@ -230,6 +301,13 @@ func TestGetContextParams(t *testing.T) {
ArrayVal: []string{"$(params.b[*])"},
},
},
{
Name: "c",
Value: ArrayOrString{
Type: ParamTypeObject,
ObjectVal: map[string]string{"key2": "val2"},
},
},
},
},
} {
Expand Down Expand Up @@ -257,6 +335,13 @@ func TestGetContextParamSpecs(t *testing.T) {
Default: NewArrayOrString("bar"),
Description: "racecar",
},
{
Name: "c",
Type: ParamTypeObject,
Properties: map[string]PropertySpec{"key1": {}},
Default: NewObject(map[string]string{"key1": "val1"}),
Description: "my object",
},
}

ctx = addContextParamSpec(ctx, want)
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/pipeline/v1beta1/taskrun_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func validateNoDuplicateNames(names []string, byIndex bool) (errs *apis.FieldErr
errs = errs.Also(apis.ErrMultipleOneOf("name").ViaKey(n))
}
}
seen.Insert(n)
seen.Insert(strings.ToLower(n))
}
return errs
}

0 comments on commit 136e04d

Please sign in to comment.