Skip to content

Commit

Permalink
feat(cluster/spec): add prometheus testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
jsvisa committed Jan 16, 2021
1 parent 4cc9898 commit d6e46f8
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
70 changes: 70 additions & 0 deletions pkg/cluster/spec/prometheus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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 (
"io/ioutil"
"os"
"os/user"
"path"
"path/filepath"
"testing"

"github.com/pingcap/tiup/pkg/cluster/executor"
"github.com/pingcap/tiup/pkg/meta"
"github.com/stretchr/testify/assert"
)

func TestLocalRuleDirs(t *testing.T) {
deployDir, err := ioutil.TempDir("", "tiup-*")
assert.Nil(t, err)
defer os.RemoveAll(deployDir)
// the dashboard json files are under the bin dir,
// which is needed to copy into the dashboard dir
err = os.MkdirAll(path.Join(deployDir, "bin/prometheus"), 0755)
assert.Nil(t, err)
localDir, err := filepath.Abs("./testdata/rules")
assert.Nil(t, err)

err = ioutil.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)

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)

err = promInstance.initRules(e, promInstance.InstanceSpec.(PrometheusSpec), meta.DirPaths{Deploy: deployDir})
assert.Nil(t, err)

assert.FileExists(t, path.Join(deployDir, "conf", "dummy.rules.yml"))
fs, err := ioutil.ReadDir(localDir)
assert.Nil(t, err)
for _, f := range fs {
assert.FileExists(t, path.Join(deployDir, "conf", f.Name()))
}
}
15 changes: 15 additions & 0 deletions pkg/cluster/spec/testdata/rules/tidb.rules.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# magic-string-for-test
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

0 comments on commit d6e46f8

Please sign in to comment.