forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeatures_test.go
More file actions
168 lines (147 loc) · 3.65 KB
/
Copy pathfeatures_test.go
File metadata and controls
168 lines (147 loc) · 3.65 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package features
import (
"testing"
"github.com/elastic/elastic-agent-libs/config"
"github.com/stretchr/testify/require"
)
func TestFQDN(t *testing.T) {
tcs := []struct {
name string
yaml string
want bool
}{
{
name: "FQDN enabled",
yaml: `
features:
fqdn:
enabled: true`,
want: true,
},
{
name: "FQDN disabled",
yaml: `
features:
fqdn:
enabled: false`,
want: false,
},
{
name: "FQDN only {}",
yaml: `
features:
fqdn: {}`,
want: true,
},
{
name: "FQDN empty",
yaml: `
features:
fqdn:`,
want: false,
},
{
name: "FQDN absent",
yaml: `
features:`,
want: false,
},
{
name: "No features",
yaml: `
# no features, just a comment`,
want: false,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
c, err := config.NewConfigFrom(tc.yaml)
if err != nil {
t.Fatalf("could not parse config YAML: %v", err)
}
err = UpdateFromConfig(c)
if err != nil {
t.Fatalf("UpdateFromConfig failed: %v", err)
}
got := FQDN()
if got != tc.want {
t.Errorf("want: %t, got %t", tc.want, got)
}
})
}
}
func TestFQDNCallbacks(t *testing.T) {
cb1Called, cb2Called := false, false
err := AddFQDNOnChangeCallback(func(new, old bool) {
cb1Called = true
}, "cb1")
require.NoError(t, err)
err = AddFQDNOnChangeCallback(func(new, old bool) {
cb2Called = true
}, "cb2")
require.NoError(t, err)
defer func() {
// Cleanup in case we don't get to the end of
// this test successfully.
if _, exists := flags.fqdnCallbacks["cb1"]; exists {
RemoveFQDNOnChangeCallback("cb1")
}
if _, exists := flags.fqdnCallbacks["cb2"]; exists {
RemoveFQDNOnChangeCallback("cb2")
}
}()
require.Len(t, flags.fqdnCallbacks, 2)
flags.SetFQDNEnabled(false)
require.True(t, cb1Called)
require.True(t, cb2Called)
RemoveFQDNOnChangeCallback("cb1")
require.Len(t, flags.fqdnCallbacks, 1)
RemoveFQDNOnChangeCallback("cb2")
require.Len(t, flags.fqdnCallbacks, 0)
}
func TestFQDNWHileCallbackBlocked(t *testing.T) {
blockChan := make(chan struct{})
willBlockChan := make(chan struct{})
unblockedChan := make(chan struct{})
err := AddFQDNOnChangeCallback(func(new, old bool) {
willBlockChan <- struct{}{}
t.Logf("callback is currently blocked.")
<-blockChan
t.Logf("callback is unblocked.")
}, "test-TestFQDNWHileCallbackBlocked")
require.NoError(t, err)
// Start with FQDN off
go func() {
err = UpdateFromConfig(config.MustNewConfigFrom(map[string]interface{}{
"features.fqdn.enabled": true,
}))
unblockedChan <- struct{}{}
}()
// callback should be blocking at this point
t.Logf("Waiting for callback to block...")
<-willBlockChan
whileBlocked := FQDN()
require.True(t, whileBlocked)
//now unblock
blockChan <- struct{}{}
t.Logf("Waiting for callback to unblock...")
<-unblockedChan
unblocked := FQDN()
require.True(t, unblocked)
}