github.com/Financial-Times/publish-availability-monitor@v1.12.0/metrics/graphite_test.go (about)

     1  package metrics
     2  
     3  import (
     4  	"io"
     5  	"net"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/Financial-Times/go-logger/v2"
    11  	"github.com/Financial-Times/publish-availability-monitor/config"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestGraphiteSend(t *testing.T) {
    17  	tests := map[string]struct {
    18  		AppConfig            *config.AppConfig
    19  		PublishOk            bool
    20  		ExpectedStatusPrefix string
    21  		ExpectedTimePrefix   string
    22  		ExpectTimeout        bool
    23  	}{
    24  		"capability metric success should be sent to graphite": {
    25  			AppConfig: &config.AppConfig{
    26  				MetricConf: []config.MetricConfig{
    27  					{
    28  						Alias: "test-metric",
    29  					},
    30  				},
    31  				Capabilities: []config.Capability{
    32  					{
    33  						Name:        "test-capability",
    34  						MetricAlias: "test-metric",
    35  					},
    36  				},
    37  				GraphiteUUID: "e435d5ef-20da-4c61-928c-71bfbec9fa3e",
    38  				Environment:  "test",
    39  			},
    40  			PublishOk:            true,
    41  			ExpectedStatusPrefix: "e435d5ef-20da-4c61-928c-71bfbec9fa3e.test-capability.test.status 1",
    42  			ExpectedTimePrefix:   "e435d5ef-20da-4c61-928c-71bfbec9fa3e.test-capability.test.time 3",
    43  		},
    44  		"capability metric fail should be sent to graphite": {
    45  			AppConfig: &config.AppConfig{
    46  				MetricConf: []config.MetricConfig{
    47  					{
    48  						Alias: "test-metric",
    49  					},
    50  				},
    51  				Capabilities: []config.Capability{
    52  					{
    53  						Name:        "test-capability",
    54  						MetricAlias: "test-metric",
    55  					},
    56  				},
    57  				GraphiteUUID: "e435d5ef-20da-4c61-928c-71bfbec9fa3e",
    58  				Environment:  "test",
    59  			},
    60  			PublishOk:            false,
    61  			ExpectedStatusPrefix: "e435d5ef-20da-4c61-928c-71bfbec9fa3e.test-capability.test.status 0",
    62  			ExpectedTimePrefix:   "e435d5ef-20da-4c61-928c-71bfbec9fa3e.test-capability.test.time 3",
    63  		},
    64  		"regular metric should not be sent to graphite": {
    65  			AppConfig: &config.AppConfig{
    66  				MetricConf: []config.MetricConfig{
    67  					{
    68  						Alias: "test-metric",
    69  					},
    70  				},
    71  				GraphiteUUID: "e435d5ef-20da-4c61-928c-71bfbec9fa3e",
    72  				Environment:  "test",
    73  			},
    74  			ExpectTimeout: true,
    75  		},
    76  	}
    77  
    78  	log := logger.NewUPPLogger("test", "PANIC")
    79  
    80  	for name, test := range tests {
    81  		t.Run(name, func(t *testing.T) {
    82  			srv, err := net.Listen("tcp", "127.0.0.1:0")
    83  			require.NoError(t, err)
    84  
    85  			defer srv.Close()
    86  
    87  			test.AppConfig.GraphiteAddress = srv.Addr().String()
    88  
    89  			sender := NewGraphiteSender(test.AppConfig, log)
    90  			metricConfig := test.AppConfig.MetricConf[0]
    91  			metric := PublishMetric{
    92  				PublishOK:       test.PublishOk,
    93  				Config:          metricConfig,
    94  				Capability:      test.AppConfig.GetCapability(metricConfig.Alias),
    95  				PublishInterval: Interval{UpperBound: 3},
    96  			}
    97  
    98  			go sender.Send(metric)
    99  
   100  			var resultCh = make(chan []byte)
   101  			var errCh = make(chan error)
   102  			go func() {
   103  				conn, err := srv.Accept()
   104  				if err != nil {
   105  					errCh <- err
   106  					return
   107  				}
   108  				defer conn.Close()
   109  
   110  				buf, err := io.ReadAll(conn)
   111  				if err != nil {
   112  					errCh <- err
   113  					return
   114  				}
   115  				resultCh <- buf
   116  			}()
   117  
   118  			select {
   119  			case result := <-resultCh:
   120  				metrics := strings.FieldsFunc(string(result), func(c rune) bool {
   121  					return c == '\n'
   122  				})
   123  
   124  				statusMetric, timeMetric := metrics[0], metrics[1]
   125  
   126  				assert.True(t, strings.HasPrefix(statusMetric, test.ExpectedStatusPrefix))
   127  				assert.True(t, strings.HasPrefix(timeMetric, test.ExpectedTimePrefix))
   128  			case err := <-errCh:
   129  				t.Fatal(err)
   130  			case <-time.After(1 * time.Second):
   131  				if !test.ExpectTimeout {
   132  					t.Fatalf("test timed out while sending metric")
   133  				}
   134  			}
   135  		})
   136  	}
   137  }