github.com/m3db/m3@v1.5.0/src/cmd/services/m3query/config/remote_config_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 config
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/m3db/m3/src/query/storage"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  	yaml "gopkg.in/yaml.v2"
    31  )
    32  
    33  func TestMakeRemote(t *testing.T) {
    34  	var (
    35  		name      = "name"
    36  		addresses = []string{"a", "b", "c"}
    37  		global    = storage.BehaviorWarn
    38  		overwrite = storage.BehaviorFail
    39  	)
    40  
    41  	remote := makeRemote(name, addresses, global, nil)
    42  	assert.Equal(t, name, remote.Name)
    43  	assert.Equal(t, addresses, remote.Addresses)
    44  	assert.Equal(t, global, remote.ErrorBehavior)
    45  
    46  	remote = makeRemote(name, addresses, global, &overwrite)
    47  	assert.Equal(t, name, remote.Name)
    48  	assert.Equal(t, addresses, remote.Addresses)
    49  	assert.Equal(t, overwrite, remote.ErrorBehavior)
    50  }
    51  
    52  var tests = []struct {
    53  	name              string
    54  	cfg               string
    55  	serveEnabled      bool
    56  	serveAddress      string
    57  	listenEnabled     bool
    58  	reflectionEnabled bool
    59  	remotes           []Remote
    60  }{
    61  	{
    62  		name: "empty",
    63  		cfg:  ``,
    64  	},
    65  	{
    66  		name: "enabled, no options",
    67  		cfg:  `enabled: true`,
    68  	},
    69  	{
    70  		name: "listen disabled",
    71  		cfg: `
    72  enabled: false
    73  listenAddress: "abc"
    74  `,
    75  	},
    76  	{
    77  		name: "listen enabled",
    78  		cfg: `
    79  enabled: true
    80  listenAddress: "abc"
    81  `,
    82  		serveEnabled: true,
    83  		serveAddress: "abc",
    84  	}, {
    85  		name:         "listen default",
    86  		cfg:          `listenAddress: "abc"`,
    87  		serveEnabled: true,
    88  		serveAddress: "abc",
    89  	},
    90  	{
    91  		name: "legacy default",
    92  		cfg: `
    93  enabled: true
    94  remoteListenAddresses: ["abc","def"]
    95  `,
    96  		listenEnabled: true,
    97  		remotes: []Remote{
    98  			Remote{
    99  				ErrorBehavior: storage.BehaviorWarn,
   100  				Name:          "default",
   101  				Addresses:     []string{"abc", "def"},
   102  			},
   103  		},
   104  	},
   105  	{
   106  		name: "legacy disabled",
   107  		cfg: `
   108  enabled: false
   109  remoteListenAddresses: ["abc","def"]
   110  `,
   111  	},
   112  	{
   113  		name: "legacy enabled with fail error behavior",
   114  		cfg: `
   115  enabled: true
   116  remoteListenAddresses: ["abc","def"]
   117  errorBehavior: "fail"
   118  `,
   119  		listenEnabled: true,
   120  		remotes: []Remote{
   121  			Remote{
   122  				ErrorBehavior: storage.BehaviorFail,
   123  				Name:          "default",
   124  				Addresses:     []string{"abc", "def"},
   125  			},
   126  		},
   127  	},
   128  	{
   129  		name: "multi-zone",
   130  		cfg: `
   131  remotes:
   132   - name: "foo"
   133     remoteListenAddresses: ["abc","def"]
   134  `,
   135  		listenEnabled: true,
   136  		remotes: []Remote{
   137  			Remote{
   138  				ErrorBehavior: storage.BehaviorWarn,
   139  				Name:          "foo",
   140  				Addresses:     []string{"abc", "def"},
   141  			},
   142  		},
   143  	},
   144  	{
   145  		name: "mixed",
   146  		cfg: `
   147  listenAddress: "pat rafter"
   148  remoteListenAddresses: ["abc","def"]
   149  errorBehavior: "fail"
   150  reflectionEnabled: true
   151  remotes:
   152   - name: "foo"
   153     remoteListenAddresses: ["ghi","jkl"]
   154     errorBehavior: "warn"
   155   - name: "bar"
   156     remoteListenAddresses: ["mno","pqr"]
   157  `,
   158  		listenEnabled:     true,
   159  		serveEnabled:      true,
   160  		reflectionEnabled: true,
   161  		serveAddress:      "pat rafter",
   162  		remotes: []Remote{
   163  			Remote{
   164  				ErrorBehavior: storage.BehaviorFail,
   165  				Name:          "default",
   166  				Addresses:     []string{"abc", "def"},
   167  			},
   168  			Remote{
   169  				ErrorBehavior: storage.BehaviorWarn,
   170  				Name:          "foo",
   171  				Addresses:     []string{"ghi", "jkl"},
   172  			},
   173  			Remote{
   174  				ErrorBehavior: storage.BehaviorFail,
   175  				Name:          "bar",
   176  				Addresses:     []string{"mno", "pqr"},
   177  			},
   178  		},
   179  	},
   180  	{
   181  		name: "mixed disabled",
   182  		cfg: `
   183  enabled: false
   184  listenAddress: "pat rafter"
   185  remoteListenAddresses: ["abc","def"]
   186  errorBehavior: "fail"
   187  reflectionEnabled: false
   188  remotes:
   189   - name: "foo"
   190     remoteListenAddresses: ["ghi","jkl"]
   191     errorBehavior: "warn"
   192   - name: "bar"
   193     remoteListenAddresses: ["mno","pqr"]
   194  `,
   195  	},
   196  }
   197  
   198  func TestParseRemoteOptions(t *testing.T) {
   199  	for _, tt := range tests {
   200  		t.Run(tt.name, func(t *testing.T) {
   201  			var cfg *RPCConfiguration
   202  			require.NoError(t, yaml.Unmarshal([]byte(tt.cfg), &cfg))
   203  			rOpts := RemoteOptionsFromConfig(cfg)
   204  			assert.Equal(t, tt.listenEnabled, rOpts.ListenEnabled())
   205  			assert.Equal(t, tt.serveEnabled, rOpts.ServeEnabled())
   206  			assert.Equal(t, tt.reflectionEnabled, rOpts.ReflectionEnabled())
   207  
   208  			if tt.serveEnabled {
   209  				assert.Equal(t, tt.serveAddress, rOpts.ServeAddress())
   210  			}
   211  
   212  			if tt.listenEnabled {
   213  				assert.Equal(t, tt.remotes, rOpts.Remotes())
   214  			}
   215  		})
   216  	}
   217  }