github.com/jvandenbroek/directory_stat_exporter@v0.2.0/dirstat_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "regexp" 6 "sort" 7 "strconv" 8 "strings" 9 "testing" 10 ) 11 12 func TestMetricsWriter(t *testing.T) { 13 t.Run("each metric must end with a line feed (\\n)", func(t *testing.T) { 14 namespace := "namespace" 15 name := "name" 16 value := 1 17 18 txt := sprintMetric(namespace, name, int64(value), nil) 19 20 //expected := fmt.Sprintf("%v_%v %v\n", namespace, name, value) 21 if !strings.HasSuffix(txt, "\n") { 22 t.Fail() 23 t.Errorf("the generated code must end in a line feed (\\n)") 24 } 25 }) 26 27 t.Run("should write single metric without label", func(t *testing.T) { 28 namespace := "dirstat" 29 name := "name" 30 value := 1 31 32 txt := sprintMetric(namespace, name, int64(value), nil) 33 34 expected := fmt.Sprintf("%v_%v %v\n", namespace, name, value) 35 if txt != expected { 36 t.Fail() 37 t.Errorf("the expected text was not retured:\nexpected: %v\nreturned: %v\n", expected, txt) 38 } 39 }) 40 41 t.Run("should write single metric with a single label", func(t *testing.T) { 42 namespace := "dirstat" 43 name := "name" 44 value := 1 45 46 lblKey := "lbl" 47 lblValue := "lblValue" 48 49 lbls := make(map[string]string, 1) 50 lbls[lblKey] = lblValue 51 52 txt := sprintMetric(namespace, name, int64(value), lbls) 53 54 expected := fmt.Sprintf("%s_%s{%s=\"%s\"} %v\n", namespace, name, lblKey, lblValue, value) 55 if txt != expected { 56 t.Fail() 57 t.Errorf("the expected text was not retured:\nexpected: %v\nreturned: %v\n", expected, txt) 58 } 59 }) 60 61 t.Run("should write single metric with a multiple labels", func(t *testing.T) { 62 namespace := "dirstat" 63 name := "name" 64 value := 1 65 66 lbls := make(map[string]string, 2) 67 lbls["lblKey1"] = "lblValue1" 68 lbls["lblKey2"] = "lblValue2" 69 70 var keys []string 71 for k := range lbls { 72 keys = append(keys, k) 73 } 74 sort.Strings(keys) 75 76 lblSlce := make([]string, 0) 77 for _, k := range keys { 78 lblSlce = append(lblSlce, fmt.Sprintf("%s=\"%s\"", k, lbls[k])) 79 } 80 lblTxt := fmt.Sprintf("{%s}", strings.Join(lblSlce, ",")) 81 82 txt := sprintMetric(namespace, name, int64(value), lbls) 83 84 expected := fmt.Sprintf("%s_%s%s %v\n", namespace, name, lblTxt, value) 85 if txt != expected { 86 t.Fail() 87 t.Errorf("the expected text was not retured:\nexpected: %v\nreturned: %v\n", expected, txt) 88 } 89 }) 90 } 91 92 func TestDirMetric(t *testing.T) { 93 // setup some metrics to test. 94 m := metric{ 95 metricName: "name", 96 metricType: "type", 97 metricValues: map[string]metricValue{ 98 "name": metricValue{ 99 value: 1, 100 labels: map[string]string{ 101 "dir": "dir", 102 "recursive": strconv.FormatBool(false), 103 }, 104 }, 105 }, 106 } 107 108 returned := sprintDirMetric(m) 109 110 t.Run("given a not empty metric when response is generated then the result must not be empty", func(t *testing.T) { 111 if len(returned) == 0 { 112 t.Fail() 113 t.Errorf("the result metric string is empty") 114 } 115 }) 116 117 t.Run("given a not empty metric when response is generated then the result must start with expected value", func(t *testing.T) { 118 expectedStart := fmt.Sprintf("# HELP %[1]s_%[2]s\n# TYPE %[1]s_%[2]s type\n%[1]s_%[2]s", namespace, m.metricName) 119 120 if strings.HasPrefix(returned, expectedStart) { 121 t.Fail() 122 t.Errorf("it must be in the correct format.\n\texpected: %s\n\treturned: %s\n", expectedStart, returned) 123 } 124 }) 125 126 t.Run("given a not empty metric with labels when response is generated then the labels must be in the result string", func(t *testing.T) { 127 r := regexp.MustCompile("{([^}]+)}") 128 129 matches := r.FindAllStringSubmatch(returned, -1) // i only want the first one 130 131 if len(matches) == 0 { 132 t.Fail() 133 t.Errorf("I'd expect some labels.\n%s", returned) 134 } else { 135 // it has labels, now parse and test if they are correct$ 136 labels := strings.Split(matches[0][1], ",") 137 for _, label := range labels { 138 fmt.Println("testing", label) 139 keyValue := strings.Split(label, "=") 140 key := keyValue[0] 141 value := strings.Replace(keyValue[1], "\"", "", -1) 142 143 fmt.Println("key, value", key, value) 144 145 if value != m.metricValues["name"].labels[key] { 146 t.Fail() 147 t.Errorf("the label does not exist or the label does not contain the correct value.\n%s\n", returned) 148 t.Error(m.metricValues["name"].labels) 149 } 150 } 151 } 152 }) 153 }