@@ -10,6 +10,7 @@ import (
10
10
"github.com/stretchr/testify/assert"
11
11
corev1 "k8s.io/api/core/v1"
12
12
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13
+ "k8s.io/apimachinery/pkg/runtime"
13
14
"k8s.io/apimachinery/pkg/types"
14
15
15
16
"github.com/elastic/cloud-on-k8s/pkg/controller/common/name"
@@ -18,53 +19,86 @@ import (
18
19
19
20
func TestBuildMetricbeatBaseConfig (t * testing.T ) {
20
21
tests := []struct {
21
- name string
22
- isTLS bool
23
- baseConfig string
22
+ name string
23
+ isTLS bool
24
+ certsSecret * corev1.Secret
25
+ hasCA bool
26
+ baseConfig string
24
27
}{
25
28
{
26
- name : "with tls " ,
29
+ name : "with TLS and a CA " ,
27
30
isTLS : true ,
31
+ certsSecret : & corev1.Secret {
32
+ ObjectMeta : metav1.ObjectMeta {Name : "name-es-http-certs-public" , Namespace : "namespace" },
33
+ Data : map [string ][]byte {
34
+ "tls.crt" : []byte ("1234567890" ),
35
+ "ca.crt" : []byte ("1234567890" ),
36
+ },
37
+ },
28
38
baseConfig : `
29
39
hosts: ["scheme://localhost:1234"]
30
40
username: elastic-internal-monitoring
31
41
password: 1234567890
32
- ssl.certificate_authorities: ["/mnt/elastic-internal/xx-monitoring/namespace/name/certs/ca.crt"]
42
+ ssl.enabled: true
43
+ ssl.verification_mode: "certificate"
44
+ ssl.certificate_authorities: ["/mnt/elastic-internal/xx-monitoring/namespace/name/certs/ca.crt"]` ,
45
+ },
46
+ {
47
+ name : "with TLS and no CA" ,
48
+ isTLS : true ,
49
+ certsSecret : & corev1.Secret {
50
+ ObjectMeta : metav1.ObjectMeta {Name : "name-es-http-certs-public" , Namespace : "namespace" },
51
+ Data : map [string ][]byte {
52
+ "tls.crt" : []byte ("1234567890" ),
53
+ },
54
+ },
55
+ baseConfig : `
56
+ hosts: ["scheme://localhost:1234"]
57
+ username: elastic-internal-monitoring
58
+ password: 1234567890
59
+ ssl.enabled: true
33
60
ssl.verification_mode: "certificate"` ,
34
61
},
35
62
{
36
- name : "without tls " ,
63
+ name : "without TLS " ,
37
64
isTLS : false ,
38
65
baseConfig : `
39
66
hosts: ["scheme://localhost:1234"]
40
67
username: elastic-internal-monitoring
41
- password: 1234567890` ,
68
+ password: 1234567890
69
+ ssl.enabled: false
70
+ ssl.verification_mode: "certificate"` ,
42
71
},
43
72
}
44
-
45
73
baseConfigTemplate := `
46
74
hosts: ["{{ .URL }}"]
47
75
username: {{ .Username }}
48
76
password: {{ .Password }}
49
- {{- if .IsSSL }}
50
- ssl.certificate_authorities: ["{{ .SSLPath }}"]
51
- ssl.verification_mode: "{{ .SSLMode }}"
77
+ ssl.enabled: {{ .IsSSL }}
78
+ ssl.verification_mode: "certificate"
79
+ {{- if .HasCA }}
80
+ ssl.certificate_authorities: ["{{ .CAPath }}"]
52
81
{{- end }}`
53
- sampleURL := "scheme://localhost:1234"
54
82
55
- fakeClient := k8s .NewFakeClient (& corev1.Secret {
83
+ sampleURL := "scheme://localhost:1234"
84
+ internalUsersSecret := & corev1.Secret {
56
85
ObjectMeta : metav1.ObjectMeta {Name : "name-es-internal-users" , Namespace : "namespace" },
57
86
Data : map [string ][]byte {"elastic-internal-monitoring" : []byte ("1234567890" )},
58
- })
87
+ }
59
88
60
89
for _ , tc := range tests {
61
90
t .Run (tc .name , func (t * testing.T ) {
91
+ initObjects := []runtime.Object {internalUsersSecret }
92
+ if tc .certsSecret != nil {
93
+ initObjects = append (initObjects , tc .certsSecret )
94
+ }
95
+ fakeClient := k8s .NewFakeClient (initObjects ... )
62
96
baseConfig , _ , err := buildMetricbeatBaseConfig (
63
97
fakeClient ,
64
98
"xx-monitoring" ,
65
99
types.NamespacedName {Namespace : "namespace" , Name : "name" },
66
100
types.NamespacedName {Namespace : "namespace" , Name : "name" },
67
- name .NewNamer ("xx " ),
101
+ name .NewNamer ("es " ),
68
102
sampleURL ,
69
103
tc .isTLS ,
70
104
baseConfigTemplate ,
0 commit comments