Пример #1
0
func TestConfig(t *testing.T) {
	config := dummyConfig()

	assert.Equal(t, len(config.Hosts), 2)
	assert.Equal(t, config.Hosts["toto"].Host, "1.2.3.4")
	assert.Equal(t, config.Defaults.Port, uint(22))
}
Пример #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"])
}
Пример #3
0
func TestUserSuppliedMsgFieldHasPrefix(t *testing.T) {
	LogAndAssertJSON(t, func(log *Logger) {
		log.WithField("msg", "hello").Info("test")
	}, func(fields Fields) {
		assert.Equal(t, fields["msg"], "test")
		assert.Equal(t, fields["fields.msg"], "hello")
	})
}
Пример #4
0
func TestUserSuppliedLevelFieldHasPrefix(t *testing.T) {
	LogAndAssertJSON(t, func(log *Logger) {
		log.WithField("level", 1).Info("test")
	}, func(fields Fields) {
		assert.Equal(t, fields["level"], "info")
		assert.Equal(t, fields["fields.level"], 1.0) // JSON has floats only
	})
}
Пример #5
0
func TestInfo(t *testing.T) {
	LogAndAssertJSON(t, func(log *Logger) {
		log.Info("test")
	}, func(fields Fields) {
		assert.Equal(t, fields["msg"], "test")
		assert.Equal(t, fields["level"], "info")
	})
}
Пример #6
0
func TestWarn(t *testing.T) {
	LogAndAssertJSON(t, func(log *Logger) {
		log.Warn("test")
	}, func(fields Fields) {
		assert.Equal(t, fields["msg"], "test")
		assert.Equal(t, fields["level"], "warning")
	})
}
Пример #7
0
func TestNew(t *testing.T) {
	config := New()

	assert.Equal(t, len(config.Hosts), 0)
	assert.Equal(t, config.Defaults.Port, uint(0))
	assert.Equal(t, config.Defaults.Host, "")
	assert.Equal(t, config.Defaults.User, "")
}
Пример #8
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)
	})
}
Пример #9
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)
	})
}
Пример #10
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())
}
Пример #11
0
func TestInfoShouldAddSpacesBetweenTwoNonStrings(t *testing.T) {
	LogAndAssertJSON(t, func(log *Logger) {
		log.Infoln(10, 10)
	}, func(fields Fields) {
		assert.Equal(t, fields["msg"], "10 10")
	})
}
Пример #12
0
func TestInfolnShouldAddSpacesBetweenStringAndNonstring(t *testing.T) {
	LogAndAssertJSON(t, func(log *Logger) {
		log.Infoln("test", 10)
	}, func(fields Fields) {
		assert.Equal(t, fields["msg"], "test 10")
	})
}
Пример #13
0
func TestInfoShouldNotAddSpacesBetweenStrings(t *testing.T) {
	LogAndAssertJSON(t, func(log *Logger) {
		log.Info("test", "test")
	}, func(fields Fields) {
		assert.Equal(t, fields["msg"], "testtest")
	})
}
Пример #14
0
func TestUserSuppliedFieldDoesNotOverwriteDefaults(t *testing.T) {
	LogAndAssertJSON(t, func(log *Logger) {
		log.WithField("msg", "hello").Info("test")
	}, func(fields Fields) {
		assert.Equal(t, fields["msg"], "test")
	})
}
Пример #15
0
func TestHostApplyDefaults_empty(t *testing.T) {
	host := &Host{}
	defaults := &Host{}
	host.ApplyDefaults(defaults)
	assert.Equal(t, host.Port, uint(22))
	assert.Equal(t, host.Name, "")
	assert.Equal(t, host.Host, "")
	assert.Equal(t, host.User, "")
	assert.Equal(t, len(host.Gateways), 0)
	assert.Equal(t, host.ProxyCommand, "")
	assert.Equal(t, len(host.ResolveNameservers), 0)
	assert.Equal(t, host.ResolveCommand, "")
	assert.Equal(t, host.ControlPath, "")
}
Пример #16
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)
	})
}
Пример #17
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")
	})
}
Пример #18
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)
}
Пример #19
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")

}
Пример #20
0
func TestConvertLevelToString(t *testing.T) {
	assert.Equal(t, "debug", DebugLevel.String())
	assert.Equal(t, "info", InfoLevel.String())
	assert.Equal(t, "warning", WarnLevel.String())
	assert.Equal(t, "error", ErrorLevel.String())
	assert.Equal(t, "fatal", FatalLevel.String())
	assert.Equal(t, "panic", PanicLevel.String())
}
Пример #21
0
func TestHostApplyDefaults(t *testing.T) {
	host := &Host{
		Name: "example",
		Host: "example.com",
		User: "******",
	}
	defaults := &Host{
		User: "******",
		Port: 42,
	}
	host.ApplyDefaults(defaults)
	assert.Equal(t, host.Port, uint(42))
	assert.Equal(t, host.Name, "example")
	assert.Equal(t, host.Host, "example.com")
	assert.Equal(t, host.User, "root")
	assert.Equal(t, len(host.Gateways), 0)
	assert.Equal(t, host.ProxyCommand, "")
	assert.Equal(t, len(host.ResolveNameservers), 0)
	assert.Equal(t, host.ResolveCommand, "")
	assert.Equal(t, host.ControlPath, "")
}
Пример #22
0
func TestConfigLoadFile(t *testing.T) {
	config := New()
	err := config.LoadConfig(strings.NewReader(`
hosts:
  aaa:
    host: 1.2.3.4
  bbb:
    port: 21
  ccc:
    host: 5.6.7.8
    port: 24
    user: toor
defaults:
  port: 22
  user: root
`))
	assert.Nil(t, err)
	assert.Equal(t, len(config.Hosts), 3)
	assert.Equal(t, config.Hosts["aaa"].Host, "1.2.3.4")
	assert.Equal(t, config.Hosts["aaa"].Port, uint(0))
	assert.Equal(t, config.Hosts["aaa"].User, "")
	assert.Equal(t, config.Hosts["bbb"].Host, "")
	assert.Equal(t, config.Hosts["bbb"].Port, uint(21))
	assert.Equal(t, config.Hosts["bbb"].User, "")
	assert.Equal(t, config.Hosts["ccc"].Host, "5.6.7.8")
	assert.Equal(t, config.Hosts["ccc"].Port, uint(24))
	assert.Equal(t, config.Hosts["ccc"].User, "toor")
	assert.Equal(t, config.Defaults.Port, uint(22))
	assert.Equal(t, config.Defaults.User, "root")
}