-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_framework_test.go
More file actions
118 lines (104 loc) · 2.91 KB
/
main_framework_test.go
File metadata and controls
118 lines (104 loc) · 2.91 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/deckhouse/module-sdk/pkg"
"github.com/deckhouse/module-sdk/testing/framework"
singlefileexample "singlefileexample"
)
// hookConfig mirrors the configuration in main.go but is built locally so
// that the functional test does not depend on the global registry being
// pristine when go test runs the package.
var hookConfig = &pkg.HookConfig{
Kubernetes: []pkg.KubernetesConfig{
{
Name: singlefileexample.SnapshotKey,
APIVersion: "v1",
Kind: "Pod",
NamespaceSelector: &pkg.NamespaceSelector{
NameSelector: &pkg.NameSelector{MatchNames: []string{"kube-system"}},
},
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{"component": "kube-apiserver"},
},
JqFilter: ".metadata.name",
},
},
}
// TestHandle_DiscoversAPIServerPodsFromCluster is the canonical
// end-to-end test for this example: kube-apiserver Pods are seeded into
// the fake cluster, the framework filters them by namespace + labels,
// passes the names to the hook, and the hook writes them to internal
// values.
func TestHandle_DiscoversAPIServerPodsFromCluster(t *testing.T) {
const state = `
---
apiVersion: v1
kind: Pod
metadata:
name: kube-apiserver-1
namespace: kube-system
labels:
component: kube-apiserver
---
apiVersion: v1
kind: Pod
metadata:
name: kube-apiserver-2
namespace: kube-system
labels:
component: kube-apiserver
---
apiVersion: v1
kind: Pod
metadata:
name: not-an-apiserver
namespace: kube-system
labels:
component: scheduler
---
apiVersion: v1
kind: Pod
metadata:
name: kube-apiserver-other
namespace: default
labels:
component: kube-apiserver
`
f := framework.HookExecutionConfigInit(t,
hookConfig,
singlefileexample.Handle,
`{}`, `{}`,
)
f.KubeStateSet(state)
f.RunHook()
require.NoError(t, f.HookError())
// Only the two kube-system + apiserver Pods should be in the snapshot.
snaps := f.Snapshots().Get(singlefileexample.SnapshotKey)
require.Len(t, snaps, 2)
got := f.ValuesGet("test.internal.apiServers")
require.True(t, got.Exists())
arr := got.Array()
names := make([]string, 0, len(arr))
for _, item := range arr {
names = append(names, item.String())
}
assert.ElementsMatch(t, []string{"kube-apiserver-1", "kube-apiserver-2"}, names)
}
// TestHandle_NoPodsResultsInEmptyValues verifies the empty path: when
// the cluster has nothing matching the binding, the hook still writes an
// empty list to keep the rest of the chart deterministic.
func TestHandle_NoPodsResultsInEmptyValues(t *testing.T) {
f := framework.HookExecutionConfigInit(t,
hookConfig,
singlefileexample.Handle,
`{}`, `{}`,
)
f.RunHook()
require.NoError(t, f.HookError())
got := f.ValuesGet("test.internal.apiServers")
require.True(t, got.Exists())
assert.Empty(t, got.Array())
}