@@ -38,9 +38,10 @@ func TestRunFunctionSimple(t *testing.T) {
3838 )
3939
4040 cases := map [string ]struct {
41- reason string
42- args args
43- want want
41+ reason string
42+ defaultSource string
43+ args args
44+ want want
4445 }{
4546 "ResponseIsReturned" : {
4647 reason : "The Function should return a fatal result if no input was specified" ,
@@ -387,6 +388,60 @@ func TestRunFunctionSimple(t *testing.T) {
387388 },
388389 },
389390 },
391+ "EmptyInputWithDefaultSource" : {
392+ reason : "The function should use the default source when input is not provided and default source is set" ,
393+ defaultSource : "{\n apiVersion: \" example.org/v1\" \n kind: \" Generated\" \n }" ,
394+ args : args {
395+ req : & fnv1.RunFunctionRequest {
396+ Meta : & fnv1.RequestMeta {Tag : "empty-input" },
397+ Input : nil ,
398+ Observed : & fnv1.State {
399+ Composite : & fnv1.Resource {
400+ Resource : resource .MustStructJSON (`{"apiVersion":"example.org/v1","kind":"XR"}` ),
401+ },
402+ },
403+ },
404+ },
405+ want : want {
406+ rsp : & fnv1.RunFunctionResponse {
407+ Meta : & fnv1.ResponseMeta {Tag : "empty-input" , Ttl : durationpb .New (response .DefaultTTL )},
408+ Desired : & fnv1.State {
409+ Composite : & fnv1.Resource {
410+ Resource : resource .MustStructJSON (`{"apiVersion":"example.org/v1","kind":"XR"}` ),
411+ },
412+ Resources : map [string ]* fnv1.Resource {
413+ "" : {
414+ Resource : resource .MustStructJSON (`{"apiVersion":"example.org/v1","kind":"Generated"}` ),
415+ },
416+ },
417+ },
418+ },
419+ },
420+ },
421+ "EmptyInputWithoutDefaultSource" : {
422+ reason : "The function should fail when input is not provided and default source is not set" ,
423+ args : args {
424+ req : & fnv1.RunFunctionRequest {
425+ Meta : & fnv1.RequestMeta {Tag : "empty-input" },
426+ Input : nil ,
427+ Observed : & fnv1.State {
428+ Composite : & fnv1.Resource {
429+ Resource : resource .MustStructJSON (`{"apiVersion":"example.org/v1","kind":"XR"}` ),
430+ },
431+ },
432+ },
433+ },
434+ want : want {
435+ rsp : & fnv1.RunFunctionResponse {
436+ Meta : & fnv1.ResponseMeta {Tag : "empty-input" , Ttl : durationpb .New (response .DefaultTTL )},
437+ Results : []* fnv1.Result {{
438+ Target : fnv1 .Target_TARGET_COMPOSITE .Enum (),
439+ Severity : fnv1 .Severity_SEVERITY_FATAL ,
440+ Message : "invalid function input: spec.source: Required value: kcl source cannot be empty" ,
441+ }},
442+ },
443+ },
444+ },
390445 // TODO: disable the resource check, and fix the kcl dup resource evaluation issues.
391446 // "MultipleResourceError": {
392447 // reason: "The Function should return a fatal result if input resources have duplicate names",
@@ -425,6 +480,9 @@ func TestRunFunctionSimple(t *testing.T) {
425480
426481 for name , tc := range cases {
427482 t .Run (name , func (t * testing.T ) {
483+ // NOTE: This means we can't run tests in parallel.
484+ defaultSource = tc .defaultSource
485+
428486 // f := &Function{log: logging.NewNopLogger()}
429487 f := & Function {log : logging .NewLogrLogger (testr .New (t ))}
430488 rsp , err := f .RunFunction (tc .args .ctx , tc .args .req )
0 commit comments