diff --git a/pkg/cloud/cloud_api_unit_test.go b/pkg/cloud/cloud_api_unit_test.go index 4feb33fc9..828be1f30 100644 --- a/pkg/cloud/cloud_api_unit_test.go +++ b/pkg/cloud/cloud_api_unit_test.go @@ -3,6 +3,7 @@ package cloud import ( "encoding/json" "fmt" + mock "github.com/newrelic/newrelic-client-go/v2/pkg/testhelpers" "github.com/stretchr/testify/assert" "math/rand" "net/http" @@ -22,7 +23,7 @@ var ( "inventoryPollingInterval": null, "metricsPollingInterval": 1200, "name": "Azure Monitor metrics", - "nrAccountId": ` + nrAccountId + `, + "nrAccountId": ` + nrAccountID + `, "resourceGroups": ["resource_groups"], "resourceTypes": ["microsoft.datashare/accounts"], "updatedAt": 1682413262 @@ -37,7 +38,7 @@ var ( "inventoryPollingInterval": null, "metricsPollingInterval": 1200, "name": "Azure Monitor metrics", - "nrAccountId": ` + nrAccountId + `, + "nrAccountId": ` + nrAccountID + `, "resourceGroups": ["resource_groups"], "resourceTypes": ["microsoft.datashare/accounts"], "updatedAt": 1682437515 @@ -60,7 +61,7 @@ var ( } }` linkedAccountID = fmt.Sprintf("%06d", rand.Int63n(1e6)) - nrAccountId = os.Getenv("NEW_RELIC_ACCOUNT_ID") + nrAccountID = os.Getenv("NEW_RELIC_ACCOUNT_ID") ) // Unit Test to test the creation of an Azure Monitor. @@ -68,40 +69,34 @@ var ( func TestUnitCreateAzureMonitor(t *testing.T) { t.Parallel() createAzureMonitorResponse := newMockResponse(t, testCreateAzureMonitor, http.StatusCreated) - linkedAccountIDAsInt, err := strconv.Atoi(linkedAccountID) - var azureMonitorIntegrationRequest CloudAzureMonitorIntegrationInput - azureMonitorIntegrationRequest = CloudAzureMonitorIntegrationInput{ - LinkedAccountId: linkedAccountIDAsInt, - Enabled: true, - ExcludeTags: []string{"env:staging", "env:testing"}, - IncludeTags: []string{"env:production"}, - MetricsPollingInterval: 1200, - ResourceTypes: []string{"microsoft.datashare/accounts"}, - ResourceGroups: []string{"resource_groups"}, - } - - azureMonitorIntegrationInputList := make([]CloudAzureMonitorIntegrationInput, 1) - azureMonitorIntegrationInputList[0] = azureMonitorIntegrationRequest - - cloudAzureIntegration := CloudAzureIntegrationsInput{} - cloudAzureIntegration.AzureMonitor = azureMonitorIntegrationInputList - + linkedAccountIDAsInt, _ := strconv.Atoi(linkedAccountID) createAzureMonitorInput := CloudIntegrationsInput{ - Azure: cloudAzureIntegration, + Azure: CloudAzureIntegrationsInput{ + AzureMonitor: []CloudAzureMonitorIntegrationInput{{ + LinkedAccountId: linkedAccountIDAsInt, + Enabled: true, + ExcludeTags: []string{"env:staging", "env:testing"}, + IncludeTags: []string{"env:production"}, + MetricsPollingInterval: 1200, + ResourceTypes: []string{"microsoft.datashare/accounts"}, + ResourceGroups: []string{"resource_groups"}, + }}, + }, } - if nrAccountId == "" { + if nrAccountID == "" { t.Skipf("Skipping the test case, since no Account ID is available.") } - NRAccountIDInt, err := strconv.Atoi(nrAccountId) + NRAccountIDInt, _ := strconv.Atoi(nrAccountID) actual, err := createAzureMonitorResponse.CloudConfigureIntegration(NRAccountIDInt, createAzureMonitorInput) - responseJSON, err := json.Marshal(actual.Integrations[0]) + responseJSON, _ := json.Marshal(actual.Integrations[0]) responseJSONAsString := string(responseJSON) - objActual, objExpected := unmarshalAzureCloudIntegrationJSON(responseJSONAsString, testCreateAzureMonitorIntegration) + objActual, objExpected, objError := unmarshalAzureCloudIntegrationJSON(responseJSONAsString, testCreateAzureMonitorIntegration) assert.NoError(t, err) + assert.NoError(t, objError) assert.NotNil(t, actual) assert.Equal(t, objActual, objExpected) @@ -111,46 +106,55 @@ func TestUnitCreateAzureMonitor(t *testing.T) { func TestUnitDeleteAzureMonitor(t *testing.T) { t.Parallel() deleteAzureMonitorResponse := newMockResponse(t, testDeleteAzureMonitor, http.StatusCreated) - linkedAccountIDAsInt, err := strconv.Atoi(linkedAccountID) - var azureMonitorIntegrationRequest CloudDisableAccountIntegrationInput - azureMonitorIntegrationRequest = CloudDisableAccountIntegrationInput{ - LinkedAccountId: linkedAccountIDAsInt, - } - - azureMonitorDisableInputList := make([]CloudDisableAccountIntegrationInput, 1) - azureMonitorDisableInputList[0] = azureMonitorIntegrationRequest - - cloudAzureIntegration := CloudAzureDisableIntegrationsInput{} - cloudAzureIntegration.AzureMonitor = azureMonitorDisableInputList + linkedAccountIDAsInt, _ := strconv.Atoi(linkedAccountID) deleteAzureMonitorInput := CloudDisableIntegrationsInput{ - Azure: cloudAzureIntegration, + Azure: CloudAzureDisableIntegrationsInput{ + AzureMonitor: []CloudDisableAccountIntegrationInput{{ + LinkedAccountId: linkedAccountIDAsInt, + }}, + }, } - if nrAccountId == "" { + if nrAccountID == "" { t.Skipf("Skipping the test case, since no Account ID is available.") } - NRAccountIDInt, err := strconv.Atoi(nrAccountId) + NRAccountIDInt, _ := strconv.Atoi(nrAccountID) actual, err := deleteAzureMonitorResponse.CloudDisableIntegration(NRAccountIDInt, deleteAzureMonitorInput) - responseJSON, err := json.Marshal(actual.DisabledIntegrations[0]) + responseJSON, _ := json.Marshal(actual.DisabledIntegrations[0]) responseJSONAsString := string(responseJSON) - objActual, objExpected := unmarshalAzureCloudIntegrationJSON(responseJSONAsString, testDeleteAzureMonitorDisabledIntegration) + objActual, objExpected, objError := unmarshalAzureCloudIntegrationJSON(responseJSONAsString, testDeleteAzureMonitorDisabledIntegration) assert.NoError(t, err) + assert.NoError(t, objError) assert.NotNil(t, actual) assert.Equal(t, objActual, objExpected) } -func unmarshalAzureCloudIntegrationJSON(actualJSONString string, expectedJSONString string) (CloudAzureMonitorIntegration, CloudAzureMonitorIntegration) { +func unmarshalAzureCloudIntegrationJSON(actualJSONString string, expectedJSONString string) (CloudAzureMonitorIntegration, CloudAzureMonitorIntegration, error) { var actual CloudAzureMonitorIntegration - json.Unmarshal([]byte(actualJSONString), &actual) - var expected CloudAzureMonitorIntegration - json.Unmarshal([]byte(expectedJSONString), &expected) - return actual, expected + errActual := json.Unmarshal([]byte(actualJSONString), &actual) + errExpected := json.Unmarshal([]byte(expectedJSONString), &expected) + + if errActual != nil { + return actual, expected, errActual + } + + if errExpected != nil { + return actual, expected, errExpected + } + return actual, expected, nil +} + +func newMockResponse(t *testing.T, mockJSONResponse string, statusCode int) Cloud { + ts := mock.NewMockServer(t, mockJSONResponse, statusCode) + tc := mock.NewTestConfig(t, ts) + + return New(tc) } diff --git a/pkg/cloud/cloud_test.go b/pkg/cloud/cloud_test.go deleted file mode 100644 index 11ffd4cb7..000000000 --- a/pkg/cloud/cloud_test.go +++ /dev/null @@ -1,14 +0,0 @@ -package cloud - -import ( - "testing" - - mock "github.com/newrelic/newrelic-client-go/v2/pkg/testhelpers" -) - -func newMockResponse(t *testing.T, mockJSONResponse string, statusCode int) Cloud { - ts := mock.NewMockServer(t, mockJSONResponse, statusCode) - tc := mock.NewTestConfig(t, ts) - - return New(tc) -}