This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
/
data-volume.go
68 lines (59 loc) · 2.42 KB
/
data-volume.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package matchers
import (
"context"
"fmt"
"github.com/kubevirt/vm-import-operator/tests/framework"
"github.com/onsi/gomega/types"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type haveStorageClassMatcher struct {
storageClass *string
testFramework *framework.Framework
}
// HaveDefaultStorageClass whether data volume of a given name has default storage class (nil)
func HaveDefaultStorageClass(f *framework.Framework) types.GomegaMatcher {
return &haveStorageClassMatcher{testFramework: f}
}
// HaveStorageClass creates matcher checking whether data volume of a given name has expected storage class
func HaveStorageClass(sc string, f *framework.Framework) types.GomegaMatcher {
return &haveStorageClassMatcher{storageClass: &sc, testFramework: f}
}
// HaveStorageClassReference creates matcher checking whether data volume of a given name has expected storage class
func HaveStorageClassReference(sc *string, f *framework.Framework) types.GomegaMatcher {
return &haveStorageClassMatcher{storageClass: sc, testFramework: f}
}
// Match checks whether data volume of a given name has expected storage class
func (matcher *haveStorageClassMatcher) Match(dvName interface{}) (bool, error) {
f := matcher.testFramework
dv, err := f.CdiClient.CdiV1alpha1().DataVolumes(f.Namespace.Name).Get(context.TODO(), dvName.(string), v1.GetOptions{})
if err != nil {
return false, err
}
actual := dv.Spec.PVC.StorageClassName
expected := matcher.storageClass
if expected == nil && actual == nil {
return true, nil
}
if expected != nil && actual != nil && *expected == *actual {
return true, nil
}
return false, nil
}
// FailureMessage is a message shown for failure
func (matcher *haveStorageClassMatcher) FailureMessage(actual interface{}) (message string) {
dvName := actual.(string)
if matcher.storageClass != nil {
return fmt.Sprintf("Expected\nData volume %s\nto have storage class\n%s", dvName, *matcher.storageClass)
} else {
return fmt.Sprintf("Expected\nData volume %s\nto have no storage class", dvName)
}
}
// NegatedFailureMessage us message shown for negated failure
func (matcher *haveStorageClassMatcher) NegatedFailureMessage(actual interface{}) (message string) {
dvName := actual.(string)
if matcher.storageClass != nil {
return fmt.Sprintf("Expected\nData volume %s\nnot to have storage class\n%s", dvName, *matcher.storageClass)
} else {
return fmt.Sprintf("Expected\nData volume %s\nto have storage class", dvName)
}
}