Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions pkg/common/common_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2023 The Fluid 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 common

import (
"testing"

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

func TestCommon(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "Package Common Suite")
}
320 changes: 111 additions & 209 deletions pkg/common/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,256 +13,158 @@ 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 common

import (
"reflect"
"testing"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)

func TestLabelToModify(t *testing.T) {
var testCase = []struct {
labelToModify LabelToModify
expectedLabelKey string
expectedLabelValue string
expectedLabelOperationType OperationType
}{
{
labelToModify: LabelToModify{
var _ = ginkgo.Describe("LabelToModify", func() {
ginkgo.Describe("GetLabelKey, GetLabelValue, GetOperationType", func() {
ginkgo.It("should return correct values", func() {
labelToModify := LabelToModify{
labelKey: "commonLabel",
labelValue: "true",
operationType: AddLabel,
},
expectedLabelKey: "commonLabel",
expectedLabelValue: "true",
expectedLabelOperationType: AddLabel,
},
}

for _, test := range testCase {
labelKey := test.labelToModify.GetLabelKey()
labelValue := test.labelToModify.GetLabelValue()
operationType := test.labelToModify.GetOperationType()

if labelKey != test.expectedLabelKey || labelValue != test.expectedLabelValue || operationType != test.expectedLabelOperationType {
t.Errorf("expected labelKey %s, get labelKey %s; expected labelValue %s, get labelValue %s; expected labelOperationType %s, get labelOperationType %s",
test.expectedLabelKey, labelKey, test.expectedLabelValue, labelValue, test.expectedLabelOperationType, operationType)
}

}
}

func TestGetLabels(t *testing.T) {
var testCase = []struct {
labelsToModify LabelsToModify
expectedResult []LabelToModify
}{
{
labelsToModify: LabelsToModify{},
expectedResult: []LabelToModify{
}

gomega.Expect(labelToModify.GetLabelKey()).To(gomega.Equal("commonLabel"))
gomega.Expect(labelToModify.GetLabelValue()).To(gomega.Equal("true"))
gomega.Expect(labelToModify.GetOperationType()).To(gomega.Equal(AddLabel))
})
})
})

var _ = ginkgo.Describe("LabelsToModify", func() {
ginkgo.Describe("GetLabels", func() {
ginkgo.It("should return labels after adding", func() {
var labelsToModify LabelsToModify
labelsToModify.Add("commonLabel", "true")

expected := []LabelToModify{
{
labelKey: "commonLabel",
labelValue: "true",
operationType: AddLabel,
},
},
},
}

for _, test := range testCase {
test.labelsToModify.Add("commonLabel", "true")
label := test.labelsToModify.GetLabels()
if !reflect.DeepEqual(label, test.expectedResult) {
t.Errorf("fail to get the labels")
}
}
}

}
gomega.Expect(labelsToModify.GetLabels()).To(gomega.Equal(expected))
})
})

func TestOperator(t *testing.T) {
var labelsToModify LabelsToModify
ginkgo.Describe("operator", func() {
ginkgo.It("should add label with AddLabel operation", func() {
var labelsToModify LabelsToModify
labelsToModify.operator("commonLabel", "true", AddLabel)

var testCase = []struct {
labelKey string
labelValue string
operationType OperationType
expectedLabel LabelToModify
}{
{
labelKey: "commonLabel",
labelValue: "true",
operationType: AddLabel,
expectedLabel: LabelToModify{
expected := LabelToModify{
labelKey: "commonLabel",
labelValue: "true",
operationType: AddLabel,
},
},
{
labelKey: "commonLabel",
labelValue: "true",
operationType: DeleteLabel,
expectedLabel: LabelToModify{
}

gomega.Expect(labelsToModify.labels[0]).To(gomega.Equal(expected))
})

ginkgo.It("should add label with DeleteLabel operation without value", func() {
var labelsToModify LabelsToModify
labelsToModify.operator("commonLabel", "true", DeleteLabel)

expected := LabelToModify{
labelKey: "commonLabel",
operationType: DeleteLabel,
},
},
}
}

gomega.Expect(labelsToModify.labels[0]).To(gomega.Equal(expected))
})
})

for index, test := range testCase {
labelsToModify.operator(test.labelKey, test.labelValue, test.operationType)
if !reflect.DeepEqual(labelsToModify.labels[index], test.expectedLabel) {
t.Errorf("fail to add the label")
}
}
}
ginkgo.Describe("Add", func() {
ginkgo.It("should add label to modify slice", func() {
var labelsToModify LabelsToModify
labelsToModify.Add("commonLabel", "true")

func TestAdd(t *testing.T) {
var testCase = []struct {
labelKey string
labelValue string
wantedLabelsToModify []LabelToModify
}{
{
labelKey: "commonLabel",
labelValue: "true",
wantedLabelsToModify: []LabelToModify{
expected := []LabelToModify{
{
labelKey: "commonLabel",
labelValue: "true",
operationType: AddLabel,
},
},
},
}
}

var labelsToModify LabelsToModify
for _, test := range testCase {
labelsToModify.Add(test.labelKey, test.labelValue)
if !reflect.DeepEqual(labelsToModify.GetLabels(), test.wantedLabelsToModify) {
t.Errorf("fail to add labe to modify to slice")
}
}
}
gomega.Expect(labelsToModify.GetLabels()).To(gomega.Equal(expected))
})
})

func TestDelete(t *testing.T) {
var testCase = []struct {
labelKey string
labelValue string
wantedLabelsToModify []LabelToModify
}{
{
labelKey: "commonLabel",
wantedLabelsToModify: []LabelToModify{
ginkgo.Describe("Delete", func() {
ginkgo.It("should add delete operation to modify slice", func() {
var labelsToModify LabelsToModify
labelsToModify.Delete("commonLabel")

expected := []LabelToModify{
{
labelKey: "commonLabel",
operationType: DeleteLabel,
},
},
},
}
}

var labelsToModify LabelsToModify
for _, test := range testCase {
labelsToModify.Delete(test.labelKey)
if !reflect.DeepEqual(labelsToModify.GetLabels(), test.wantedLabelsToModify) {
t.Errorf("fail to add labe to modify to slice")
}
}
}
gomega.Expect(labelsToModify.GetLabels()).To(gomega.Equal(expected))
})
})

func TestUpdate(t *testing.T) {
var testCase = []struct {
labelKey string
labelValue string
wantedLabelsToModify []LabelToModify
}{
{
labelKey: "commonLabel",
wantedLabelsToModify: []LabelToModify{
ginkgo.Describe("Update", func() {
ginkgo.It("should add update operation to modify slice", func() {
var labelsToModify LabelsToModify
labelsToModify.Update("commonLabel", "")

expected := []LabelToModify{
{
labelKey: "commonLabel",
operationType: UpdateLabel,
},
},
},
}

var labelsToModify LabelsToModify
for _, test := range testCase {
labelsToModify.Update(test.labelKey, test.labelValue)
if !reflect.DeepEqual(labelsToModify.GetLabels(), test.wantedLabelsToModify) {
t.Errorf("fail to add labe to modify to slice")
}
}
}

func TestHitTarget(t *testing.T) {
testCases := map[string]struct {
labels map[string]string
target string
targetValue string
wantHit bool
}{
"test label target hit case 1": {
labels: map[string]string{EnableFluidInjectionFlag: "true"},
target: EnableFluidInjectionFlag,
targetValue: "true",
wantHit: true,
},
"test label target hit case 2": {
labels: map[string]string{EnableFluidInjectionFlag: "false"},
target: EnableFluidInjectionFlag,
targetValue: "true",
wantHit: false,
},
"test label target hit case 3": {
labels: nil,
target: EnableFluidInjectionFlag,
targetValue: "true",
wantHit: false,
},
}
}

for index, item := range testCases {
gotHit := CheckExpectValue(item.labels, item.target, item.targetValue)
if gotHit != item.wantHit {
t.Errorf("%s check failure, want:%t,got:%t", index, item.wantHit, gotHit)
}
}
gomega.Expect(labelsToModify.GetLabels()).To(gomega.Equal(expected))
})
})
})

}

func TestLabelAnnotationPodSchedRegex(t *testing.T) {
testCases := map[string]struct {
target string
got string
match bool
}{
"correct": {
target: LabelAnnotationDataset + ".dsA.sched",
match: true,
got: "dsA",
},
"wrong fluid.io": {
target: "fluidaio/dataset.dsA.sched",
match: false,
var _ = ginkgo.Describe("CheckExpectValue", func() {
ginkgo.DescribeTable("should check if label has expected value",
func(labels map[string]string, target, targetValue string, wantHit bool) {
gomega.Expect(CheckExpectValue(labels, target, targetValue)).To(gomega.Equal(wantHit))
},
"wrong prefix": {
target: "a.fluid.io/dataset.dsA.sched",
match: false,
ginkgo.Entry("label matches target and value",
map[string]string{EnableFluidInjectionFlag: "true"},
EnableFluidInjectionFlag, "true", true),
ginkgo.Entry("label matches target but not value",
map[string]string{EnableFluidInjectionFlag: "false"},
EnableFluidInjectionFlag, "true", false),
ginkgo.Entry("nil labels",
nil,
EnableFluidInjectionFlag, "true", false),
)
})

var _ = ginkgo.Describe("LabelAnnotationPodSchedRegex", func() {
ginkgo.DescribeTable("should match correct patterns",
func(target string, shouldMatch bool, expectedGroup string) {
submatch := LabelAnnotationPodSchedRegex.FindStringSubmatch(target)
if shouldMatch {
gomega.Expect(submatch).To(gomega.HaveLen(2))
gomega.Expect(submatch[1]).To(gomega.Equal(expectedGroup))
} else {
gomega.Expect(len(submatch)).NotTo(gomega.Equal(2))
}
},
}

for index, item := range testCases {
submatch := LabelAnnotationPodSchedRegex.FindStringSubmatch(item.target)

if !item.match && len(submatch) == 2 {
t.Errorf("[%s] check match, want:%t, got:%t", index, item.match, len(submatch) == 2)
}

if len(submatch) == 2 && submatch[1] != item.got {
t.Errorf("[%s] check failure, want:%s, got:%s", index, item.got, submatch[1])
}
}
}
ginkgo.Entry("correct pattern",
LabelAnnotationDataset+".dsA.sched", true, "dsA"),
ginkgo.Entry("wrong fluid.io prefix",
"fluidaio/dataset.dsA.sched", false, ""),
ginkgo.Entry("wrong prefix",
"a.fluid.io/dataset.dsA.sched", false, ""),
)
})