package mysqllocal

import (
	"os"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	"github.com/pingcap/test-infra/sdk/core"
	"github.com/pingcap/test-infra/sdk/e2etest/framework"
	"github.com/pingcap/test-infra/sdk/resource"
	_ "github.com/pingcap/test-infra/sdk/resource/impl/local"
)

var (
	suiteTestCtx core.TestContext
)

var _ = Describe("MySQL Local", func() {
	suiteTestCtx, err := core.BuildContext()
	if err != nil {
		panic("Init test context failed.")
	}
	BeforeEach(func() {
		os.Setenv("TI_PARAM_RES_tc1", `{"provider":"github.com/pingcap/test-infra/sdk/resource/impl/local.MySQLNode", "name": "tc1","type":"MYSQL_NODE","ready":true}`)
		os.Setenv("TI_PARAM_RES_tc1_MYSQL_URL", `["mysql://root:@127.0.0.1:3306"]`)
	})
	AfterEach(func() {
		os.Unsetenv("TI_PARAM_RES_tc1")
		os.Unsetenv("TI_PARAM_RES_tc1_MYSQL_URL")
	})

	Context("MySQL Local", func() {
		It("MySQL Local.GetDB", func() {
			ctx := suiteTestCtx
			tc1 := ctx.Resource("tc1").(resource.MySQL)
			Expect(tc1.Handle().Name).Should(Equal("tc1"))
			u, _ := tc1.ServiceURL(resource.DBAddr)
			Expect(u.Host).Should(Equal("127.0.0.1:3306"))

			_, err := tc1.GetDB("test")
			framework.ExpectNoError(err)
		})
	})
})
