Exemplo n.º 1
0
// Function reads initializes pluginConfig with predefined values and
// non-predefined values in the Settings
// Plan:
//  Create a new PluginConfig
//  Create a new shared.MarshalMap with mock key values
//  Check if they are actually added, if they are, assert the key-value correctness
func TestPluginConfigRead(t *testing.T) {
	expect := shared.NewExpect(t)
	mockPluginCfg := getMockPluginConfig()

	// create a mock MarshalMap
	testMarshalMap := shared.NewMarshalMap()
	testMarshalMap["Instances"] = 0

	mockPluginCfg.Read(testMarshalMap)
	// with 0 instance, plugin should be disabled
	expect.False(mockPluginCfg.Enable)
	// if there is no stream, then array with wildcard should be returned
	expect.Equal(mockPluginCfg.Stream, []string{"*"})

	//reset mockPluginCfg
	mockPluginCfg = getMockPluginConfig()
	testMarshalMap["ID"] = "mockId"
	testMarshalMap["Enable"] = true
	testMarshalMap["Instances"] = 2
	testMarshalMap["Stream"] = "mockStats"
	testMarshalMap["Host"] = "someHost"
	testMarshalMap["Database"] = "someDatabase"

	mockPluginCfg.Read(testMarshalMap)

	// Check for the bundled config options
	expect.Equal(mockPluginCfg.ID, "mockId")
	expect.True(mockPluginCfg.Enable)
	expect.Equal(mockPluginCfg.Instances, 2)
	expect.Equal(mockPluginCfg.Stream, []string{"mockStats"})

	// check for the miscelleneous settings key added
	expect.Equal(mockPluginCfg.GetString("Host", ""), "someHost")
	expect.Equal(mockPluginCfg.GetString("Database", ""), "someDatabase")
}
Exemplo n.º 2
0
// Accepts checks JSON field values and rejects messages after testing a
// blacklist and a whitelist.
func (filter *JSON) Accepts(msg core.Message) bool {
	values := shared.NewMarshalMap()
	if err := json.Unmarshal(msg.Data, &values); err != nil {
		return false
	}

	// Check rejects
	for key, exp := range filter.rejectValues {
		if value, exists := filter.getValue(key, values); exists {
			if exp.MatchString(value) {
				return false
			}
		}
	}

	// Check accepts
	for key, exp := range filter.acceptValues {
		if value, exists := filter.getValue(key, values); exists {
			if !exp.MatchString(value) {
				return false
			}
		}
	}

	return true
}
Exemplo n.º 3
0
// NewPluginConfig creates a new plugin config with default values.
// By default the plugin is enabled, has a buffered channel with 4096 slots, has
// one instance, is bound to no streams and has no additional settings.
func NewPluginConfig(typename string) PluginConfig {
	return PluginConfig{
		Typename:  typename,
		Enable:    true,
		Instances: 1,
		Stream:    []string{},
		Settings:  shared.NewMarshalMap(),
		validKeys: make(map[string]bool),
	}
}