github.com/m3db/m3@v1.5.0/src/cluster/placementhandler/handleroptions/service_options_test.go (about)

     1  // Copyright (c) 2019 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package handleroptions
    22  
    23  import (
    24  	"net/http"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/m3db/m3/src/x/headers"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  func TestNewServiceOptions(t *testing.T) {
    34  	tests := []struct {
    35  		service string
    36  		headers map[string]string
    37  		aggOpts *M3AggServiceOptions
    38  		exp     ServiceOptions
    39  	}{
    40  		{
    41  			service: "foo",
    42  			exp: ServiceOptions{
    43  				ServiceName:        "foo",
    44  				ServiceEnvironment: headers.DefaultServiceEnvironment,
    45  				ServiceZone:        headers.DefaultServiceZone,
    46  				M3Agg: &M3AggServiceOptions{
    47  					MaxAggregationWindowSize: time.Minute,
    48  				},
    49  			},
    50  		},
    51  		{
    52  			service: "foo",
    53  			headers: map[string]string{
    54  				headers.HeaderClusterEnvironmentName: "bar",
    55  				headers.HeaderClusterZoneName:        "baz",
    56  				headers.HeaderDryRun:                 "true",
    57  			},
    58  			aggOpts: &M3AggServiceOptions{
    59  				MaxAggregationWindowSize: 2 * time.Minute,
    60  				WarmupDuration:           time.Minute,
    61  			},
    62  			exp: ServiceOptions{
    63  				ServiceName:        "foo",
    64  				ServiceEnvironment: "bar",
    65  				ServiceZone:        "baz",
    66  				DryRun:             true,
    67  				M3Agg: &M3AggServiceOptions{
    68  					MaxAggregationWindowSize: 2 * time.Minute,
    69  					WarmupDuration:           time.Minute,
    70  				},
    71  			},
    72  		},
    73  	}
    74  
    75  	for _, test := range tests {
    76  		h := http.Header{}
    77  		for k, v := range test.headers {
    78  			h.Add(k, v)
    79  		}
    80  		svcDefaults := ServiceNameAndDefaults{
    81  			ServiceName: test.service,
    82  		}
    83  		opts := NewServiceOptions(svcDefaults, h, test.aggOpts)
    84  		assert.Equal(t, test.exp, opts)
    85  	}
    86  }
    87  
    88  func TestServiceOptionsValidate(t *testing.T) {
    89  	opts := &ServiceOptions{}
    90  	assert.Error(t, opts.Validate())
    91  	opts.ServiceName = "foo"
    92  	assert.Error(t, opts.Validate())
    93  	opts.ServiceEnvironment = "foo"
    94  	assert.Error(t, opts.Validate())
    95  	opts.ServiceZone = "foo"
    96  	assert.NoError(t, opts.Validate())
    97  
    98  	opts.ServiceName = M3AggregatorServiceName
    99  	assert.Error(t, opts.Validate())
   100  
   101  	opts.M3Agg = &M3AggServiceOptions{}
   102  	assert.NoError(t, opts.Validate())
   103  }