github.com/m3db/m3@v1.5.0/src/cluster/services/config_test.go (about)

     1  // Copyright (c) 2017 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 services
    22  
    23  import (
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/stretchr/testify/require"
    28  	yaml "gopkg.in/yaml.v2"
    29  )
    30  
    31  func TestOverrideConfiguration(t *testing.T) {
    32  	configStr := `
    33  namespaces:
    34    placement: p
    35    metadata: m
    36  `
    37  
    38  	var cfg OverrideConfiguration
    39  	err := yaml.Unmarshal([]byte(configStr), &cfg)
    40  	require.NoError(t, err)
    41  	opts := cfg.NewOptions()
    42  	require.Equal(t, "p", opts.NamespaceOptions().PlacementNamespace())
    43  	require.Equal(t, "m", opts.NamespaceOptions().MetadataNamespace())
    44  }
    45  
    46  func TestNamespaceConfiguration(t *testing.T) {
    47  	configStr := `
    48  placement: p
    49  metadata: m
    50  `
    51  
    52  	var cfg NamespacesConfiguration
    53  	err := yaml.Unmarshal([]byte(configStr), &cfg)
    54  	require.NoError(t, err)
    55  	opts := cfg.NewOptions()
    56  	require.Equal(t, "p", opts.PlacementNamespace())
    57  	require.Equal(t, "m", opts.MetadataNamespace())
    58  }
    59  
    60  func TestConfig(t *testing.T) {
    61  	cf := Configuration{}
    62  	require.Equal(t, defaultInitTimeout, cf.NewOptions().InitTimeout())
    63  
    64  	sec := time.Second
    65  	cf = Configuration{InitTimeout: &sec}
    66  	require.Equal(t, time.Second, cf.NewOptions().InitTimeout())
    67  }
    68  
    69  func TestElectionOptions(t *testing.T) {
    70  	configStr := `
    71  leaderTimeout: 5s
    72  resignTimeout: 10s
    73  TTLSeconds: 15
    74  `
    75  
    76  	var cfg ElectionConfiguration
    77  	err := yaml.Unmarshal([]byte(configStr), &cfg)
    78  	require.NoError(t, err)
    79  	opts := cfg.NewOptions()
    80  	require.Equal(t, time.Second*5, opts.LeaderTimeout())
    81  	require.Equal(t, time.Second*10, opts.ResignTimeout())
    82  	require.Equal(t, 15, opts.TTLSecs())
    83  }
    84  
    85  func TestServiceID(t *testing.T) {
    86  	configStr := `
    87  name: sampleName
    88  environment: sampleEnvironment
    89  zone: sampleZone
    90  `
    91  
    92  	var cfg ServiceIDConfiguration
    93  	err := yaml.Unmarshal([]byte(configStr), &cfg)
    94  	require.NoError(t, err)
    95  	sID := cfg.NewServiceID()
    96  	require.Equal(t, "sampleName", sID.Name())
    97  	require.Equal(t, "sampleEnvironment", sID.Environment())
    98  	require.Equal(t, "sampleZone", sID.Zone())
    99  }