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

     1  //go:build unit
     2  // +build unit
     3  
     4  package alerts
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  var (
    15  	testInfrastructureConditionPolicyId      = 111111
    16  	testInfrastructureCriticalThresholdValue = 12.3
    17  	testInfrastructureCriticalThreshold      = InfrastructureConditionThreshold{
    18  		Duration: 6,
    19  		Function: "all",
    20  		Value:    &testInfrastructureCriticalThresholdValue,
    21  	}
    22  	testInfrastructureWarningThresholdValue = float64(10)
    23  	testInfrastructureWarningThreshold      = InfrastructureConditionThreshold{
    24  		Duration: 6,
    25  		Function: "all",
    26  		Value:    &testInfrastructureWarningThresholdValue,
    27  	}
    28  
    29  	testInfrastructureCondition = InfrastructureCondition{
    30  		Comparison:   "equal",
    31  		CreatedAt:    &testTimestamp,
    32  		Critical:     &testInfrastructureCriticalThreshold,
    33  		Enabled:      true,
    34  		ID:           13890,
    35  		Name:         "Java is running",
    36  		PolicyID:     testInfrastructureConditionPolicyId,
    37  		ProcessWhere: "(commandName = 'java')",
    38  		Type:         "infra_process_running",
    39  		UpdatedAt:    &testTimestamp,
    40  		Warning:      &testInfrastructureWarningThreshold,
    41  		Where:        "(hostname LIKE '%cassandra%')",
    42  	}
    43  	testInfrastructureConditionJson = `
    44  		{
    45  			"type":"infra_process_running",
    46  			"name":"Java is running",
    47  			"enabled":true,
    48  			"where_clause":"(hostname LIKE '%cassandra%')",
    49  			"id":13890,
    50  			"created_at_epoch_millis":` + testTimestampStringMs + `,
    51  			"updated_at_epoch_millis":` + testTimestampStringMs + `,
    52  			"policy_id": 111111,
    53  			"comparison":"equal",
    54  			"critical_threshold":{
    55  				"value":12.3,
    56  				"duration_minutes":6,
    57  				"time_function": "all"
    58  			},
    59  			"warning_threshold": {
    60  				"value": 10,
    61  				"duration_minutes": 6,
    62  				"time_function": "all"
    63  			},
    64  			"process_where_clause":"(commandName = 'java')"
    65  		}`
    66  )
    67  
    68  func TestListInfrastructureConditions(t *testing.T) {
    69  	t.Parallel()
    70  	respJSON := fmt.Sprintf(`{ "data":[%s] }`, testInfrastructureConditionJson)
    71  	alerts := newMockResponse(t, respJSON, http.StatusOK)
    72  
    73  	expected := []InfrastructureCondition{testInfrastructureCondition}
    74  
    75  	actual, err := alerts.ListInfrastructureConditions(testInfrastructureConditionPolicyId)
    76  
    77  	require.NoError(t, err)
    78  	require.NotNil(t, actual)
    79  	require.Equal(t, expected, actual)
    80  }
    81  
    82  func TestGetInfrastructureConditions(t *testing.T) {
    83  	t.Parallel()
    84  	respJSON := fmt.Sprintf(`{ "data":%s }`, testInfrastructureConditionJson)
    85  	alerts := newMockResponse(t, respJSON, http.StatusOK)
    86  
    87  	expected := &testInfrastructureCondition
    88  
    89  	actual, err := alerts.GetInfrastructureCondition(testInfrastructureCondition.ID)
    90  
    91  	require.NoError(t, err)
    92  	require.NotNil(t, actual)
    93  	require.Equal(t, expected, actual)
    94  }
    95  
    96  func TestCreateInfrastructureConditions(t *testing.T) {
    97  	t.Parallel()
    98  	respJSON := fmt.Sprintf(`{ "data":%s }`, testInfrastructureConditionJson)
    99  	alerts := newMockResponse(t, respJSON, http.StatusOK)
   100  
   101  	expected := &testInfrastructureCondition
   102  
   103  	actual, err := alerts.CreateInfrastructureCondition(testInfrastructureCondition)
   104  
   105  	require.NoError(t, err)
   106  	require.NotNil(t, actual)
   107  	require.Equal(t, expected, actual)
   108  }
   109  
   110  func TestUpdateInfrastructureConditions(t *testing.T) {
   111  	t.Parallel()
   112  	respJSON := fmt.Sprintf(`{ "data":%s }`, testInfrastructureConditionJson)
   113  	alerts := newMockResponse(t, respJSON, http.StatusOK)
   114  
   115  	expected := &testInfrastructureCondition
   116  
   117  	actual, err := alerts.UpdateInfrastructureCondition(testInfrastructureCondition)
   118  
   119  	require.NoError(t, err)
   120  	require.NotNil(t, actual)
   121  	require.Equal(t, expected, actual)
   122  }
   123  
   124  func TestDeleteInfrastructureConditions(t *testing.T) {
   125  	t.Parallel()
   126  	respJSON := fmt.Sprintf(`{ "data":%s }`, testInfrastructureConditionJson)
   127  	alerts := newMockResponse(t, respJSON, http.StatusOK)
   128  
   129  	err := alerts.DeleteInfrastructureCondition(testInfrastructureCondition.ID)
   130  
   131  	require.NoError(t, err)
   132  }