github.com/newrelic/newrelic-client-go@v1.1.0/pkg/alerts/policy_channels_integration_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package alerts
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	nr "github.com/newrelic/newrelic-client-go/pkg/testhelpers"
    13  )
    14  
    15  func TestIntegrationPolicyChannels(t *testing.T) {
    16  	t.Parallel()
    17  
    18  	var (
    19  		testPolicyNameRandStr = nr.RandSeq(5)
    20  		testIntegrationPolicy = Policy{
    21  			IncidentPreference: "PER_POLICY",
    22  			Name:               fmt.Sprintf("test-alert-policy-%s", testPolicyNameRandStr),
    23  		}
    24  		testIntegrationChannelA = Channel{
    25  			Name: fmt.Sprintf("test-alert-channel-%s", testPolicyNameRandStr),
    26  			Type: "slack",
    27  			Configuration: ChannelConfiguration{
    28  				URL:     "https://example-org.slack.com",
    29  				Channel: testPolicyNameRandStr,
    30  			},
    31  			Links: ChannelLinks{
    32  				PolicyIDs: []int{},
    33  			},
    34  		}
    35  		testIntegrationChannelB = Channel{
    36  			Name: fmt.Sprintf("test-alert-channel-%s", nr.RandSeq(5)),
    37  			Type: "slack",
    38  			Configuration: ChannelConfiguration{
    39  				URL:     "https://example-org.slack.com",
    40  				Channel: nr.RandSeq(5),
    41  			},
    42  			Links: ChannelLinks{
    43  				PolicyIDs: []int{},
    44  			},
    45  		}
    46  	)
    47  
    48  	client := newIntegrationTestClient(t)
    49  
    50  	// Setup
    51  	policyResp, err := client.CreatePolicy(testIntegrationPolicy)
    52  	policy := *policyResp
    53  
    54  	require.NoError(t, err)
    55  
    56  	channelRespA, err := client.CreateChannel(testIntegrationChannelA)
    57  	channelRespB, err := client.CreateChannel(testIntegrationChannelB)
    58  
    59  	channelA := *channelRespA
    60  	channelB := *channelRespB
    61  
    62  	require.NoError(t, err)
    63  
    64  	// Teardown
    65  	defer func() {
    66  		_, err = client.DeletePolicy(policy.ID)
    67  		if err != nil {
    68  			t.Logf("Error cleaning up alert policy %d (%s): %s", policy.ID, policy.Name, err)
    69  		}
    70  
    71  		_, err = client.DeleteChannel(channelA.ID)
    72  		if err != nil {
    73  			t.Logf("Error cleaning up alert channel %d (%s): %s", channelA.ID, channelA.Name, err)
    74  		}
    75  
    76  		_, err = client.DeleteChannel(channelB.ID)
    77  		if err != nil {
    78  			t.Logf("Error cleaning up alert channel %d (%s): %s", channelB.ID, channelB.Name, err)
    79  		}
    80  	}()
    81  
    82  	// Test: Update
    83  	updateResult, err := client.UpdatePolicyChannels(policy.ID, []int{channelA.ID, channelB.ID})
    84  
    85  	require.NoError(t, err)
    86  	require.NotNil(t, updateResult)
    87  	require.Greater(t, len(updateResult.ChannelIDs), 1)
    88  
    89  	// Test: Delete
    90  	deleteResultA, err := client.DeletePolicyChannel(policy.ID, updateResult.ChannelIDs[0])
    91  
    92  	require.NoError(t, err)
    93  	require.NotNil(t, deleteResultA)
    94  
    95  	deleteResultB, err := client.DeletePolicyChannel(policy.ID, updateResult.ChannelIDs[1])
    96  
    97  	require.NoError(t, err)
    98  	require.NotNil(t, deleteResultB)
    99  }