// 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

import (
	"fmt"
	"time"

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

func testMonitor(f *framework.Framework) {
	clusterName := "e2e-monitor"
	var pdCount int32 = 3
	var tidbCount int32 = 1
	var tikvCount int32 = 3
	done := deployCluster(f, e2eNamespace, clusterName, pdCount, tidbCount, tikvCount)

	monitorName := fmt.Sprintf("%s-monitor", clusterName)
	var monitorReplicas int32 = 1
	privilegedTidbName := fmt.Sprintf("%s-privileged-tidb", clusterName)
	var privilegedTidbReplicas int32 = 1

	// TODO(tennix): add more test about monitor and privileged-tidb
	err := wait.Poll(5*time.Second, 5*time.Minute, func() (bool, error) {
		monitorDeploy, err := f.KubeCli.AppsV1beta1().Deployments(e2eNamespace).Get(monitorName, metav1.GetOptions{})
		if err != nil {
			return false, nil
		}
		if *monitorDeploy.Spec.Replicas != monitorReplicas ||
			monitorDeploy.Status.ReadyReplicas != monitorReplicas {
			return false, nil
		}
		privilegedTidbDeploy, err := f.KubeCli.AppsV1beta1().Deployments(e2eNamespace).Get(privilegedTidbName, metav1.GetOptions{})
		if err != nil {
			return false, nil
		}
		if *privilegedTidbDeploy.Spec.Replicas != privilegedTidbReplicas ||
			privilegedTidbDeploy.Status.ReadyReplicas != privilegedTidbReplicas {
			return false, nil
		}
		return true, nil
	})
	Expect(err).NotTo(HaveOccurred())

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