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

     1  package poller
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	agentcache "github.com/Axway/agent-sdk/pkg/agent/cache"
     8  	"github.com/Axway/agent-sdk/pkg/agent/events"
     9  	apiv1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
    10  	management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
    11  	"github.com/Axway/agent-sdk/pkg/config"
    12  	hc "github.com/Axway/agent-sdk/pkg/util/healthcheck"
    13  	"github.com/Axway/agent-sdk/pkg/watchmanager/proto"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  var cfg = &config.CentralConfiguration{
    18  	AgentType:     1,
    19  	TenantID:      "12345",
    20  	Environment:   "stream-test",
    21  	EnvironmentID: "123",
    22  	AgentName:     "discoveryagents",
    23  	URL:           "http://abc.com",
    24  	TLS:           &config.TLSConfiguration{},
    25  	SingleURL:     "https://abc.com",
    26  	PollInterval:  1 * time.Second,
    27  }
    28  
    29  func TestPollClientStart(t *testing.T) {
    30  	wt := management.NewWatchTopic("mocktopic")
    31  	ri, _ := wt.AsInstance()
    32  	httpClient := &mockAPIClient{
    33  		ri: ri,
    34  	}
    35  
    36  	mockH := &mockHarvester{
    37  		readyCh: make(chan struct{}),
    38  	}
    39  
    40  	cacheManager := agentcache.NewAgentCacheManager(cfg, false)
    41  	seq := events.NewSequenceProvider(cacheManager, wt.Name)
    42  	seq.SetSequence(1)
    43  
    44  	pollClient, err := NewPollClient(httpClient, cfg, nil, WithHarvester(mockH, seq, wt.GetSelfLink()))
    45  	assert.NotNil(t, pollClient)
    46  	assert.Nil(t, err)
    47  
    48  	pollClient.newPollManager = func(interval time.Duration, options ...executorOpt) *pollExecutor {
    49  		p := newPollExecutor(cfg.PollInterval, options...)
    50  		p.harvester = mockH
    51  		return p
    52  	}
    53  
    54  	errCh := make(chan error)
    55  	go func() {
    56  		err := pollClient.Start()
    57  		errCh <- err
    58  	}()
    59  
    60  	<-mockH.readyCh
    61  
    62  	// assert the poller is healthy
    63  	assert.Nil(t, pollClient.Status())
    64  	assert.Equal(t, hc.OK, pollClient.Healthcheck("").Result)
    65  
    66  	// should stop the poller and write nil to the error channel
    67  	pollClient.Stop()
    68  
    69  	err = <-errCh
    70  	assert.Nil(t, err)
    71  
    72  	assert.Equal(t, hc.FAIL, pollClient.Healthcheck("").Result)
    73  	assert.NotNil(t, pollClient.Status())
    74  	pollClient.poller = nil
    75  	pollClient.listener = nil
    76  }
    77  
    78  type mockAPIClient struct {
    79  	ri        *apiv1.ResourceInstance
    80  	getErr    error
    81  	createErr error
    82  	updateErr error
    83  	deleteErr error
    84  }
    85  
    86  func (m mockAPIClient) GetResource(url string) (*apiv1.ResourceInstance, error) {
    87  	return m.ri, m.getErr
    88  }
    89  
    90  func (m mockAPIClient) CreateResourceInstance(_ apiv1.Interface) (*apiv1.ResourceInstance, error) {
    91  	return m.ri, m.createErr
    92  }
    93  
    94  func (m mockAPIClient) UpdateResourceInstance(_ apiv1.Interface) (*apiv1.ResourceInstance, error) {
    95  	return m.ri, m.updateErr
    96  }
    97  
    98  func (m mockAPIClient) DeleteResourceInstance(_ apiv1.Interface) error {
    99  	return m.deleteErr
   100  }
   101  
   102  func (m mockAPIClient) GetAPIV1ResourceInstances(map[string]string, string) ([]*apiv1.ResourceInstance, error) {
   103  	return nil, nil
   104  }
   105  
   106  type mockTokenGetter struct {
   107  	token string
   108  	err   error
   109  }
   110  
   111  func (m *mockTokenGetter) GetToken() (string, error) {
   112  	return m.token, m.err
   113  }
   114  
   115  type mockHarvester struct {
   116  	eventCh chan *proto.Event
   117  	err     error
   118  	readyCh chan struct{}
   119  }
   120  
   121  func (m mockHarvester) EventCatchUp(_ string, _ chan *proto.Event) error {
   122  	return nil
   123  }
   124  
   125  func (m mockHarvester) ReceiveSyncEvents(_ string, _ int64, _ chan *proto.Event) (int64, error) {
   126  	if m.readyCh != nil {
   127  		m.readyCh <- struct{}{}
   128  	}
   129  
   130  	if m.eventCh != nil {
   131  		m.eventCh <- &proto.Event{
   132  			Id: "1",
   133  		}
   134  	}
   135  	return 0, m.err
   136  }
   137  
   138  var watchTopic = &management.WatchTopic{
   139  	ResourceMeta: apiv1.ResourceMeta{},
   140  	Owner:        nil,
   141  	Spec: management.WatchTopicSpec{
   142  		Description: "",
   143  		Filters: []management.WatchTopicSpecFilters{
   144  			{
   145  				Group: "management",
   146  				Kind:  management.APIServiceGVK().Kind,
   147  				Name:  "*",
   148  				Scope: &management.WatchTopicSpecScope{
   149  					Kind: "Environment",
   150  					Name: "mockEnvName",
   151  				},
   152  			},
   153  		},
   154  	},
   155  }