//go:build unit || all
// +build unit all

package sdk

import (
	"testing"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

	cloudv1 "github.com/tidbcloud/infra-api/api/v1"
	"github.com/tidbcloud/infra-provider/pkg/gitops-sdk/consts"
)

func TestOverrideBaseImage(t *testing.T) {
	cases := []struct {
		name    string
		image   string
		cluster *cloudv1.Cluster
		want    string
	}{
		{
			name:    "no override",
			cluster: &cloudv1.Cluster{},
			image:   "tidb",
			want:    "tidb",
		},
		{
			name: "image with registry name",
			cluster: &cloudv1.Cluster{
				ObjectMeta: metav1.ObjectMeta{
					Annotations: map[string]string{
						consts.OverrideRegistryKey: "gcr.io",
					},
				},
			},
			image: "pingcap.com/tidbcloud/tidb",
			want:  "gcr.io/tidbcloud/tidb",
		},
		{
			name: "image with repo name",
			cluster: &cloudv1.Cluster{
				ObjectMeta: metav1.ObjectMeta{
					Annotations: map[string]string{
						consts.OverrideRepoPrefixKey: "pingcap-public",
					},
				},
			},
			image: "pingcap.com/tidbcloud/tidb",
			want:  "pingcap.com/pingcap-public/tidb",
		},
		{
			name: "image with registry and repo name",
			cluster: &cloudv1.Cluster{
				ObjectMeta: metav1.ObjectMeta{
					Annotations: map[string]string{
						consts.OverrideRegistryKey:   "gcr.io",
						consts.OverrideRepoPrefixKey: "pingcap-public",
					},
				},
			},
			image: "pingcap.com/tidbcloud/tidb",
			want:  "gcr.io/pingcap-public/tidb",
		},
		{
			name: "image with no repo name",
			cluster: &cloudv1.Cluster{
				ObjectMeta: metav1.ObjectMeta{
					Annotations: map[string]string{
						consts.OverrideRegistryKey:   "gcr.io",
						consts.OverrideRepoPrefixKey: "pingcap-public",
					},
				},
			},
			image: "pingcap.com/tidb",
			want:  "gcr.io/pingcap-public/tidb",
		},
		{
			name: "image with no registry name",
			cluster: &cloudv1.Cluster{
				ObjectMeta: metav1.ObjectMeta{
					Annotations: map[string]string{
						consts.OverrideRegistryKey:   "gcr.io",
						consts.OverrideRepoPrefixKey: "pingcap-public",
					},
				},
			},
			image: "tidb",
			want:  "gcr.io/pingcap-public/tidb",
		},
		{
			name: "image with multi levels and tag",
			cluster: &cloudv1.Cluster{
				ObjectMeta: metav1.ObjectMeta{
					Annotations: map[string]string{
						consts.OverrideRegistryKey:   "gcr.io",
						consts.OverrideRepoPrefixKey: "pingcap-public",
					},
				},
			},
			image: "docker.io/a/b/c/mysqlclient:20201014",
			want:  "gcr.io/pingcap-public/mysqlclient:20201014",
		},
		{
			name: "override with multi level repo prefix",
			cluster: &cloudv1.Cluster{
				ObjectMeta: metav1.ObjectMeta{
					Annotations: map[string]string{
						consts.OverrideRegistryKey:   "12345.dkr.ecr.us-west-2.amazonaws.com",
						consts.OverrideRepoPrefixKey: "third-party/prom",
					},
				},
			},
			image: "gcr.io/pingcap-public/prometheus:v1.2.3",
			want:  "12345.dkr.ecr.us-west-2.amazonaws.com/third-party/prom/prometheus:v1.2.3",
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			image := OverrideBaseImage(tc.cluster, tc.image)
			if image != tc.want {
				t.Errorf("OverrideBaseImage(%s) got = %s, want = %s", tc.image, image, tc.want)
			}
		})
	}
}

func TestOverrideImageWithThirdPartyRepoPrefix(t *testing.T) {
	registry := "12345.dkr.ecr.us-west-2.amazonaws.com"
	repoPrefix := "third-party"

	tests := []struct {
		name  string
		image string
		want  string
	}{
		{
			name:  "image without registry name and repo name",
			image: "test:v1.2.3",
			want:  registry + "/" + repoPrefix + "/test:v1.2.3",
		},
		{
			name:  "image without registry name",
			image: "test/test:v1.2.3",
			want:  registry + "/" + repoPrefix + "/test/test:v1.2.3",
		},
		{
			name:  "image with registry name",
			image: "test.com/test/test:v1.2.3",
			want:  registry + "/" + repoPrefix + "/test/test:v1.2.3",
		},
		{
			name:  "image with registry name without repo name",
			image: "test.com/test:v1.2.3",
			want:  registry + "/" + repoPrefix + "/test:v1.2.3",
		},
		{
			name:  "image with multi levels repo",
			image: "docker.io/a/b/c/test:v1.2.3",
			want:  registry + "/" + repoPrefix + "/a/b/c/test:v1.2.3",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := OverrideImageWithThirdPartyRepoPrefix(registry, repoPrefix, tt.image); got != tt.want {
				t.Errorf("OverrideImageWithThirdPartyRepoPrefix() = %v, want %v", got, tt.want)
			}
		})
	}
}
