func TestEndpointsOpen(t *testing.T) { assert := assert.Assert(t) var yml string var setting *kasi_conf.CoreSetting var err error yml = loadFile("config_simple_endpoint0.yml") setting, err = kasi.ParseConfig(yml) assert.Nil(err) assert.Equal(len(setting.Services[0].Endpoints), 1) yml = loadFile("config_opened_endpoint.yml") setting, err = kasi.ParseConfig(yml) assert.Nil(err) assert.Equal(len(setting.Services[0].Endpoints), 1) yml = loadFile("config_closed_endpoint.yml") setting, err = kasi.ParseConfig(yml) assert.Nil(err) assert.False(setting.Services[0].Opened(), "") assert.Equal(len(setting.Services[0].Endpoints), 0) }
func TestHostnamesInheritFromDefault(t *testing.T) { assert := assert.Assert(t) yml := loadFile("config_hostnames_inherit_from_default.yml") setting, err := kasi.ParseConfig(yml) assert.Equal(err, nil) assert.Equal(len(setting.Services), 1) assert.Equal(len(setting.Services[0].Hostnames), 2) for _, h := range []string{"my0.github.com", "my1.github.com"} { if sort.SearchStrings(setting.Services[0].Hostnames, h) == 2 { t.Error("failed to parse hostnames") } } ymlOverride := loadFile("config_hostnames_override_default.yml") setting, err = kasi.ParseConfig(ymlOverride) assert.Nil(err) assert.Equal(len(setting.Services), 1) assert.Equal(len(setting.Services[0].Hostnames), 1) for _, h := range []string{"my2.github.com"} { if sort.SearchStrings(setting.Services[0].Hostnames, h) == 2 { t.Error("failed to parse hostnames") } } }
func TestMiddleware(t *testing.T) { assert := assert.Assert(t) yml := loadFile("conf_middlewares.yml") setting, err := kasi.ParseConfig(yml) assert.Equal(err, nil) assert.Equal(len(setting.Middlewares), 2) service0, _ := setting.GetServiceByID("service0") assert.Equal(service0.Middleware, "base") service1, _ := setting.GetServiceByID("service1") assert.Equal(service1.Middleware, "extend") assert.Equal(len(service0.Endpoints[0].Middleware), 2) assert.Equal(service0.Endpoints[0].Middleware[0], "a.js") assert.Equal(service0.Endpoints[0].Middleware[1], "b.js") assert.Equal(len(service0.Endpoints[0].Middleware), 2) assert.Nil(service0.Endpoints[1].Middleware) assert.Equal(len(service1.Endpoints[0].Middleware), 2) assert.Equal(service1.Endpoints[0].Middleware[0], "a.js") assert.Equal(service1.Endpoints[0].Middleware[1], "b.js") assert.Equal(len(service1.Endpoints[1].Middleware), 2) assert.Equal(service1.Endpoints[1].Middleware[0], "c.js") assert.Equal(service1.Endpoints[1].Middleware[1], "d.js") }
func TestEnv(t *testing.T) { assert := assert.Assert(t) yml := loadFile("config_env.yml") setting, err := kasi.ParseConfig(yml) assert.Equal(err, nil) assert.NotEqual(setting.Env.Hostname, "") assert.NotEqual(setting.Env.GOOS, "") }
func TestEndpoints(t *testing.T) { assert := assert.Assert(t) var yml string var setting *kasi_conf.CoreSetting var err error yml = loadFile("config_simple_endpoint0.yml") setting, err = kasi.ParseConfig(yml) assert.Nil(err) assert.Equal(len(setting.Services[0].Endpoints), 1) yml = loadFile("config_simple_endpoint1.yml") setting, err = kasi.ParseConfig(yml) assert.Nil(err) assert.Equal(len(setting.Services[0].Endpoints), 2) }
func TestDuplicatedID(t *testing.T) { assert := assert.Assert(t) var yml string var err error yml = loadFile("config_duplicated_id.yml") _, err = kasi.ParseConfig(yml) assert.NotNil(err) }
func TestHostnamesWithString(t *testing.T) { assert := assert.Assert(t) yml := loadFile("config_hostnames_string.yml") setting, err := kasi.ParseConfig(yml) assert.Nil(err) assert.Equal(len(setting.Services), 1) assert.Equal(len(setting.Services[0].Hostnames), 1) assert.Equal(setting.Services[0].Hostnames[0], "my0.github.com") }
func TestID(t *testing.T) { assert := assert.Assert(t) var yml string var setting *kasi_conf.CoreSetting var err error yml = loadFile("config_check_id.yml") setting, err = kasi.ParseConfig(yml) assert.Equal(err, nil) assert.Equal(setting.Services[0].GetID(), "this-is-service-id") assert.Equal(setting.Services[0].Endpoints[0].GetID(), "findme") assert.Equal(len(setting.Services[0].Endpoints[1].GetID()), 36) assert.Equal(setting.Services[0].Endpoints[1].GetID(), setting.Services[0].Endpoints[1].GetID()) }
func TestHostnamesWithStrings(t *testing.T) { assert := assert.Assert(t) yml := loadFile("config_hostnames_strings.yml") setting, err := kasi.ParseConfig(yml) assert.Nil(err) assert.Equal(len(setting.Services), 1) assert.Equal(len(setting.Services[0].Hostnames), 2) for _, h := range []string{"my0.github.com", "my1.github.com"} { if sort.SearchStrings(setting.Services[0].Hostnames, h) == 2 { t.Error("failed to parse hostnames") } } }
func TestBindInheritFromDefault(t *testing.T) { assert := assert.Assert(t) yml := loadFile("conf_default_bind.yml") setting, err := kasi.ParseConfig(yml) assert.Equal(err, nil) cases := map[string]*net.TCPAddr{ "a.com": setting.GetDefaultSetting().Bind, "b.com": &net.TCPAddr{Port: 90}, } for _, service := range setting.Services { assert.Equal(cases[service.Hostnames[0]], service.Bind) } }
func TestTimeout(t *testing.T) { assert := assert.Assert(t) yml := loadFile("config_timeout.yml") setting, err := kasi.ParseConfig(yml) assert.Equal(err, nil) cases := map[string]time.Duration{ "a.com": time.Second * 9, "b.com": time.Second * 0, "c.com": setting.GetDefaultSetting().Timeout, "d.com": setting.GetDefaultSetting().Timeout, } for _, service := range setting.Services { assert.Equal(service.Timeout, cases[service.Hostnames[0]]) } }
func TestBind(t *testing.T) { assert := assert.Assert(t) yml := loadFile("config_multiple_bind.yml") setting, err := kasi.ParseConfig(yml) assert.Equal(err, nil) cases := map[string]*net.TCPAddr{ "a.com": &net.TCPAddr{Port: 8000}, "b.com": &net.TCPAddr{Port: 8000}, "c.com": &net.TCPAddr{Port: 8000}, "d.com": &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8000}, } for _, service := range setting.Services { assert.Equal(cases[service.Hostnames[0]], service.Bind) } }
func TestGetEndpointByID(t *testing.T) { assert := assert.Assert(t) var yml string var setting *kasi_conf.CoreSetting var endpointSetting *kasi_conf.EndpointSetting var err error yml = loadFile("config_check_id.yml") setting, err = kasi.ParseConfig(yml) assert.Equal(err, nil) endpointSetting, err = setting.GetEndpointByID("findme") assert.Equal(endpointSetting, setting.Services[0].Endpoints[0]) endpointSettingToFind := setting.Services[0].Endpoints[1] endpointSetting, err = setting.GetEndpointByID(endpointSettingToFind.GetID()) assert.Equal(endpointSettingToFind, endpointSetting) }