Beispiel #1
0
// In normal operation, we expect one access log entry,
// and one data collector entry. Let's assume both will succeed.
// We should return a HTTP 200 status.
func TestCollectSuccessfully(t *testing.T) {
	dataCollectorMock := mocks.NewSyncProducer(t, nil)
	dataCollectorMock.ExpectSendMessageAndSucceed()

	accessLogProducerMock := mocks.NewAsyncProducer(t, nil)
	accessLogProducerMock.ExpectInputAndSucceed()

	// Now, use dependency injection to use the mocks.
	s := &Server{
		DataCollector:     dataCollectorMock,
		AccessLogProducer: accessLogProducerMock,
	}

	// The Server's Close call is important; it will call Close on
	// the two mock producers, which will then validate whether all
	// expectations are resolved.
	defer safeClose(t, s)

	req, err := http.NewRequest("GET", "http://example.com/?data", nil)
	if err != nil {
		t.Fatal(err)
	}
	res := httptest.NewRecorder()
	s.Handler().ServeHTTP(res, req)

	if res.Code != 200 {
		t.Errorf("Expected HTTP status 200, found %d", res.Code)
	}

	if string(res.Body.Bytes()) != "Your data is stored with unique identifier important/0/1" {
		t.Error("Unexpected response body", res.Body)
	}
}
Beispiel #2
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))

}
Beispiel #3
0
// We don't expect any data collector calls because the path is wrong,
// so we are not setting any expectations on the dataCollectorMock. It
// will still generate an access log entry though.
func TestWrongPath(t *testing.T) {
	dataCollectorMock := mocks.NewSyncProducer(t, nil)

	accessLogProducerMock := mocks.NewAsyncProducer(t, nil)
	accessLogProducerMock.ExpectInputAndSucceed()

	s := &Server{
		DataCollector:     dataCollectorMock,
		AccessLogProducer: accessLogProducerMock,
	}
	defer safeClose(t, s)

	req, err := http.NewRequest("GET", "http://example.com/wrong?data", nil)
	if err != nil {
		t.Fatal(err)
	}
	res := httptest.NewRecorder()

	s.Handler().ServeHTTP(res, req)

	if res.Code != 404 {
		t.Errorf("Expected HTTP status 404, found %d", res.Code)
	}
}
Beispiel #4
0
// Now, let's see if we handle the case of not being able to produce
// to the data collector properly. In this case we should return a 500 status.
func TestCollectionFailure(t *testing.T) {
	dataCollectorMock := mocks.NewSyncProducer(t, nil)
	dataCollectorMock.ExpectSendMessageAndFail(sarama.ErrRequestTimedOut)

	accessLogProducerMock := mocks.NewAsyncProducer(t, nil)
	accessLogProducerMock.ExpectInputAndSucceed()

	s := &Server{
		DataCollector:     dataCollectorMock,
		AccessLogProducer: accessLogProducerMock,
	}
	defer safeClose(t, s)

	req, err := http.NewRequest("GET", "http://example.com/?data", nil)
	if err != nil {
		t.Fatal(err)
	}
	res := httptest.NewRecorder()
	s.Handler().ServeHTTP(res, req)

	if res.Code != 500 {
		t.Errorf("Expected HTTP status 500, found %d", res.Code)
	}
}