github.com/Axway/agent-sdk@v1.1.101/pkg/traceability/healthcheck_test.go (about)

     1  package traceability
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/Axway/agent-sdk/pkg/agent"
     9  	"github.com/Axway/agent-sdk/pkg/apic"
    10  	hc "github.com/Axway/agent-sdk/pkg/util/healthcheck"
    11  	"github.com/elastic/beats/v7/libbeat/publisher"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  type mockTransportClient struct {
    16  	connectErr bool
    17  }
    18  
    19  func (m mockTransportClient) Close() error {
    20  	return nil
    21  }
    22  
    23  func (m mockTransportClient) Publish(context.Context, publisher.Batch) error {
    24  	return nil
    25  }
    26  
    27  func (m mockTransportClient) String() string {
    28  	return ""
    29  }
    30  
    31  func (m mockTransportClient) Connect() error {
    32  	if m.connectErr {
    33  		return fmt.Errorf("error")
    34  	}
    35  	return nil
    36  }
    37  
    38  func TestExecute(t *testing.T) {
    39  	job := newTraceabilityHealthCheckJob()
    40  	// hc not okay
    41  	err := job.Execute()
    42  	assert.NotNil(t, err)
    43  }
    44  
    45  func TestReady(t *testing.T) {
    46  	agent.InitializeForTest(&apic.ServiceClient{})
    47  	job := newTraceabilityHealthCheckJob()
    48  
    49  	// hc not okay
    50  	client := &mockTransportClient{connectErr: true}
    51  	addClient(&Client{transportClient: client})
    52  	ready := job.Ready()
    53  	assert.False(t, ready)
    54  
    55  	// hc okay
    56  	client.connectErr = false
    57  	ready = job.Ready()
    58  	assert.True(t, ready)
    59  }
    60  
    61  func TestStatus(t *testing.T) {
    62  	job := newTraceabilityHealthCheckJob()
    63  
    64  	// no previous errors, status ok
    65  	err := job.Status()
    66  	assert.Nil(t, err)
    67  
    68  	// previous errors, status not ok
    69  	job.prevErr = fmt.Errorf("")
    70  	err = job.Status()
    71  	assert.NotNil(t, err)
    72  }
    73  
    74  func TestHealthCheck(t *testing.T) {
    75  	agent.InitializeForTest(&apic.ServiceClient{})
    76  	job := newTraceabilityHealthCheckJob()
    77  	client := &mockTransportClient{connectErr: true}
    78  	addClient(&Client{transportClient: client})
    79  
    80  	testCases := map[string]struct {
    81  		isReady    bool
    82  		expRes     hc.StatusLevel
    83  		prevErr    error
    84  		expDetails string
    85  	}{
    86  		"success when read and no error": {
    87  			isReady: true,
    88  			expRes:  hc.OK,
    89  		},
    90  		"expect error when not ready": {
    91  			expRes:     hc.FAIL,
    92  			expDetails: "agent not connected to traceability yet",
    93  		},
    94  		"expect error when previous error": {
    95  			expRes:     hc.FAIL,
    96  			isReady:    true,
    97  			prevErr:    fmt.Errorf("error"),
    98  			expDetails: "connection error: name Failed. error",
    99  		},
   100  	}
   101  	for name, tc := range testCases {
   102  		t.Run(name, func(t *testing.T) {
   103  			job.prevErr = tc.prevErr
   104  			job.ready = tc.isReady
   105  
   106  			status := job.healthcheck("name")
   107  			assert.NotNil(t, status)
   108  			assert.Equal(t, tc.expRes, status.Result)
   109  			assert.Equal(t, tc.expDetails, status.Details)
   110  		})
   111  	}
   112  }