package tiup_upgrade

import (
	"context"
	"fmt"

	"net/url"
	"time"

	. "github.com/onsi/ginkgo"
	. "github.com/pingcap/endless/pkg/matcher"
	"github.com/pingcap/endless/pkg/workloadnode"
	"github.com/pingcap/log"

	_ "github.com/go-sql-driver/mysql"

	. "github.com/pingcap/endless/pkg/workloadnode/tiup"

	"github.com/pingcap/test-infra/sdk/resource"
	_ "github.com/pingcap/test-infra/sdk/resource/impl/k8s" // register k8s impl

	_ "github.com/pingcap/endless/pkg/client/tidb"
	"go.uber.org/zap"
)

func UTF_Test(
	cluster *TiUPCluster,
	testcase string,
) {
	utftest := ctx.Resource("utf").(resource.WorkloadNode)
	dbURL := Try(cluster.ServiceURL(resource.DBAddr)).(*url.URL)
	log.Info("dbDSN info:", zap.String("dbdsn:", dbURL.Host))
	// dbDSN := dbURL.Host
	// dbDSN := "tiup-peer:3390"
	cmd := fmt.Sprintf(`python -m cases.cli case run %s`, testcase)
	duration := 10 * time.Hour
	ctx2, cancel := context.WithTimeout(ctx, duration)
	defer cancel()
	cmd1 := workloadnode.CommandContext(ctx2, cmd)
	defer cmd1.Stop()
	err := cmd1.Run(utftest)
	if err != nil {
		log.Info("tidb test result: ", zap.String("err:", err.Error()))
		Fail("utf test fail")
	}
}
