-
Notifications
You must be signed in to change notification settings - Fork 270
Expand file tree
/
Copy pathlocaldns_exporter_spec.sh
More file actions
163 lines (137 loc) · 6.24 KB
/
Copy pathlocaldns_exporter_spec.sh
File metadata and controls
163 lines (137 loc) · 6.24 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
#!/bin/bash
Describe 'localdns-exporter@.service security hardening'
UNIT_FILE="./parts/linux/cloud-init/artifacts/localdns-exporter@.service"
It 'should have DynamicUser=yes'
When run grep -q "^DynamicUser=yes$" "$UNIT_FILE"
The status should be success
End
It 'should have PrivateTmp=yes'
When run grep -q "^PrivateTmp=yes$" "$UNIT_FILE"
The status should be success
End
It 'should have ProtectSystem=strict'
When run grep -q "^ProtectSystem=strict$" "$UNIT_FILE"
The status should be success
End
It 'should have ProtectHome=yes'
When run grep -q "^ProtectHome=yes$" "$UNIT_FILE"
The status should be success
End
It 'should have ReadOnlyPaths=/'
When run grep -q "^ReadOnlyPaths=/$" "$UNIT_FILE"
The status should be success
End
It 'should have NoNewPrivileges=yes'
When run grep -q "^NoNewPrivileges=yes$" "$UNIT_FILE"
The status should be success
End
It 'should have ProtectKernelTunables=yes'
When run grep -q "^ProtectKernelTunables=yes$" "$UNIT_FILE"
The status should be success
End
It 'should have ProtectKernelModules=yes'
When run grep -q "^ProtectKernelModules=yes$" "$UNIT_FILE"
The status should be success
End
It 'should have ProtectControlGroups=yes'
When run grep -q "^ProtectControlGroups=yes$" "$UNIT_FILE"
The status should be success
End
It 'should have RestrictAddressFamilies with AF_UNIX AF_INET AF_INET6'
When run grep -q "^RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6$" "$UNIT_FILE"
The status should be success
End
It 'should have RestrictNamespaces=yes'
When run grep -q "^RestrictNamespaces=yes$" "$UNIT_FILE"
The status should be success
End
It 'should have LockPersonality=yes'
When run grep -q "^LockPersonality=yes$" "$UNIT_FILE"
The status should be success
End
It 'should have RestrictRealtime=yes'
When run grep -q "^RestrictRealtime=yes$" "$UNIT_FILE"
The status should be success
End
It 'should have RestrictSUIDSGID=yes'
When run grep -q "^RestrictSUIDSGID=yes$" "$UNIT_FILE"
The status should be success
End
It 'should have RemoveIPC=yes'
When run grep -q "^RemoveIPC=yes$" "$UNIT_FILE"
The status should be success
End
It 'should have PrivateMounts=yes'
When run grep -q "^PrivateMounts=yes$" "$UNIT_FILE"
The status should be success
End
End
Describe 'localdns_exporter.sh HTTP request routing'
SCRIPT_PATH="./parts/linux/cloud-init/artifacts/localdns_exporter.sh"
It 'should return 404 for root path'
When run bash -c "echo 'GET / HTTP/1.1' | $SCRIPT_PATH"
The status should be success
The output should include "HTTP/1.1 404 Not Found"
The output should include "Content-Type: text/plain"
The output should include "404 Not Found - Metrics available at /metrics"
The output should not include "localdns_service_status"
The output should not include "localdns_memory_usage_bytes"
End
It 'should return 404 for /health path'
When run bash -c "echo 'GET /health HTTP/1.1' | $SCRIPT_PATH"
The status should be success
The output should include "HTTP/1.1 404 Not Found"
The output should include "Content-Type: text/plain"
The output should include "404 Not Found - Metrics available at /metrics"
End
It 'should return 404 for /status path'
When run bash -c "echo 'GET /status HTTP/1.1' | $SCRIPT_PATH"
The status should be success
The output should include "HTTP/1.1 404 Not Found"
The output should include "404 Not Found - Metrics available at /metrics"
End
It 'should return 404 for /api/v1/metrics path'
When run bash -c "echo 'GET /api/v1/metrics HTTP/1.1' | $SCRIPT_PATH"
The status should be success
The output should include "HTTP/1.1 404 Not Found"
The output should include "404 Not Found - Metrics available at /metrics"
End
It 'should return 404 for invalid path'
When run bash -c "echo 'GET /invalid HTTP/1.1' | $SCRIPT_PATH"
The status should be success
The output should include "HTTP/1.1 404 Not Found"
The output should include "404 Not Found - Metrics available at /metrics"
End
It 'should have proper CRLF line endings in 404 response'
When run bash -c "echo 'GET / HTTP/1.1' | $SCRIPT_PATH | head -c 100 | od -A n -t x1"
The status should be success
# Check for CRLF (0d 0a) in the HTTP header - look across multiple lines
The output should include "0d"
The output should include "0a"
End
It 'should exit cleanly when client disconnects without sending request'
# Simulate client disconnect by providing no input (EOF immediately)
When run bash -c "$SCRIPT_PATH < /dev/null"
The status should be success
The output should equal ""
End
It 'should return 200 and Prometheus metrics for /metrics path'
When run bash -c "echo 'GET /metrics HTTP/1.1' | $SCRIPT_PATH"
The status should be success
The output should include "HTTP/1.1 200 OK"
The output should include "Content-Type: text/plain; version=0.0.4"
# Verify metric type declarations are present
The output should include "# TYPE localdns_service_status gauge"
The output should include "# TYPE localdns_memory_usage_bytes gauge"
The output should include "# TYPE localdns_cpu_usage_seconds_total counter"
The output should include "# TYPE localdns_metrics_last_update_timestamp_seconds gauge"
# Verify metric names are present (values will vary)
The output should include "localdns_service_status"
The output should include "localdns_memory_usage_bytes"
The output should include "localdns_cpu_usage_seconds_total"
The output should include "localdns_metrics_last_update_timestamp_seconds"
# Verify forward-info metrics are present (either actual metrics or fallback)
The output should include "localdns_vnetdns_forward_info"
The output should include "localdns_kubedns_forward_info"
End
End