// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.package spec package e2e

package e2e

import (
	"fmt"
	"os/exec"
	"time"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/pingcap/tidb-operator/test/e2e/framework"
	"github.com/pingcap/tidb-operator/test/e2e/util"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/util/wait"
)

func testLightning(f *framework.Framework) {
	var tidbCount int32 = 1
	var pdCount int32 = 1
	var tikvCount int32 = 3
	clusterName := "e2e-lightning"

	done := deployCluster(f, e2eNamespace, clusterName, pdCount, tidbCount, tikvCount)

	By("When deploy tikv-importer")
	cmd := fmt.Sprintf("helm install /tmp/importer --namespace=%s --name=tikv-importer --values=/tmp/data/lightning/importer-values.yaml",
		e2eNamespace)
	data, err := exec.Command("/bin/sh", "-c", cmd).CombinedOutput()
	Expect(err).NotTo(HaveOccurred(), string(data))

	By("Then tikv-importer is running")
	err = wait.Poll(5*time.Second, 5*time.Minute, func() (bool, error) {
		pod, err := f.KubeCli.CoreV1().Pods(e2eNamespace).Get("tikv-importer", metav1.GetOptions{})
		if err != nil {
			return false, nil
		}
		if pod.Status.Phase != corev1.PodRunning {
			return false, nil
		}
		for _, cond := range pod.Status.Conditions {
			if cond.Type == corev1.PodReady && cond.Status == corev1.ConditionTrue {
				return true, nil
			}
		}

		return false, nil
	})
	Expect(err).NotTo(HaveOccurred())

	By("When deploy tidb-lightning")
	cmd = fmt.Sprintf("helm install /tmp/lightning --namespace=%s --name=tidb-lightning --values=/tmp/data/lightning/lightning-values.yaml",
		e2eNamespace)
	data, err = exec.Command("/bin/sh", "-c", cmd).CombinedOutput()
	if err != nil {
		util.Logf(string(data))
	}
	Expect(err).NotTo(HaveOccurred())

	By("Then tidb-lightning is running")
	err = wait.Poll(5*time.Second, 5*time.Minute, func() (bool, error) {
		pod, err := f.KubeCli.CoreV1().Pods(e2eNamespace).Get("tidb-lightning", metav1.GetOptions{})
		if err != nil {
			return false, nil
		}
		if pod.Status.Phase != corev1.PodRunning {
			return false, nil
		}
		for _, cond := range pod.Status.Conditions {
			if cond.Type == corev1.PodReady && cond.Status == corev1.ConditionTrue {
				return true, nil
			}
		}

		return false, nil
	})
	Expect(err).NotTo(HaveOccurred())

	By("And data is imported successfully")
	svcName := fmt.Sprintf("%s-tidb", clusterName)
	ensureDataIsRight(svcName, e2eNamespace, "mydumper", "dog", "cat")

	deleteCluster(f, e2eNamespace, clusterName, done, pdCount+tikvCount)
}
