github.com/newrelic/newrelic-client-go@v1.1.0/pkg/workloads/workloads_api_integration_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package workloads
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/newrelic/newrelic-client-go/pkg/common"
    14  	mock "github.com/newrelic/newrelic-client-go/pkg/testhelpers"
    15  )
    16  
    17  func TestIntegrationWorkloadCreate(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	testAccountID, err := mock.GetTestAccountID()
    21  	if err != nil {
    22  		t.Skipf("%s", err)
    23  	}
    24  
    25  	client := newIntegrationTestClient(t)
    26  
    27  	// Test vars
    28  	name := "newrelic-client-go-test-workload-" + mock.RandSeq(5)
    29  	workload := WorkloadCreateInput{
    30  		Name: name,
    31  		ScopeAccounts: &WorkloadScopeAccountsInput{
    32  			AccountIDs: []int{testAccountID},
    33  		},
    34  		EntitySearchQueries: []WorkloadEntitySearchQueryInput{
    35  			{
    36  				Query: "(name like 'tf_test' or id = 'tf_test' or domainId = 'tf_test')",
    37  			},
    38  		},
    39  	}
    40  
    41  	created, err := client.WorkloadCreate(testAccountID, workload)
    42  	require.NoError(t, err)
    43  	require.NotNil(t, created)
    44  
    45  	assert.Equal(t, name, created.Name)
    46  	assert.NotEmpty(t, created.GUID)
    47  	assert.Equal(t, testAccountID, created.Account.ID)
    48  	assert.Equal(t, 1, len(created.EntitySearchQueries))
    49  
    50  	// Wait for indexing to catch up
    51  	time.Sleep(30 * time.Second)
    52  
    53  	// Cleanup
    54  	_, err = client.WorkloadDelete(common.EntityGUID(created.GUID))
    55  	require.NoError(t, err)
    56  }
    57  
    58  func TestIntegrationWorkloadDuplicate(t *testing.T) {
    59  	t.Parallel()
    60  
    61  	testAccountID, err := mock.GetTestAccountID()
    62  	if err != nil {
    63  		t.Skipf("%s", err)
    64  	}
    65  
    66  	client := newIntegrationTestClient(t)
    67  
    68  	// Test vars
    69  	name := "newrelic-client-go-test-workload-" + mock.RandSeq(5)
    70  	workload := WorkloadCreateInput{
    71  		Name: name,
    72  		ScopeAccounts: &WorkloadScopeAccountsInput{
    73  			AccountIDs: []int{testAccountID},
    74  		},
    75  		EntitySearchQueries: []WorkloadEntitySearchQueryInput{
    76  			{
    77  				Query: "(name like 'tf_test' or id = 'tf_test' or domainId = 'tf_test')",
    78  			},
    79  		},
    80  	}
    81  
    82  	created, err := client.WorkloadCreate(testAccountID, workload)
    83  	require.NoError(t, err)
    84  	require.NotNil(t, created)
    85  	require.NotEmpty(t, created.GUID)
    86  
    87  	// Wait for indexing to catch up
    88  	time.Sleep(10 * time.Second)
    89  
    90  	dup, err := client.WorkloadDuplicate(testAccountID, created.GUID, WorkloadDuplicateInput{
    91  		Name: name + "-duplicate",
    92  	})
    93  	require.NoError(t, err)
    94  	require.NotNil(t, dup)
    95  	require.NotEmpty(t, dup.GUID)
    96  
    97  	// Wait for indexing to catch up
    98  	time.Sleep(30 * time.Second)
    99  
   100  	// Cleanup
   101  	_, err = client.WorkloadDelete(common.EntityGUID(created.GUID))
   102  	assert.NoError(t, err)
   103  	_, err = client.WorkloadDelete(common.EntityGUID(dup.GUID))
   104  	assert.NoError(t, err)
   105  }
   106  
   107  func TestIntegrationWorkloadUpdate(t *testing.T) {
   108  	t.Parallel()
   109  
   110  	testAccountID, err := mock.GetTestAccountID()
   111  	if err != nil {
   112  		t.Skipf("%s", err)
   113  	}
   114  
   115  	client := newIntegrationTestClient(t)
   116  
   117  	// Test vars
   118  	name := "newrelic-client-go-test-workload-" + mock.RandSeq(5)
   119  	workload := WorkloadCreateInput{
   120  		Name: name,
   121  		ScopeAccounts: &WorkloadScopeAccountsInput{
   122  			AccountIDs: []int{testAccountID},
   123  		},
   124  		EntitySearchQueries: []WorkloadEntitySearchQueryInput{
   125  			{
   126  				Query: "(name like 'tf_test' or id = 'tf_test' or domainId = 'tf_test')",
   127  			},
   128  		},
   129  	}
   130  
   131  	created, err := client.WorkloadCreate(testAccountID, workload)
   132  	require.NoError(t, err)
   133  	require.NotNil(t, created)
   134  	require.NotEmpty(t, created.GUID)
   135  
   136  	// Wait for indexing to catch up
   137  	time.Sleep(30 * time.Second)
   138  
   139  	up, err := client.WorkloadUpdate(created.GUID, WorkloadUpdateInput{
   140  		Name: name + "-updated",
   141  	})
   142  	require.NoError(t, err)
   143  	require.NotNil(t, up)
   144  	require.NotEmpty(t, up.GUID)
   145  
   146  	// Cleanup
   147  	_, err = client.WorkloadDelete(common.EntityGUID(up.GUID))
   148  	assert.NoError(t, err)
   149  }
   150  
   151  func TestIntegrationWorkloadGet(t *testing.T) {
   152  	t.Parallel()
   153  
   154  	testAccountID, err := mock.GetTestAccountID()
   155  	if err != nil {
   156  		t.Skipf("%s", err)
   157  	}
   158  
   159  	client := newIntegrationTestClient(t)
   160  
   161  	// Test vars
   162  	name := "newrelic-client-go-test-workload-" + mock.RandSeq(5)
   163  	workload := WorkloadCreateInput{
   164  		Name: name,
   165  		ScopeAccounts: &WorkloadScopeAccountsInput{
   166  			AccountIDs: []int{testAccountID},
   167  		},
   168  		EntitySearchQueries: []WorkloadEntitySearchQueryInput{
   169  			{
   170  				Query: "(name like 'tf_test' or id = 'tf_test' or domainId = 'tf_test')",
   171  			},
   172  		},
   173  	}
   174  
   175  	created, err := client.WorkloadCreate(testAccountID, workload)
   176  	require.NoError(t, err)
   177  	require.NotNil(t, created)
   178  
   179  	assert.Equal(t, name, created.Name)
   180  	assert.NotEmpty(t, created.GUID)
   181  	assert.Equal(t, testAccountID, created.Account.ID)
   182  	assert.Equal(t, 1, len(created.EntitySearchQueries))
   183  
   184  	// Wait for indexing to catch up
   185  	time.Sleep(30 * time.Second)
   186  
   187  	// Get created workload
   188  	collection, err := client.GetCollection(testAccountID, created.GUID)
   189  	require.NoError(t, err)
   190  	require.NotNil(t, workload)
   191  
   192  	assert.Equal(t, created.Name, collection.Name)
   193  	assert.Equal(t, created.GUID, collection.GUID)
   194  	assert.Equal(t, testAccountID, collection.Account.ID)
   195  
   196  	// Cleanup
   197  	_, err = client.WorkloadDelete(common.EntityGUID(created.GUID))
   198  	require.NoError(t, err)
   199  }
   200  
   201  func newIntegrationTestClient(t *testing.T) Workloads {
   202  	cfg := mock.NewIntegrationTestConfig(t)
   203  	client := New(cfg)
   204  
   205  	return client
   206  }