/
testcase.go
56 lines (46 loc) · 1.4 KB
/
testcase.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
package example
import (
"context"
"errors"
"fmt"
"github.com/thatchd/thatchd/pkg/thatchd/testcase"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// PodAnnotationTestCase asserts that a pod has an annotation with an expected
// value
type PodAnnotationTestCase struct {
PodName string
ExpectedAnnotation string
ExpectedValue string
}
var _ testcase.Interface = &PodAnnotationTestCase{}
func (tc *PodAnnotationTestCase) ShouldRun(s interface{}) bool {
state := s.(PodSuiteState)
podState, ok := state[tc.PodName]
return ok && podState == PodAnnotated
}
func (tc *PodAnnotationTestCase) Run(c client.Client, namespace string) error {
pod := &v1.Pod{}
if err := c.Get(context.TODO(), client.ObjectKey{
Name: tc.PodName,
Namespace: namespace,
}, pod); err != nil {
return fmt.Errorf("Failed to obtain pod: %v", err)
}
annotations := pod.Annotations
if annotations == nil {
return errors.New("Pod has no annotations")
}
if value, ok := annotations[tc.ExpectedAnnotation]; !ok || value != tc.ExpectedValue {
return fmt.Errorf("Annotation %s: %s not found in Pod", tc.ExpectedAnnotation, tc.ExpectedValue)
}
return nil
}
func NewTestCase(config map[string]string) interface{} {
return &PodAnnotationTestCase{
PodName: config["podName"],
ExpectedAnnotation: config["expectedAnnotation"],
ExpectedValue: config["expectedValue"],
}
}