package parent

import (
	"context"
	"errors"
	"testing"

	"github.com/go-logr/logr/testr"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	observabilitypingcapcomv1 "github.com/tidbcloud/observability/api/regionalserver/resourceworker/v1"
	"github.com/tidbcloud/observability/pkg/common/structs"
	"github.com/tidbcloud/observability/pkg/resourceworker/controllers/serverless/task"
	apierrors "k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestNewEndpointService(t *testing.T) {
	ctx := &task.Context{
		Cluster: &observabilitypingcapcomv1.Cluster{
			ObjectMeta: metav1.ObjectMeta{
				Name:      "test-cluster",
				Namespace: "test-namespace",
			},
		},
		Logger: testr.New(t),
	}

	es := NewEndpointService(ctx)
	assert.NotNil(t, es)
	assert.Equal(t, ctx, es.Context)
}

func TestEndpointService_Name(t *testing.T) {
	ctx := &task.Context{
		Cluster: &observabilitypingcapcomv1.Cluster{
			ObjectMeta: metav1.ObjectMeta{
				Name:      "test-cluster",
				Namespace: "test-namespace",
			},
		},
		Logger: testr.New(t),
	}

	es := NewEndpointService(ctx)
	assert.Equal(t, "EndpointService", es.Name())
}

func TestEndpointService_Create_NilRemote(t *testing.T) {
	ctx := &task.Context{
		Cluster: &observabilitypingcapcomv1.Cluster{
			ObjectMeta: metav1.ObjectMeta{
				Name:      "test-cluster",
				Namespace: "test-namespace",
			},
		},
		Logger: testr.New(t),
		Remote: nil,
	}

	es := NewEndpointService(ctx)
	result := es.Create(context.Background())

	assert.False(t, result.IsDoneState())
	assert.NotNil(t, result.Error())
	assert.Contains(t, result.Error().Error(), "remote client is not initialized")
}

func TestEndpointService_Create_NilCluster(t *testing.T) {
	scheme := runtime.NewScheme()
	_ = observabilitypingcapcomv1.AddToScheme(scheme)

	ctx := &task.Context{
		Cluster: nil,
		Logger:  testr.New(t),
		Remote:  fake.NewClientBuilder().WithScheme(scheme).Build(),
	}

	es := NewEndpointService(ctx)
	result := es.Create(context.Background())

	assert.False(t, result.IsDoneState())
	assert.NotNil(t, result.Error())
	assert.Contains(t, result.Error().Error(), "cluster context is nil")
}

func TestEndpointService_Create_Success_NonShared(t *testing.T) {
	scheme := runtime.NewScheme()
	_ = observabilitypingcapcomv1.AddToScheme(scheme)

	cluster := &observabilitypingcapcomv1.Cluster{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-cluster",
			Namespace: "test-namespace",
		},
		Spec: observabilitypingcapcomv1.ClusterSpec{
			Endpoint: observabilitypingcapcomv1.Endpoint{
				Name:    "test-endpoint",
				UserARN: "arn:aws:iam::123456789012:user/testuser",
				Shared:  false,
			},
		},
	}

	// Create an existing EndpointService with ACTIVE status
	existingEndpoint := &observabilitypingcapcomv1.EndpointService{
		ObjectMeta: metav1.ObjectMeta{
			Name:      svcPrefix + cluster.Spec.Endpoint.Name,
			Namespace: cluster.Namespace,
		},
		Spec: observabilitypingcapcomv1.EndpointServiceSpec{
			ServiceName: svcPrefix + cluster.Spec.Endpoint.Name,
			Shared:      false,
		},
		Status: observabilitypingcapcomv1.EndpointServiceStatus{
			Phase:       "ACTIVE",
			ServiceName: "test-service-name",
		},
	}

	ctx := &task.Context{
		Cluster: cluster,
		Logger:  testr.New(t),
		Remote:  fake.NewClientBuilder().WithScheme(scheme).WithObjects(existingEndpoint).Build(),
		Config: structs.ServiceInfo{
			AZConfig: map[string]structs.AvailabilityZoneConfig{},
		},
	}

	es := NewEndpointService(ctx)
	result := es.Create(context.Background())

	assert.True(t, result.IsDoneState())
	assert.Nil(t, result.Error())

	// Verify that the EndpointService was updated
	created := &observabilitypingcapcomv1.EndpointService{}
	key := client.ObjectKey{
		Namespace: cluster.Namespace,
		Name:      svcPrefix + cluster.Spec.Endpoint.Name,
	}
	err := ctx.Remote.Get(context.Background(), key, created)
	require.NoError(t, err)
	assert.Equal(t, svcPrefix+cluster.Spec.Endpoint.Name, created.Name)
	assert.Equal(t, svcPrefix+cluster.Spec.Endpoint.Name, created.Spec.ServiceName)
	assert.False(t, created.Spec.Shared)
	assert.Contains(t, created.Spec.UserARNs, cluster.Spec.Endpoint.UserARN)
}

func TestEndpointService_Create_Success_Shared(t *testing.T) {
	scheme := runtime.NewScheme()
	_ = observabilitypingcapcomv1.AddToScheme(scheme)

	cluster := &observabilitypingcapcomv1.Cluster{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-cluster",
			Namespace: "test-namespace",
		},
		Spec: observabilitypingcapcomv1.ClusterSpec{
			Endpoint: observabilitypingcapcomv1.Endpoint{
				Name:    "test-endpoint",
				UserARN: "arn:aws:iam::123456789012:user/testuser",
				Shared:  true,
			},
			VictoriaAccount: observabilitypingcapcomv1.VictoriaAccount{
				AvailabilityZone: "us-west-2a",
			},
		},
	}

	// Create an existing EndpointService with ACTIVE status
	existingEndpoint := &observabilitypingcapcomv1.EndpointService{
		ObjectMeta: metav1.ObjectMeta{
			Name:      svcPrefix + cluster.Spec.Endpoint.Name,
			Namespace: cluster.Namespace,
		},
		Spec: observabilitypingcapcomv1.EndpointServiceSpec{
			ServiceName:       svcPrefix + cluster.Spec.Endpoint.Name,
			Shared:            true,
			EndpointServiceID: "vpce-svc-123456",
		},
		Status: observabilitypingcapcomv1.EndpointServiceStatus{
			Phase:       "ACTIVE",
			ServiceName: "test-service-name",
		},
	}

	ctx := &task.Context{
		Cluster: cluster,
		Logger:  testr.New(t),
		Remote:  fake.NewClientBuilder().WithScheme(scheme).WithObjects(existingEndpoint).Build(),
		Config: structs.ServiceInfo{
			AZConfig: map[string]structs.AvailabilityZoneConfig{
				"us-west-2a": {
					EndpointServiceID: "vpce-svc-123456",
					DnsName:           "test-dns-name",
				},
			},
		},
	}

	es := NewEndpointService(ctx)
	result := es.Create(context.Background())

	assert.True(t, result.IsDoneState())
	assert.Nil(t, result.Error())

	// Verify that the EndpointService was updated
	created := &observabilitypingcapcomv1.EndpointService{}
	key := client.ObjectKey{
		Namespace: cluster.Namespace,
		Name:      svcPrefix + cluster.Spec.Endpoint.Name,
	}
	err := ctx.Remote.Get(context.Background(), key, created)
	require.NoError(t, err)
	assert.Equal(t, svcPrefix+cluster.Spec.Endpoint.Name, created.Name)
	assert.True(t, created.Spec.Shared)
	assert.Equal(t, "vpce-svc-123456", created.Spec.EndpointServiceID)
	assert.Contains(t, created.Spec.UserARNs, cluster.Spec.Endpoint.UserARN)
}

func TestEndpointService_Create_Success_Shared_NoAZConfig(t *testing.T) {
	scheme := runtime.NewScheme()
	_ = observabilitypingcapcomv1.AddToScheme(scheme)

	cluster := &observabilitypingcapcomv1.Cluster{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-cluster",
			Namespace: "test-namespace",
		},
		Spec: observabilitypingcapcomv1.ClusterSpec{
			Endpoint: observabilitypingcapcomv1.Endpoint{
				Name:    "test-endpoint",
				UserARN: "arn:aws:iam::123456789012:user/testuser",
				Shared:  true,
			},
			VictoriaAccount: observabilitypingcapcomv1.VictoriaAccount{
				AvailabilityZone: "",
			},
		},
	}

	ctx := &task.Context{
		Cluster: cluster,
		Logger:  testr.New(t),
		Remote:  fake.NewClientBuilder().WithScheme(scheme).Build(),
		Config: structs.ServiceInfo{
			AZConfig: map[string]structs.AvailabilityZoneConfig{},
		},
	}

	es := NewEndpointService(ctx)
	result := es.Create(context.Background())

	assert.False(t, result.IsDoneState())
	assert.NotNil(t, result.Error())
	assert.Contains(t, result.Error().Error(), "not found in config")
}

func TestEndpointService_Create_Update_Existing(t *testing.T) {
	scheme := runtime.NewScheme()
	_ = observabilitypingcapcomv1.AddToScheme(scheme)

	cluster := &observabilitypingcapcomv1.Cluster{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-cluster",
			Namespace: "test-namespace",
		},
		Spec: observabilitypingcapcomv1.ClusterSpec{
			Endpoint: observabilitypingcapcomv1.Endpoint{
				Name:    "test-endpoint",
				UserARN: "arn:aws:iam::123456789012:user/newuser",
				Shared:  false,
			},
		},
	}

	// Create an existing EndpointService with ACTIVE status
	existing := &observabilitypingcapcomv1.EndpointService{
		ObjectMeta: metav1.ObjectMeta{
			Name:      svcPrefix + cluster.Spec.Endpoint.Name,
			Namespace: cluster.Namespace,
		},
		Spec: observabilitypingcapcomv1.EndpointServiceSpec{
			ServiceName: svcPrefix + cluster.Spec.Endpoint.Name,
			Shared:      false,
			UserARNs:    []string{"arn:aws:iam::123456789012:user/olduser"},
		},
		Status: observabilitypingcapcomv1.EndpointServiceStatus{
			Phase:       "ACTIVE",
			ServiceName: "test-service-name",
		},
	}

	ctx := &task.Context{
		Cluster: cluster,
		Logger:  testr.New(t),
		Remote:  fake.NewClientBuilder().WithScheme(scheme).WithObjects(existing).Build(),
		Config: structs.ServiceInfo{
			AZConfig: map[string]structs.AvailabilityZoneConfig{},
		},
	}

	es := NewEndpointService(ctx)
	result := es.Create(context.Background())

	assert.True(t, result.IsDoneState())
	assert.Nil(t, result.Error())

	// Verify that the EndpointService was updated
	updated := &observabilitypingcapcomv1.EndpointService{}
	key := client.ObjectKey{
		Namespace: cluster.Namespace,
		Name:      svcPrefix + cluster.Spec.Endpoint.Name,
	}
	err := ctx.Remote.Get(context.Background(), key, updated)
	require.NoError(t, err)
	assert.Contains(t, updated.Spec.UserARNs, cluster.Spec.Endpoint.UserARN)
}

func TestEndpointService_Delete_NilRemote(t *testing.T) {
	ctx := &task.Context{
		Cluster: &observabilitypingcapcomv1.Cluster{
			ObjectMeta: metav1.ObjectMeta{
				Name:      "test-cluster",
				Namespace: "test-namespace",
			},
		},
		Logger: testr.New(t),
		Remote: nil,
	}

	es := NewEndpointService(ctx)
	result := es.Delete(context.Background())

	assert.False(t, result.IsDoneState())
	assert.NotNil(t, result.Error())
	assert.Contains(t, result.Error().Error(), "remote client is not initialized")
}

func TestEndpointService_Delete_NilCluster(t *testing.T) {
	scheme := runtime.NewScheme()
	_ = observabilitypingcapcomv1.AddToScheme(scheme)

	ctx := &task.Context{
		Cluster: nil,
		Logger:  testr.New(t),
		Remote:  fake.NewClientBuilder().WithScheme(scheme).Build(),
	}

	es := NewEndpointService(ctx)
	result := es.Delete(context.Background())

	assert.False(t, result.IsDoneState())
	assert.NotNil(t, result.Error())
	assert.Contains(t, result.Error().Error(), "cluster context is nil")
}

func TestEndpointService_Delete_Success(t *testing.T) {
	scheme := runtime.NewScheme()
	_ = observabilitypingcapcomv1.AddToScheme(scheme)

	cluster := &observabilitypingcapcomv1.Cluster{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-cluster",
			Namespace: "test-namespace",
		},
		Spec: observabilitypingcapcomv1.ClusterSpec{
			Endpoint: observabilitypingcapcomv1.Endpoint{
				Name: "test-endpoint",
			},
		},
	}

	// Create an EndpointService to delete
	endpointService := &observabilitypingcapcomv1.EndpointService{
		ObjectMeta: metav1.ObjectMeta{
			Name:      svcPrefix + cluster.Spec.Endpoint.Name,
			Namespace: cluster.Namespace,
		},
		Spec: observabilitypingcapcomv1.EndpointServiceSpec{
			ServiceName: svcPrefix + cluster.Spec.Endpoint.Name,
		},
	}

	ctx := &task.Context{
		Cluster: cluster,
		Logger:  testr.New(t),
		Remote:  fake.NewClientBuilder().WithScheme(scheme).WithObjects(endpointService).Build(),
	}

	es := NewEndpointService(ctx)
	result := es.Delete(context.Background())

	assert.True(t, result.IsDoneState())
	assert.Nil(t, result.Error())

	// Verify that the EndpointService was deleted
	deleted := &observabilitypingcapcomv1.EndpointService{}
	key := client.ObjectKey{
		Namespace: cluster.Namespace,
		Name:      svcPrefix + cluster.Spec.Endpoint.Name,
	}
	err := ctx.Remote.Get(context.Background(), key, deleted)
	assert.True(t, apierrors.IsNotFound(err))
}

func TestEndpointService_Delete_NotFound(t *testing.T) {
	scheme := runtime.NewScheme()
	_ = observabilitypingcapcomv1.AddToScheme(scheme)

	cluster := &observabilitypingcapcomv1.Cluster{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-cluster",
			Namespace: "test-namespace",
		},
		Spec: observabilitypingcapcomv1.ClusterSpec{
			Endpoint: observabilitypingcapcomv1.Endpoint{
				Name: "test-endpoint",
			},
		},
	}

	ctx := &task.Context{
		Cluster: cluster,
		Logger:  testr.New(t),
		Remote:  fake.NewClientBuilder().WithScheme(scheme).Build(),
	}

	es := NewEndpointService(ctx)
	result := es.Delete(context.Background())

	assert.True(t, result.IsDoneState())
	assert.Nil(t, result.Error())
}

func TestEndpointService_MakeEndpointService_NonShared(t *testing.T) {
	cluster := &observabilitypingcapcomv1.Cluster{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-cluster",
			Namespace: "test-namespace",
		},
		Spec: observabilitypingcapcomv1.ClusterSpec{
			Endpoint: observabilitypingcapcomv1.Endpoint{
				Name:    "test-endpoint",
				UserARN: "arn:aws:iam::123456789012:user/testuser",
				Shared:  false,
			},
		},
	}

	ctx := &task.Context{
		Cluster: cluster,
		Logger:  testr.New(t),
		Config: structs.ServiceInfo{
			AZConfig: map[string]structs.AvailabilityZoneConfig{},
		},
	}

	es := NewEndpointService(ctx)
	endpoint, err := es.makeEndpointService()

	require.NoError(t, err)
	assert.Equal(t, svcPrefix+cluster.Spec.Endpoint.Name, endpoint.Name)
	assert.Equal(t, cluster.Namespace, endpoint.Namespace)
	assert.Equal(t, svcPrefix+cluster.Spec.Endpoint.Name, endpoint.Spec.ServiceName)
	assert.False(t, endpoint.Spec.Shared)
	assert.Empty(t, endpoint.Spec.EndpointServiceID)
	assert.Contains(t, endpoint.Spec.UserARNs, cluster.Spec.Endpoint.UserARN)
}

func TestEndpointService_MakeEndpointService_Shared_WithAZConfig(t *testing.T) {
	cluster := &observabilitypingcapcomv1.Cluster{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-cluster",
			Namespace: "test-namespace",
		},
		Spec: observabilitypingcapcomv1.ClusterSpec{
			Endpoint: observabilitypingcapcomv1.Endpoint{
				Name:    "test-endpoint",
				UserARN: "arn:aws:iam::123456789012:user/testuser",
				Shared:  true,
			},
			VictoriaAccount: observabilitypingcapcomv1.VictoriaAccount{
				AvailabilityZone: "us-west-2a",
			},
		},
	}

	ctx := &task.Context{
		Cluster: cluster,
		Logger:  testr.New(t),
		Config: structs.ServiceInfo{
			AZConfig: map[string]structs.AvailabilityZoneConfig{
				"us-west-2a": {
					EndpointServiceID: "vpce-svc-123456",
				},
			},
		},
	}

	es := NewEndpointService(ctx)
	endpoint, err := es.makeEndpointService()

	require.NoError(t, err)

	assert.Equal(t, svcPrefix+cluster.Spec.Endpoint.Name, endpoint.Name)
	assert.Equal(t, cluster.Namespace, endpoint.Namespace)
	assert.Equal(t, svcPrefix+cluster.Spec.Endpoint.Name, endpoint.Spec.ServiceName)
	assert.True(t, endpoint.Spec.Shared)
	assert.Equal(t, "vpce-svc-123456", endpoint.Spec.EndpointServiceID)
	assert.Contains(t, endpoint.Spec.UserARNs, cluster.Spec.Endpoint.UserARN)
}

func TestEndpointService_MakeEndpointService_Shared_NoAZConfig(t *testing.T) {
	cluster := &observabilitypingcapcomv1.Cluster{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-cluster",
			Namespace: "test-namespace",
		},
		Spec: observabilitypingcapcomv1.ClusterSpec{
			Endpoint: observabilitypingcapcomv1.Endpoint{
				Name:    "test-endpoint",
				UserARN: "arn:aws:iam::123456789012:user/testuser",
				Shared:  true,
			},
			VictoriaAccount: observabilitypingcapcomv1.VictoriaAccount{
				AvailabilityZone: "us-west-2b",
			},
		},
	}

	ctx := &task.Context{
		Cluster: cluster,
		Logger:  testr.New(t),
		Config: structs.ServiceInfo{
			AZConfig: map[string]structs.AvailabilityZoneConfig{},
		},
	}

	es := NewEndpointService(ctx)
	endpoint, err := es.makeEndpointService()

	assert.Error(t, err)
	assert.Nil(t, endpoint)
	assert.Contains(t, err.Error(), "az us-west-2b not found in config")
}

func TestEndpointService_EnsureEndpointServiceDeleted_Success(t *testing.T) {
	scheme := runtime.NewScheme()
	_ = observabilitypingcapcomv1.AddToScheme(scheme)

	cluster := &observabilitypingcapcomv1.Cluster{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-cluster",
			Namespace: "test-namespace",
		},
		Spec: observabilitypingcapcomv1.ClusterSpec{
			Endpoint: observabilitypingcapcomv1.Endpoint{
				Name: "test-endpoint",
			},
		},
	}

	// Create an EndpointService to delete
	endpointService := &observabilitypingcapcomv1.EndpointService{
		ObjectMeta: metav1.ObjectMeta{
			Name:      svcPrefix + cluster.Spec.Endpoint.Name,
			Namespace: cluster.Namespace,
		},
		Spec: observabilitypingcapcomv1.EndpointServiceSpec{
			ServiceName: svcPrefix + cluster.Spec.Endpoint.Name,
		},
	}

	ctx := &task.Context{
		Cluster: cluster,
		Logger:  testr.New(t),
		Remote:  fake.NewClientBuilder().WithScheme(scheme).WithObjects(endpointService).Build(),
	}

	es := NewEndpointService(ctx)
	err := es.ensureEndpointServiceDeleted(context.Background())

	assert.NoError(t, err)

	// Verify that the EndpointService was deleted
	deleted := &observabilitypingcapcomv1.EndpointService{}
	key := client.ObjectKey{
		Namespace: cluster.Namespace,
		Name:      svcPrefix + cluster.Spec.Endpoint.Name,
	}
	getErr := ctx.Remote.Get(context.Background(), key, deleted)
	assert.True(t, apierrors.IsNotFound(getErr))
}

func TestEndpointService_EnsureEndpointServiceDeleted_NotFound(t *testing.T) {
	scheme := runtime.NewScheme()
	_ = observabilitypingcapcomv1.AddToScheme(scheme)

	cluster := &observabilitypingcapcomv1.Cluster{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-cluster",
			Namespace: "test-namespace",
		},
		Spec: observabilitypingcapcomv1.ClusterSpec{
			Endpoint: observabilitypingcapcomv1.Endpoint{
				Name: "test-endpoint",
			},
		},
	}

	ctx := &task.Context{
		Cluster: cluster,
		Logger:  testr.New(t),
		Remote:  fake.NewClientBuilder().WithScheme(scheme).Build(),
	}

	es := NewEndpointService(ctx)
	err := es.ensureEndpointServiceDeleted(context.Background())

	assert.NoError(t, err)
}

// Mock client that returns errors
type mockErrorClient struct {
	client.Client
	getError    error
	deleteError error
	scheme      *runtime.Scheme
}

func (m *mockErrorClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
	if m.getError != nil {
		return m.getError
	}
	// If no get error, return success by setting a minimal object
	if endpoint, ok := obj.(*observabilitypingcapcomv1.EndpointService); ok {
		endpoint.SetName(key.Name)
		endpoint.SetNamespace(key.Namespace)
		return nil
	}
	return apierrors.NewNotFound(schema.GroupResource{}, key.Name)
}

func (m *mockErrorClient) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
	if m.deleteError != nil {
		return m.deleteError
	}
	return nil
}

func (m *mockErrorClient) Scheme() *runtime.Scheme {
	if m.scheme == nil {
		m.scheme = runtime.NewScheme()
		_ = observabilitypingcapcomv1.AddToScheme(m.scheme)
	}
	return m.scheme
}

func TestEndpointService_EnsureEndpointServiceDeleted_DeleteError(t *testing.T) {
	cluster := &observabilitypingcapcomv1.Cluster{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-cluster",
			Namespace: "test-namespace",
		},
		Spec: observabilitypingcapcomv1.ClusterSpec{
			Endpoint: observabilitypingcapcomv1.Endpoint{
				Name: "test-endpoint",
			},
		},
	}

	mockClient := &mockErrorClient{
		getError:    nil,
		deleteError: errors.New("delete error"),
	}

	ctx := &task.Context{
		Cluster: cluster,
		Logger:  testr.New(t),
		Remote:  mockClient,
	}

	es := NewEndpointService(ctx)
	err := es.ensureEndpointServiceDeleted(context.Background())

	assert.Error(t, err)
	assert.Contains(t, err.Error(), "delete error")
}

func TestEndpointService_EnsureEndpointServiceDeleted_GetError(t *testing.T) {
	cluster := &observabilitypingcapcomv1.Cluster{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-cluster",
			Namespace: "test-namespace",
		},
		Spec: observabilitypingcapcomv1.ClusterSpec{
			Endpoint: observabilitypingcapcomv1.Endpoint{
				Name: "test-endpoint",
			},
		},
	}

	mockClient := &mockErrorClient{
		getError: errors.New("get error"),
	}

	ctx := &task.Context{
		Cluster: cluster,
		Logger:  testr.New(t),
		Remote:  mockClient,
	}

	es := NewEndpointService(ctx)
	err := es.ensureEndpointServiceDeleted(context.Background())

	assert.Error(t, err)
	assert.Contains(t, err.Error(), "get error")
}
