示例#1
0
func TestHookFires(t *testing.T) {
	hook := new(TestHook)

	LogAndAssertJSON(t, func(log *Logger) {
		log.Hooks.Add(hook)
		assert.Equal(t, hook.Fired, false)

		log.Print("test")
	}, func(fields Fields) {
		assert.Equal(t, hook.Fired, true)
	})
}
示例#2
0
func TestCanFireMultipleHooks(t *testing.T) {
	hook1 := new(ModifyHook)
	hook2 := new(TestHook)

	LogAndAssertJSON(t, func(log *Logger) {
		log.Hooks.Add(hook1)
		log.Hooks.Add(hook2)

		log.WithField("wow", "elephant").Print("test")
	}, func(fields Fields) {
		assert.Equal(t, fields["wow"], "whale")
		assert.Equal(t, hook2.Fired, true)
	})
}
示例#3
0
func TestHookCanModifyEntry(t *testing.T) {
	hook := new(ModifyHook)

	LogAndAssertJSON(t, func(log *Logger) {
		log.Hooks.Add(hook)
		log.WithField("wow", "elephant").Print("test")
	}, func(fields Fields) {
		assert.Equal(t, fields["wow"], "whale")
	})
}
示例#4
0
func TestErrorHookShouldFireOnError(t *testing.T) {
	hook := new(ErrorHook)

	LogAndAssertJSON(t, func(log *Logger) {
		log.Hooks.Add(hook)
		log.Error("test")
	}, func(fields Fields) {
		assert.Equal(t, hook.Fired, true)
	})
}
示例#5
0
func TestPushToMessageMap(t *testing.T) {
	gpe := githubPushEvent{}
	json.Unmarshal([]byte(ghPushPayload), &gpe)
	// FIXME mock this somehow; we don't want to actually reach out
	m := gpe.ToMessage("")

	if len(m.C) != 2 {
		t.Errorf("Commits array should have two elements, got %d\n", len(m.C))
	}

	assert.Equal(t, m.C[0], semantic.Commit{
		Sha1Str:    "b75da01c073384926e782a4371195c851f45b20f",
		Subject:    "one commit, will it show?",
		Author:     "\"Sam Boyer\" <*****@*****.**>",
		Date:       "Mon Oct 12 2015 21:39:52 -0400",
		Repository: "https://github.com/sdboyer/testrepo",
		ParentsStr: []string{
			"5627b4bf954465918bd9ede94a2484be03ddb44b",
		},
	})

	assert.Equal(t, m.C[1], semantic.Commit{
		Sha1Str:    "4d59fb584b15a94d7401e356d2875c472d76ef45",
		Subject:    "second commit, should show both",
		Author:     "\"Sam Boyer\" <*****@*****.**>",
		Date:       "Mon Oct 12 2015 21:40:18 -0400",
		Repository: "https://github.com/sdboyer/testrepo",
		ParentsStr: []string{
			"b75da01c073384926e782a4371195c851f45b20f",
		},
	})

	if len(m.Cm) != 1 {
		t.Errorf("Should be one item in commit meta, found %d\n", len(m.Cm))
	}

	if !reflect.DeepEqual(m.Cm[0], semantic.CommitMeta{
		Sha1Str:  "4d59fb584b15a94d7401e356d2875c472d76ef45",
		Branches: []string{"master"},
	}) {
		t.Error("Commit meta not as expected")
	}
}
示例#6
0
func TestLogstashFormatter(t *testing.T) {
	assert := assert.New(t)

	lf := LogstashFormatter{Type: "abc"}

	fields := logrus.Fields{
		"message": "def",
		"level":   "ijk",
		"type":    "lmn",
		"one":     1,
		"pi":      3.14,
		"bool":    true,
	}

	entry := logrus.WithFields(fields)
	entry.Message = "msg"
	entry.Level = logrus.InfoLevel

	b, _ := lf.Format(entry)

	var data map[string]interface{}
	dec := json.NewDecoder(bytes.NewReader(b))
	dec.UseNumber()
	dec.Decode(&data)

	// base fields
	assert.Equal(json.Number("1"), data["@version"])
	assert.NotEmpty(data["@timestamp"])
	assert.Equal("abc", data["type"])
	assert.Equal("msg", data["message"])
	assert.Equal("info", data["level"])

	// substituted fields
	assert.Equal("def", data["fields.message"])
	assert.Equal("ijk", data["fields.level"])
	assert.Equal("lmn", data["fields.type"])

	// formats
	assert.Equal(json.Number("1"), data["one"])
	assert.Equal(json.Number("3.14"), data["pi"])
	assert.Equal(true, data["bool"])
}
示例#7
0
func TestEntryPanicf(t *testing.T) {
	errBoom := fmt.Errorf("boom again")

	defer func() {
		p := recover()
		assert.NotNil(t, p)

		switch pVal := p.(type) {
		case *Entry:
			assert.Equal(t, "kaboom true", pVal.Message)
			assert.Equal(t, errBoom, pVal.Data["err"])
		default:
			t.Fatalf("want type *Entry, got %T: %#v", pVal, pVal)
		}
	}()

	logger := New()
	logger.Out = &bytes.Buffer{}
	entry := NewEntry(logger)
	entry.WithField("err", errBoom).Panicf("kaboom %v", true)
}
示例#8
0
func TestBothFilter(t *testing.T) {
	var _ system.VEFilter = bothFilter{}

	assert.Equal(t, Qbe().And(Qbv()), bothFilter{vertexFilter{}, edgeFilter{}}, "Qbe And()'d with Qbv creates an empty bothFilter")
	assert.Equal(t, Qbv().And(Qbe()), bothFilter{vertexFilter{}, edgeFilter{}}, "Qbv And()'d with Qbe creates an empty bothFilter")

	correctv := vertexFilter{vtype: system.VTypeNone, props: []system.PropPair{{"Vfoo", "Vbar"}}}
	correcte := edgeFilter{etype: system.ETypeNone, props: []system.PropPair{{"Efoo", "Ebar"}}}
	correct := bothFilter{correctv, correcte}

	vfirst := Qbv(system.VTypeNone, "Vfoo", "Vbar").And(Qbe(system.ETypeNone, "Efoo", "Ebar"))
	efirst := Qbe(system.ETypeNone, "Efoo", "Ebar").And(Qbv(system.VTypeNone, "Vfoo", "Vbar"))

	assert.Equal(t, vfirst, correct, "Qbv And()d with Qbe creates a correct bothFilter struct instance")
	assert.Equal(t, efirst, correct, "Qbe And()d with Qbv creates a correct bothFilter struct instance")

	assert.Equal(t, correct.VType(), correctv.vtype, "correct bothFilter reports correct VType")
	assert.Equal(t, correct.EType(), correcte.etype, "correct bothFilter reports correct EType")
	assert.Equal(t, correct.VProps(), correctv.props, "correct BothFilter reports correct vertex props")
	assert.Equal(t, correct.EProps(), correcte.props, "correct BothFilter reports correct edge props")
}
示例#9
0
func TestQbv(t *testing.T) {
	// ensure implement both VFilter and EFilter interfaces, and the V chainer
	var _ system.VFilterChain = vertexFilter{}
	var _ system.VEFilter = vertexFilter{}

	assert.Equal(t, Qbv(), vertexFilter{}, "qbv with no args creates an empty vertexFilter")
	assert.Equal(t, Qbv(), vertexFilter{vtype: system.VTypeNone}, "qbv with no args creates equivalent of passing VTypeNone as first arg")
	assert.Equal(t,
		Qbv(system.VType("foo")),
		vertexFilter{vtype: system.VType("foo")},
		"qbv with single arg assigns to VType struct prop")
	assert.Equal(t,
		Qbv(system.VTypeNone, "foo"),
		vertexFilter{vtype: system.VTypeNone},
		"qbv with two args ignores second (unpaired) arg")
	assert.Equal(t,
		Qbv(system.VTypeNone, "foo", "bar"),
		vertexFilter{vtype: system.VTypeNone, props: []system.PropPair{{"foo", "bar"}}},
		"qbv with three args creates one pair of second (key) and third (value) args")
	assert.Equal(t,
		Qbv(system.VTypeNone, "foo", "bar", "baz"),
		vertexFilter{vtype: system.VTypeNone, props: []system.PropPair{{"foo", "bar"}}},
		"qbv with four args creates one pair from 2nd and 3rd args, ignores 4th")

	// ensure that some incorrect things owing to loose typing correctly panic
	assert.Panics(t, func() {
		Qbv("foo")
	}, "qbv panics on type conversion when passing a string instead of VType")

	assert.Panics(t, func() {
		Qbv(system.VTypeNone, 1, "foo")
	}, "qbv panics on type conversion when second argument (with corresponding pair val 3rd arg) is non-string")

	assert.Panics(t, func() {
		Qbv(system.VTypeNone, "foo", "bar", 1, "baz")
	}, "qbv panics on type conversion when Nth even argument (with corresponding pair val N+1 arg) is non-string")
}
示例#10
0
func TestResultErrorFormatNumber(t *testing.T) {
	assert.Equal(t, "1", resultErrorFormatNumber(1))
	assert.Equal(t, "-1", resultErrorFormatNumber(-1))
	assert.Equal(t, "0", resultErrorFormatNumber(0))
	// unfortunately, can not be recognized as float
	assert.Equal(t, "0", resultErrorFormatNumber(0.0))

	assert.Equal(t, "1.001", resultErrorFormatNumber(1.001))
	assert.Equal(t, "-1.001", resultErrorFormatNumber(-1.001))
	assert.Equal(t, "0.0001", resultErrorFormatNumber(0.0001))

	// casting math.MaxInt64 (1<<63 -1) to float back to int64
	// becomes negative. obviousely because of bit missinterpretation.
	// so simply test a slightly smaller "large" integer here
	assert.Equal(t, "4.611686018427388e+18", resultErrorFormatNumber(1<<62))
	// with negative int64 max works
	assert.Equal(t, "-9.223372036854776e+18", resultErrorFormatNumber(math.MinInt64))
	assert.Equal(t, "-4.611686018427388e+18", resultErrorFormatNumber(-1<<62))

	assert.Equal(t, "10000000000", resultErrorFormatNumber(1e10))
	assert.Equal(t, "-10000000000", resultErrorFormatNumber(-1e10))

	assert.Equal(t, "1.000000000001", resultErrorFormatNumber(1.000000000001))
	assert.Equal(t, "-1.000000000001", resultErrorFormatNumber(-1.000000000001))
	assert.Equal(t, "1e-10", resultErrorFormatNumber(1e-10))
	assert.Equal(t, "-1e-10", resultErrorFormatNumber(-1e-10))
	assert.Equal(t, "4.6116860184273876e+07", resultErrorFormatNumber(4.611686018427387904e7))
	assert.Equal(t, "-4.6116860184273876e+07", resultErrorFormatNumber(-4.611686018427387904e7))

}