github.com/nginxinc/kubernetes-ingress@v1.12.5/pkg/apis/configuration/validation/globalconfiguration_test.go (about)

     1  package validation
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1alpha1"
     7  	"k8s.io/apimachinery/pkg/util/validation/field"
     8  )
     9  
    10  func createGlobalConfigurationValidator() *GlobalConfigurationValidator {
    11  	return &GlobalConfigurationValidator{}
    12  }
    13  
    14  func TestValidateGlobalConfiguration(t *testing.T) {
    15  	globalConfiguration := v1alpha1.GlobalConfiguration{
    16  		Spec: v1alpha1.GlobalConfigurationSpec{
    17  			Listeners: []v1alpha1.Listener{
    18  				{
    19  					Name:     "tcp-listener",
    20  					Port:     53,
    21  					Protocol: "TCP",
    22  				},
    23  				{
    24  					Name:     "udp-listener",
    25  					Port:     53,
    26  					Protocol: "UDP",
    27  				},
    28  			},
    29  		},
    30  	}
    31  
    32  	gcv := createGlobalConfigurationValidator()
    33  
    34  	err := gcv.ValidateGlobalConfiguration(&globalConfiguration)
    35  	if err != nil {
    36  		t.Errorf("ValidateGlobalConfiguration() returned error %v for valid input", err)
    37  	}
    38  }
    39  
    40  func TestValidateListenerPort(t *testing.T) {
    41  	forbiddenListenerPorts := map[int]bool{
    42  		1234: true,
    43  	}
    44  
    45  	gcv := &GlobalConfigurationValidator{
    46  		forbiddenListenerPorts: forbiddenListenerPorts,
    47  	}
    48  
    49  	allErrs := gcv.validateListenerPort(5555, field.NewPath("port"))
    50  	if len(allErrs) > 0 {
    51  		t.Errorf("validateListenerPort() returned errors %v for valid input", allErrs)
    52  	}
    53  
    54  	allErrs = gcv.validateListenerPort(1234, field.NewPath("port"))
    55  	if len(allErrs) == 0 {
    56  		t.Errorf("validateListenerPort() returned no errors for invalid input")
    57  	}
    58  }
    59  
    60  func TestValidateListeners(t *testing.T) {
    61  	listeners := []v1alpha1.Listener{
    62  		{
    63  			Name:     "tcp-listener",
    64  			Port:     53,
    65  			Protocol: "TCP",
    66  		},
    67  		{
    68  			Name:     "udp-listener",
    69  			Port:     53,
    70  			Protocol: "UDP",
    71  		},
    72  	}
    73  
    74  	gcv := createGlobalConfigurationValidator()
    75  
    76  	allErrs := gcv.validateListeners(listeners, field.NewPath("listeners"))
    77  	if len(allErrs) > 0 {
    78  		t.Errorf("validateListeners() returned errors %v for valid intput", allErrs)
    79  	}
    80  }
    81  
    82  func TestValidateListenersFails(t *testing.T) {
    83  	tests := []struct {
    84  		listeners []v1alpha1.Listener
    85  		msg       string
    86  	}{
    87  		{
    88  			listeners: []v1alpha1.Listener{
    89  				{
    90  					Name:     "tcp-listener",
    91  					Port:     2201,
    92  					Protocol: "TCP",
    93  				},
    94  				{
    95  					Name:     "tcp-listener",
    96  					Port:     2202,
    97  					Protocol: "TCP",
    98  				},
    99  			},
   100  			msg: "duplicated name",
   101  		},
   102  		{
   103  			listeners: []v1alpha1.Listener{
   104  				{
   105  					Name:     "tcp-listener-1",
   106  					Port:     2201,
   107  					Protocol: "TCP",
   108  				},
   109  				{
   110  					Name:     "tcp-listener-2",
   111  					Port:     2201,
   112  					Protocol: "TCP",
   113  				},
   114  			},
   115  			msg: "duplicated port/protocol combination",
   116  		},
   117  	}
   118  
   119  	gcv := createGlobalConfigurationValidator()
   120  
   121  	for _, test := range tests {
   122  		allErrs := gcv.validateListeners(test.listeners, field.NewPath("listeners"))
   123  		if len(allErrs) == 0 {
   124  			t.Errorf("validateListeners() returned no errors for invalid input for the case of %s", test.msg)
   125  		}
   126  	}
   127  }
   128  
   129  func TestValidateListener(t *testing.T) {
   130  	listener := v1alpha1.Listener{
   131  		Name:     "tcp-listener",
   132  		Port:     53,
   133  		Protocol: "TCP",
   134  	}
   135  
   136  	gcv := createGlobalConfigurationValidator()
   137  
   138  	allErrs := gcv.validateListener(listener, field.NewPath("listener"))
   139  	if len(allErrs) > 0 {
   140  		t.Errorf("validateListener() returned errors %v for valid intput", allErrs)
   141  	}
   142  }
   143  
   144  func TestValidateListenerFails(t *testing.T) {
   145  	tests := []struct {
   146  		Listener v1alpha1.Listener
   147  		msg      string
   148  	}{
   149  		{
   150  			Listener: v1alpha1.Listener{
   151  				Name:     "@",
   152  				Port:     2201,
   153  				Protocol: "TCP",
   154  			},
   155  			msg: "invalid name",
   156  		},
   157  		{
   158  			Listener: v1alpha1.Listener{
   159  				Name:     "tcp-listener",
   160  				Port:     -1,
   161  				Protocol: "TCP",
   162  			},
   163  			msg: "invalid port",
   164  		},
   165  		{
   166  			Listener: v1alpha1.Listener{
   167  				Name:     "name",
   168  				Port:     2201,
   169  				Protocol: "IP",
   170  			},
   171  			msg: "invalid protocol",
   172  		},
   173  		{
   174  			Listener: v1alpha1.Listener{
   175  				Name:     "tls-passthrough",
   176  				Port:     2201,
   177  				Protocol: "TCP",
   178  			},
   179  			msg: "name of a built-in listener",
   180  		},
   181  	}
   182  
   183  	gcv := createGlobalConfigurationValidator()
   184  
   185  	for _, test := range tests {
   186  		allErrs := gcv.validateListener(test.Listener, field.NewPath("listener"))
   187  		if len(allErrs) == 0 {
   188  			t.Errorf("validateListener() returned no errors for invalid input for the case of %s", test.msg)
   189  		}
   190  	}
   191  }
   192  
   193  func TestGeneratePortProtocolKey(t *testing.T) {
   194  	port := 53
   195  	protocol := "UDP"
   196  
   197  	expected := "53/UDP"
   198  
   199  	result := generatePortProtocolKey(port, protocol)
   200  
   201  	if result != expected {
   202  		t.Errorf("generatePortProtocolKey(%d, %q) returned %q but expected %q", port, protocol, result, expected)
   203  	}
   204  }