2022-03-23 21:02:21 +00:00
|
|
|
// Copyright 2022 DigitalOcean
|
2016-01-06 18:24:20 +00:00
|
|
|
//
|
|
|
|
// 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,
|
|
|
|
// 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.
|
|
|
|
|
2022-02-23 23:43:46 +00:00
|
|
|
package ceph
|
2016-01-06 18:24:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2020-10-28 18:42:52 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
2016-01-06 18:24:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestClusterUsage(t *testing.T) {
|
2016-01-22 04:21:39 +00:00
|
|
|
for _, tt := range []struct {
|
|
|
|
input string
|
2023-02-14 19:17:51 +00:00
|
|
|
version string
|
2016-01-22 04:21:39 +00:00
|
|
|
reMatch, reUnmatch []*regexp.Regexp
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: `
|
2016-01-06 18:24:20 +00:00
|
|
|
{
|
|
|
|
"stats": {
|
|
|
|
"total_bytes": 10,
|
|
|
|
"total_used_bytes": 6,
|
2019-12-03 22:03:22 +00:00
|
|
|
"total_avail_bytes": 4
|
2016-01-06 18:24:20 +00:00
|
|
|
}
|
2016-01-22 04:21:39 +00:00
|
|
|
}`,
|
2023-02-14 19:17:51 +00:00
|
|
|
version: `{"version":"ceph version 16.2.11-22-wasd (1984a8c33225d70559cdf27dbab81e3ce153f6ac) pacific (stable)"}`,
|
2016-01-22 04:21:39 +00:00
|
|
|
reMatch: []*regexp.Regexp{
|
2017-03-23 20:20:25 +00:00
|
|
|
regexp.MustCompile(`ceph_cluster_capacity_bytes{cluster="ceph"} 10`),
|
|
|
|
regexp.MustCompile(`ceph_cluster_used_bytes{cluster="ceph"} 6`),
|
|
|
|
regexp.MustCompile(`ceph_cluster_available_bytes{cluster="ceph"} 4`),
|
2016-01-22 04:21:39 +00:00
|
|
|
},
|
|
|
|
reUnmatch: []*regexp.Regexp{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `
|
|
|
|
{
|
|
|
|
"stats": {
|
|
|
|
"total_used_bytes": 6,
|
2019-12-03 22:03:22 +00:00
|
|
|
"total_avail_bytes": 4
|
2016-01-22 04:21:39 +00:00
|
|
|
}
|
|
|
|
}`,
|
2023-02-14 19:17:51 +00:00
|
|
|
version: `{"version":"ceph version 16.2.11-98-wasd (1984a8c33225d70559cdf27dbab81e3ce153f6ac) pacific (stable)"}`,
|
2016-01-22 04:21:39 +00:00
|
|
|
reMatch: []*regexp.Regexp{
|
2017-03-23 20:20:25 +00:00
|
|
|
regexp.MustCompile(`ceph_cluster_capacity_bytes{cluster="ceph"} 0`),
|
|
|
|
regexp.MustCompile(`ceph_cluster_used_bytes{cluster="ceph"} 6`),
|
|
|
|
regexp.MustCompile(`ceph_cluster_available_bytes{cluster="ceph"} 4`),
|
2016-01-22 04:21:39 +00:00
|
|
|
},
|
|
|
|
reUnmatch: []*regexp.Regexp{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `
|
|
|
|
{
|
|
|
|
"stats": {
|
|
|
|
"total_bytes": 10,
|
2019-12-03 22:03:22 +00:00
|
|
|
"total_avail_bytes": 4
|
2016-01-22 04:21:39 +00:00
|
|
|
}
|
|
|
|
}`,
|
2023-02-14 19:17:51 +00:00
|
|
|
version: `{"version":"ceph version 16.2.11-22-wasd (1984a8c33225d70559cdf27dbab81e3ce153f6ac) pacific (stable)"}`,
|
2016-01-22 04:21:39 +00:00
|
|
|
reMatch: []*regexp.Regexp{
|
2017-03-23 20:20:25 +00:00
|
|
|
regexp.MustCompile(`ceph_cluster_capacity_bytes{cluster="ceph"} 10`),
|
|
|
|
regexp.MustCompile(`ceph_cluster_used_bytes{cluster="ceph"} 0`),
|
|
|
|
regexp.MustCompile(`ceph_cluster_available_bytes{cluster="ceph"} 4`),
|
2016-01-22 04:21:39 +00:00
|
|
|
},
|
|
|
|
reUnmatch: []*regexp.Regexp{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `
|
|
|
|
{
|
|
|
|
"stats": {
|
|
|
|
"total_bytes": 10,
|
2019-12-03 22:03:22 +00:00
|
|
|
"total_used_bytes": 6
|
2016-01-06 18:24:20 +00:00
|
|
|
}
|
2016-01-22 04:21:39 +00:00
|
|
|
}`,
|
2023-02-14 19:17:51 +00:00
|
|
|
version: `{"version":"ceph version 16.2.11-22-wasd (1984a8c33225d70559cdf27dbab81e3ce153f6ac) pacific (stable)"}`,
|
2016-01-22 04:21:39 +00:00
|
|
|
reMatch: []*regexp.Regexp{
|
2017-03-23 20:20:25 +00:00
|
|
|
regexp.MustCompile(`ceph_cluster_capacity_bytes{cluster="ceph"} 10`),
|
|
|
|
regexp.MustCompile(`ceph_cluster_used_bytes{cluster="ceph"} 6`),
|
|
|
|
regexp.MustCompile(`ceph_cluster_available_bytes{cluster="ceph"} 0`),
|
2016-01-22 04:21:39 +00:00
|
|
|
},
|
|
|
|
reUnmatch: []*regexp.Regexp{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `
|
|
|
|
{
|
|
|
|
"stats": {
|
|
|
|
"total_bytes": 10,
|
|
|
|
"total_used_bytes": 6,
|
|
|
|
"total_avail_bytes": 4
|
|
|
|
}
|
|
|
|
}`,
|
2023-02-14 19:17:51 +00:00
|
|
|
version: `{"version":"ceph version 16.2.11-22-wasd (1984a8c33225d70559cdf27dbab81e3ce153f6ac) pacific (stable)"}`,
|
2016-01-22 04:21:39 +00:00
|
|
|
reMatch: []*regexp.Regexp{
|
2017-03-23 20:20:25 +00:00
|
|
|
regexp.MustCompile(`ceph_cluster_capacity_bytes{cluster="ceph"} 10`),
|
|
|
|
regexp.MustCompile(`ceph_cluster_used_bytes{cluster="ceph"} 6`),
|
|
|
|
regexp.MustCompile(`ceph_cluster_available_bytes{cluster="ceph"} 4`),
|
2016-01-22 04:21:39 +00:00
|
|
|
},
|
|
|
|
reUnmatch: []*regexp.Regexp{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `
|
|
|
|
{
|
|
|
|
"stats": {{{
|
|
|
|
"total_bytes": 10,
|
|
|
|
"total_used_bytes": 6,
|
2019-12-03 22:03:22 +00:00
|
|
|
"total_avail_bytes": 4
|
2016-01-22 04:21:39 +00:00
|
|
|
}
|
|
|
|
}`,
|
2023-02-14 19:17:51 +00:00
|
|
|
version: `{"version":"ceph version 16.2.11-22-wasd (1984a8c33225d70559cdf27dbab81e3ce153f6ac) pacific (stable)"}`,
|
2016-01-22 04:21:39 +00:00
|
|
|
reMatch: []*regexp.Regexp{},
|
|
|
|
reUnmatch: []*regexp.Regexp{
|
2017-03-23 20:20:25 +00:00
|
|
|
regexp.MustCompile(`ceph_cluster_capacity_bytes{cluster="ceph"}`),
|
|
|
|
regexp.MustCompile(`ceph_cluster_used_bytes{cluster="ceph"}`),
|
|
|
|
regexp.MustCompile(`ceph_cluster_available_bytes{cluster="ceph"}`),
|
2016-01-22 04:21:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
func() {
|
2023-02-14 19:36:09 +00:00
|
|
|
conn := setupVersionMocks(tt.version, "{}")
|
2020-10-28 18:42:52 +00:00
|
|
|
conn.On("MonCommand", mock.Anything).Return(
|
|
|
|
[]byte(tt.input), "", nil,
|
|
|
|
)
|
|
|
|
|
2023-02-14 19:17:51 +00:00
|
|
|
e := &Exporter{Conn: conn, Cluster: "ceph", Logger: logrus.New()}
|
2023-02-14 20:24:34 +00:00
|
|
|
e.cc = map[string]versionedCollector{
|
2023-02-14 19:17:51 +00:00
|
|
|
"clusterUsage": NewClusterUsageCollector(e),
|
|
|
|
}
|
|
|
|
err := prometheus.Register(e)
|
2020-10-28 18:42:52 +00:00
|
|
|
require.NoError(t, err)
|
2023-02-14 19:17:51 +00:00
|
|
|
defer prometheus.Unregister(e)
|
2016-01-06 18:24:20 +00:00
|
|
|
|
2020-10-28 18:42:52 +00:00
|
|
|
server := httptest.NewServer(promhttp.Handler())
|
2016-01-22 04:21:39 +00:00
|
|
|
defer server.Close()
|
2016-01-06 18:24:20 +00:00
|
|
|
|
2016-01-22 04:21:39 +00:00
|
|
|
resp, err := http.Get(server.URL)
|
2020-10-28 18:42:52 +00:00
|
|
|
require.NoError(t, err)
|
2016-01-22 04:21:39 +00:00
|
|
|
defer resp.Body.Close()
|
2016-01-06 18:24:20 +00:00
|
|
|
|
2016-01-22 04:21:39 +00:00
|
|
|
buf, err := ioutil.ReadAll(resp.Body)
|
2020-10-28 18:42:52 +00:00
|
|
|
require.NoError(t, err)
|
2016-01-06 18:24:20 +00:00
|
|
|
|
2016-01-22 04:21:39 +00:00
|
|
|
for _, re := range tt.reMatch {
|
2020-10-28 18:42:52 +00:00
|
|
|
require.True(t, re.Match(buf))
|
2016-01-22 04:21:39 +00:00
|
|
|
}
|
|
|
|
for _, re := range tt.reUnmatch {
|
2020-10-28 18:42:52 +00:00
|
|
|
require.False(t, re.Match(buf))
|
2016-01-22 04:21:39 +00:00
|
|
|
}
|
|
|
|
}()
|
2016-01-06 18:24:20 +00:00
|
|
|
}
|
|
|
|
}
|