@@ -579,3 +579,195 @@ func Test_convertAutoFields(t *testing.T) {
579
579
consumerGroupPluginConfig := got .ConsumerGroups [0 ].Plugins [0 ].Config
580
580
assert .NotEmpty (t , consumerGroupPluginConfig ["namespace" ])
581
581
}
582
+
583
+ func Test_convertPreFunction (t * testing.T ) {
584
+ content := & file.Content {
585
+ Services : []file.FService {
586
+ {
587
+ Service : kong.Service {
588
+ Name : kong .String ("s1" ),
589
+ Host : kong .String ("httpbin.org" ),
590
+ },
591
+ Plugins : []* file.FPlugin {
592
+ {
593
+ Plugin : kong.Plugin {
594
+ Name : kong .String ("pre-function" ),
595
+ Config : kong.Configuration {
596
+ "functions" : "kong.log.err(\" service\" )" ,
597
+ },
598
+ },
599
+ },
600
+ },
601
+ },
602
+ },
603
+ Routes : []file.FRoute {
604
+ {
605
+ Route : kong.Route {
606
+ Name : kong .String ("r1" ),
607
+ Paths : []* string {kong .String ("/r1" )},
608
+ },
609
+ Plugins : []* file.FPlugin {
610
+ {
611
+ Plugin : kong.Plugin {
612
+ Name : kong .String ("pre-function" ),
613
+ Config : kong.Configuration {
614
+ "functions" : "kong.log.err(\" route\" )" ,
615
+ },
616
+ },
617
+ },
618
+ },
619
+ },
620
+ },
621
+ Consumers : []file.FConsumer {
622
+ {
623
+ Consumer : kong.Consumer {
624
+ Username : kong .String ("foo" ),
625
+ },
626
+ Plugins : []* file.FPlugin {
627
+ {
628
+ Plugin : kong.Plugin {
629
+ Name : kong .String ("pre-function" ),
630
+ Config : kong.Configuration {
631
+ "functions" : "kong.log.err(\" consumer\" )" ,
632
+ },
633
+ },
634
+ },
635
+ },
636
+ },
637
+ },
638
+ ConsumerGroups : []file.FConsumerGroupObject {
639
+ {
640
+ ConsumerGroup : kong.ConsumerGroup {
641
+ Name : kong .String ("my_consumer_group" ),
642
+ },
643
+ Plugins : []* kong.ConsumerGroupPlugin {
644
+ {
645
+ Name : kong .String ("pre-function" ),
646
+ Config : kong.Configuration {
647
+ "functions" : "kong.log.err(\" consumer_group\" )" ,
648
+ },
649
+ },
650
+ },
651
+ },
652
+ },
653
+ Plugins : []file.FPlugin {
654
+ {
655
+ Plugin : kong.Plugin {
656
+ Name : kong .String ("pre-function" ),
657
+ Config : kong.Configuration {
658
+ "functions" : "kong.log.err(\" global\" )" ,
659
+ },
660
+ },
661
+ },
662
+ },
663
+ }
664
+
665
+ got , err := convertKongGateway2xTo3x (content , "-" )
666
+ assert .NoError (t , err )
667
+
668
+ globalPluginConfig := got .Plugins [0 ].Config
669
+ assert .Contains (t , globalPluginConfig ["access" ], "global" )
670
+ assert .Empty (t , globalPluginConfig ["functions" ])
671
+
672
+ servicePluginConfig := got .Services [0 ].Plugins [0 ].Config
673
+ assert .Contains (t , servicePluginConfig ["access" ], "service" )
674
+ assert .Empty (t , servicePluginConfig ["functions" ])
675
+
676
+ routePluginConfig := got .Routes [0 ].Plugins [0 ].Config
677
+ assert .Contains (t , routePluginConfig ["access" ], "route" )
678
+ assert .Empty (t , routePluginConfig ["functions" ])
679
+
680
+ consumerPluginConfig := got .Consumers [0 ].Plugins [0 ].Config
681
+ assert .Contains (t , consumerPluginConfig ["access" ], "consumer" )
682
+ assert .Empty (t , consumerPluginConfig ["functions" ])
683
+
684
+ consumerGroupPluginConfig := got .ConsumerGroups [0 ].Plugins [0 ].Config
685
+ assert .Contains (t , consumerGroupPluginConfig ["access" ], "consumer_group" )
686
+ assert .Empty (t , consumerGroupPluginConfig ["functions" ])
687
+ }
688
+
689
+ func Test_convertPreFunctionWithError (t * testing.T ) {
690
+ content := & file.Content {
691
+ Services : []file.FService {
692
+ {
693
+ Service : kong.Service {
694
+ Name : kong .String ("s1" ),
695
+ Host : kong .String ("httpbin.org" ),
696
+ },
697
+ Plugins : []* file.FPlugin {
698
+ {
699
+ Plugin : kong.Plugin {
700
+ Name : kong .String ("pre-function" ),
701
+ Config : kong.Configuration {
702
+ "should_be_functions" : "kong.log.err(\" service\" )" ,
703
+ },
704
+ },
705
+ },
706
+ },
707
+ },
708
+ },
709
+ Routes : []file.FRoute {
710
+ {
711
+ Route : kong.Route {
712
+ Name : kong .String ("r1" ),
713
+ Paths : []* string {kong .String ("/r1" )},
714
+ },
715
+ Plugins : []* file.FPlugin {
716
+ {
717
+ Plugin : kong.Plugin {
718
+ Name : kong .String ("pre-function" ),
719
+ Config : kong.Configuration {
720
+ "should_be_functions" : "kong.log.err(\" route\" )" ,
721
+ },
722
+ },
723
+ },
724
+ },
725
+ },
726
+ },
727
+ Consumers : []file.FConsumer {
728
+ {
729
+ Consumer : kong.Consumer {
730
+ Username : kong .String ("foo" ),
731
+ },
732
+ Plugins : []* file.FPlugin {
733
+ {
734
+ Plugin : kong.Plugin {
735
+ Name : kong .String ("pre-function" ),
736
+ Config : kong.Configuration {
737
+ "should_be_functions" : "kong.log.err(\" consumer\" )" ,
738
+ },
739
+ },
740
+ },
741
+ },
742
+ },
743
+ },
744
+ ConsumerGroups : []file.FConsumerGroupObject {
745
+ {
746
+ ConsumerGroup : kong.ConsumerGroup {
747
+ Name : kong .String ("my_consumer_group" ),
748
+ },
749
+ Plugins : []* kong.ConsumerGroupPlugin {
750
+ {
751
+ Name : kong .String ("pre-function" ),
752
+ Config : kong.Configuration {
753
+ "should_be_functions" : "kong.log.err(\" consumer_group\" )" ,
754
+ },
755
+ },
756
+ },
757
+ },
758
+ },
759
+ Plugins : []file.FPlugin {
760
+ {
761
+ Plugin : kong.Plugin {
762
+ Name : kong .String ("pre-function" ),
763
+ Config : kong.Configuration {
764
+ "should_be_functions" : "kong.log.err(\" global\" )" ,
765
+ },
766
+ },
767
+ },
768
+ },
769
+ }
770
+
771
+ _ , err := convertKongGateway2xTo3x (content , "-" )
772
+ assert .Error (t , err )
773
+ }
0 commit comments