Exemplo n.º 1
0
func TestCreateEntitiesFromData(t *testing.T) {

	yamlString := []byte(`input:
  logA:
    type: ssh
    host:
      - 1.1.1.1
      - 2.2.2.2
    user: test
    pass: password
    command: abc
  logB:
    type: ssh
    host: 3.3.3.3
    user: test2
    pass: password2
    command: abc
`)

	var yamlData interface{}
	err := yaml.Unmarshal(yamlString, &yamlData)
	if err != nil {
		t.Log("Failed to parse yaml.")
		t.Fail()
		return
	}

	inputSection, ok := yamlData.(map[interface{}]interface{})["input"].(map[interface{}]interface{})
	if !ok {
		t.Log("Failed to find input section.")
		t.Fail()
		return
	}

	var entries []entity.InputEntry
	var tempEntries []entity.InputEntry
	config := entity.NewConfig()
	inputEntryParser := extssh.NewInputEntryParser()
	for name, inputEntry := range inputSection {
		input, ok := inputEntry.(map[interface{}]interface{})
		if !ok {
			t.Log("Failed to find input entry.")
			t.Fail()
			return
		}

		tempEntries, err = inputEntryParser.CreateInputEntriesFromData(config, name.(string), input)
		if err != nil {
			t.Log("Failed to parse input entry.")
			t.Fail()
			return
		}
		entries = append(entries, tempEntries...)
	}

	if len(entries) != 3 {
		t.Logf("Config entry count does not matched. expected: 3, actual: %d", len(entries))
		t.Fail()
		return
	}

	expects := []map[string]string{
		{"host": "1.1.1.1", "name": "logA", "user": "******"},
		{"host": "2.2.2.2", "name": "logA", "user": "******"},
		{"host": "3.3.3.3", "name": "logB", "user": "******"},
	}

	for i, expected := range expects {
		entry := entries[i].(*extssh.InputEntry)

		if entry.Host != expected["host"] {
			t.Logf("Host is not expected. index=%d: value=%s", i, entry.Host)
			t.Fail()
		}
		if entry.Name != expected["name"] {
			t.Logf("Log name is not expected. index=%d: value=%s", i, entry.Name)
			t.Fail()
		}
		if entry.Cred.User != expected["user"] {
			t.Logf("user is not expected. index=%d: value=%s", i, entry.Cred.User)
			t.Fail()
		}
	}

}
Exemplo n.º 2
0
func TestParse(t *testing.T) {

	extensionManager := lib.NewExtensionManager()
	extensionManager.RegisterExtensionPoint(ext.POINT_INPUT_CONFIG_PARSER, extssh.NewInputEntryParser())

	cs := service.NewConfig(extensionManager)

	yaml := []byte(`
input:
  test01:
    type: ssh
    host:
      - example.com
      - example02.com
    user: user_name
    identity: identity-file-name
    command: testtest
log:
  logging: true
  path: /tmp/test.log
`)

	conf := entity.NewConfig()
	err := cs.Parse(yaml, conf)
	if err != nil {
		t.Logf(err.Error())
		t.Fail()
	}

	if len(conf.InputEntries) != 2 {
		t.Logf("input handler count. expected: 1, actual: %d", len(conf.InputEntries))
		t.Fail()
	}

	inputSsh := conf.InputEntries[0].(*extssh.InputEntry)
	if inputSsh.Name != "test01" {
		t.Logf("input handler name not matched. expected: test01, actual: %s", inputSsh.Name)
		t.Fail()
	}
	if inputSsh.Host != "example.com" {
		t.Logf("host name not matched. expected: example.com, actual: %s", inputSsh.Host)
		t.Fail()
	}

	inputSsh2 := conf.InputEntries[1].(*extssh.InputEntry)
	if inputSsh2.Name != "test01" {
		t.Logf("input handler name not matched. expected: test01, actual: %s", inputSsh2.Name)
		t.Fail()
	}
	if inputSsh2.Host != "example02.com" {
		t.Logf("host name not matched. expected: example02.com, actual: %s", inputSsh2.Host)
		t.Fail()
	}

	if conf.Logging != true {
		t.Logf("Logging not enabled.")
		t.Fail()
	}
	if conf.LogPath != "/tmp/test.log" {
		t.Logf("LogPath not matched. actual: " + conf.LogPath)
		t.Fail()
	}
}