Skip to content

Commit

Permalink
Merge pull request #21 from ezgidemirel/add-nil-checks
Browse files Browse the repository at this point in the history
Fix nil pointer crashes with wrong input
  • Loading branch information
ezgidemirel authored Nov 8, 2023
2 parents 680f061 + d0455ff commit 0269607
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
43 changes: 43 additions & 0 deletions fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,49 @@ func TestRunFunction(t *testing.T) {
},
},
},
"WrongInlineInput": {
reason: "The Function should return a fatal result if there is no inline template provided",
args: args{
req: &fnv1beta1.RunFunctionRequest{
Input: resource.MustStructObject(
&v1beta1.GoTemplate{
Source: v1beta1.InlineSource,
}),
},
},
want: want{
rsp: &fnv1beta1.RunFunctionResponse{
Meta: &fnv1beta1.ResponseMeta{Ttl: durationpb.New(response.DefaultTTL)},
Results: []*fnv1beta1.Result{
{
Severity: fnv1beta1.Severity_SEVERITY_FATAL,
Message: "invalid function input: inline.template should be provided",
},
},
},
},
},
"WrongFileSystemInput": {
args: args{
req: &fnv1beta1.RunFunctionRequest{
Input: resource.MustStructObject(
&v1beta1.GoTemplate{
Source: v1beta1.FileSystemSource,
}),
},
},
want: want{
rsp: &fnv1beta1.RunFunctionResponse{
Meta: &fnv1beta1.ResponseMeta{Ttl: durationpb.New(response.DefaultTTL)},
Results: []*fnv1beta1.Result{
{
Severity: fnv1beta1.Severity_SEVERITY_FATAL,
Message: "invalid function input: fileSystem.dirPath should be provided",
},
},
},
},
},
"NoResourceNameAnnotation": {
reason: "The Function should return a fatal result if the cd does not have a composition-resource-name annotation",
args: args{
Expand Down
8 changes: 8 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (is *InlineSource) GetTemplates() string {
}

func newInlineSource(in *v1beta1.GoTemplate) (*InlineSource, error) {
if in.Inline == nil || in.Inline.Template == "" {
return nil, errors.New("inline.template should be provided")
}

return &InlineSource{
Template: in.Inline.Template,
}, nil
Expand All @@ -59,6 +63,10 @@ func (fs *FileSource) GetTemplates() string {
}

func newFileSource(in *v1beta1.GoTemplate) (*FileSource, error) {
if in.FileSystem == nil || in.FileSystem.DirPath == "" {
return nil, errors.New("fileSystem.dirPath should be provided")
}

d := in.FileSystem.DirPath

tmpl, err := readTemplates(d)
Expand Down

0 comments on commit 0269607

Please sign in to comment.