github.com/corverroos/quorum@v21.1.0+incompatible/plugin/settings_test.go (about)

     1  package plugin
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"os"
     7  	"regexp"
     8  	"testing"
     9  
    10  	"github.com/ethereum/go-ethereum/plugin/account"
    11  	"github.com/naoina/toml"
    12  	testifyassert "github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestReadMultiFormatConfig_whenConfigEmbeddedAsArray(t *testing.T) {
    17  	assert := testifyassert.New(t)
    18  
    19  	av1 := "arbitrary value1"
    20  	av2 := "arbitrary value2"
    21  
    22  	cfg, err := ReadMultiFormatConfig([]string{av1, av2})
    23  
    24  	assert.NoError(err)
    25  	assert.Contains(string(cfg), av1)
    26  	assert.Contains(string(cfg), av2)
    27  }
    28  
    29  func TestReadMultiFormatConfig_whenConfigEmbeddedAsFile(t *testing.T) {
    30  	tmpFile, err := ioutil.TempFile("", "q-")
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	defer func() {
    35  		_ = os.Remove(tmpFile.Name())
    36  	}()
    37  	av1 := "arbitrary value1"
    38  	_, err = tmpFile.WriteString(av1)
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	t.Log("wrote tmp file: " + tmpFile.Name())
    43  	assert := testifyassert.New(t)
    44  
    45  	cfg, err := ReadMultiFormatConfig("file://" + tmpFile.Name())
    46  
    47  	assert.NoError(err)
    48  	assert.Equal(av1, string(cfg))
    49  }
    50  
    51  func TestReadMultiFormatConfig_whenConfigEmbeddedAsString(t *testing.T) {
    52  	av1 := "arbitrary value1"
    53  	assert := testifyassert.New(t)
    54  
    55  	cfg, err := ReadMultiFormatConfig(av1)
    56  
    57  	assert.NoError(err)
    58  	assert.Equal(av1, string(cfg))
    59  }
    60  
    61  func TestReadMultiFormatConfig_whenFromEnvVariable(t *testing.T) {
    62  	assert := testifyassert.New(t)
    63  
    64  	arbitraryString := "arbitrary config string"
    65  	if err := os.Setenv("KEY1", arbitraryString); err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	cfg, err := ReadMultiFormatConfig("env://KEY1")
    69  
    70  	assert.NoError(err)
    71  	assert.Equal(arbitraryString, string(cfg))
    72  }
    73  
    74  func TestReadMultiFormatConfig_whenFromEnvFile(t *testing.T) {
    75  	tmpFile, err := ioutil.TempFile("", "q-")
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	defer func() {
    80  		_ = os.Remove(tmpFile.Name())
    81  	}()
    82  	av1 := "arbitrary value1"
    83  	_, err = tmpFile.WriteString(av1)
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  	t.Log("wrote tmp file: " + tmpFile.Name())
    88  	if err := os.Setenv("KEY1", tmpFile.Name()); err != nil {
    89  		t.Fatal(err)
    90  	}
    91  
    92  	assert := testifyassert.New(t)
    93  	cfg, err := ReadMultiFormatConfig("env://KEY1?type=file")
    94  
    95  	assert.NoError(err)
    96  	assert.Equal(av1, string(cfg))
    97  }
    98  
    99  func TestEnvironmentAwaredValue_UnmarshalJSON_whenValueFromEnvVariable(t *testing.T) {
   100  	assert := testifyassert.New(t)
   101  
   102  	if err := os.Setenv("KEY1", "foo"); err != nil {
   103  		t.Fatal(err)
   104  	}
   105  
   106  	var value struct {
   107  		Vinstance EnvironmentAwaredValue
   108  		Vpointer  *EnvironmentAwaredValue
   109  	}
   110  	assert.NoError(json.Unmarshal([]byte(`{"Vinstance": "env://KEY1", "Vpointer": "env://KEY1"}`), &value))
   111  	assert.Equal("foo", value.Vinstance.String())
   112  	assert.Equal("foo", value.Vpointer.String())
   113  }
   114  
   115  func TestEnvironmentAwaredValue_UnmarshalJSON_whenTypical(t *testing.T) {
   116  	assert := testifyassert.New(t)
   117  
   118  	var value struct {
   119  		Vinstance EnvironmentAwaredValue
   120  		Vpointer  *EnvironmentAwaredValue
   121  	}
   122  	assert.NoError(json.Unmarshal([]byte(`{"Vinstance": "foo", "Vpointer": "bar"}`), &value))
   123  	assert.Equal("foo", value.Vinstance.String())
   124  	assert.Equal("bar", value.Vpointer.String())
   125  }
   126  
   127  func TestEnvironmentAwaredValue_UnmarshalTOML_whenTypical(t *testing.T) {
   128  	assert := testifyassert.New(t)
   129  
   130  	var value struct {
   131  		Vinstance EnvironmentAwaredValue
   132  		Vpointer  *EnvironmentAwaredValue
   133  	}
   134  	assert.NoError(toml.Unmarshal([]byte(`
   135  Vinstance = "foo"
   136  Vpointer = "bar"`), &value))
   137  	assert.Equal("foo", value.Vinstance.String())
   138  	assert.Equal("bar", value.Vpointer.String())
   139  }
   140  
   141  func TestEnvironmentAwaredValue_UnmarshalTOML_whenValueFromEnvVariable(t *testing.T) {
   142  	assert := testifyassert.New(t)
   143  
   144  	if err := os.Setenv("KEY1", "foo"); err != nil {
   145  		t.Fatal(err)
   146  	}
   147  
   148  	var value struct {
   149  		Vinstance EnvironmentAwaredValue
   150  		Vpointer  *EnvironmentAwaredValue
   151  	}
   152  	assert.NoError(toml.Unmarshal([]byte(`
   153  Vinstance = "env://KEY1"
   154  Vpointer = "env://KEY1"`), &value))
   155  	assert.Equal("foo", value.Vinstance.String())
   156  	assert.Equal("foo", value.Vpointer.String())
   157  }
   158  
   159  func TestPluginInterfaceName_UnmarshalTOML_whenTypical(t *testing.T) {
   160  	assert := testifyassert.New(t)
   161  
   162  	var value struct {
   163  		MyMap map[PluginInterfaceName]string
   164  	}
   165  	assert.NoError(toml.Unmarshal([]byte(`
   166  [MyMap]
   167  Foo = "a1"
   168  BAR = "a2"
   169  `), &value))
   170  	assert.Contains(value.MyMap, PluginInterfaceName("foo"))
   171  	assert.Contains(value.MyMap, PluginInterfaceName("bar"))
   172  }
   173  
   174  // For JSON, keys are not being changed. Might be a bug in the decoder
   175  func TestPluginInterfaceName_UnmarshalJSON_whenTypical(t *testing.T) {
   176  	assert := testifyassert.New(t)
   177  
   178  	var value struct {
   179  		MyMap map[PluginInterfaceName]string
   180  	}
   181  	assert.NoError(json.Unmarshal([]byte(`
   182  {
   183  	"MyMap": {
   184  		"Foo" : "a1",
   185  		"BAR" : "a2"
   186  	}
   187  }
   188  `), &value))
   189  	assert.Contains(value.MyMap, PluginInterfaceName("foo"))
   190  	assert.Contains(value.MyMap, PluginInterfaceName("bar"))
   191  }
   192  
   193  func TestAccountAPIProviderFunc_OnlyExposeAccountCreationAPI(t *testing.T) {
   194  	pm, err := NewPluginManager(
   195  		"arbitraryName",
   196  		&Settings{
   197  			Providers: map[PluginInterfaceName]PluginDefinition{
   198  				AccountPluginInterfaceName: {
   199  					Name:    "arbitrary-account",
   200  					Version: "1.0.0",
   201  					Config:  "arbitrary config",
   202  				},
   203  			},
   204  		},
   205  		false,
   206  		false,
   207  		"",
   208  	)
   209  	require.NoError(t, err)
   210  
   211  	provider, ok := pluginProviders[AccountPluginInterfaceName]
   212  	require.True(t, ok)
   213  
   214  	api, err := provider.apiProviderFunc("namespace", pm)
   215  	require.NoError(t, err)
   216  	require.Len(t, api, 1)
   217  	require.Equal(t, "namespace", api[0].Namespace)
   218  	require.Implements(t, (*account.CreatorService)(nil), api[0].Service)
   219  
   220  	_, ok = api[0].Service.(account.Service)
   221  	require.False(t, ok)
   222  }
   223  
   224  func TestSettings_CheckSettingsAreSupported_AllSupported(t *testing.T) {
   225  	s := Settings{
   226  		Providers: map[PluginInterfaceName]PluginDefinition{
   227  			AccountPluginInterfaceName:    {},
   228  			HelloWorldPluginInterfaceName: {},
   229  		},
   230  	}
   231  	supported := []PluginInterfaceName{AccountPluginInterfaceName, HelloWorldPluginInterfaceName}
   232  
   233  	err := s.CheckSettingsAreSupported(supported)
   234  
   235  	require.NoError(t, err)
   236  }
   237  
   238  func TestSettings_CheckSettingsAreSupported_NoneSupported(t *testing.T) {
   239  	s := Settings{
   240  		Providers: map[PluginInterfaceName]PluginDefinition{
   241  			AccountPluginInterfaceName:    {},
   242  			HelloWorldPluginInterfaceName: {},
   243  		},
   244  	}
   245  	supported := []PluginInterfaceName{}
   246  
   247  	err := s.CheckSettingsAreSupported(supported)
   248  
   249  	require.Error(t, err)
   250  
   251  	wantMsgPattern := regexp.MustCompile(`^unsupported plugins configured: \[(account|helloworld) (account|helloworld)\]$`)
   252  	matches := wantMsgPattern.FindStringSubmatch(err.Error())
   253  
   254  	// make sure the msg matches the pattern and the same plugin is not listed twice
   255  	require.Regexp(t, wantMsgPattern, err.Error())
   256  
   257  	require.NotNil(t, matches, "error message did not match wanted pattern")
   258  	require.Len(t, matches, 3)
   259  	require.NotEmpty(t, matches[1])
   260  	require.NotEmpty(t, matches[2])
   261  	require.NotEqualf(t, matches[1], matches[2], "\"%v\" listed twice", matches[1])
   262  }
   263  
   264  func TestSettings_CheckSettingsAreSupported_SomeSupported(t *testing.T) {
   265  	s := Settings{
   266  		Providers: map[PluginInterfaceName]PluginDefinition{
   267  			AccountPluginInterfaceName:    {},
   268  			HelloWorldPluginInterfaceName: {},
   269  		},
   270  	}
   271  	supported := []PluginInterfaceName{AccountPluginInterfaceName}
   272  
   273  	err := s.CheckSettingsAreSupported(supported)
   274  
   275  	require.EqualError(t, err, "unsupported plugins configured: [helloworld]")
   276  }