package cmii_operator

import (
	"bufio"
	"fmt"
	"strings"
	"testing"
	"wdd.io/agent-go/assert"
	"wdd.io/agent-go/utils"
)

func TestGetRunningContainer(t *testing.T) {

	allContainer := GetRunningContainer()

	for _, container := range allContainer {
		utils.BeautifulPrint(container)
	}

}
func TestGetAllContainer(t *testing.T) {

	allContainer := GetAllContainer()

	for _, container := range allContainer {
		utils.BeautifulPrint(container)
	}
}

func TestImageGetAll(t *testing.T) {
	imageGetAll := ImageGetAll()

	for _, summary := range imageGetAll {
		utils.BeautifulPrint(summary)
	}
}

func TestImageGetByName(t *testing.T) {
	image := "harbor.cdcyy.com.cn/cmii/cmii-uav-gateway:4.1.6-beta"
	imageGetByName := ImageGetByName(image)

	utils.BeautifulPrint(imageGetByName)
}

func TestImageDelete(t *testing.T) {
	image := "harbor.cdcyy.com.cn/cmii/cmii-uav-gateway:4.1.6-beta"
	imageDelete := ImageDelete(image)

	for _, item := range imageDelete {
		utils.BeautifulPrint(item)
	}
}

func TestImagePullFromCmiiHarbor(t *testing.T) {
	image := "harbor.cdcyy.com.cn/cmii/cmii-uav-gateway:4.1.6-beta"

	pullFromCmiiHarbor := ImagePullFromCmiiHarbor(image)
	defer pullFromCmiiHarbor.Close()

	scanner := bufio.NewScanner(pullFromCmiiHarbor)

	for scanner.Scan() {
		line := scanner.Text()

		if strings.Contains(line, "\"status\":\"Pulling from") {
			fmt.Println(line)
		}
		if strings.Contains(line, "Status: Image is up to date for") {
			fmt.Println(line)
		}
	}

	//var fs uintptr
	//
	//tools.DisplayJSONMessagesStream(pullFromCmiiHarbor, os.Stdout, fs, true, func(message tools.JSONMessage) {
	//
	//})
}

func TestImagePushToOctopusKindHarbor(t *testing.T) {

	// re-tag
	image := "harbor.cdcyy.com.cn/cmii/cmii-uav-gateway:4.1.6-beta"
	newTag := "10.250.0.100:8033/cmii/cmii-uav-gateway:4.1.6-beta"
	target := ImageTagFromSourceToTarget(image, newTag)
	assert.Equal(t, target, true, "image re-tag error !")

	// push
	pushResult := ImagePushToOctopusKindHarbor(newTag)
	defer pushResult.Close()

	scanner := bufio.NewScanner(pushResult)
	for scanner.Scan() {

	}

	fmt.Println("image push success!")
}

func TestImageSaveToTarGZ(t *testing.T) {
	image := "harbor.cdcyy.com.cn/cmii/cmii-uav-gateway:4.1.6-beta"

	imageSaveToTarGZ := ImageSaveToTarGZ(image, "/home/wdd/IdeaProjects/ProjectOctopus/cmii_operator/log")

	assert.Equal(t, imageSaveToTarGZ, true, "image save to tar gz file error !")
}
