Skip to content

Commit

Permalink
feat(cloud): fix to previous commit v3
Browse files Browse the repository at this point in the history
  • Loading branch information
pranav-new-relic committed Apr 26, 2023
1 parent 7f0ee63 commit e8a8cec
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 61 deletions.
98 changes: 51 additions & 47 deletions pkg/cloud/cloud_api_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -60,48 +61,42 @@ 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.
// Applies to update too, as create and update use the same mutation 'CloudConfigureIntegration'.
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)

Expand All @@ -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)
}
14 changes: 0 additions & 14 deletions pkg/cloud/cloud_test.go

This file was deleted.

0 comments on commit e8a8cec

Please sign in to comment.