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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
// Copyright 2020 PingCAP, Inc.
//
// Licensed 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package spec
import (
"context"
"fmt"
"os"
"os/user"
"path"
"path/filepath"
"testing"
"github.com/pingcap/tiup/pkg/checkpoint"
"github.com/pingcap/tiup/pkg/cluster/executor"
"github.com/pingcap/tiup/pkg/meta"
"github.com/pingcap/tiup/pkg/utils"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
func TestLocalRuleDirs(t *testing.T) {
deployDir, err := os.MkdirTemp("", "tiup-*")
assert.Nil(t, err)
defer os.RemoveAll(deployDir)
err = utils.MkdirAll(path.Join(deployDir, "bin/prometheus"), 0755)
assert.Nil(t, err)
localDir, err := filepath.Abs("./testdata/rules")
assert.Nil(t, err)
err = os.WriteFile(path.Join(deployDir, "bin/prometheus", "dummy.rules.yml"), []byte("dummy"), 0644)
assert.Nil(t, err)
topo := new(Specification)
topo.Monitors = append(topo.Monitors, &PrometheusSpec{
Host: "127.0.0.1",
Port: 9090,
RuleDir: localDir,
})
comp := MonitorComponent{topo}
ints := comp.Instances()
assert.Equal(t, len(ints), 1)
promInstance := ints[0].(*MonitorInstance)
assert.Contains(t, promInstance.Ports, topo.GetMonitoredOptions().NodeExporterPort)
assert.Contains(t, promInstance.Ports, topo.GetMonitoredOptions().BlackboxExporterPort)
user, err := user.Current()
assert.Nil(t, err)
e, err := executor.New(executor.SSHTypeNone, false, executor.SSHConfig{Host: "127.0.0.1", User: user.Username})
assert.Nil(t, err)
ctx := checkpoint.NewContext(context.Background())
err = promInstance.initRules(ctx, e, promInstance.InstanceSpec.(*PrometheusSpec), meta.DirPaths{Deploy: deployDir}, "dummy-cluster")
assert.Nil(t, err)
assert.FileExists(t, path.Join(deployDir, "conf", "dummy.rules.yml"))
fs, err := os.ReadDir(localDir)
assert.Nil(t, err)
for _, f := range fs {
assert.FileExists(t, path.Join(deployDir, "conf", f.Name()))
}
}
func TestNoLocalRuleDirs(t *testing.T) {
deployDir, err := os.MkdirTemp("", "tiup-*")
assert.Nil(t, err)
defer os.RemoveAll(deployDir)
err = utils.MkdirAll(path.Join(deployDir, "bin/prometheus"), 0755)
assert.Nil(t, err)
localDir, err := filepath.Abs("./testdata/rules")
assert.Nil(t, err)
err = os.WriteFile(path.Join(deployDir, "bin/prometheus", "dummy.rules.yml"), []byte(`
groups:
- name: alert.rules
rules:
- alert: TiDB_schema_error
expr: increase(tidb_session_schema_lease_error_total{type="outdated"}[15m]) > 0
for: 1m
labels:
env: ENV_LABELS_ENV
level: emergency
expr: increase(tidb_session_schema_lease_error_total{type="outdated"}[15m]) > 0
annotations:
description: "cluster: ENV_LABELS_ENV, instance: {{ $labels.instance }}, values:{{ $value }}"
value: "{{ $value }}"
summary: TiDB schema error
`), 0644)
assert.Nil(t, err)
topo := new(Specification)
topo.Monitors = append(topo.Monitors, &PrometheusSpec{
Host: "127.0.0.1",
Port: 9090,
})
comp := MonitorComponent{topo}
ints := comp.Instances()
assert.Equal(t, len(ints), 1)
promInstance := ints[0].(*MonitorInstance)
user, err := user.Current()
assert.Nil(t, err)
e, err := executor.New(executor.SSHTypeNone, false, executor.SSHConfig{Host: "127.0.0.1", User: user.Username})
assert.Nil(t, err)
ctx := checkpoint.NewContext(context.Background())
err = promInstance.initRules(ctx, e, promInstance.InstanceSpec.(*PrometheusSpec), meta.DirPaths{Deploy: deployDir}, "dummy-cluster")
assert.Nil(t, err)
body, err := os.ReadFile(path.Join(deployDir, "conf", "dummy.rules.yml"))
assert.Nil(t, err)
assert.Contains(t, string(body), "dummy-cluster")
assert.NotContains(t, string(body), "ENV_LABELS_ENV")
assert.FileExists(t, path.Join(deployDir, "conf", "dummy.rules.yml"))
fs, err := os.ReadDir(localDir)
assert.Nil(t, err)
for _, f := range fs {
assert.NoFileExists(t, path.Join(deployDir, "conf", f.Name()))
}
}
func TestMergeAdditionalScrapeConf(t *testing.T) {
file, err := os.CreateTemp("", "tiup-cluster-spec-test")
if err != nil {
panic(fmt.Sprintf("create temp file: %s", err))
}
defer os.Remove(file.Name())
_, err = file.WriteString(`---
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
evaluation_interval: 15s # By default, scrape targets every 15 seconds.
# scrape_timeout is set to the global default (10s).
external_labels:
cluster: 'test'
monitor: "prometheus"
scrape_configs:
- job_name: "tidb"
honor_labels: true # don't overwrite job & instance labels
static_configs:
- targets:
- '192.168.122.215:10080'
- job_name: "tikv"
honor_labels: true # don't overwrite job & instance labels
static_configs:
- targets:
- '192.168.122.25:20180'`)
assert.Nil(t, err)
expected := `global:
evaluation_interval: 15s
external_labels:
cluster: test
monitor: prometheus
scrape_interval: 15s
scrape_configs:
- honor_labels: true
job_name: tidb
metric_relabel_configs:
- action: drop
regex: tikv_thread_nonvoluntary_context_switches|tikv_thread_voluntary_context_switches|tikv_threads_io_bytes_total
separator: ;
source_labels:
- __name__
- action: drop
regex: tikv_thread_cpu_seconds_total;(tokio|rocksdb).+
separator: ;
source_labels:
- __name__
- name
static_configs:
- targets:
- 192.168.122.215:10080
- honor_labels: true
job_name: tikv
metric_relabel_configs:
- action: drop
regex: tikv_thread_nonvoluntary_context_switches|tikv_thread_voluntary_context_switches|tikv_threads_io_bytes_total
separator: ;
source_labels:
- __name__
- action: drop
regex: tikv_thread_cpu_seconds_total;(tokio|rocksdb).+
separator: ;
source_labels:
- __name__
- name
static_configs:
- targets:
- 192.168.122.25:20180
`
var addition map[string]any
err = yaml.Unmarshal([]byte(`metric_relabel_configs:
- source_labels: [__name__]
separator: ;
regex: tikv_thread_nonvoluntary_context_switches|tikv_thread_voluntary_context_switches|tikv_threads_io_bytes_total
action: drop
- source_labels: [__name__,name]
separator: ;
regex: tikv_thread_cpu_seconds_total;(tokio|rocksdb).+
action: drop`), &addition)
assert.Nil(t, err)
err = mergeAdditionalScrapeConf(file.Name(), addition)
assert.Nil(t, err)
result, err := os.ReadFile(file.Name())
assert.Nil(t, err)
assert.Equal(t, expected, string(result))
}
func TestGetRetention(t *testing.T) {
var val string
val = getRetention("-1d")
assert.EqualValues(t, "30d", val)
val = getRetention("0d")
assert.EqualValues(t, "30d", val)
val = getRetention("01d")
assert.EqualValues(t, "30d", val)
val = getRetention("1dd")
assert.EqualValues(t, "30d", val)
val = getRetention("*1d")
assert.EqualValues(t, "30d", val)
val = getRetention("1d ")
assert.EqualValues(t, "30d", val)
val = getRetention("ddd")
assert.EqualValues(t, "30d", val)
val = getRetention("60d")
assert.EqualValues(t, "60d", val)
val = getRetention("999d")
assert.EqualValues(t, "999d", val)
}
// TestHandleRemoteWrite verifies that remote write configurations are properly handled
func TestHandleRemoteWrite(t *testing.T) {
// Create spec and monitoring instances
spec := &PrometheusSpec{
Host: "192.168.1.10",
Port: 9090,
PromRemoteWriteToVM: true,
}
monitoring := &PrometheusSpec{
Host: "192.168.1.20",
NgPort: 12020,
}
// Set up expected remote write URL
expectedURL := fmt.Sprintf("http://%s/api/v1/write", utils.JoinHostPort(monitoring.Host, monitoring.NgPort))
monitorInstance := &MonitorInstance{
BaseInstance: BaseInstance{
InstanceSpec: spec,
Host: spec.Host,
Port: spec.Port,
SSHP: 22,
},
}
// Execute handleRemoteWrite
monitorInstance.handleRemoteWrite(spec, monitoring)
// Check remote write config was added
assert.Len(t, spec.RemoteConfig.RemoteWrite, 1)
assert.Equal(t, expectedURL, spec.RemoteConfig.RemoteWrite[0]["url"])
// Add the same remote write URL again
monitorInstance.handleRemoteWrite(spec, monitoring)
// Check that no duplicate remote write config was added
assert.Len(t, spec.RemoteConfig.RemoteWrite, 1)
assert.Equal(t, expectedURL, spec.RemoteConfig.RemoteWrite[0]["url"])
}
// TestPromRemoteWriteToVM tests remote write configuration
func TestPromRemoteWriteToVM(t *testing.T) {
// Create a PrometheusSpec with PromRemoteWriteToVM
spec := PrometheusSpec{
Host: "127.0.0.1",
Port: 9090,
PromRemoteWriteToVM: true,
}
// Validate field is accessible
assert.True(t, spec.PromRemoteWriteToVM)
// Test setting field
spec.PromRemoteWriteToVM = false
assert.False(t, spec.PromRemoteWriteToVM)
}
// TestVMRemoteWriteYAMLBackwardsCompatibility tests loading YAML with and without PromRemoteWriteToVM field
func TestVMRemoteWriteYAMLBackwardsCompatibility(t *testing.T) {
// Old YAML without PromRemoteWriteToVM
oldYAML := `
host: 127.0.0.1
port: 9090
ng_port: 12020
`
// New YAML with PromRemoteWriteToVM
newYAML := `
host: 127.0.0.1
port: 9090
ng_port: 12020
prom_remote_write_to_vm: true
`
// Test unmarshaling old YAML
var oldSpec PrometheusSpec
err := yaml.Unmarshal([]byte(oldYAML), &oldSpec)
assert.NoError(t, err)
// Default value should be false
assert.False(t, oldSpec.PromRemoteWriteToVM)
// Test unmarshaling new YAML
var newSpec PrometheusSpec
err = yaml.Unmarshal([]byte(newYAML), &newSpec)
assert.NoError(t, err)
// New value should match what's in the YAML
assert.True(t, newSpec.PromRemoteWriteToVM)
}
// TestHandleRemoteWriteDisabled tests that VM remote write configuration is removed when PromRemoteWriteToVM is false
func TestHandleRemoteWriteDisabled(t *testing.T) {
// Create spec with existing remote write config and PromRemoteWriteToVM=false
spec := &PrometheusSpec{
Host: "192.168.1.10",
Port: 9090,
PromRemoteWriteToVM: false,
}
monitoring := &PrometheusSpec{
Host: "192.168.1.20",
NgPort: 12020,
}
// Add VM remote write URL
vmURL := fmt.Sprintf("http://%s/api/v1/write", utils.JoinHostPort(monitoring.Host, monitoring.NgPort))
spec.RemoteConfig.RemoteWrite = []map[string]any{
{"url": vmURL},
}
// Add another remote write URL that should be preserved
otherURL := "http://some-other-target:9090/api/v1/write"
spec.RemoteConfig.RemoteWrite = append(spec.RemoteConfig.RemoteWrite, map[string]any{
"url": otherURL,
})
monitorInstance := &MonitorInstance{
BaseInstance: BaseInstance{
InstanceSpec: spec,
Host: spec.Host,
Port: spec.Port,
SSHP: 22,
},
}
// Execute handleRemoteWrite with PromRemoteWriteToVM=false
monitorInstance.handleRemoteWrite(spec, monitoring)
// Check that VM remote write config was removed but other config was preserved
assert.Len(t, spec.RemoteConfig.RemoteWrite, 1)
assert.Equal(t, otherURL, spec.RemoteConfig.RemoteWrite[0]["url"])
// Test with no remote write configs
spec.RemoteConfig.RemoteWrite = nil
monitorInstance.handleRemoteWrite(spec, monitoring)
// No remote write configs should still be nil/empty
assert.Empty(t, spec.RemoteConfig.RemoteWrite)
// Now test with PromRemoteWriteToVM toggled back to true
spec.PromRemoteWriteToVM = true
monitorInstance.handleRemoteWrite(spec, monitoring)
// VM remote write config should be added back
assert.Len(t, spec.RemoteConfig.RemoteWrite, 1)
assert.Equal(t, vmURL, spec.RemoteConfig.RemoteWrite[0]["url"])
}
|