/*
Copyright 2021 The Volcano Authors.

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,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package jobseq

import (
	"context"

	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

	busv1alpha1 "volcano.sh/apis/pkg/apis/bus/v1alpha1"
	schedulingv1beta1 "volcano.sh/apis/pkg/apis/scheduling/v1beta1"
	"volcano.sh/volcano/pkg/cli/util"

	e2eutil "volcano.sh/volcano/test/e2e/util"
)

var _ = Describe("Queue E2E Test", func() {
	var testCtx *e2eutil.TestContext

	JustAfterEach(func() {
		e2eutil.DumpTestContextIfFailed(testCtx, CurrentSpecReport())
	})
	It("Queue Command Close And Open With State Check", func() {
		q1 := "queue-command-close"
		defaultQueue := "default"
		testCtx = e2eutil.InitTestContext(e2eutil.Options{
			Queues: []string{q1},
		})
		DeferCleanup(e2eutil.CleanupTestContext, testCtx)

		By("Close queue command check")
		err := util.CreateQueueCommand(testCtx.Vcclient, defaultQueue, q1, busv1alpha1.CloseQueueAction)
		if err != nil {
			Expect(err).NotTo(HaveOccurred(), "Error send close queue command")
		}

		err = e2eutil.WaitQueueStatus(func() (bool, error) {
			queue, err := testCtx.Vcclient.SchedulingV1beta1().Queues().Get(context.TODO(), q1, metav1.GetOptions{})
			Expect(err).NotTo(HaveOccurred(), "Get queue %s failed", q1)
			return queue.Status.State == schedulingv1beta1.QueueStateClosed, nil
		})
		Expect(err).NotTo(HaveOccurred(), "Wait for closed queue %s failed", q1)

		By("Open queue command check")
		err = util.CreateQueueCommand(testCtx.Vcclient, defaultQueue, q1, busv1alpha1.OpenQueueAction)
		if err != nil {
			Expect(err).NotTo(HaveOccurred(), "Error send open queue command")
		}

		err = e2eutil.WaitQueueStatus(func() (bool, error) {
			queue, err := testCtx.Vcclient.SchedulingV1beta1().Queues().Get(context.TODO(), q1, metav1.GetOptions{})
			Expect(err).NotTo(HaveOccurred(), "Get queue %s failed", q1)
			return queue.Status.State == schedulingv1beta1.QueueStateOpen, nil
		})
		Expect(err).NotTo(HaveOccurred(), "Wait for reopen queue %s failed", q1)

	})

	It("Queue Command Close And Open With Hierarchical Queues", func() {
		q1 := "queue1"
		q2 := "queue2"
		q11 := "queue11"
		defaultQueue := "default"
		rootQueue := "root"
		testCtx = e2eutil.InitTestContext(e2eutil.Options{
			Queues: []string{q1, q2, q11},
			QueueParent: map[string]string{
				q1:  rootQueue,
				q2:  rootQueue,
				q11: q1,
			},
		})
		testCtx.Queues = []string{q11, q1, q2}
		DeferCleanup(e2eutil.CleanupTestContext, testCtx)

		By("Close queue with child queues")
		err := util.CreateQueueCommand(testCtx.Vcclient, defaultQueue, q1, busv1alpha1.CloseQueueAction)
		if err != nil {
			Expect(err).NotTo(HaveOccurred(), "Error send close queue command")
		}

		err = e2eutil.WaitQueueStatus(func() (bool, error) {
			queue, err := testCtx.Vcclient.SchedulingV1beta1().Queues().Get(context.TODO(), q11, metav1.GetOptions{})
			Expect(err).NotTo(HaveOccurred(), "Get queue %s failed", q11)
			return queue.Status.State == schedulingv1beta1.QueueStateClosed, nil
		})
		Expect(err).NotTo(HaveOccurred(), "Wait for closed queue %s failed", q11)

		By("Open queue with closed parent queue")
		err = util.CreateQueueCommand(testCtx.Vcclient, defaultQueue, q11, busv1alpha1.OpenQueueAction)
		if err != nil {
			Expect(err).NotTo(HaveOccurred(), "Error send open queue command")
		}

		err = e2eutil.WaitQueueStatus(func() (bool, error) {
			queue, err := testCtx.Vcclient.SchedulingV1beta1().Queues().Get(context.TODO(), q11, metav1.GetOptions{})
			Expect(err).NotTo(HaveOccurred(), "Get queue %s failed", q11)
			return queue.Status.State == schedulingv1beta1.QueueStateClosed, nil
		})
		Expect(err).NotTo(HaveOccurred(), "Wait for reopen queue %s failed", q11)

		By("Open queue with open parent queue")
		err = util.CreateQueueCommand(testCtx.Vcclient, defaultQueue, q1, busv1alpha1.OpenQueueAction)
		if err != nil {
			Expect(err).NotTo(HaveOccurred(), "Error send open queue command")
		}

		err = e2eutil.WaitQueueStatus(func() (bool, error) {
			queue, err := testCtx.Vcclient.SchedulingV1beta1().Queues().Get(context.TODO(), q1, metav1.GetOptions{})
			Expect(err).NotTo(HaveOccurred(), "Get queue %s failed", q1)
			return queue.Status.State == schedulingv1beta1.QueueStateOpen, nil
		})
		Expect(err).NotTo(HaveOccurred(), "Wait for reopen queue %s failed", q1)
	})
})
