github.com/netdata/go.d.plugin@v0.58.1/modules/springboot2/springboot2_test.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package springboot2
     4  
     5  import (
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  var (
    15  	testdata, _  = os.ReadFile("tests/testdata.txt")
    16  	testdata2, _ = os.ReadFile("tests/testdata2.txt")
    17  )
    18  
    19  func TestSpringboot2_Collect(t *testing.T) {
    20  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    21  		switch r.URL.Path {
    22  		case "/actuator/prometheus":
    23  			_, _ = w.Write(testdata)
    24  		case "/actuator/prometheus2":
    25  			_, _ = w.Write(testdata2)
    26  		}
    27  	}))
    28  	defer ts.Close()
    29  	job1 := New()
    30  	job1.HTTP.Request.URL = ts.URL + "/actuator/prometheus"
    31  	assert.True(t, job1.Init())
    32  	assert.True(t, job1.Check())
    33  	assert.EqualValues(
    34  		t,
    35  		map[string]int64{
    36  			"threads":                 23,
    37  			"threads_daemon":          21,
    38  			"resp_1xx":                1,
    39  			"resp_2xx":                19,
    40  			"resp_3xx":                1,
    41  			"resp_4xx":                4,
    42  			"resp_5xx":                1,
    43  			"heap_used_eden":          129649936,
    44  			"heap_used_survivor":      8900136,
    45  			"heap_used_old":           17827920,
    46  			"heap_committed_eden":     153616384,
    47  			"heap_committed_survivor": 8912896,
    48  			"heap_committed_old":      40894464,
    49  			"mem_free":                47045752,
    50  			"uptime":                  191730,
    51  		},
    52  		job1.Collect(),
    53  	)
    54  
    55  	job2 := New()
    56  	job2.HTTP.Request.URL = ts.URL + "/actuator/prometheus2"
    57  	assert.True(t, job2.Init())
    58  	assert.True(t, job2.Check())
    59  	assert.EqualValues(
    60  		t,
    61  		map[string]int64{
    62  			"threads":                 36,
    63  			"threads_daemon":          22,
    64  			"resp_1xx":                0,
    65  			"resp_2xx":                57740,
    66  			"resp_3xx":                0,
    67  			"resp_4xx":                4,
    68  			"resp_5xx":                0,
    69  			"heap_used_eden":          18052960,
    70  			"heap_used_survivor":      302704,
    71  			"heap_used_old":           40122672,
    72  			"heap_committed_eden":     21430272,
    73  			"heap_committed_survivor": 2621440,
    74  			"heap_committed_old":      53182464,
    75  			"mem_free":                18755840,
    76  			"uptime":                  45501125,
    77  		},
    78  		job2.Collect(),
    79  	)
    80  }
    81  
    82  func TestSpringboot2_404(t *testing.T) {
    83  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    84  		w.WriteHeader(404)
    85  	}))
    86  	defer ts.Close()
    87  	job := New()
    88  	job.HTTP.Request.URL = ts.URL + "/actuator/prometheus"
    89  
    90  	job.Init()
    91  
    92  	assert.False(t, job.Check())
    93  
    94  	job.Cleanup()
    95  }
    96  
    97  func TestSpringBoot2_Charts(t *testing.T) {
    98  	job := New()
    99  	charts := job.Charts()
   100  
   101  	assert.True(t, charts.Has("response_codes"))
   102  	assert.True(t, charts.Has("uptime"))
   103  }