github.com/m3db/m3@v1.5.0/src/dbnode/environment/config_test.go (about)

     1  // Copyright (c) 2018 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 environment
    22  
    23  import (
    24  	"testing"
    25  	"time"
    26  
    27  	etcdclient "github.com/m3db/m3/src/cluster/client/etcd"
    28  	"github.com/m3db/m3/src/cluster/services"
    29  	"github.com/m3db/m3/src/dbnode/namespace"
    30  	"github.com/m3db/m3/src/dbnode/retention"
    31  	"github.com/m3db/m3/src/dbnode/topology"
    32  	"github.com/m3db/m3/src/x/instrument"
    33  
    34  	"github.com/stretchr/testify/assert"
    35  	"gopkg.in/yaml.v2"
    36  )
    37  
    38  var initTimeout = time.Minute
    39  
    40  func TestConfigureStatic(t *testing.T) {
    41  	config := Configuration{
    42  		Statics: StaticConfiguration{
    43  			&StaticCluster{
    44  				Namespaces: []namespace.MetadataConfiguration{
    45  					namespace.MetadataConfiguration{
    46  						ID: "metrics",
    47  						Retention: retention.Configuration{
    48  							RetentionPeriod: 24 * time.Hour,
    49  							BlockSize:       time.Hour,
    50  						},
    51  					},
    52  					namespace.MetadataConfiguration{
    53  						ID: "other-metrics",
    54  						Retention: retention.Configuration{
    55  							RetentionPeriod: 24 * time.Hour,
    56  							BlockSize:       time.Hour,
    57  						},
    58  					},
    59  				},
    60  				TopologyConfig: &topology.StaticConfiguration{
    61  					Shards: 2,
    62  					Hosts: []topology.HostShardConfig{
    63  						topology.HostShardConfig{
    64  							HostID:        "localhost",
    65  							ListenAddress: "0.0.0.0:1234",
    66  						},
    67  					},
    68  				},
    69  				ListenAddress: "0.0.0.0:9000",
    70  			},
    71  		},
    72  	}
    73  
    74  	configRes, err := config.Configure(ConfigurationParameters{})
    75  	assert.NotNil(t, configRes)
    76  	assert.NoError(t, err)
    77  }
    78  
    79  func TestConfigureDynamic(t *testing.T) {
    80  	config := Configuration{
    81  		Services: DynamicConfiguration{
    82  			&DynamicCluster{
    83  				Service: &etcdclient.Configuration{
    84  					Zone:     "local",
    85  					Env:      "test",
    86  					Service:  "m3dbnode_test",
    87  					CacheDir: "/",
    88  					ETCDClusters: []etcdclient.ClusterConfig{
    89  						etcdclient.ClusterConfig{
    90  							Zone:      "local",
    91  							Endpoints: []string{"localhost:1111"},
    92  						},
    93  					},
    94  					SDConfig: services.Configuration{
    95  						InitTimeout: &initTimeout,
    96  					},
    97  				},
    98  			},
    99  		},
   100  	}
   101  
   102  	cfgParams := ConfigurationParameters{
   103  		InstrumentOpts: instrument.NewOptions(),
   104  	}
   105  
   106  	configRes, err := config.Configure(cfgParams)
   107  	assert.NotNil(t, configRes)
   108  	assert.NoError(t, err)
   109  }
   110  
   111  func TestUnmarshalDynamicSingle(t *testing.T) {
   112  	in := `
   113  service:
   114    zone: dca8
   115    env: test
   116  `
   117  
   118  	var cfg Configuration
   119  	err := yaml.Unmarshal([]byte(in), &cfg)
   120  	assert.NoError(t, err)
   121  	assert.NoError(t, cfg.Validate())
   122  	assert.Len(t, cfg.Services, 1)
   123  }
   124  
   125  func TestUnmarshalDynamicList(t *testing.T) {
   126  	in := `
   127  services:
   128    - service:
   129        zone: dca8
   130        env: test
   131    - service:
   132        zone: phx3
   133        env: test
   134      async: true
   135  `
   136  
   137  	var cfg Configuration
   138  	err := yaml.Unmarshal([]byte(in), &cfg)
   139  	assert.NoError(t, err)
   140  	assert.NoError(t, cfg.Validate())
   141  	assert.Len(t, cfg.Services, 2)
   142  }
   143  
   144  var configValidationTests = []struct {
   145  	name      string
   146  	in        string
   147  	expectErr error
   148  }{
   149  	{
   150  		name:      "empty config",
   151  		in:        ``,
   152  		expectErr: errInvalidConfig,
   153  	},
   154  	{
   155  		name: "static and dynamic",
   156  		in: `
   157  services:
   158    - service:
   159        zone: dca8
   160        env: test
   161  statics:
   162    - listenAddress: 0.0.0.0:9000`,
   163  		expectErr: errInvalidConfig,
   164  	},
   165  	{
   166  		name: "invalid dynamic config",
   167  		in: `
   168  services:
   169    - async: true`,
   170  		expectErr: errInvalidSyncCount,
   171  	},
   172  	{
   173  		name: "invalid static config",
   174  		in: `
   175  statics:
   176    - async: true`,
   177  		expectErr: errInvalidSyncCount,
   178  	},
   179  	{
   180  		name: "valid config",
   181  		in: `
   182  services:
   183    - service:
   184        zone: dca8
   185        env: test
   186    - service:
   187        zone: phx3
   188        env: test
   189      async: true`,
   190  		expectErr: nil,
   191  	},
   192  }
   193  
   194  func TestConfigValidation(t *testing.T) {
   195  	for _, tt := range configValidationTests {
   196  		var cfg Configuration
   197  		err := yaml.Unmarshal([]byte(tt.in), &cfg)
   198  		assert.NoError(t, err)
   199  		assert.Equal(t, tt.expectErr, cfg.Validate())
   200  	}
   201  }