@@ -20,6 +20,8 @@ limitations under the License.
20
20
package app
21
21
22
22
import (
23
+ "context"
24
+ "errors"
23
25
"fmt"
24
26
"net"
25
27
"os"
@@ -30,8 +32,8 @@ import (
30
32
"testing"
31
33
"time"
32
34
35
+ "github.com/google/go-cmp/cmp"
33
36
"github.com/spf13/pflag"
34
-
35
37
v1 "k8s.io/api/core/v1"
36
38
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
37
39
"k8s.io/apimachinery/pkg/runtime"
@@ -719,3 +721,167 @@ func TestProxyServer_platformSetup(t *testing.T) {
719
721
})
720
722
}
721
723
}
724
+
725
+ type fakeConntracker struct {
726
+ called []string
727
+ err error
728
+ }
729
+
730
+ // SetMax value is calculated based on the number of CPUs by getConntrackMax()
731
+ func (fc * fakeConntracker ) SetMax (ctx context.Context , max int ) error {
732
+ fc .called = append (fc .called , "SetMax" )
733
+ return fc .err
734
+ }
735
+ func (fc * fakeConntracker ) SetTCPEstablishedTimeout (ctx context.Context , seconds int ) error {
736
+ fc .called = append (fc .called , fmt .Sprintf ("SetTCPEstablishedTimeout(%d)" , seconds ))
737
+ return fc .err
738
+ }
739
+ func (fc * fakeConntracker ) SetTCPCloseWaitTimeout (ctx context.Context , seconds int ) error {
740
+ fc .called = append (fc .called , fmt .Sprintf ("SetTCPCloseWaitTimeout(%d)" , seconds ))
741
+ return fc .err
742
+ }
743
+ func (fc * fakeConntracker ) SetTCPBeLiberal (ctx context.Context , value int ) error {
744
+ fc .called = append (fc .called , fmt .Sprintf ("SetTCPBeLiberal(%d)" , value ))
745
+ return fc .err
746
+ }
747
+ func (fc * fakeConntracker ) SetUDPTimeout (ctx context.Context , seconds int ) error {
748
+ fc .called = append (fc .called , fmt .Sprintf ("SetUDPTimeout(%d)" , seconds ))
749
+ return fc .err
750
+ }
751
+ func (fc * fakeConntracker ) SetUDPStreamTimeout (ctx context.Context , seconds int ) error {
752
+ fc .called = append (fc .called , fmt .Sprintf ("SetUDPStreamTimeout(%d)" , seconds ))
753
+ return fc .err
754
+ }
755
+
756
+ func TestSetupConntrack (t * testing.T ) {
757
+ _ , ctx := ktesting .NewTestContext (t )
758
+ tests := []struct {
759
+ name string
760
+ config proxyconfigapi.KubeProxyConntrackConfiguration
761
+ expect []string
762
+ conntrackErr error
763
+ wantErr bool
764
+ }{
765
+ {
766
+ name : "do nothing if conntrack config is empty" ,
767
+ config : proxyconfigapi.KubeProxyConntrackConfiguration {},
768
+ expect : nil ,
769
+ },
770
+ {
771
+ name : "SetMax is called if conntrack.maxPerCore is specified" ,
772
+ config : proxyconfigapi.KubeProxyConntrackConfiguration {
773
+ MaxPerCore : ptr .To (int32 (12 )),
774
+ },
775
+ expect : []string {"SetMax" },
776
+ },
777
+ {
778
+ name : "SetMax is not called if conntrack.maxPerCore is 0" ,
779
+ config : proxyconfigapi.KubeProxyConntrackConfiguration {
780
+ MaxPerCore : ptr .To (int32 (0 )),
781
+ },
782
+ expect : nil ,
783
+ },
784
+ {
785
+ name : "SetTCPEstablishedTimeout is called if conntrack.tcpEstablishedTimeout is specified" ,
786
+ config : proxyconfigapi.KubeProxyConntrackConfiguration {
787
+ TCPEstablishedTimeout : & metav1.Duration {Duration : 5 * time .Second },
788
+ },
789
+ expect : []string {"SetTCPEstablishedTimeout(5)" },
790
+ },
791
+ {
792
+ name : "SetTCPEstablishedTimeout is not called if conntrack.tcpEstablishedTimeout is 0" ,
793
+ config : proxyconfigapi.KubeProxyConntrackConfiguration {
794
+ TCPEstablishedTimeout : & metav1.Duration {Duration : 0 * time .Second },
795
+ },
796
+ expect : nil ,
797
+ },
798
+ {
799
+ name : "SetTCPCloseWaitTimeout is called if conntrack.tcpCloseWaitTimeout is specified" ,
800
+ config : proxyconfigapi.KubeProxyConntrackConfiguration {
801
+ TCPCloseWaitTimeout : & metav1.Duration {Duration : 5 * time .Second },
802
+ },
803
+ expect : []string {"SetTCPCloseWaitTimeout(5)" },
804
+ },
805
+ {
806
+ name : "SetTCPCloseWaitTimeout is not called if conntrack.tcpCloseWaitTimeout is 0" ,
807
+ config : proxyconfigapi.KubeProxyConntrackConfiguration {
808
+ TCPCloseWaitTimeout : & metav1.Duration {Duration : 0 * time .Second },
809
+ },
810
+ expect : nil ,
811
+ },
812
+ {
813
+ name : "SetTCPBeLiberal is called if conntrack.tcpBeLiberal is true" ,
814
+ config : proxyconfigapi.KubeProxyConntrackConfiguration {
815
+ TCPBeLiberal : true ,
816
+ },
817
+ expect : []string {"SetTCPBeLiberal(1)" },
818
+ },
819
+ {
820
+ name : "SetTCPBeLiberal is not called if conntrack.tcpBeLiberal is false" ,
821
+ config : proxyconfigapi.KubeProxyConntrackConfiguration {
822
+ TCPBeLiberal : false ,
823
+ },
824
+ expect : nil ,
825
+ },
826
+ {
827
+ name : "SetUDPTimeout is called if conntrack.udpTimeout is specified" ,
828
+ config : proxyconfigapi.KubeProxyConntrackConfiguration {
829
+ UDPTimeout : metav1.Duration {Duration : 5 * time .Second },
830
+ },
831
+ expect : []string {"SetUDPTimeout(5)" },
832
+ },
833
+ {
834
+ name : "SetUDPTimeout is called if conntrack.udpTimeout is zero" ,
835
+ config : proxyconfigapi.KubeProxyConntrackConfiguration {
836
+ UDPTimeout : metav1.Duration {Duration : 0 * time .Second },
837
+ },
838
+ expect : nil ,
839
+ },
840
+ {
841
+ name : "SetUDPStreamTimeout is called if conntrack.udpStreamTimeout is specified" ,
842
+ config : proxyconfigapi.KubeProxyConntrackConfiguration {
843
+ UDPStreamTimeout : metav1.Duration {Duration : 5 * time .Second },
844
+ },
845
+ expect : []string {"SetUDPStreamTimeout(5)" },
846
+ },
847
+ {
848
+ name : "SetUDPStreamTimeout is called if conntrack.udpStreamTimeout is zero" ,
849
+ config : proxyconfigapi.KubeProxyConntrackConfiguration {
850
+ UDPStreamTimeout : metav1.Duration {Duration : 0 * time .Second },
851
+ },
852
+ expect : nil ,
853
+ },
854
+ {
855
+ name : "an error is returned if conntrack.SetTCPEstablishedTimeout fails" ,
856
+ config : proxyconfigapi.KubeProxyConntrackConfiguration {
857
+ TCPEstablishedTimeout : & metav1.Duration {Duration : 5 * time .Second },
858
+ },
859
+ expect : []string {"SetTCPEstablishedTimeout(5)" },
860
+ conntrackErr : errors .New ("random error" ),
861
+ wantErr : true ,
862
+ },
863
+ }
864
+
865
+ for _ , test := range tests {
866
+ t .Run (test .name , func (t * testing.T ) {
867
+ fc := & fakeConntracker {err : test .conntrackErr }
868
+ s := & ProxyServer {
869
+ Config : & proxyconfigapi.KubeProxyConfiguration {
870
+ Linux : proxyconfigapi.KubeProxyLinuxConfiguration {
871
+ Conntrack : test .config ,
872
+ },
873
+ },
874
+ }
875
+ err := s .setupConntrack (ctx , fc )
876
+ if test .wantErr && err == nil {
877
+ t .Errorf ("Test %q: Expected error, got nil" , test .name )
878
+ }
879
+ if ! test .wantErr && err != nil {
880
+ t .Errorf ("Test %q: Expected no error, got %v" , test .name , err )
881
+ }
882
+ if ! cmp .Equal (fc .called , test .expect ) {
883
+ t .Errorf ("Test %q: Expected conntrack calls: %v, got: %v" , test .name , test .expect , fc .called )
884
+ }
885
+ })
886
+ }
887
+ }
0 commit comments