Exemplo n.º 1
0
// Load config file(YAML) and populate it into config.Config.
func (a *Application) loadConfig(configPath string, c *entity.Config) (err error) {
	globalConfigService := service.NewGlobalConfig()
	globalConfigPath, found := globalConfigService.GetFilePath()
	if found {
		err = globalConfigService.ParseFromFile(globalConfigPath, c)
		if err != nil {
			return
		}
	}

	configService := service.NewConfig(a.extensionManager)
	err = configService.ParseFromFile(configPath, c)
	return
}
Exemplo n.º 2
0
func TestGlobalConfigParse(t *testing.T) {
	globalConfigService := service.NewGlobalConfig()

	yamlData := []byte(`default:
  user: default-user
  pass: default-pass
  identity: default-identity

host:
  host-a:
    user: host-a-user
    pass: host-a-pass
    identity: host-a-identity
  host-b:
    user: host-b-user
    pass: host-b-pass
    identity: host-b-identity
`)

	var data map[interface{}]interface{}
	data = make(map[interface{}]interface{})
	err := yaml.Unmarshal(yamlData, data)
	if err != nil {
		t.Log(err.Error())
		t.Fail()
		return
	}

	config := entity.NewConfig()
	err = globalConfigService.Parse(data, config)
	if err != nil {
		t.Log(err.Error())
		t.Fail()
		return
	}

	if config.DefaultCredential.User != "default-user" {
		t.Log("DefaultCredential.User not matched.")
		t.Logf("%#v", config.DefaultCredential)
		t.Fail()
	}
	if config.DefaultCredential.Pass != "default-pass" {
		t.Log("DefaultCredential.Pass not matched.")
		t.Fail()
	}
	if config.DefaultCredential.Identity != "default-identity" {
		t.Log("DefaultCredential.Identity not matched.")
		t.Fail()
	}

	hosts := map[string]map[string]string{
		"host-a": {"user": "******", "pass": "******", "identity": "host-a-identity"},
		"host-b": {"user": "******", "pass": "******", "identity": "host-b-identity"},
	}

	for hostName, host := range hosts {
		hostCredential := config.Hosts[hostName]

		if config.Hosts[hostName].User != host["user"] {
			t.Logf("host '%s': user does not matched.", hostCredential.User)
			t.Fail()
			return
		}

		if config.Hosts[hostName].Pass != host["pass"] {
			t.Logf("host '%s': pass does not matched.", hostCredential.Pass)
			t.Fail()
			return
		}
		if config.Hosts[hostName].Identity != host["identity"] {
			t.Logf("host '%s': identity does not matched.", hostCredential.Identity)
			t.Fail()
			return
		}
	}
}