forked from estafette/estafette-gke-preemptible-killer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
101 lines (79 loc) · 2.81 KB
/
main_test.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"math/rand"
"testing"
"time"
"github.com/ericchiang/k8s"
apiv1 "github.com/ericchiang/k8s/api/v1"
metav1 "github.com/ericchiang/k8s/apis/meta/v1"
"github.com/rs/zerolog"
)
type FakeKubernetes struct {
}
func FakeNewKubernetesClient() KubernetesClient {
return &FakeKubernetes{}
}
func (k *FakeKubernetes) GetProjectIdAndZoneFromNode(name string) (string, string, error) {
return "", "", nil
}
func (k *FakeKubernetes) DrainNode(node string, drainTimeout int) error {
return nil
}
func (k *FakeKubernetes) DrainKubeDNSFromNode(node string, drainTimeout int) error {
return nil
}
func (k *FakeKubernetes) GetNode(name string) (*apiv1.Node, error) {
return &apiv1.Node{}, nil
}
func (k *FakeKubernetes) DeleteNode(name string) error {
return nil
}
func (k *FakeKubernetes) SetNodeAnnotation(name string, key string, value string) error {
return nil
}
func (k *FakeKubernetes) SetUnschedulableState(name string, unschedulable bool) error {
return nil
}
func (k *FakeKubernetes) GetPreemptibleNodes(map[string]string) (*apiv1.NodeList, error) {
return &apiv1.NodeList{}, nil
}
func TestGetCurrentNodeState(t *testing.T) {
zerolog.SetGlobalLevel(zerolog.Disabled)
node := &apiv1.Node{
Metadata: &metav1.ObjectMeta{
Name: k8s.String("node-1"),
Annotations: map[string]string{
"estafette.io/gke-preemptible-killer-state": "2017-11-11T11:11:11Z",
},
},
}
state := getCurrentNodeState(node)
if state.ExpiryDatetime != "2017-11-11T11:11:11Z" {
t.Errorf("Expect expiry date time to be 2017-11-11T11:11:11Z, instead got %s", state.ExpiryDatetime)
}
}
func TestGetDesiredNodeState(t *testing.T) {
zerolog.SetGlobalLevel(zerolog.Disabled)
creationTimestamp := time.Date(2017, 11, 11, 12, 00, 00, 0, time.UTC)
creationTimestampUnix := creationTimestamp.Unix()
creationTimestamp12HoursLater := creationTimestamp.Add(12 * time.Hour)
creationTimestamp24HoursLater := creationTimestamp.Add(24 * time.Hour)
node := &apiv1.Node{
Metadata: &metav1.ObjectMeta{
Name: k8s.String("node-1"),
CreationTimestamp: &metav1.Time{Seconds: &creationTimestampUnix},
},
}
client := FakeNewKubernetesClient()
whitelistInstance.parseArguments()
state, _ := getDesiredNodeState(client, node)
stateTS, _ := time.Parse(time.RFC3339, state.ExpiryDatetime)
if !creationTimestamp12HoursLater.Before(stateTS) && !creationTimestamp24HoursLater.After(stateTS) {
t.Errorf("Expect expiry date time to be between 12 and 24h after the creation date %s, instead got %s", creationTimestamp, state.ExpiryDatetime)
}
randomEstafette = rand.New(rand.NewSource(0))
stateWithPreseed, _ := getDesiredNodeState(client, node)
if stateWithPreseed.ExpiryDatetime != "2017-11-12T11:27:54Z" {
t.Errorf("Expect expiry date time to be 2017-11-12T11:27:54Z, instead got %s", stateWithPreseed.ExpiryDatetime)
}
}