func Test_strategy_registry_enable_api_server(t *testing.T) {
	logging.SetLevel("info")
	config.CORE_CONF_FILEPATH, _ = filepath.Abs("./test/core_config_registry_enable_api.yml")
	//	config.CONF_FILEPATH, _ = filepath.Abs("./test/config.yml")
	err := config.LoadCoreConfig()
	if err != nil || config.CoreConf.Enable_http_api != true {
		t.Error("CoreConf.EnableHTTPAPI != true, err: %v", err)
		return
	}

	Stop() // stop if already exists while test all cases
	Start()

	//	resp, err := http.Get("http://localhost:3031/sys_info")
	hResp, err := goreq.Request{
		Method:      "Get",
		Uri:         "http://localhost:3031/sys_info",
		Accept:      "application/json",
		ContentType: "application/json",
		UserAgent:   "hickwall",
		Timeout:     100 * time.Millisecond,
	}.Do()
	if err != nil {
		t.Errorf("api server doesn't work. %v", err)
		return
	}
	defer hResp.Body.Close()
	t.Log(hResp)

}
func Test_strategy_etcd_LoadConfigStrategyEtcd_Nil(t *testing.T) {
	logging.SetLevel("debug")
	defer close_core()

	stopCh := make(chan error)

	for idx, tcase := range nil_core_tests {
		request_cnt := 0
		ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			request_cnt += 1
			logging.Infof("case: %d, -- we got request: cnt:%d ------------\n", idx, request_cnt)
			// pretty.Println(r)
			iswait := r.URL.Query().Get("wait")
			if strings.ToLower(iswait) == "true" {
				logging.Info("watching")
				// watch, long polling
				// time.Sleep(time.Second * 1)
			} else {
				logging.Info("getting")
			}

			v, _ := _fack_etcd_respose(1, tcase)
			fmt.Printf("case: %d, content: %s\n", idx, v)

			fmt.Fprintln(w, v)

		}))

		go new_core_from_etcd([]string{ts.URL}, "/config/host/DST54869.yml", stopCh)
		tick := time.After(time.Second * 1)
		timeout := time.After(time.Second * 2)

	main_loop:
		for {
			select {
			case <-tick:
				stopCh <- nil
				if the_core != nil {
					t.Error("the_core has been created with invalid configuration!")
					return
				} else {
					t.Log("test case successed")
					ts.Close()
					break main_loop
				}
			case <-timeout:
				t.Errorf("timed out. somethings is blocking")
				return
			}
		}
	}

}
Exemple #3
0
func LoadCoreConfig() error {
	data, err := ioutil.ReadFile(CORE_CONF_FILEPATH)
	if err != nil {
		return fmt.Errorf("faild to read core config: %v", err)
	}
	CoreConf = CoreConfig{}
	// we can use yaml to load config directly. only because
	// core config structure is very simple and flat.
	err = yaml.Unmarshal(data, &CoreConf)
	if err != nil {
		return fmt.Errorf("unable to unmarshal yaml: %v", err)
	}

	if CoreConf.Rss_limit_mb <= 0 {
		CoreConf.Rss_limit_mb = 50 //deffault rss limit
	}
	if CoreConf.Listen_port <= 0 {
		CoreConf.Listen_port = 3031
	}
	if CoreConf.Hostname != "" {
		newcore.SetHostname(CoreConf.Hostname)
	}

	logging.SetLevel(CoreConf.Log_level)
	if err != nil {
		return fmt.Errorf("LoadCoreConfFile failed: %v", err)
	}

	if CoreConf.Enable_http_api && (CoreConf.Secure_api_read || CoreConf.Secure_api_write) {
		// we should check public key config.
		_, err := utils.LoadPublicKeyFromPath(CoreConf.Server_pub_key_path)
		if err != nil {
			logging.Criticalf("unable to load server public key while SecureAPIx is set to be true: %s", err)
		}
	}

	logging.Debugf("SHARED_DIR:            %s\n", SHARED_DIR)
	logging.Debugf("LOG_DIR:               %s\n", LOG_DIR)
	logging.Debugf("LOG_FILEPATH:          %s\n", LOG_FILEPATH)
	logging.Debugf("CORE_CONF_FILEPATH:    %s\n", CORE_CONF_FILEPATH)
	logging.Debugf("CONF_FILEPATH:         %s\n", CONF_FILEPATH)
	logging.Debugf("REGISTRY_FILEPATH:     %s\n", REGISTRY_FILEPATH)
	logging.Debugf("CONF_GROUP_DIRECTORY:  %s\n", CONF_GROUP_DIRECTORY)
	logging.Debugf("CoreConfig:            %+v\n", CoreConf)
	logging.Debug("CoreConfig Loaded ==============================================")

	core_conf_loaded = true
	return nil
}
func Test_strategy_file_new_core_from_file_Single(t *testing.T) {
	logging.SetLevel("debug")
	config.CONF_FILEPATH, _ = filepath.Abs("./test/config.yml")
	_, err := new_core_from_file()
	if err != nil {
		t.Error("failed")
		return
	}
	// panic sucks.
	if the_core == nil {
		t.Error("no core created. ")
		return
	}
	defer close_core()
}
func TestKafkaSubscription(t *testing.T) {
	logging.SetLevel("debug")
	config.SHARED_DIR = "."

	opts := Config_KafkaSubscription{
		Name: "kafka_sub",
		Broker_list: []string{
			"opsdevhdp02.qa.nt.ctripcorp.com:9092",
			// "oleubuntu:9092",
		},
		Topic:          "test",
		Max_batch_size: 10,
		Flush_interval: "100ms",
	}
	sub, err := NewKafkaSubscription(opts)
	if err != nil {
		t.Errorf("failed to create kafka subscription: %v", err)
		return
	}

	time.AfterFunc(time.Second*10, func() {
		sub.Close()
	})

	timeout := time.After(time.Second * time.Duration(11))

main_loop:
	for {
		select {
		case md, openning := <-sub.Updates():
			if openning {
				if md == nil {
					fmt.Println("md is nil")
				} else {
					fmt.Printf("count: %d\n", len(md))
					for _, dp := range md {
						fmt.Println("dp: ---> ", dp)
					}
				}
			} else {
				break main_loop
			}
		case <-timeout:
			t.Error("timed out! something is blocking")
			break main_loop
		}
	}
}
Exemple #6
0
func Test_kafka_KafkaBackend_mock1(t *testing.T) {
	logging.SetLevel("error")

	conf := &config.Transport_kafka{
		Broker_list: []string{
			"localhost:9092",
		},

		Topic_id:           "test",
		Ack_timeout_ms:     100,
		Flush_frequency_ms: 100,
	}

	b1 := MustNewKafkaBackend("b1", conf)
	b1.kconf.Producer.Return.Successes = true
	mp := mocks.NewAsyncProducer(t, b1.kconf)

	mp.ExpectInputAndSucceed()
	mp.ExpectInputAndSucceed()

	b1.producer = mp

	b1.Updates() <- newcore.MultiDataPoint{
		newcore.NewDataPoint("metric1", 1, time.Now(), nil, "", "", ""),
		newcore.NewDataPoint("metric2", 2, time.Now(), nil, "", "", ""),
	}

	msg1 := <-mp.Successes()
	if msg1.Topic != "test" {
		t.Errorf("topic is not 'test'")
	}

	if key, err := msg1.Key.Encode(); string(key) != "metric1" || err != nil {
		t.Errorf("metric key")
	}

	b1.Close()
	t.Logf("%+v", msg1)
	d, err := msg1.Value.Encode()
	if err != nil {
		t.Errorf("failed to encode data")
	}
	if strings.Index(string(d), "host") < 0 {
		t.Errorf("host tag is not set.")
	}
	t.Logf("data: %s", string(d))

}
func Test_api_info(t *testing.T) {
	logging.SetLevel("debug")
	go serve_api()

	resp, err := http.Get("http://localhost:3031/sys_info?detail=true")
	if err != nil {
		t.Errorf("%v", err)
		return
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Errorf("%v", err)
	}
	t.Log(resp.StatusCode, string(body))
}
func Test_api_registry_revoke(t *testing.T) {
	logging.SetLevel("debug")
	go serve_api()
	config.CORE_CONF_FILEPATH, _ = filepath.Abs("./test/core_config.yml")
	config.CONF_FILEPATH, _ = filepath.Abs("./test/config_wo_groups.yml")

	ioutil.WriteFile(config.REGISTRY_FILEPATH, []byte(`test`), 0644)

	uri, _ := url.Parse("http://localhost:3031/registry/revoke")

	values := url.Values{}
	values.Add("hostname", newcore.GetHostname())
	values.Encode()
	uri.RawQuery = values.Encode()

	resp, err := goreq.Request{
		Method:  "DELETE",
		Uri:     uri.String(),
		Accept:  "application/json",
		Timeout: 1 * time.Second,
	}.Do()

	if err != nil {
		t.Errorf("failed to do revoke: %s", err)
		return
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		t.Errorf("statuscode != 200, %d", resp.StatusCode)
	}

	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Errorf("failed to read response")
	}
	t.Logf("data: %s", data)

	b, err := utils.PathExists(config.REGISTRY_FILEPATH)
	if b != false || err != nil {
		t.Errorf("revoke not working.")
	}
}
Exemple #9
0
func TestInfluxdbBackend(t *testing.T) {
	logging.SetLevel("debug")

	conf := &config.Transport_influxdb{
		Version:         "0.9.0",
		URL:             "http://10.3.6.225:8086/",
		Username:        "******",
		Password:        "******",
		Database:        "mydb",
		RetentionPolicy: "test",
	}

	merge := newcore.Merge(
		newcore.Subscribe(newcore.NewDummyCollector("c1", time.Millisecond*100, 1), nil),
		newcore.Subscribe(newcore.NewDummyCollector("c2", time.Millisecond*100, 1), nil),
	)

	b1, _ := NewInfluxdbBackend("b1", conf)
	fset := newcore.FanOut(merge, b1)

	fset_closed_chan := make(chan error)

	time.AfterFunc(time.Second*time.Duration(2), func() {
		// merge will be closed within FanOut
		fset_closed_chan <- fset.Close()
	})

	timeout := time.After(time.Second * time.Duration(3))

main_loop:
	for {
		select {
		case <-fset_closed_chan:
			fmt.Println("fset closed")
			break main_loop
		case <-timeout:
			t.Error("timed out! something is blocking")
			break main_loop
		}
	}

}
func TestElasticsearchBackend(t *testing.T) {
	logging.SetLevel("debug")

	conf := &config.Transport_elasticsearch{
		URL:   "http://192.168.81.129:9200",
		Index: "test",
		Type:  "test",
	}

	merge := newcore.Merge(
		newcore.Subscribe(newcore.NewDummyCollector("c1", time.Millisecond*100, 1), nil),
		newcore.Subscribe(newcore.NewDummyCollector("c2", time.Millisecond*100, 1), nil),
	)

	b1, _ := NewElasticsearchBackend("b1", conf)
	fset := newcore.FanOut(merge, b1)

	fset_closed_chan := make(chan error)

	time.AfterFunc(time.Second*time.Duration(2), func() {
		// merge will be closed within FanOut
		fset_closed_chan <- fset.Close()
	})

	timeout := time.After(time.Second * time.Duration(3))

main_loop:
	for {
		select {
		case <-fset_closed_chan:
			fmt.Println("fset closed")
			break main_loop
		case <-timeout:
			t.Error("timed out! something is blocking")
			break main_loop
		}
	}

}
Exemple #11
0
func Test_api_registry_revoke_secure(t *testing.T) {
	logging.SetLevel("debug")
	go serve_api()
	config.CORE_CONF_FILEPATH, _ = filepath.Abs("./test/core_config.yml")
	config.CONF_FILEPATH, _ = filepath.Abs("./test/config_wo_groups.yml")
	config.CoreConf.Secure_api_write = true // need to add signature

	unsigner, _ = utils.LoadPublicKey(public_key) // override interval unsigner
	signer, _ := utils.LoadPrivateKey(private_key)

	hostname := newcore.GetHostname()
	now := fmt.Sprintf("%d", time.Now().Unix())

	uri, _ := url.Parse("http://localhost:3031/registry/revoke")
	values := url.Values{}
	values.Add("hostname", newcore.GetHostname())
	values.Add("time", now)
	values.Encode()
	uri.RawQuery = values.Encode()

	go_req := goreq.Request{
		Method:  "DELETE",
		Uri:     uri.String(),
		Accept:  "application/json",
		Timeout: 1 * time.Second,
	}

	// mock registry file before call api
	ioutil.WriteFile(config.REGISTRY_FILEPATH, []byte(`test`), 0644)
	resp, _ := go_req.Do()
	//	if err == nil {
	//		t.Errorf("should fail but not.")
	//		return
	//	}

	if resp.StatusCode == 200 {
		t.Errorf("should fail but not")
		return
	}

	if b, _ := utils.PathExists(config.REGISTRY_FILEPATH); b != true {
		t.Errorf("should fail but not")
		return
	}
	resp.Body.Close()

	toSign := fmt.Sprintf("%s%s", hostname, now)
	sign, _ := signer.Sign([]byte(toSign))
	sign_str := base64.StdEncoding.EncodeToString(sign)
	go_req.AddHeader("HICKWALL_ADMIN_SIGN", sign_str)

	// mock registry file before call api
	ioutil.WriteFile(config.REGISTRY_FILEPATH, []byte(`test`), 0644)
	resp, err := go_req.Do()
	if err != nil {
		t.Errorf("should work but not: %v", err)
		return
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		t.Errorf("statuscode != 200, %d", resp.StatusCode)
		return
	}

	b, err := utils.PathExists(config.REGISTRY_FILEPATH)
	if b != false || err != nil {
		t.Errorf("revoke not working.")
	}
}