<commit_before>/*
Copyright 2019 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package net

import (
	"fmt"
	"testing"

	"github.com/google/go-cmp/cmp"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/util/sets"
	"knative.dev/networking/pkg/apis/networking"
)

func TestEndpointsToDests(t *testing.T) {
	for _, tc := range []struct {
		name           string
		endpoints      corev1.Endpoints
		protocol       networking.ProtocolType
		expectReady    sets.String
		expectNotReady sets.String
	}{{
		name:        "no endpoints",
		endpoints:   corev1.Endpoints{},
		expectReady: sets.NewString(),
	}, {
		name: "single endpoint single address",
		endpoints: corev1.Endpoints{
			Subsets: []corev1.EndpointSubset{{
				Addresses: []corev1.EndpointAddress{{
					IP: "172.16.17.32",
				}},
				Ports: []corev1.EndpointPort{{
					Name: networking.ServicePortNameHTTP1,
					Port: 1234,
				}},
			}},
		},
		expectReady: sets.NewString("172.16.17.32:1234"),
	}, {
		name: "single endpoint multiple address",
		endpoints: corev1.Endpoints{
			Subsets: []corev1.EndpointSubset{{
				Addresses: []corev1.EndpointAddress{{
					IP: "172.16.17.32",
				}, {
					IP: "172.16.31.10",
				}},
				Ports: []corev1.EndpointPort{{
					Name: networking.ServicePortNameHTTP1,
					Port: 1234,
				}},
			}},
		},
		expectReady: sets.NewString("172.16.17.32:1234", "172.16.31.10:1234"),
	}, {
		name: "single endpoint multiple addresses, including no ready addresses",
		endpoints: corev1.Endpoints{
			Subsets: []corev1.EndpointSubset{{
				Addresses: []corev1.EndpointAddress{{
					IP: "172.16.17.32",
				}, {
					IP: "172.16.31.10",
				}},
				NotReadyAddresses: []corev1.EndpointAddress{{
					IP: "192.168.127.12",
				}},
				Ports: []corev1.EndpointPort{{
					Name: networking.ServicePortNameHTTP1,
					Port: 1234,
				}},
			}},
		},
		expectReady:    sets.NewString("172.16.17.32:1234", "172.16.31.10:1234"),
		expectNotReady: sets.NewString("192.168.127.12:1234"),
	}, {
		name: "multiple endpoint filter port",
		endpoints: corev1.Endpoints{
			Subsets: []corev1.EndpointSubset{{
				Addresses: []corev1.EndpointAddress{{
					IP: "172.16.17.32",
				}},
				Ports: []corev1.EndpointPort{{
					Name: networking.ServicePortNameHTTP1,
					Port: 1234,
				}},
			}, {
				Addresses: []corev1.EndpointAddress{{
					IP: "172.16.31.10",
				}},
				Ports: []corev1.EndpointPort{{
					Name: "other-protocol",
					Port: 1234,
				}},
			}},
		},
		expectReady: sets.NewString("172.16.17.32:1234"),
	}} {
		t.Run(tc.name, func(t *testing.T) {
			if tc.protocol == "" {
				tc.protocol = networking.ProtocolHTTP1
			}
			ready, notReady := endpointsToDests(&tc.endpoints, networking.ServicePortName(tc.protocol))

			if got, want := ready, tc.expectReady; !got.Equal(want) {
				t.Error("Got unexpected ready dests (-want, +got):", cmp.Diff(want, got))
			}
			if got, want := notReady, tc.expectNotReady; !got.Equal(want) {
				t.Error("Got unexpected notReady dests (-want, +got):", cmp.Diff(want, got))
			}
		})
	}
}

func TestGetServicePort(t *testing.T) {
	for _, tc := range []struct {
		name     string
		protocol networking.ProtocolType
		ports    []corev1.ServicePort
		expect   int
		expectOK bool
	}{{
		name:     "Single port",
		protocol: networking.ProtocolHTTP1,
		ports: []corev1.ServicePort{{
			Name: "http",<commit_msg>Backfill missing test (#10585)<commit_after>/*
Copyright 2019 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package net

import (
	"fmt"
	"testing"

	"github.com/google/go-cmp/cmp"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/util/sets"
	"knative.dev/networking/pkg/apis/networking"
)

func TestEndpointsToDests(t *testing.T) {
	for _, tc := range []struct {
		name           string
		endpoints      corev1.Endpoints
		protocol       networking.ProtocolType
		expectReady    sets.String
		expectNotReady sets.String
	}{{
		name:        "no endpoints",
		endpoints:   corev1.Endpoints{},
		expectReady: sets.NewString(),
	}, {
		name: "single endpoint single address",
		endpoints: corev1.Endpoints{
			Subsets: []corev1.EndpointSubset{{
				Addresses: []corev1.EndpointAddress{{
					IP: "172.16.17.32",
				}},
				Ports: []corev1.EndpointPort{{
					Name: networking.ServicePortNameHTTP1,
					Port: 1234,
				}},
			}},
		},
		expectReady: sets.NewString("172.16.17.32:1234"),
	}, {
		name: "single endpoint multiple address",
		endpoints: corev1.Endpoints{
			Subsets: []corev1.EndpointSubset{{
				Addresses: []corev1.EndpointAddress{{
					IP: "172.16.17.32",
				}, {
					IP: "172.16.31.10",
				}},
				Ports: []corev1.EndpointPort{{
					Name: networking.ServicePortNameHTTP1,
					Port: 1234,
				}},
			}},
		},
		expectReady: sets.NewString("172.16.17.32:1234", "172.16.31.10:1234"),
	}, {
		name: "single endpoint multiple addresses, including no ready addresses",
		endpoints: corev1.Endpoints{
			Subsets: []corev1.EndpointSubset{{
				Addresses: []corev1.EndpointAddress{{
					IP: "172.16.17.32",
				}, {
					IP: "172.16.31.10",
				}},
				NotReadyAddresses: []corev1.EndpointAddress{{
					IP: "192.168.127.12",
				}},
				Ports: []corev1.EndpointPort{{
					Name: networking.ServicePortNameHTTP1,
					Port: 1234,
				}},
			}},
		},
		expectReady:    sets.NewString("172.16.17.32:1234", "172.16.31.10:1234"),
		expectNotReady: sets.NewString("192.168.127.12:1234"),
	}, {
		name: "multiple endpoint filter port",
		endpoints: corev1.Endpoints{
			Subsets: []corev1.EndpointSubset{{
				Addresses: []corev1.EndpointAddress{{
					IP: "172.16.17.32",
				}},
				Ports: []corev1.EndpointPort{{
					Name: networking.ServicePortNameHTTP1,
					Port: 1234,
				}},
			}, {
				Addresses: []corev1.EndpointAddress{{
					IP: "172.16.31.10",
				}},
				Ports: []corev1.EndpointPort{{
					Name: "other-protocol",
					Port: 1234,
				}},
			}},
		},
		expectReady: sets.NewString("172.16.17.32:1234"),
	}, {
		name:     "multiple endpoint, different protocol",
		protocol: networking.ProtocolH2C,
		endpoints: corev1.Endpoints{
			Subsets: []corev1.EndpointSubset{{
				Addresses: []corev1.EndpointAddress{{
					IP: "172.16.17.32",
				}, {
					IP: "172.16.31.10",
				}},
				Ports: []corev1.EndpointPort{{
					Name: networking.ServicePortNameHTTP1,
					Port: 1234,
				}},
			}, {
				Addresses: []corev1.EndpointAddress{{
					IP: "192.168.127.12",
				}, {
					IP: "192.168.3.11",
				}},
				Ports: []corev1.EndpointPort{{
					Name: networking.ServicePortNameH2C,
					Port: 5678,
				}},
			}},
		},
		expectReady: sets.NewString("192.168.127.12:5678", "192.168.3.11:5678"),
	}} {
		t.Run(tc.name, func(t *testing.T) {
			if tc.protocol == "" {
				tc.protocol = networking.ProtocolHTTP1
			}
			ready, notReady := endpointsToDests(&tc.endpoints, networking.ServicePortName(tc.protocol))

			if got, want := ready, tc.expectReady; !got.Equal(want) {
				t.Error("Got unexpected ready dests (-want, +got):", cmp.Diff(want, got))
			}
			if got, want := notReady, tc.expectNotReady; !got.Equal(want) {
				t.Error("Got unexpected notReady dests (-want, +got):", cmp.Diff(want, got))
			}
		})
	}
}

func TestGetServicePort(t *testing.T) {
	for _, tc := range []struct {
		name     string
		protocol networking.ProtocolType
		ports    []corev1.ServicePort
		expect   int
		expectOK bool
	}{{
		name:     "Single port",
		protocol: networking.ProtocolHTTP1,
		ports: []corev1.ServicePort{{
			Name: "http",
{'fraction_non_alphanumeric': 0.15121544327931363, 'fraction_numerical': 0.06482364156339371, 'mean_word_length': 3.2604060913705584, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 36, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4492129', 'n_tokens_mistral': 3736, 'n_tokens_neox': 2919, 'n_words': 626}