// Copyright 2020 The Prometheus 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 kubernetes

import (
	"testing"

	"github.com/stretchr/testify/require"
	v1 "k8s.io/api/discovery/v1"
	"k8s.io/api/discovery/v1beta1"
)

func Test_EndpointSliceAdaptor_v1(t *testing.T) {
	endpointSlice := makeEndpointSliceV1()
	adaptor := newEndpointSliceAdaptorFromV1(endpointSlice)

	require.Equal(t, endpointSlice.ObjectMeta.Name, adaptor.name())
	require.Equal(t, endpointSlice.ObjectMeta.Namespace, adaptor.namespace())
	require.Equal(t, endpointSlice.AddressType, v1.AddressType(adaptor.addressType()))
	require.Equal(t, endpointSlice.Labels, adaptor.labels())
	require.Equal(t, endpointSlice.Labels[v1.LabelServiceName], "testendpoints")

	for i, endpointAdaptor := range adaptor.endpoints() {
		require.Equal(t, endpointSlice.Endpoints[i].Addresses, endpointAdaptor.addresses())
		require.Equal(t, endpointSlice.Endpoints[i].Hostname, endpointAdaptor.hostname())
		require.Equal(t, endpointSlice.Endpoints[i].Conditions.Ready, endpointAdaptor.conditions().ready())
		require.Equal(t, endpointSlice.Endpoints[i].Conditions.Serving, endpointAdaptor.conditions().serving())
		require.Equal(t, endpointSlice.Endpoints[i].Conditions.Terminating, endpointAdaptor.conditions().terminating())
		require.Equal(t, endpointSlice.Endpoints[i].TargetRef, endpointAdaptor.targetRef())
		require.Equal(t, endpointSlice.Endpoints[i].DeprecatedTopology, endpointAdaptor.topology())
	}

	for i, portAdaptor := range adaptor.ports() {
		require.Equal(t, endpointSlice.Ports[i].Name, portAdaptor.name())
		require.Equal(t, endpointSlice.Ports[i].Port, portAdaptor.port())
		require.EqualValues(t, endpointSlice.Ports[i].Protocol, portAdaptor.protocol())
		require.Equal(t, endpointSlice.Ports[i].AppProtocol, portAdaptor.appProtocol())
	}
}

func Test_EndpointSliceAdaptor_v1beta1(t *testing.T) {
	endpointSlice := makeEndpointSliceV1beta1()
	adaptor := newEndpointSliceAdaptorFromV1beta1(endpointSlice)

	require.Equal(t, endpointSlice.ObjectMeta.Name, adaptor.name())
	require.Equal(t, endpointSlice.ObjectMeta.Namespace, adaptor.namespace())
	require.Equal(t, endpointSlice.AddressType, v1beta1.AddressType(adaptor.addressType()))
	require.Equal(t, endpointSlice.Labels, adaptor.labels())
	require.Equal(t, endpointSlice.Labels[v1beta1.LabelServiceName], "testendpoints")

	for i, endpointAdaptor := range adaptor.endpoints() {
		require.Equal(t, endpointSlice.Endpoints[i].Addresses, endpointAdaptor.addresses())
		require.Equal(t, endpointSlice.Endpoints[i].Hostname, endpointAdaptor.hostname())
		require.Equal(t, endpointSlice.Endpoints[i].Conditions.Ready, endpointAdaptor.conditions().ready())
		require.Equal(t, endpointSlice.Endpoints[i].Conditions.Serving, endpointAdaptor.conditions().serving())
		require.Equal(t, endpointSlice.Endpoints[i].Conditions.Terminating, endpointAdaptor.conditions().terminating())
		require.Equal(t, endpointSlice.Endpoints[i].TargetRef, endpointAdaptor.targetRef())
		require.Equal(t, endpointSlice.Endpoints[i].Topology, endpointAdaptor.topology())
	}

	for i, portAdaptor := range adaptor.ports() {
		require.Equal(t, endpointSlice.Ports[i].Name, portAdaptor.name())
		require.Equal(t, endpointSlice.Ports[i].Port, portAdaptor.port())
		require.EqualValues(t, endpointSlice.Ports[i].Protocol, portAdaptor.protocol())
		require.Equal(t, endpointSlice.Ports[i].AppProtocol, portAdaptor.appProtocol())
	}
}
