@@ -896,6 +896,114 @@ func TestRunFunction(t *testing.T) {
896896 Context : contextWithEnvironment (map [string ]interface {}{
897897 "widgets" : "30" ,
898898 })}}},
899+ "PatchToCompositeSupportsFromEnvironmentFieldPath" : {
900+ reason : "A basic FromEnvironmentFieldPath patch should work with environment.patches." ,
901+ args : args {
902+ req : & fnv1beta1.RunFunctionRequest {
903+ Input : resource .MustStructObject (& v1beta1.Resources {
904+ Resources : []v1beta1.ComposedTemplate {
905+ {
906+ Name : "cool-resource" ,
907+ Base : & runtime.RawExtension {Raw : []byte (`{"apiVersion":"example.org/v1","kind":"CD"}` )},
908+ }},
909+ Environment : & v1beta1.Environment {
910+ Patches : []v1beta1.EnvironmentPatch {
911+ {
912+ Type : v1beta1 .PatchTypeFromEnvironmentFieldPath ,
913+ Patch : v1beta1.Patch {
914+ FromFieldPath : ptr.To [string ]("widgets" ),
915+ ToFieldPath : ptr.To [string ]("spec.watchers" ),
916+ Transforms : []v1beta1.Transform {
917+ {
918+ Type : v1beta1 .TransformTypeConvert ,
919+ Convert : & v1beta1.ConvertTransform {
920+ ToType : v1beta1 .TransformIOTypeInt64 ,
921+ },
922+ },
923+ {
924+ Type : v1beta1 .TransformTypeMath ,
925+ Math : & v1beta1.MathTransform {
926+ Type : v1beta1 .MathTransformTypeMultiply ,
927+ Multiply : ptr.To [int64 ](3 ),
928+ }}}}}}}}),
929+ Observed : & fnv1beta1.State {
930+ Composite : & fnv1beta1.Resource {
931+ Resource : resource .MustStructJSON (`{"apiVersion":"example.org/v1","kind":"CD","spec":{}}` ),
932+ },
933+ Resources : map [string ]* fnv1beta1.Resource {},
934+ },
935+ Context : contextWithEnvironment (map [string ]interface {}{
936+ "widgets" : "10" ,
937+ })},
938+ },
939+ want : want {
940+ rsp : & fnv1beta1.RunFunctionResponse {
941+ Meta : & fnv1beta1.ResponseMeta {Ttl : durationpb .New (response .DefaultTTL )},
942+ Desired : & fnv1beta1.State {
943+ Composite : & fnv1beta1.Resource {
944+ // spec.watchers = 10 * 3 = 30
945+ Resource : resource .MustStructJSON (`{"apiVersion":"example.org/v1","kind":"CD","spec":{"watchers":30}}` ),
946+ },
947+ Resources : map [string ]* fnv1beta1.Resource {
948+ "cool-resource" : {
949+ Resource : resource .MustStructJSON (`{"apiVersion":"example.org/v1","kind":"CD"}` ),
950+ }}},
951+ Context : contextWithEnvironment (map [string ]interface {}{
952+ "widgets" : "10" ,
953+ })}}},
954+ "EnvironmentPatchSupportsToEnvironmentFieldPath" : {
955+ reason : "ToEnvironmentFieldPath patch should work with environment.patches." ,
956+ args : args {
957+ req : & fnv1beta1.RunFunctionRequest {
958+ Input : resource .MustStructObject (& v1beta1.Resources {
959+ Resources : []v1beta1.ComposedTemplate {
960+ {
961+ Name : "cool-resource" ,
962+ Base : & runtime.RawExtension {Raw : []byte (`{"apiVersion":"example.org/v1","kind":"CD"}` )},
963+ }},
964+ Environment : & v1beta1.Environment {
965+ Patches : []v1beta1.EnvironmentPatch {
966+ {
967+ Type : v1beta1 .PatchTypeToEnvironmentFieldPath ,
968+ Patch : v1beta1.Patch {
969+ FromFieldPath : ptr.To [string ]("spec.watchers" ),
970+ ToFieldPath : ptr.To [string ]("widgets" ),
971+ Transforms : []v1beta1.Transform {
972+ {
973+ Type : v1beta1 .TransformTypeMath ,
974+ Math : & v1beta1.MathTransform {
975+ Type : v1beta1 .MathTransformTypeMultiply ,
976+ Multiply : ptr.To [int64 ](3 ),
977+ },
978+ },
979+ {
980+ Type : v1beta1 .TransformTypeConvert ,
981+ Convert : & v1beta1.ConvertTransform {
982+ ToType : v1beta1 .TransformIOTypeString ,
983+ },
984+ }}}}}}}),
985+ Observed : & fnv1beta1.State {
986+ Composite : & fnv1beta1.Resource {
987+ Resource : resource .MustStructJSON (`{"apiVersion":"example.org/v1","kind":"CD","spec":{"watchers":10}}` ),
988+ },
989+ Resources : map [string ]* fnv1beta1.Resource {},
990+ },
991+ Context : contextWithEnvironment (nil )},
992+ },
993+ want : want {
994+ rsp : & fnv1beta1.RunFunctionResponse {
995+ Meta : & fnv1beta1.ResponseMeta {Ttl : durationpb .New (response .DefaultTTL )},
996+ Desired : & fnv1beta1.State {
997+ Composite : & fnv1beta1.Resource {
998+ Resource : resource .MustStructJSON (`{"apiVersion":"example.org/v1","kind":"CD"}` ),
999+ },
1000+ Resources : map [string ]* fnv1beta1.Resource {
1001+ "cool-resource" : {
1002+ Resource : resource .MustStructJSON (`{"apiVersion":"example.org/v1","kind":"CD"}` ),
1003+ }}},
1004+ Context : contextWithEnvironment (map [string ]interface {}{
1005+ "widgets" : "30" ,
1006+ })}}},
8991007 "EnvironmentPatchOptionalNotFoundSkipped" : {
9001008 reason : "A basic ToEnvironment patch with optional or not field path policy should be skipped" ,
9011009 args : args {
0 commit comments