@@ -18,12 +18,109 @@ limitations under the License.
1818package emlb
1919
2020import (
21+ "os"
2122 "reflect"
2223 "testing"
2324
2425 corev1 "k8s.io/api/core/v1"
2526)
2627
28+ func Test_getResourceName (t * testing.T ) {
29+ loadBalancerName := "my-loadbalancer"
30+ resourceType := "pool"
31+ want := "my-loadbalancer-pool"
32+
33+ got := getResourceName (loadBalancerName , resourceType )
34+
35+ if got != want {
36+ t .Errorf ("getResourceName() = %v, want %v" , got , want )
37+ }
38+ }
39+
40+ func Test_checkDebugEnabled (t * testing.T ) {
41+ // Set the PACKNGO_DEBUG environment variable to enable debug mode
42+ if err := os .Setenv ("PACKNGO_DEBUG" , "true" ); err != nil {
43+ t .Errorf ("Error testing checkDebugEnabled: %v" , err )
44+ }
45+
46+ // Call the checkDebugEnabled function
47+ debugEnabled := checkDebugEnabled ()
48+
49+ // Check if debugEnabled is true
50+ if ! debugEnabled {
51+ t .Errorf ("checkDebugEnabled() = %v, want %v" , debugEnabled , true )
52+ }
53+
54+ // Unset the PACKNGO_DEBUG environment variable
55+ os .Unsetenv ("PACKNGO_DEBUG" )
56+
57+ // Call the checkDebugEnabled function again
58+ debugEnabled = checkDebugEnabled ()
59+
60+ // Check if debugEnabled is false
61+ if debugEnabled {
62+ t .Errorf ("checkDebugEnabled() = %v, want %v" , debugEnabled , false )
63+ }
64+ }
65+
66+ func Test_convertToTarget (t * testing.T ) {
67+ type args struct {
68+ devaddr corev1.NodeAddress
69+ }
70+ tests := []struct {
71+ name string
72+ args args
73+ want * Target
74+ }{
75+ {
76+ name : "Internal IP" ,
77+ args : args {
78+ corev1.NodeAddress {
79+ Type : "InternalIP" ,
80+ Address : "10.2.1.5" ,
81+ },
82+ },
83+ want : & Target {
84+ IP : "10.2.1.5" ,
85+ Port : loadBalancerVIPPort ,
86+ },
87+ },
88+ {
89+ name : "External IP" ,
90+ args : args {
91+ corev1.NodeAddress {
92+ Type : "ExternalIP" ,
93+ Address : "1.2.3.4" ,
94+ },
95+ },
96+ want : & Target {
97+ IP : "1.2.3.4" ,
98+ Port : loadBalancerVIPPort ,
99+ },
100+ },
101+ {
102+ name : "Empty IP" ,
103+ args : args {
104+ corev1.NodeAddress {
105+ Type : "ExternalIP" ,
106+ Address : "" ,
107+ },
108+ },
109+ want : & Target {
110+ IP : "" ,
111+ Port : loadBalancerVIPPort ,
112+ },
113+ },
114+ }
115+ for _ , tt := range tests {
116+ t .Run (tt .name , func (t * testing.T ) {
117+ if got := convertToTarget (tt .args .devaddr ); ! reflect .DeepEqual (got , tt .want ) {
118+ t .Errorf ("convertToTarget() = %v, want %v" , got , tt .want )
119+ }
120+ })
121+ }
122+ }
123+
27124func Test_getExternalIPv4Target (t * testing.T ) {
28125 type args struct {
29126 deviceAddr []corev1.NodeAddress
0 commit comments