github.com/adevinta/lava@v0.7.2/internal/metrics/metrics_test.go (about)

     1  // Copyright 2023 Adevinta
     2  
     3  package metrics
     4  
     5  import (
     6  	"bytes"
     7  	"encoding/json"
     8  	"os"
     9  	"path"
    10  	"testing"
    11  
    12  	"github.com/google/go-cmp/cmp"
    13  )
    14  
    15  var testdata = []struct {
    16  	name    string
    17  	metrics map[string]any
    18  	want    map[string]any
    19  }{
    20  	{
    21  		name: "happy path",
    22  		metrics: map[string]any{
    23  			"metric 1": "metric value 1",
    24  			"metric 2": 12345,
    25  			"metric 3": 25.5,
    26  			"metric 4": map[string]int{
    27  				"key 1": 1,
    28  				"key 2": 2,
    29  			},
    30  			"metric 5": []string{
    31  				"one", "two", "three",
    32  			},
    33  		},
    34  		want: map[string]any{
    35  			"metric 1": "metric value 1",
    36  			"metric 2": float64(12345),
    37  			"metric 3": 25.5,
    38  			"metric 4": map[string]any{
    39  				"key 1": float64(1),
    40  				"key 2": float64(2),
    41  			},
    42  			"metric 5": []any{
    43  				"one", "two", "three",
    44  			},
    45  		},
    46  	},
    47  }
    48  
    49  func TestWrite(t *testing.T) {
    50  	for _, tt := range testdata {
    51  		t.Run(tt.name, func(t *testing.T) {
    52  			oldDefaultCollector := DefaultCollector
    53  			defer func() { DefaultCollector = oldDefaultCollector }()
    54  
    55  			DefaultCollector = NewCollector()
    56  
    57  			var buf bytes.Buffer
    58  
    59  			for key, value := range tt.metrics {
    60  				Collect(key, value)
    61  			}
    62  
    63  			if err := Write(&buf); err != nil {
    64  				t.Fatalf("error writing metrics: %v", err)
    65  			}
    66  
    67  			var got map[string]any
    68  			if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
    69  				t.Errorf("error decoding JSON metrics: %v", err)
    70  			}
    71  
    72  			if diff := cmp.Diff(tt.want, got); diff != "" {
    73  				t.Errorf("metrics mismatch (-want +got):\n%v", diff)
    74  			}
    75  		})
    76  	}
    77  }
    78  
    79  func TestWriteFile(t *testing.T) {
    80  	for _, tt := range testdata {
    81  		t.Run(tt.name, func(t *testing.T) {
    82  			oldDefaultCollector := DefaultCollector
    83  			defer func() { DefaultCollector = oldDefaultCollector }()
    84  
    85  			DefaultCollector = NewCollector()
    86  
    87  			tmpPath, err := os.MkdirTemp("", "")
    88  			if err != nil {
    89  				t.Fatalf("error creating temp dir: %v", err)
    90  			}
    91  			defer os.RemoveAll(tmpPath)
    92  
    93  			file := path.Join(tmpPath, "metrics.json")
    94  
    95  			for key, value := range tt.metrics {
    96  				Collect(key, value)
    97  			}
    98  
    99  			if err = WriteFile(file); err != nil {
   100  				t.Fatalf("error writing metrics: %v", err)
   101  			}
   102  
   103  			data, err := os.ReadFile(file)
   104  			if err != nil {
   105  				t.Fatalf("error reading metrics file: %v", err)
   106  			}
   107  
   108  			var got map[string]any
   109  			if err := json.Unmarshal(data, &got); err != nil {
   110  				t.Errorf("error decoding JSON metrics: %v", err)
   111  			}
   112  
   113  			if diff := cmp.Diff(tt.want, got); diff != "" {
   114  				t.Errorf("metrics mismatch (-want +got):\n%v", diff)
   115  			}
   116  		})
   117  	}
   118  }