package main

import (
	"testing"

	image2 "wdd.io/agent-common/image"
	"wdd.io/agent-common/real_project"
	"wdd.io/agent-common/utils"
)

/* 拉取
 */

func TestFetchDependencyRepos_Middle(t *testing.T) {

	errorPullImageList, errorGzipImageList, realCmiiImageName, allGzipFileNameList := C_DownloadCompressUploadDependency(true, true, false)

	utils.BeautifulPrintListWithTitle(errorPullImageList, "errorPullImageList")
	utils.BeautifulPrintListWithTitle(errorGzipImageList, "errorGzipImageList")
	utils.BeautifulPrintListWithTitle(realCmiiImageName, "realCmiiImageName")
	utils.BeautifulPrintListWithTitle(allGzipFileNameList, "allGzipFileNameList")

}

func TestFetchDependencyRepos_RKE(t *testing.T) {

	errorPullImageList, errorGzipImageList, realCmiiImageName, allGzipFileNameList := C_DownloadCompressUploadDependency(true, true, true)

	utils.BeautifulPrintListWithTitle(errorPullImageList, "errorPullImageList")
	utils.BeautifulPrintListWithTitle(errorGzipImageList, "errorGzipImageList")
	utils.BeautifulPrintListWithTitle(realCmiiImageName, "realCmiiImageName")
	utils.BeautifulPrintListWithTitle(allGzipFileNameList, "allGzipFileNameList")

}

func TestLoadSplitGzipImageToTargetHarbor(t *testing.T) {
	errorLoadImageNameList, errorPushImageNameList := LoadSplitCmiiGzipImageToTargetHarbor("xmyd", DirectPushDeployHarborHost)

	utils.BeautifulPrintListWithTitle(errorLoadImageNameList, "errorLoadImageNameList")
	utils.BeautifulPrintListWithTitle(errorPushImageNameList, "errorPushImageNameList")
}

func TestLoadSplitDepGzipImageToTargetHarbor(t *testing.T) {
	errorLoadImageNameList, errorPushImageNameList := LoadSplitDepGzipImageToTargetHarbor(DirectPushDeployHarborHost)

	utils.BeautifulPrintListWithTitle(errorLoadImageNameList, "errorLoadImageNameList")
	utils.BeautifulPrintListWithTitle(errorPushImageNameList, "errorPushImageNameList")
}

func TestPullFromEntityAndSyncConditionally(t *testing.T) {

	// 全部镜像列表
	fullImageList := []string{}

	fullImageList = append(fullImageList, real_project.CmiiUas22ImageList...)
	fullImageList = append(fullImageList, real_project.CmiiSRSImageList...)
	//fullImageList = append(fullImageList, real_project.CmiiDorisImageList...)

	// 下载镜像的核心函数
	sync := ImageSyncEntity{
		DownloadCondition: &DownloadEntity{
			ShouldDownloadImage:  true,
			ProjectName:          "xauas22",
			ProjectVersion:       "",
			CmiiNameTagList:      []string{},
			FullNameImageList:    fullImageList,
			DownloadAuthUserName: "",
			DownloadAuthPassword: "",
		},

		CompressCondition: &CompressEntity{
			ShouldCompressImageToGzip: true,
			ShouldGzipSplit:           true,
			GzipLocalFolder:           "",
		},
		UploadCondition: &UploadEntity{
			ShouldUploadToDemoMinio: true,
		},
		DirectHarborHost:         "harbor.wdd.io:8033",
		ShouldDirectPushToHarbor: false,
	}

	// 调用函数并获取结果。这里需要根据你的实际需求来验证返回的结果是否符合预期。
	result := sync.PullFromEntityAndSyncConditionally()

	utils.BeautifulPrint(result)

	// 添加断言以检查函数的输出，例如：
	// ...其他验证逻辑...
}

func TestPullFromEntityAndSyncConditionally_ChongQingErJiPingTai(t *testing.T) {
	// 创建一个模拟的sync对象，用于测试函数的行为。这里需要根据你的实际需求来设置mock数据和预期结果。
	sync := ImageSyncEntity{
		DownloadCondition: &DownloadEntity{
			ShouldDownloadImage: true,
			ProjectName:         "cqejpt",
			ProjectVersion:      "",
			CmiiNameTagList:     []string{
				//"cmii-uav-mqtthandler:5.4.0-bjdyt-052102",
			},
			FullNameImageList: []string{
				"docker.107421.xyz/rancher/backup-restore-operator:v1.0.3",
				"docker.107421.xyz/rancher/calico-cni:v3.17.2",
				"docker.107421.xyz/rancher/calico-ctl:v3.17.2",
				"docker.107421.xyz/rancher/calico-kube-controllers:v3.17.2",
				"docker.107421.xyz/rancher/calico-node:v3.17.2",
				"docker.107421.xyz/rancher/calico-pod2daemon-flexvol:v3.17.2",
				"docker.107421.xyz/rancher/cis-operator:v1.0.3",
				"docker.107421.xyz/rancher/cluster-proportional-autoscaler:1.7.1",
				"docker.107421.xyz/rancher/coredns-coredns:1.8.0",
				"docker.107421.xyz/rancher/coreos-etcd:v3.4.14-rancher1",
				"docker.107421.xyz/rancher/coreos-kube-state-metrics:v1.9.7",
				"docker.107421.xyz/rancher/coreos-prometheus-config-reloader:v0.39.0",
				"docker.107421.xyz/rancher/coreos-prometheus-operator:v0.39.0",
				"docker.107421.xyz/rancher/externalip-webhook:v0.1.6",
				"docker.107421.xyz/rancher/flannel-cni:v0.3.0-rancher6",
				"docker.107421.xyz/rancher/coreos-flannel:v0.13.0-rancher1",
				"docker.107421.xyz/rancher/fleet-agent:v0.3.4",
				"docker.107421.xyz/rancher/fleet:v0.3.4",
				"docker.107421.xyz/rancher/fluentd:v0.1.24",
				"docker.107421.xyz/rancher/grafana-grafana:7.1.5",
				"docker.107421.xyz/rancher/hyperkube:v1.20.4-rancher1",
				"docker.107421.xyz/rancher/jimmidyson-configmap-reload:v0.3.0",
				"docker.107421.xyz/rancher/k8s-dns-dnsmasq-nanny:1.15.2",
				"docker.107421.xyz/rancher/k8s-dns-kube-dns:1.15.2",
				"docker.107421.xyz/rancher/k8s-dns-node-cache:1.15.13",
				"docker.107421.xyz/rancher/k8s-dns-sidecar:1.15.2",
				"docker.107421.xyz/rancher/klipper-lb:v0.1.2",
				"docker.107421.xyz/rancher/kube-api-auth:v0.1.4",
				"docker.107421.xyz/rancher/kubectl:v1.20.4",
				"docker.107421.xyz/rancher/kubernetes-external-dns:v0.7.3",
				"docker.107421.xyz/rancher/cluster-proportional-autoscaler:1.8.1",
				"docker.107421.xyz/rancher/library-busybox:1.31.1",
				"docker.107421.xyz/rancher/library-busybox:1.32.1",
				"docker.107421.xyz/rancher/library-nginx:1.19.2-alpine",
				"docker.107421.xyz/rancher/library-traefik:1.7.19",
				"docker.107421.xyz/rancher/local-path-provisioner:v0.0.11",
				"docker.107421.xyz/rancher/local-path-provisioner:v0.0.14",
				"docker.107421.xyz/rancher/local-path-provisioner:v0.0.19",
				"docker.107421.xyz/rancher/log-aggregator:v0.1.7",
				"docker.107421.xyz/rancher/istio-kubectl:1.5.10",
				"docker.107421.xyz/rancher/metrics-server:v0.4.1",
				"docker.107421.xyz/rancher/configmap-reload:v0.3.0-rancher4",
				"docker.107421.xyz/rancher/nginx-ingress-controller-defaultbackend:1.5-rancher1",
				"docker.107421.xyz/rancher/nginx-ingress-controller:nginx-0.43.0-rancher1",
				"docker.107421.xyz/rancher/opa-gatekeeper:v3.1.0-beta.7",
				"docker.107421.xyz/rancher/openzipkin-zipkin:2.14.2",
				"docker.107421.xyz/rancher/pause:3.2",
				"docker.107421.xyz/rancher/plugins-docker:18.09",
				"docker.107421.xyz/rancher/prom-alertmanager:v0.21.0",
				"docker.107421.xyz/rancher/prom-node-exporter:v1.0.1",
				"docker.107421.xyz/rancher/prom-prometheus:v2.18.2",
				"docker.107421.xyz/rancher/prometheus-auth:v0.2.1",
				"docker.107421.xyz/rancher/rancher-agent:v2.5.7",
				"docker.107421.xyz/rancher/rancher-webhook:v0.1.0-beta9",
				"docker.107421.xyz/rancher/rancher:v2.5.7",
				"docker.107421.xyz/rancher/rke-tools:v0.1.72",
				"docker.107421.xyz/rancher/security-scan:v0.1.14",
				"docker.107421.xyz/rancher/security-scan:v0.2.2",
				"docker.107421.xyz/rancher/shell:v0.1.6",
				"docker.107421.xyz/rancher/sonobuoy-sonobuoy:v0.16.3",
				"docker.107421.xyz/rancher/system-upgrade-controller:v0.6.2",
			},
			DownloadAuthUserName: "",
			DownloadAuthPassword: "",
		},

		CompressCondition: &CompressEntity{
			ShouldCompressImageToGzip: false,
			ShouldGzipSplit:           true,
			GzipLocalFolder:           "",
		},
		UploadCondition: &UploadEntity{
			ShouldUploadToDemoMinio: false,
		},
		DirectHarborHost:         "chongqingshcis-a189ec98.ecis.chongqing-1.cmecloud.cn",
		ShouldDirectPushToHarbor: true,
	}

	// 调用函数并获取结果。这里需要根据你的实际需求来验证返回的结果是否符合预期。
	result := sync.PullFromEntityAndSyncConditionally()

	utils.BeautifulPrint(result)

	// 添加断言以检查函数的输出，例如：
	// ...其他验证逻辑...
}

func TestDownloadLoadTagUpload(t *testing.T) {

	localGzipFileList := []string{
		"/root/octopus_image/middle",
		"/root/octopus_image/rke",
		"/root/octopus_image/xjyd",
	}

	for _, s := range localGzipFileList {
		log.InfoF("start to sync => %s", s)

		targetImageFullNameList := A_DownloadLoadTagUpload(false, "", "", s, "172.28.0.251:8033")

		utils.BeautifulPrintListWithTitle(targetImageFullNameList, "targetImageFullNameList")

	}

}

func TestConcatAndUniformCmiiImage(t *testing.T) {
	// 创建一个模拟的fullImageList和cmiiImageList，用于测试函数的行为。这里需要根据你的实际需求来设置mock数据和预期结果。
	fullImageList := []string{"image3", "image4"}
	cmiiImageList := []string{"image1", "image2"}

	// 调用函数并获取结果。这里需要根据你的实际需求来验证返回的结果是否符合预期。
	result := concatAndUniformCmiiImage(fullImageList, cmiiImageList)

	// 添加断言以检查函数的输出，例如：
	expectedResult := []string{"image3", "image4", image2.CmiiHarborPrefix + "image1", image2.CmiiHarborPrefix + "image2"}
	if len(result) != len(expectedResult) {
		t.Errorf("Expected %v, got %v", expectedResult, result)
	}
	// ...其他验证逻辑...
}

func TestImageSyncEntity_PullFromEntityAndSyncConditionally(t *testing.T) {
	imageSyncEntity := ImageSyncEntity{
		DownloadCondition: &DownloadEntity{
			ShouldDownloadImage:  true,
			ProjectName:          "",
			ProjectVersion:       "5.4.0",
			CmiiNameTagList:      nil,
			FullNameImageList:    nil,
			DownloadAuthUserName: "",
			DownloadAuthPassword: "",
		},
		DirectHarborHost: "36.134.71.138",
	}

	imageSyncResult := imageSyncEntity.PullFromEntityAndSyncConditionally()

	utils.BeautifulPrint(imageSyncResult)

}
