Skip to content

Commit

Permalink
more tests, but a new working thing.
Browse files Browse the repository at this point in the history
  • Loading branch information
codestoke committed Dec 27, 2018
1 parent 7ab2dbe commit c7dd324
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
19 changes: 18 additions & 1 deletion dirstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/codestoke/directory_stat_exporter/cfg"
"net/http"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -65,22 +66,38 @@ func handleMetrics(w http.ResponseWriter, r *http.Request) {
value: int64(getFileCountInDirRecursively(dir.Path)),
recursive: dir.Recursive,
name: dir.Name,
labels: map[string]string{
"dir": dir.Name,
"recursive": strconv.FormatBool(dir.Recursive),
},
}
metricRegister[metricOldestFileTime].metricValues[dir.Path] = metricValue{
value: int64(getOldestAgeInDirRecursively(dir.Path)),
recursive: dir.Recursive,
name: dir.Name,
labels: map[string]string{
"dir": dir.Name,
"recursive": strconv.FormatBool(dir.Recursive),
},
}
} else {
metricRegister[metricFilesInDir].metricValues[dir.Path] = metricValue{
value: int64(getFileCountInDir(dir.Path)),
recursive: dir.Recursive,
name: dir.Name,
labels: map[string]string{
"dir": dir.Name,
"recursive": strconv.FormatBool(dir.Recursive),
},
}
metricRegister[metricOldestFileTime].metricValues[dir.Path] = metricValue{
value: int64(getOldestAgeInDir(dir.Path)),
recursive: dir.Recursive,
name: dir.Name,
labels: map[string]string{
"dir": dir.Name,
"recursive": strconv.FormatBool(dir.Recursive),
},
}
}
}
Expand Down Expand Up @@ -132,6 +149,6 @@ func sprintMetric(ns string, name string, value int64, labels map[string]string)
strLbls += strings.Join(lblArr, ",")
strLbls += "}"
}
str := fmt.Sprintf("%s_%s%s %v", ns, name, strLbls, value)
str := fmt.Sprintf("%s_%s%s %v\n", ns, name, strLbls, value)
return str
}
20 changes: 17 additions & 3 deletions dirstat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,28 @@ import (
)

func TestMetricsWriter(t *testing.T) {
t.Run("each metric must end with a line feed (\\n)", func(t *testing.T) {
namespace := "namespace"
name := "name"
value := 1

txt := sprintMetric(namespace, name, int64(value), nil)

//expected := fmt.Sprintf("%v_%v %v\n", namespace, name, value)
if !strings.HasSuffix(txt, "\n") {
t.Fail()
t.Errorf("the generated code must end in a line feed (\\n)")
}
})

t.Run("should write single metric without label", func(t *testing.T) {
namespace := "dirstat"
name := "name"
value := 1

txt := sprintMetric(namespace, name, int64(value), nil)

expected := fmt.Sprintf("%v_%v %v", namespace, name, value)
expected := fmt.Sprintf("%v_%v %v\n", namespace, name, value)
if txt != expected {
t.Fail()
t.Errorf("the expected text was not retured:\nexpected: %v\nreturned: %v\n", expected, txt)
Expand All @@ -37,7 +51,7 @@ func TestMetricsWriter(t *testing.T) {

txt := sprintMetric(namespace, name, int64(value), lbls)

expected := fmt.Sprintf("%s_%s{%s=\"%s\"} %v", namespace, name, lblKey, lblValue, value)
expected := fmt.Sprintf("%s_%s{%s=\"%s\"} %v\n", namespace, name, lblKey, lblValue, value)
if txt != expected {
t.Fail()
t.Errorf("the expected text was not retured:\nexpected: %v\nreturned: %v\n", expected, txt)
Expand Down Expand Up @@ -67,7 +81,7 @@ func TestMetricsWriter(t *testing.T) {

txt := sprintMetric(namespace, name, int64(value), lbls)

expected := fmt.Sprintf("%s_%s%s %v", namespace, name, lblTxt, value)
expected := fmt.Sprintf("%s_%s%s %v\n", namespace, name, lblTxt, value)
if txt != expected {
t.Fail()
t.Errorf("the expected text was not retured:\nexpected: %v\nreturned: %v\n", expected, txt)
Expand Down

0 comments on commit c7dd324

Please sign in to comment.