Exemplo n.º 1
0
func TestDeleteData(t *testing.T) {
	intern := 0
	key := "test"
	data := []byte("pippo")
	var value []byte
	var err error

	//INTERNAL
	dataStore = intern
	Initialize()
	StoreData(key, data, enum.STATS)
	StoreData(key, data, enum.ANALYTICS)
	StoreData(key, data, enum.POLICIES)
	StoreData(key, data, enum.SHARED)

	err = DeleteData(key, enum.STATS)
	assert.NoError(t, err)
	value, _ = GetData(key, enum.STATS)
	assert.Nil(t, value)
	err = DeleteData(key, enum.ANALYTICS)
	assert.NoError(t, err)
	value, _ = GetData(key, enum.ANALYTICS)
	assert.Nil(t, value)
	err = DeleteData(key, enum.POLICIES)
	assert.NoError(t, err)
	value, _ = GetData(key, enum.POLICIES)
	assert.Nil(t, value)
	err = DeleteData(key, enum.SHARED)
	assert.NoError(t, err)
	value, _ = GetData(key, enum.SHARED)
	assert.Nil(t, value)
}
Exemplo n.º 2
0
func TestWithFieldsShouldAllowAssignments(t *testing.T) {
	var buffer bytes.Buffer
	var fields Fields

	logger := New()
	logger.Out = &buffer
	logger.Formatter = new(JSONFormatter)

	localLog := logger.WithFields(Fields{
		"key1": "value1",
	})

	localLog.WithField("key2", "value2").Info("test")
	err := json.Unmarshal(buffer.Bytes(), &fields)
	assert.Nil(t, err)

	assert.Equal(t, "value2", fields["key2"])
	assert.Equal(t, "value1", fields["key1"])

	buffer = bytes.Buffer{}
	fields = Fields{}
	localLog.Info("test")
	err = json.Unmarshal(buffer.Bytes(), &fields)
	assert.Nil(t, err)

	_, ok := fields["key2"]
	assert.Equal(t, false, ok)
	assert.Equal(t, "value1", fields["key1"])
}
Exemplo n.º 3
0
func TestPushValues(t *testing.T) {
	values := []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0}
	var b Buffer
	var res []float64

	b = BuildBuffer(10)
	res = b.PushValues(values)
	assert.Nil(t, res)
	assert.Len(t, b.values, len(values))

	b = BuildBuffer(6)
	res = b.PushValues(values)
	assert.NotNil(t, res)
	assert.Len(t, res, len(values))
	assert.Empty(t, b.values)

	b = BuildBuffer(2)
	res = b.PushValues(values)
	assert.NotNil(t, res)
	assert.Len(t, res, len(values))
	assert.Empty(t, b.values)

	b = BuildBuffer(4)
	res = b.PushValues(values)
	assert.NotNil(t, res)
	assert.Len(t, res, b.capacity)
	assert.Len(t, b.values, len(values)-b.capacity)
}
Exemplo n.º 4
0
func TestDoubleLoggingDoesntPrefixPreviousFields(t *testing.T) {

	var buffer bytes.Buffer
	var fields Fields

	logger := New()
	logger.Out = &buffer
	logger.Formatter = new(JSONFormatter)

	llog := logger.WithField("context", "eating raw fish")

	llog.Info("looks delicious")

	err := json.Unmarshal(buffer.Bytes(), &fields)
	assert.NoError(t, err, "should have decoded first message")
	assert.Equal(t, len(fields), 4, "should only have msg/time/level/context fields")
	assert.Equal(t, fields["msg"], "looks delicious")
	assert.Equal(t, fields["context"], "eating raw fish")

	buffer.Reset()

	llog.Warn("omg it is!")

	err = json.Unmarshal(buffer.Bytes(), &fields)
	assert.NoError(t, err, "should have decoded second message")
	assert.Equal(t, len(fields), 4, "should only have msg/time/level/context fields")
	assert.Equal(t, fields["msg"], "omg it is!")
	assert.Equal(t, fields["context"], "eating raw fish")
	assert.Nil(t, fields["fields.msg"], "should not have prefixed previous `msg` entry")

}
Exemplo n.º 5
0
func TestParseLevel(t *testing.T) {
	l, err := ParseLevel("panic")
	assert.Nil(t, err)
	assert.Equal(t, PanicLevel, l)

	l, err = ParseLevel("fatal")
	assert.Nil(t, err)
	assert.Equal(t, FatalLevel, l)

	l, err = ParseLevel("error")
	assert.Nil(t, err)
	assert.Equal(t, ErrorLevel, l)

	l, err = ParseLevel("warn")
	assert.Nil(t, err)
	assert.Equal(t, WarnLevel, l)

	l, err = ParseLevel("warning")
	assert.Nil(t, err)
	assert.Equal(t, WarnLevel, l)

	l, err = ParseLevel("info")
	assert.Nil(t, err)
	assert.Equal(t, InfoLevel, l)

	l, err = ParseLevel("debug")
	assert.Nil(t, err)
	assert.Equal(t, DebugLevel, l)

	l, err = ParseLevel("invalid")
	assert.Equal(t, "not a valid logrus Level: \"invalid\"", err.Error())
}
Exemplo n.º 6
0
func LogAndAssertJSON(t *testing.T, log func(*Logger), assertions func(fields Fields)) {
	var buffer bytes.Buffer
	var fields Fields

	logger := New()
	logger.Out = &buffer
	logger.Formatter = new(JSONFormatter)

	log(logger)

	err := json.Unmarshal(buffer.Bytes(), &fields)
	assert.Nil(t, err)

	assertions(fields)
}
Exemplo n.º 7
0
func TestPushValue(t *testing.T) {
	value := 1.0
	var b Buffer
	var res []float64

	b = BuildBuffer(10)
	res = b.PushValue(value)
	res = b.PushValue(value)
	res = b.PushValue(value)
	assert.Nil(t, res)
	assert.Len(t, b.values, 3)

	b = BuildBuffer(1)
	res = b.PushValue(value)
	assert.NotNil(t, res)
	assert.Empty(t, b.values)
}