github.com/cilium/cilium@v1.16.2/operator/pkg/model/ingestion/gamma_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package ingestion
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	corev1 "k8s.io/api/core/v1"
    11  	gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
    12  
    13  	"github.com/cilium/cilium/operator/pkg/model"
    14  )
    15  
    16  var meshSplitInput = GammaInput{
    17  	Services: []corev1.Service{
    18  		gammaEchoService,
    19  		gammaEchoV1Service,
    20  		gammaEchoV2Service,
    21  	},
    22  	HTTPRoutes: []gatewayv1.HTTPRoute{
    23  		meshSplitHTTPRoute,
    24  	},
    25  }
    26  
    27  var meshSplitListeners = []model.HTTPListener{
    28  	{
    29  		Name: "gateway-conformance-mesh-echo-80",
    30  		Sources: []model.FullyQualifiedResource{
    31  			{
    32  				Name:      "echo",
    33  				Namespace: "gateway-conformance-mesh",
    34  				Version:   "v1",
    35  				Kind:      "Service",
    36  			},
    37  			{
    38  				Name:      "mesh-split",
    39  				Namespace: "gateway-conformance-mesh",
    40  				Version:   "v1",
    41  				Group:     "gateway.networking.k8s.io",
    42  				Kind:      "HTTPRoute",
    43  			},
    44  		},
    45  		Port:     80,
    46  		Hostname: "*",
    47  		Routes: []model.HTTPRoute{
    48  			{
    49  				Hostnames: []string{
    50  					"*",
    51  				},
    52  				PathMatch: model.StringMatch{
    53  					Exact: "/v1",
    54  				},
    55  				Backends: []model.Backend{
    56  					{
    57  						Name:      "echo-v1",
    58  						Namespace: "gateway-conformance-mesh",
    59  						Port: &model.BackendPort{
    60  							Port: 80,
    61  						},
    62  						AppProtocol: model.AddressOf("http"),
    63  					},
    64  				},
    65  			},
    66  			{
    67  				Hostnames: []string{
    68  					"*",
    69  				},
    70  				PathMatch: model.StringMatch{
    71  					Exact: "/v2",
    72  				},
    73  				Backends: []model.Backend{
    74  					{
    75  						Name:      "echo-v2",
    76  						Namespace: "gateway-conformance-mesh",
    77  						Port: &model.BackendPort{
    78  							Port: 80,
    79  						},
    80  						AppProtocol: model.AddressOf("http"),
    81  					},
    82  				},
    83  			},
    84  		},
    85  		Service: &model.Service{
    86  			Type: "ClusterIP",
    87  		},
    88  	},
    89  	{
    90  		Name: "gateway-conformance-mesh-echo-8080",
    91  		Sources: []model.FullyQualifiedResource{
    92  			{
    93  				Name:      "echo",
    94  				Namespace: "gateway-conformance-mesh",
    95  				Version:   "v1",
    96  				Kind:      "Service",
    97  			},
    98  			{
    99  				Name:      "mesh-split",
   100  				Namespace: "gateway-conformance-mesh",
   101  				Version:   "v1",
   102  				Group:     "gateway.networking.k8s.io",
   103  				Kind:      "HTTPRoute",
   104  			},
   105  		},
   106  		Port:     8080,
   107  		Hostname: "*",
   108  		Routes: []model.HTTPRoute{
   109  			{
   110  				Hostnames: []string{
   111  					"*",
   112  				},
   113  				PathMatch: model.StringMatch{
   114  					Exact: "/v1",
   115  				},
   116  				Backends: []model.Backend{
   117  					{
   118  						Name:      "echo-v1",
   119  						Namespace: "gateway-conformance-mesh",
   120  						Port: &model.BackendPort{
   121  							Port: 80,
   122  						},
   123  						AppProtocol: model.AddressOf("http"),
   124  					},
   125  				},
   126  			},
   127  			{
   128  				Hostnames: []string{
   129  					"*",
   130  				},
   131  				PathMatch: model.StringMatch{
   132  					Exact: "/v2",
   133  				},
   134  				Backends: []model.Backend{
   135  					{
   136  						Name:      "echo-v2",
   137  						Namespace: "gateway-conformance-mesh",
   138  						Port: &model.BackendPort{
   139  							Port: 80,
   140  						},
   141  						AppProtocol: model.AddressOf("http"),
   142  					},
   143  				},
   144  			},
   145  		},
   146  		Service: &model.Service{
   147  			Type: "ClusterIP",
   148  		},
   149  	},
   150  }
   151  
   152  var meshPortsInput = GammaInput{
   153  	Services: []corev1.Service{
   154  		gammaEchoService,
   155  		gammaEchoV1Service,
   156  		gammaEchoV2Service,
   157  	},
   158  	HTTPRoutes: []gatewayv1.HTTPRoute{
   159  		meshPortsHTTPRoute,
   160  	},
   161  }
   162  
   163  var meshPortsListeners = []model.HTTPListener{
   164  	{
   165  		Name: "gateway-conformance-mesh-echo-v1-80",
   166  		Sources: []model.FullyQualifiedResource{
   167  			{
   168  				Name:      "echo-v1",
   169  				Namespace: "gateway-conformance-mesh",
   170  				Version:   "v1",
   171  				Kind:      "Service",
   172  			},
   173  			{
   174  				Name:      "mesh-ports",
   175  				Namespace: "gateway-conformance-mesh",
   176  				Version:   "v1",
   177  				Group:     "gateway.networking.k8s.io",
   178  				Kind:      "HTTPRoute",
   179  			},
   180  		},
   181  		Port:     80,
   182  		Hostname: "*",
   183  		Routes: []model.HTTPRoute{
   184  			{
   185  				Hostnames: []string{
   186  					"*",
   187  				},
   188  				Backends: []model.Backend{
   189  					{
   190  						Name:      "echo-v1",
   191  						Namespace: "gateway-conformance-mesh",
   192  						Port: &model.BackendPort{
   193  							Port: 80,
   194  						},
   195  						AppProtocol: model.AddressOf("http"),
   196  					},
   197  				},
   198  				ResponseHeaderModifier: &model.HTTPHeaderFilter{
   199  					HeadersToSet: []model.Header{
   200  						{
   201  							Name:  "X-Header-Set",
   202  							Value: "v1",
   203  						},
   204  					},
   205  				},
   206  			},
   207  		},
   208  		Service: &model.Service{
   209  			Type: "ClusterIP",
   210  		},
   211  	},
   212  }
   213  
   214  var meshFrontendInput = GammaInput{
   215  	Services: []corev1.Service{
   216  		gammaEchoService,
   217  		gammaEchoV1Service,
   218  		gammaEchoV2Service,
   219  	},
   220  	HTTPRoutes: []gatewayv1.HTTPRoute{
   221  		meshFrontendHTTPRoute,
   222  	},
   223  }
   224  
   225  var meshFrontendListeners = []model.HTTPListener{
   226  	{
   227  		Name: "gateway-conformance-mesh-echo-v2-80",
   228  		Sources: []model.FullyQualifiedResource{
   229  			{
   230  				Name:      "echo-v2",
   231  				Namespace: "gateway-conformance-mesh",
   232  				Version:   "v1",
   233  				Kind:      "Service",
   234  			},
   235  			{
   236  				Name:      "mesh-split-v1",
   237  				Namespace: "gateway-conformance-mesh",
   238  				Version:   "v1",
   239  				Group:     "gateway.networking.k8s.io",
   240  				Kind:      "HTTPRoute",
   241  			},
   242  		},
   243  		Port:     80,
   244  		Hostname: "*",
   245  		Routes: []model.HTTPRoute{
   246  			{
   247  				Hostnames: []string{
   248  					"*",
   249  				},
   250  				Backends: []model.Backend{
   251  					{
   252  						Name:      "echo-v2",
   253  						Namespace: "gateway-conformance-mesh",
   254  						Port: &model.BackendPort{
   255  							Port: 80,
   256  						},
   257  						AppProtocol: model.AddressOf("http"),
   258  					},
   259  				},
   260  				ResponseHeaderModifier: &model.HTTPHeaderFilter{
   261  					HeadersToSet: []model.Header{
   262  						{
   263  							Name:  "X-Header-Set",
   264  							Value: "set",
   265  						},
   266  					},
   267  				},
   268  			},
   269  		},
   270  		Service: &model.Service{
   271  			Type: "ClusterIP",
   272  		},
   273  	},
   274  }
   275  
   276  func TestGammaConformance(t *testing.T) {
   277  	tests := map[string]struct {
   278  		input GammaInput
   279  		want  []model.HTTPListener
   280  	}{
   281  		"Mesh Split": {
   282  			input: meshSplitInput,
   283  			want:  meshSplitListeners,
   284  		},
   285  		"Mesh Ports": {
   286  			input: meshPortsInput,
   287  			want:  meshPortsListeners,
   288  		},
   289  		"Mesh Frontend": {
   290  			input: meshFrontendInput,
   291  			want:  meshFrontendListeners,
   292  		},
   293  	}
   294  
   295  	for name, tc := range tests {
   296  		t.Run(name, func(t *testing.T) {
   297  			listeners := GammaHTTPRoutes(tc.input)
   298  			assert.Equal(t, tc.want, listeners, "Listeners did not match")
   299  		})
   300  	}
   301  }