github.com/newrelic/newrelic-client-go@v1.1.0/pkg/plugins/plugins_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package plugins
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  
    15  	mock "github.com/newrelic/newrelic-client-go/pkg/testhelpers"
    16  )
    17  
    18  // nolint
    19  func newTestClient(t *testing.T, handler http.Handler) Plugins {
    20  	ts := httptest.NewServer(handler)
    21  	tc := mock.NewTestConfig(t, ts)
    22  
    23  	return New(tc)
    24  }
    25  
    26  // nolint
    27  func newMockResponse(t *testing.T, mockJSONResponse string, statusCode int) Plugins {
    28  	ts := mock.NewMockServer(t, mockJSONResponse, statusCode)
    29  	tc := mock.NewTestConfig(t, ts)
    30  
    31  	return New(tc)
    32  }
    33  
    34  // nolint
    35  func newIntegrationTestClient(t *testing.T) Plugins {
    36  	tc := mock.NewIntegrationTestConfig(t)
    37  
    38  	return New(tc)
    39  }
    40  
    41  var (
    42  	testPluginJSON = `{
    43  		"id": 999,
    44  		"name": "Redis",
    45  		"guid": "net.jondoe.newrelic_redis_plugin",
    46  		"publisher": "Jon Doe",
    47  		"summary_metrics": [
    48  			{
    49  				"id": 123,
    50  				"name": "Connected Clients",
    51  				"metric": "Component/Connection/Clients[connections]",
    52  				"value_function": "average_value",
    53  				"thresholds": {
    54  					"caution": null,
    55  					"critical": null
    56  				}
    57  			},
    58  			{
    59  				"id": 124,
    60  				"name": "Rejected Connections",
    61  				"metric": "Component/ConnectionRate/Rejected[connections]",
    62  				"value_function": "average_value",
    63  				"thresholds": {
    64  					"caution": null,
    65  					"critical": null
    66  				}
    67  			}
    68  		]
    69  	}`
    70  
    71  	testPluginDetailedJSON = `{
    72  		"id": 999,
    73  		"name": "Redis",
    74  		"guid": "net.jondoe.newrelic_redis_plugin",
    75  		"publisher": "Jon Doe",
    76  		"details": {
    77  			"description": "Example description",
    78  			"is_public": null,
    79  			"created_at": "2019-11-22T13:34:57-08:00",
    80  			"updated_at": "2019-11-22T13:34:58-08:00",
    81  			"last_published_at": null,
    82  			"has_unpublished_changes": true,
    83  			"branding_image_url": "https://url.com/path/to/image.jpg",
    84  			"upgraded_at": "2019-11-22T13:34:57-08:00",
    85  			"short_name": "Redis",
    86  			"publisher_about_url": "https://github.com/publisher/newrelic_redis_plugin",
    87  			"publisher_support_url": "https://github.com/publisher/newrelic_redis_plugin/wiki",
    88  			"download_url": "https://github.com/publisher/newrelic_redis_plugin/releases",
    89  			"first_edited_at": null,
    90  			"last_edited_at": null,
    91  			"first_published_at": null,
    92  			"published_version": "1.0.1"
    93  		},
    94  		"summary_metrics": [
    95  			{
    96  				"id": 123,
    97  				"name": "Connected Clients",
    98  				"metric": "Component/Connection/Clients[connections]",
    99  				"value_function": "average_value",
   100  				"thresholds": {
   101  					"caution": null,
   102  					"critical": null
   103  				}
   104  			},
   105  			{
   106  				"id": 124,
   107  				"name": "Rejected Connections",
   108  				"metric": "Component/ConnectionRate/Rejected[connections]",
   109  				"value_function": "average_value",
   110  				"thresholds": {
   111  					"caution": null,
   112  					"critical": null
   113  				}
   114  			}
   115  		]
   116  	}`
   117  
   118  	testPlugin = Plugin{
   119  		ID:        999,
   120  		Name:      "Redis",
   121  		GUID:      "net.jondoe.newrelic_redis_plugin",
   122  		Publisher: "Jon Doe",
   123  		SummaryMetrics: []SummaryMetric{
   124  			{
   125  				ID:            123,
   126  				Name:          "Connected Clients",
   127  				Metric:        "Component/Connection/Clients[connections]",
   128  				ValueFunction: "average_value",
   129  				Thresholds:    MetricThreshold{},
   130  			},
   131  			{
   132  				ID:            124,
   133  				Name:          "Rejected Connections",
   134  				Metric:        "Component/ConnectionRate/Rejected[connections]",
   135  				ValueFunction: "average_value",
   136  				Thresholds:    MetricThreshold{},
   137  			},
   138  		},
   139  	}
   140  
   141  	testPluginDetails = PluginDetails{
   142  		Description:           "Example description",
   143  		IsPublic:              false,
   144  		CreatedAt:             "2019-11-22T13:34:57-08:00",
   145  		UpdatedAt:             "2019-11-22T13:34:58-08:00",
   146  		HasUnpublishedChanges: true,
   147  		BrandingImageURL:      "https://url.com/path/to/image.jpg",
   148  		UpgradedAt:            "2019-11-22T13:34:57-08:00",
   149  		ShortName:             "Redis",
   150  		PublisherAboutURL:     "https://github.com/publisher/newrelic_redis_plugin",
   151  		PublisherSupportURL:   "https://github.com/publisher/newrelic_redis_plugin/wiki",
   152  		DownloadURL:           "https://github.com/publisher/newrelic_redis_plugin/releases",
   153  		PublishedVersion:      "1.0.1",
   154  	}
   155  )
   156  
   157  func TestListPlugins(t *testing.T) {
   158  	t.Parallel()
   159  	responseJSON := fmt.Sprintf(`{"plugins": [%s]}`, testPluginJSON)
   160  	plugins := newMockResponse(t, responseJSON, http.StatusOK)
   161  
   162  	expected := []*Plugin{
   163  		&testPlugin,
   164  	}
   165  
   166  	actual, err := plugins.ListPlugins(nil)
   167  
   168  	assert.NoError(t, err)
   169  	assert.NotNil(t, actual)
   170  	assert.Equal(t, expected, actual)
   171  }
   172  
   173  func TestListPluginsWithParams(t *testing.T) {
   174  	t.Parallel()
   175  	guidFilter := "net.jondoe.newrelic_redis_plugin"
   176  	idsFilter := "999"
   177  
   178  	plugins := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   179  		values := r.URL.Query()
   180  
   181  		guid := values.Get("filter[guid]")
   182  		require.Equal(t, guidFilter, guid)
   183  
   184  		ids := values.Get("filter[ids]")
   185  		require.Equal(t, idsFilter, ids)
   186  
   187  		w.Header().Set("Content-Type", "application/json")
   188  		_, err := w.Write([]byte(fmt.Sprintf(`{"plugins": [%s]}`, testPluginJSON)))
   189  
   190  		require.NoError(t, err)
   191  	}))
   192  
   193  	params := ListPluginsParams{
   194  		GUID: guidFilter,
   195  		IDs:  []int{999},
   196  	}
   197  
   198  	expected := []*Plugin{
   199  		&testPlugin,
   200  	}
   201  
   202  	actual, err := plugins.ListPlugins(&params)
   203  
   204  	assert.NoError(t, err)
   205  	assert.NotNil(t, actual)
   206  	assert.Equal(t, expected, actual)
   207  }
   208  
   209  func TestListPluginsWithDetailedParam(t *testing.T) {
   210  	t.Parallel()
   211  	expectedDetailed := "true"
   212  
   213  	plugins := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   214  		guid := r.URL.Query().Get("detailed")
   215  		require.Equal(t, expectedDetailed, guid)
   216  
   217  		w.Header().Set("Content-Type", "application/json")
   218  		_, err := w.Write([]byte(fmt.Sprintf(`{"plugins": [%s]}`, testPluginDetailedJSON)))
   219  
   220  		require.NoError(t, err)
   221  	}))
   222  
   223  	params := ListPluginsParams{
   224  		Detailed: true,
   225  	}
   226  
   227  	plugin := testPlugin
   228  	plugin.Details = testPluginDetails
   229  
   230  	expected := []*Plugin{
   231  		&plugin,
   232  	}
   233  
   234  	actual, err := plugins.ListPlugins(&params)
   235  
   236  	assert.NoError(t, err)
   237  	assert.NotNil(t, actual)
   238  	assert.Equal(t, expected, actual)
   239  }
   240  
   241  func TestGetPlugin(t *testing.T) {
   242  	t.Parallel()
   243  	responseJSON := fmt.Sprintf(`{"plugin": %s}`, testPluginJSON)
   244  	plugins := newMockResponse(t, responseJSON, http.StatusOK)
   245  
   246  	actual, err := plugins.GetPlugin(999, nil)
   247  
   248  	assert.NoError(t, err)
   249  	assert.NotNil(t, actual)
   250  	assert.Equal(t, &testPlugin, actual)
   251  }
   252  
   253  func TestGetPluginWithParams(t *testing.T) {
   254  	t.Parallel()
   255  	expectedDetailed := "true"
   256  
   257  	plugins := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   258  		detailed := r.URL.Query().Get("detailed")
   259  		require.Equal(t, expectedDetailed, detailed)
   260  
   261  		w.Header().Set("Content-Type", "application/json")
   262  		_, err := w.Write([]byte(fmt.Sprintf(`{"plugin": %s}`, testPluginDetailedJSON)))
   263  
   264  		require.NoError(t, err)
   265  	}))
   266  
   267  	plugin := testPlugin
   268  	plugin.Details = testPluginDetails
   269  
   270  	params := GetPluginParams{
   271  		Detailed: true,
   272  	}
   273  
   274  	actual, err := plugins.GetPlugin(999, &params)
   275  
   276  	assert.NoError(t, err)
   277  	assert.NotNil(t, actual)
   278  	assert.Equal(t, &plugin, actual)
   279  }