github.com/xmidt-org/webpa-common@v1.11.9/service/consul/datacenterWatch_test.go (about)

     1  package consul
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"sync"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/go-kit/kit/log"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  	"github.com/xmidt-org/argus/chrysom"
    14  	"github.com/xmidt-org/argus/model"
    15  	"github.com/xmidt-org/webpa-common/service"
    16  	"github.com/xmidt-org/webpa-common/xmetrics"
    17  )
    18  
    19  func TestNewDatacenterWatcher(t *testing.T) {
    20  	logger := log.NewNopLogger()
    21  	r, err := xmetrics.NewRegistry(nil, Metrics)
    22  	require.Nil(t, err)
    23  	envShutdownChan := make(<-chan struct{})
    24  
    25  	mockServiceEnvironment := new(service.MockEnvironment)
    26  	mockServiceEnvironment.On("Provider").Return(r, true)
    27  	mockServiceEnvironment.On("Closed").Return(envShutdownChan)
    28  
    29  	noProviderEnv := new(service.MockEnvironment)
    30  	noProviderEnv.On("Provider").Return(nil, false)
    31  
    32  	validChrysomConfig := chrysom.ClientConfig{
    33  		Bucket: "random-bucket",
    34  		Listen: chrysom.ListenerConfig{
    35  			PullInterval: 10 * time.Second,
    36  		},
    37  		Address: "http://argus:6600",
    38  		Auth: chrysom.Auth{
    39  			Basic: "Basic auth",
    40  		},
    41  		Logger: logger,
    42  	}
    43  
    44  	tests := []struct {
    45  		description     string
    46  		logger          log.Logger
    47  		environment     Environment
    48  		options         Options
    49  		ctx             context.Context
    50  		expectedWatcher *datacenterWatcher
    51  		expectedErr     error
    52  	}{
    53  
    54  		{
    55  			description: "Successful Consul Datacenter Watcher",
    56  			logger:      logger,
    57  			environment: environment{
    58  				mockServiceEnvironment, new(mockClient),
    59  			},
    60  			options: Options{
    61  				DatacenterWatchInterval: 10 * time.Second,
    62  			},
    63  			expectedWatcher: &datacenterWatcher{
    64  				logger: logger,
    65  				environment: environment{
    66  					mockServiceEnvironment, new(mockClient),
    67  				},
    68  				options: Options{
    69  					DatacenterWatchInterval: 10 * time.Second,
    70  				},
    71  				inactiveDatacenters: make(map[string]bool),
    72  				consulWatchInterval: 10 * time.Second,
    73  			},
    74  		},
    75  		{
    76  			description: "Empty Chrysom Client Bucket",
    77  			logger:      logger,
    78  			environment: environment{
    79  				mockServiceEnvironment, new(mockClient),
    80  			},
    81  			options: Options{
    82  				Chrysom: chrysom.ClientConfig{
    83  					Bucket: "",
    84  				},
    85  			},
    86  			expectedWatcher: &datacenterWatcher{
    87  				logger: logger,
    88  				environment: environment{
    89  					mockServiceEnvironment, new(mockClient),
    90  				},
    91  				options: Options{
    92  					DatacenterWatchInterval: defaultWatchInterval,
    93  				},
    94  				inactiveDatacenters: make(map[string]bool),
    95  				consulWatchInterval: defaultWatchInterval,
    96  			},
    97  		},
    98  		{
    99  			description: "Successful Chrysom Client",
   100  			logger:      logger,
   101  			environment: environment{
   102  				mockServiceEnvironment, new(mockClient),
   103  			},
   104  			options: Options{
   105  				Chrysom: validChrysomConfig,
   106  			},
   107  			expectedWatcher: &datacenterWatcher{
   108  				logger: logger,
   109  				environment: environment{
   110  					mockServiceEnvironment, new(mockClient),
   111  				},
   112  				options: Options{
   113  					DatacenterWatchInterval: defaultWatchInterval,
   114  					Chrysom:                 validChrysomConfig,
   115  				},
   116  				consulWatchInterval: defaultWatchInterval,
   117  				inactiveDatacenters: make(map[string]bool),
   118  				chrysomClient:       &chrysom.Client{},
   119  			},
   120  		},
   121  		{
   122  			description: "Successful Consul and Chrysom Datacenter Watcher",
   123  			logger:      logger,
   124  			environment: environment{
   125  				mockServiceEnvironment, new(mockClient),
   126  			},
   127  			options: Options{
   128  				DatacenterWatchInterval: 10 * time.Second,
   129  				Chrysom:                 validChrysomConfig,
   130  			},
   131  			expectedWatcher: &datacenterWatcher{
   132  				logger: logger,
   133  				environment: environment{
   134  					mockServiceEnvironment, new(mockClient),
   135  				},
   136  				options: Options{
   137  					DatacenterWatchInterval: 10 * time.Second,
   138  					Chrysom:                 validChrysomConfig,
   139  				},
   140  				inactiveDatacenters: make(map[string]bool),
   141  				consulWatchInterval: 10 * time.Second,
   142  				chrysomClient:       &chrysom.Client{},
   143  			},
   144  		},
   145  		{
   146  			description: "Success with Default Logger",
   147  			environment: environment{
   148  				mockServiceEnvironment, new(mockClient),
   149  			},
   150  			options: Options{
   151  				DatacenterWatchInterval: 10 * time.Second,
   152  			},
   153  			expectedWatcher: &datacenterWatcher{
   154  				logger: defaultLogger,
   155  				environment: environment{
   156  					mockServiceEnvironment, new(mockClient),
   157  				},
   158  				options: Options{
   159  					DatacenterWatchInterval: 10 * time.Second,
   160  				},
   161  				consulWatchInterval: 10 * time.Second,
   162  				inactiveDatacenters: make(map[string]bool),
   163  			},
   164  		},
   165  		{
   166  			description: "Default Consul Watch Interval",
   167  			logger:      logger,
   168  			environment: environment{
   169  				mockServiceEnvironment, new(mockClient),
   170  			},
   171  			options: Options{
   172  				DatacenterWatchInterval: 0,
   173  			},
   174  			expectedWatcher: &datacenterWatcher{
   175  				logger: logger,
   176  				environment: environment{
   177  					mockServiceEnvironment, new(mockClient),
   178  				},
   179  				options: Options{
   180  					DatacenterWatchInterval: defaultWatchInterval,
   181  				},
   182  				consulWatchInterval: defaultWatchInterval,
   183  				inactiveDatacenters: make(map[string]bool),
   184  			},
   185  		},
   186  		{
   187  			description: "No Provider",
   188  			logger:      logger,
   189  			environment: environment{
   190  				noProviderEnv, new(mockClient),
   191  			},
   192  			options: Options{
   193  				Chrysom: validChrysomConfig,
   194  			},
   195  			expectedErr: errors.New("must pass in a metrics provider"),
   196  		},
   197  		{
   198  			description: "Invalid chrysom watcher interval",
   199  			logger:      logger,
   200  			environment: environment{
   201  				mockServiceEnvironment, new(mockClient),
   202  			},
   203  			options: Options{
   204  				Chrysom: chrysom.ClientConfig{
   205  					Bucket: "random-bucket",
   206  					Listen: chrysom.ListenerConfig{
   207  						PullInterval: 0,
   208  					},
   209  					Address: "http://argus:6600",
   210  					Auth: chrysom.Auth{
   211  						Basic: "Basic auth",
   212  					},
   213  					Logger: logger,
   214  				},
   215  			},
   216  			expectedErr: errors.New("chrysom pull interval cannot be 0"),
   217  		},
   218  	}
   219  
   220  	for _, testCase := range tests {
   221  		t.Run(testCase.description, func(t *testing.T) {
   222  			assert := assert.New(t)
   223  			w, err := newDatacenterWatcher(testCase.logger, testCase.environment, testCase.options)
   224  
   225  			if testCase.expectedErr == nil {
   226  				assert.NotNil(w.inactiveDatacenters)
   227  				assert.Equal(testCase.expectedWatcher.consulWatchInterval, w.consulWatchInterval)
   228  
   229  				if testCase.expectedWatcher.chrysomClient != nil {
   230  					assert.NotNil(w.chrysomClient)
   231  					testCase.expectedWatcher.chrysomClient = w.chrysomClient
   232  				}
   233  
   234  				assert.Equal(testCase.expectedWatcher, w)
   235  			} else {
   236  				assert.Equal(testCase.expectedErr, err)
   237  			}
   238  
   239  		})
   240  	}
   241  
   242  }
   243  
   244  func TestUpdateInactiveDatacenters(t *testing.T) {
   245  	logger := log.NewNopLogger()
   246  
   247  	tests := []struct {
   248  		description                 string
   249  		items                       []model.Item
   250  		currentInactiveDatacenters  map[string]bool
   251  		expectedInactiveDatacenters map[string]bool
   252  		lock                        sync.RWMutex
   253  	}{
   254  		{
   255  			description:                 "Empty database results, empty inactive datacenters",
   256  			items:                       []model.Item{},
   257  			currentInactiveDatacenters:  map[string]bool{},
   258  			expectedInactiveDatacenters: map[string]bool{},
   259  		},
   260  		{
   261  			description: "Empty database results, non-empty inactive datacenters",
   262  			items:       []model.Item{},
   263  			currentInactiveDatacenters: map[string]bool{
   264  				"testDC": true,
   265  			},
   266  			expectedInactiveDatacenters: map[string]bool{},
   267  		},
   268  		{
   269  			description: "Non-Empty Database Results",
   270  			items: []model.Item{
   271  				{
   272  					ID: "random-id",
   273  					Data: map[string]interface{}{
   274  						"name":     "testDC1",
   275  						"inactive": true,
   276  					},
   277  				},
   278  				{
   279  					ID: "random-id2",
   280  					Data: map[string]interface{}{
   281  						"name":     "testDC2",
   282  						"inactive": false,
   283  					},
   284  				},
   285  				{
   286  					ID: "random-id3",
   287  					Data: map[string]interface{}{
   288  						"name":     "testDC3",
   289  						"inactive": true,
   290  					},
   291  				},
   292  			},
   293  			currentInactiveDatacenters: map[string]bool{
   294  				"testDC2": true,
   295  			},
   296  			expectedInactiveDatacenters: map[string]bool{
   297  				"testDC1": true,
   298  				"testDC3": true,
   299  			},
   300  		},
   301  	}
   302  
   303  	for _, tc := range tests {
   304  		t.Run(tc.description, func(t *testing.T) {
   305  			assert := assert.New(t)
   306  			updateInactiveDatacenters(tc.items, tc.currentInactiveDatacenters, &tc.lock, logger)
   307  			assert.Equal(tc.expectedInactiveDatacenters, tc.currentInactiveDatacenters)
   308  
   309  		})
   310  	}
   311  }