package cluster

import (
	"testing"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

	"github.com/pingcap/dbaas/central/config"
	cloudv1 "github.com/tidbcloud/infra-api/api/v1"

	. "github.com/smartystreets/goconvey/convey"
)

func Test_setNetworkThresholdAnnotations(t *testing.T) {
	Convey("Test_setNetworkThresholdAnnotations", t, func() {
		Convey("should init annotations when annotations is ni", func() {
			cr := &cloudv1.Cluster{}
			setNetworkThresholdAnnotations(cr, true, true)

			So(cr.Annotations, ShouldResemble, map[string]string{
				ingressTrafficAnnKey: config.ThrottledDevTierBandwidth,
				egressTrafficAnnKey:  config.ThrottledDevTierBandwidth,
			})
		})
		Convey("should set throttledBandwidth when egress is throttled", func() {
			cr := &cloudv1.Cluster{
				ObjectMeta: metav1.ObjectMeta{
					Annotations: map[string]string{
						"abc": "efg",
					},
				},
			}
			setNetworkThresholdAnnotations(cr, false, true)

			So(cr.Annotations, ShouldResemble, map[string]string{
				"abc":               "efg",
				egressTrafficAnnKey: config.ThrottledDevTierBandwidth,
			})
		})
		Convey("should clear ingressThrottledBandwidth when ingressThrottled is false", func() {
			cr := &cloudv1.Cluster{
				ObjectMeta: metav1.ObjectMeta{
					Annotations: map[string]string{
						ingressTrafficAnnKey: config.ThrottledDevTierBandwidth,
						"abc":                "efg",
					},
				},
			}
			setNetworkThresholdAnnotations(cr, false, true)

			So(cr.Annotations, ShouldResemble, map[string]string{
				"abc":               "efg",
				egressTrafficAnnKey: config.ThrottledDevTierBandwidth,
			})
		})
	})
}
