本文整理匯總了Golang中github.com/juju/loggo.TestWriter.Log方法的典型用法代碼示例。如果您正苦於以下問題:Golang TestWriter.Log方法的具體用法?Golang TestWriter.Log怎麽用?Golang TestWriter.Log使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/juju/loggo.TestWriter
的用法示例。
在下文中一共展示了TestWriter.Log方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: assertTestMongoGetFails
func (s *MongoSuite) assertTestMongoGetFails(c *gc.C, series string, packageManager string) {
s.patchSeries(series)
// Any exit code from apt-get that isn't 0 or 100 will be treated
// as unexpected, skipping the normal retry loop. failCmd causes
// the command to exit with 1.
binDir := c.MkDir()
s.PatchEnvPathPrepend(binDir)
failCmd(filepath.Join(binDir, packageManager))
// Set the mongodb service as installed but not running.
s.data.SetStatus(mongo.ServiceName, "installed")
var tw loggo.TestWriter
c.Assert(loggo.RegisterWriter("test-writer", &tw, loggo.ERROR), jc.ErrorIsNil)
defer loggo.RemoveWriter("test-writer")
dataDir := c.MkDir()
err := mongo.EnsureServer(makeEnsureServerParams(dataDir))
// Even though apt-get failed, EnsureServer should continue and
// not return the error - even though apt-get failed, the Juju
// mongodb package is most likely already installed.
// The error should be logged however.
c.Assert(err, jc.ErrorIsNil)
c.Check(tw.Log(), jc.LogMatches, []jc.SimpleMessage{
{loggo.ERROR, `packaging command failed: .+`},
{loggo.ERROR, `cannot install/upgrade mongod \(will proceed anyway\): packaging command failed`},
})
// Verify that EnsureServer continued and started the mongodb service.
c.Check(s.data.Installed(), gc.HasLen, 0)
s.data.CheckCallNames(c, "Installed", "Exists", "Running", "Start")
}
示例2: TestQuantalAptAddRepo
func (s *MongoSuite) TestQuantalAptAddRepo(c *gc.C) {
dir := c.MkDir()
// patch manager.RunCommandWithRetry for repository addition:
s.PatchValue(&manager.RunCommandWithRetry, func(string) (string, int, error) {
return "", 1, fmt.Errorf("packaging command failed: exit status 1")
})
s.PatchEnvPathPrepend(dir)
failCmd(filepath.Join(dir, "add-apt-repository"))
mockShellCommand(c, &s.CleanupSuite, "apt-get")
var tw loggo.TestWriter
c.Assert(loggo.RegisterWriter("test-writer", &tw, loggo.ERROR), jc.ErrorIsNil)
defer loggo.RemoveWriter("test-writer")
// test that we call add-apt-repository only for quantal
// (and that if it fails, we log the error)
s.PatchValue(&version.Current.Series, "quantal")
err := mongo.EnsureServer(makeEnsureServerParams(dir, ""))
c.Assert(err, jc.ErrorIsNil)
c.Assert(tw.Log(), jc.LogMatches, []jc.SimpleMessage{
{loggo.ERROR, `cannot install/upgrade mongod \(will proceed anyway\): packaging command failed`},
})
s.PatchValue(&manager.RunCommandWithRetry, func(string) (string, int, error) {
return "", 0, nil
})
s.PatchValue(&version.Current.Series, "trusty")
failCmd(filepath.Join(dir, "mongod"))
err = mongo.EnsureServer(makeEnsureServerParams(dir, ""))
c.Assert(err, jc.ErrorIsNil)
}
示例3: TestFindToolsFiltering
func (s *SimpleStreamsToolsSuite) TestFindToolsFiltering(c *gc.C) {
var tw loggo.TestWriter
c.Assert(loggo.RegisterWriter("filter-tester", &tw, loggo.TRACE), gc.IsNil)
defer loggo.RemoveWriter("filter-tester")
logger := loggo.GetLogger("juju.environs")
defer logger.SetLogLevel(logger.LogLevel())
logger.SetLogLevel(loggo.TRACE)
_, err := envtools.FindTools(
s.env, 1, -1, "released", coretools.Filter{Number: version.Number{Major: 1, Minor: 2, Patch: 3}})
c.Assert(err, jc.Satisfies, errors.IsNotFound)
// This is slightly overly prescriptive, but feel free to change or add
// messages. This still helps to ensure that all log messages are
// properly formed.
messages := []jc.SimpleMessage{
{loggo.INFO, "reading tools with major version 1"},
{loggo.INFO, "filtering tools by version: \\d+\\.\\d+\\.\\d+"},
{loggo.TRACE, "no architecture specified when finding tools, looking for "},
{loggo.TRACE, "no series specified when finding tools, looking for \\[.*\\]"},
}
sources, err := envtools.GetMetadataSources(s.env)
c.Assert(err, jc.ErrorIsNil)
for i := 0; i < 2*len(sources); i++ {
messages = append(messages,
jc.SimpleMessage{loggo.TRACE, `fetchData failed for .*`},
jc.SimpleMessage{loggo.TRACE, `cannot load index .*`})
}
c.Check(tw.Log(), jc.LogMatches, messages)
}
示例4: TestSyncToolsCommandDeprecatedDestination
func (s *syncToolsSuite) TestSyncToolsCommandDeprecatedDestination(c *gc.C) {
called := false
dir := c.MkDir()
syncTools = func(sctx *sync.SyncContext) error {
c.Assert(sctx.AllVersions, gc.Equals, false)
c.Assert(sctx.DryRun, gc.Equals, false)
c.Assert(sctx.Dev, gc.Equals, false)
c.Assert(sctx.Source, gc.Equals, "")
url, err := sctx.Target.URL("")
c.Assert(err, gc.IsNil)
c.Assert(url, gc.Equals, "file://"+dir)
called = true
return nil
}
// Register writer.
var tw loggo.TestWriter
c.Assert(loggo.RegisterWriter("deprecated-tester", &tw, loggo.DEBUG), gc.IsNil)
defer loggo.RemoveWriter("deprecated-tester")
// Add deprecated message to be checked.
messages := []jc.SimpleMessage{
{loggo.WARNING, "Use of the --destination flag is deprecated in 1.18. Please use --local-dir instead."},
}
// Run sync-tools command with --destination flag.
ctx, err := runSyncToolsCommand(c, "-e", "test-target", "--destination", dir)
c.Assert(err, gc.IsNil)
c.Assert(ctx, gc.NotNil)
c.Assert(called, jc.IsTrue)
// Check deprecated message was logged.
c.Check(tw.Log(), jc.LogMatches, messages)
s.Reset(c)
}
示例5: TestSyncToolsCommandDeprecatedDestination
func (s *syncToolsSuite) TestSyncToolsCommandDeprecatedDestination(c *gc.C) {
called := false
dir := c.MkDir()
syncTools = func(sctx *sync.SyncContext) error {
c.Assert(sctx.AllVersions, jc.IsFalse)
c.Assert(sctx.DryRun, jc.IsFalse)
c.Assert(sctx.Stream, gc.Equals, "released")
c.Assert(sctx.Source, gc.Equals, "")
c.Assert(sctx.TargetToolsUploader, gc.FitsTypeOf, sync.StorageToolsUploader{})
uploader := sctx.TargetToolsUploader.(sync.StorageToolsUploader)
url, err := uploader.Storage.URL("")
c.Assert(err, jc.ErrorIsNil)
c.Assert(url, gc.Equals, utils.MakeFileURL(dir))
called = true
return nil
}
// Register writer.
var tw loggo.TestWriter
c.Assert(loggo.RegisterWriter("deprecated-tester", &tw, loggo.DEBUG), gc.IsNil)
defer loggo.RemoveWriter("deprecated-tester")
// Add deprecated message to be checked.
messages := []jc.SimpleMessage{
{loggo.WARNING, "Use of the --destination flag is deprecated in 1.18. Please use --local-dir instead."},
}
// Run sync-tools command with --destination flag.
ctx, err := runSyncToolsCommand(c, "-m", "test-target", "--destination", dir, "--stream", "released")
c.Assert(err, jc.ErrorIsNil)
c.Assert(ctx, gc.NotNil)
c.Assert(called, jc.IsTrue)
// Check deprecated message was logged.
c.Check(tw.Log(), jc.LogMatches, messages)
}
示例6: TestConcurrentAccess
func (*diskStoreSuite) TestConcurrentAccess(c *gc.C) {
var tw loggo.TestWriter
c.Assert(loggo.RegisterWriter("test-log", &tw, loggo.DEBUG), gc.IsNil)
dir := c.MkDir()
store, err := configstore.NewDisk(dir)
c.Assert(err, jc.ErrorIsNil)
envDir := storePath(dir, "")
lock, err := configstore.AcquireEnvironmentLock(envDir, "blocking-op")
c.Assert(err, jc.ErrorIsNil)
defer lock.Unlock()
_, err = store.ReadInfo("someenv")
c.Assert(errors.Cause(err), gc.Equals, fslock.ErrTimeout)
// Using . between environments and env.lock so we don't have to care
// about forward vs. backwards slash separator.
messages := []jc.SimpleMessage{
{loggo.WARNING, `configstore lock held, lock dir: .*environments.env\.lock`},
{loggo.WARNING, `lock holder message: pid: \d+, operation: blocking-op`},
}
c.Check(tw.Log(), jc.LogMatches, messages)
}
示例7: assertSuccessWithInstallStepFailCentOS
func (s *MongoSuite) assertSuccessWithInstallStepFailCentOS(c *gc.C, exec []string, execNameFail string, returnCode int, expectedResult []jc.SimpleMessage) {
type installs struct {
series string
pkg string
}
test := installs{
"centos7", "mongodb*",
}
for _, e := range exec {
testing.PatchExecutableAsEchoArgs(c, s, e)
}
testing.PatchExecutableThrowError(c, s, execNameFail, returnCode)
dataDir := c.MkDir()
s.patchSeries(test.series)
var tw loggo.TestWriter
c.Assert(loggo.RegisterWriter("mongosuite", &tw, loggo.INFO), jc.ErrorIsNil)
defer loggo.RemoveWriter("mongosuite")
err := mongo.EnsureServer(makeEnsureServerParams(dataDir))
c.Assert(err, jc.ErrorIsNil)
c.Assert(tw.Log(), jc.LogMatches, expectedResult)
}
示例8: gatherLog
func gatherLog(f func()) []loggo.Entry {
var tw loggo.TestWriter
err := loggo.RegisterWriter("test", &tw)
if err != nil {
panic(err)
}
defer loggo.RemoveWriter("test")
f()
return tw.Log()
}
示例9: assertCall
func (s *prepareSuite) assertCall(c *gc.C, args params.Entities, expectResults *params.MachineNetworkConfigResults, expectErr string) (error, []loggo.TestLogValues) {
// Capture the logs for later inspection.
logger := loggo.GetLogger("juju.apiserver.provisioner")
defer logger.SetLogLevel(logger.LogLevel())
logger.SetLogLevel(loggo.TRACE)
var tw loggo.TestWriter
c.Assert(loggo.RegisterWriter("test", &tw, loggo.TRACE), gc.IsNil)
defer loggo.RemoveWriter("test")
results, err := s.provAPI.PrepareContainerInterfaceInfo(args)
c.Logf("PrepareContainerInterfaceInfo returned: err=%v, results=%v", err, results)
c.Assert(results.Results, gc.HasLen, len(args.Entities))
if expectErr == "" {
c.Assert(err, jc.ErrorIsNil)
c.Assert(expectResults, gc.NotNil)
c.Assert(results.Results, gc.HasLen, len(expectResults.Results))
// Check for any "regex:" prefixes first. Then replace
// addresses in expected with the actual ones, so we can use
// jc.DeepEquals on the whole result below.
// Also check MAC addresses are valid, but as they're randomly
// generated we can't test specific values.
for i, expect := range expectResults.Results {
cfg := results.Results[i].Config
c.Assert(cfg, gc.HasLen, len(expect.Config))
for j, expCfg := range expect.Config {
if strings.HasPrefix(expCfg.Address, "regex:") {
rex := strings.TrimPrefix(expCfg.Address, "regex:")
c.Assert(cfg[j].Address, gc.Matches, rex)
expectResults.Results[i].Config[j].Address = cfg[j].Address
}
macAddress := cfg[j].MACAddress
c.Assert(macAddress[:8], gc.Equals, provisioner.MACAddressTemplate[:8])
remainder := strings.Replace(macAddress[8:], ":", "", 3)
c.Assert(remainder, gc.HasLen, 6)
_, err = hex.DecodeString(remainder)
c.Assert(err, jc.ErrorIsNil)
expectResults.Results[i].Config[j].MACAddress = macAddress
}
}
c.Assert(results, jc.DeepEquals, *expectResults)
} else {
c.Assert(err, gc.ErrorMatches, expectErr)
if len(args.Entities) > 0 {
result := results.Results[0]
// Not using jc.ErrorIsNil below because
// (*params.Error)(nil) does not satisfy the error
// interface.
c.Assert(result.Error, gc.IsNil)
c.Assert(result.Config, gc.IsNil)
}
}
return err, tw.Log()
}
示例10: TestAuditEventWrittenToAuditLogger
func (*auditSuite) TestAuditEventWrittenToAuditLogger(c *gc.C) {
var tw loggo.TestWriter
c.Assert(loggo.RegisterWriter("audit-log", &tw, loggo.DEBUG), gc.IsNil)
u := &mockUser{tag: "user-agnus"}
Audit(u, "donut eaten, %v donut(s) remain", 7)
// Add deprecated message to be checked.
messages := []jc.SimpleMessage{
{loggo.INFO, `user-agnus: donut eaten, 7 donut\(s\) remain`},
}
c.Check(tw.Log(), jc.LogMatches, messages)
}
示例11: TestBootstrapJenvWarning
func (s *BootstrapSuite) TestBootstrapJenvWarning(c *gc.C) {
const envName = "devenv"
s.patchVersionAndSeries(c, envName)
store, err := configstore.Default()
c.Assert(err, jc.ErrorIsNil)
ctx := coretesting.Context(c)
environs.PrepareFromName(envName, envcmd.BootstrapContext(ctx), store)
logger := "jenv.warning.test"
var testWriter loggo.TestWriter
loggo.RegisterWriter(logger, &testWriter, loggo.WARNING)
defer loggo.RemoveWriter(logger)
_, errc := cmdtesting.RunCommand(ctx, newBootstrapCommand(), "-e", envName, "--auto-upgrade")
c.Assert(<-errc, gc.IsNil)
c.Assert(testWriter.Log(), jc.LogMatches, []string{"ignoring environments.yaml: using bootstrap config in .*"})
}
示例12: TestSetUnsupportedConstraintsWarning
func (s *ServiceSuite) TestSetUnsupportedConstraintsWarning(c *gc.C) {
defer loggo.ResetWriters()
logger := loggo.GetLogger("test")
logger.SetLogLevel(loggo.DEBUG)
var tw loggo.TestWriter
c.Assert(loggo.RegisterWriter("constraints-tester", &tw, loggo.DEBUG), gc.IsNil)
cons := constraints.MustParse("mem=4G cpu-power=10")
err := s.mysql.SetConstraints(cons)
c.Assert(err, gc.IsNil)
c.Assert(tw.Log(), jc.LogMatches, jc.SimpleMessages{{
loggo.WARNING,
`setting constraints on service "mysql": unsupported constraints: cpu-power`},
})
scons, err := s.mysql.Constraints()
c.Assert(err, gc.IsNil)
c.Assert(scons, gc.DeepEquals, cons)
}
示例13: assertLogs
func assertLogs(c *gc.C, ctx jujuc.Context, writer *loggo.TestWriter, unitname, badge string) {
msg1 := "the chickens"
msg2 := "are 110% AWESOME"
com, err := jujuc.NewCommand(ctx, cmdString("juju-log"))
c.Assert(err, jc.ErrorIsNil)
for _, t := range []struct {
args []string
level loggo.Level
}{
{
level: loggo.INFO,
}, {
args: []string{"--debug"},
level: loggo.DEBUG,
}, {
args: []string{"--log-level", "TRACE"},
level: loggo.TRACE,
}, {
args: []string{"--log-level", "info"},
level: loggo.INFO,
}, {
args: []string{"--log-level", "WaRnInG"},
level: loggo.WARNING,
}, {
args: []string{"--log-level", "error"},
level: loggo.ERROR,
},
} {
writer.Clear()
c.Assert(err, jc.ErrorIsNil)
args := append(t.args, msg1, msg2)
code := cmd.Main(com, &cmd.Context{}, args)
c.Assert(code, gc.Equals, 0)
log := writer.Log()
c.Assert(log, gc.HasLen, 1)
c.Assert(log[0].Level, gc.Equals, t.level)
c.Assert(log[0].Module, gc.Equals, fmt.Sprintf("unit.%s.juju-log", unitname))
c.Assert(log[0].Message, gc.Equals, fmt.Sprintf("%s%s %s", badge, msg1, msg2))
}
}
示例14: TestCannotRecordThenCannotStop
func (s *BootstrapSuite) TestCannotRecordThenCannotStop(c *gc.C) {
innerStorage := newStorage(s, c)
stor := &mockStorage{Storage: innerStorage}
startInstance := func(
_ string, _ constraints.Value, _ []string, _ tools.List, _ *cloudinit.MachineConfig,
) (
instance.Instance, *instance.HardwareCharacteristics, []network.Info, error,
) {
stor.putErr = fmt.Errorf("suddenly a wild blah")
return &mockInstance{id: "i-blah"}, nil, nil, nil
}
var stopped []instance.Id
stopInstances := func(instances []instance.Id) error {
stopped = append(stopped, instances...)
return fmt.Errorf("bork bork borken")
}
var tw loggo.TestWriter
c.Assert(loggo.RegisterWriter("bootstrap-tester", &tw, loggo.DEBUG), gc.IsNil)
defer loggo.RemoveWriter("bootstrap-tester")
env := &mockEnviron{
storage: stor,
startInstance: startInstance,
stopInstances: stopInstances,
config: configGetter(c),
}
ctx := coretesting.Context(c)
_, _, _, err := common.Bootstrap(ctx, env, environs.BootstrapParams{
AvailableTools: tools.List{&tools.Tools{Version: version.Current}},
})
c.Assert(err, gc.ErrorMatches, "cannot save state: suddenly a wild blah")
c.Assert(stopped, gc.HasLen, 1)
c.Assert(stopped[0], gc.Equals, instance.Id("i-blah"))
c.Assert(tw.Log(), jc.LogMatches, []jc.SimpleMessage{{
loggo.ERROR, `cannot stop failed bootstrap instance "i-blah": bork bork borken`,
}})
}
示例15: TestBootstrapJenvWarning
func (s *BootstrapSuite) TestBootstrapJenvWarning(c *gc.C) {
env := resetJujuHome(c, "devenv")
defaultSeriesVersion := version.Current
defaultSeriesVersion.Series = config.PreferredSeries(env.Config())
// Force a dev version by having a non zero build number.
// This is because we have not uploaded any tools and auto
// upload is only enabled for dev versions.
defaultSeriesVersion.Build = 1234
s.PatchValue(&version.Current, defaultSeriesVersion)
store, err := configstore.Default()
c.Assert(err, jc.ErrorIsNil)
ctx := coretesting.Context(c)
environs.PrepareFromName("devenv", envcmd.BootstrapContext(ctx), store)
logger := "jenv.warning.test"
var testWriter loggo.TestWriter
loggo.RegisterWriter(logger, &testWriter, loggo.WARNING)
defer loggo.RemoveWriter(logger)
_, errc := cmdtesting.RunCommand(ctx, envcmd.Wrap(new(BootstrapCommand)), "-e", "devenv")
c.Assert(<-errc, gc.IsNil)
c.Assert(testWriter.Log(), jc.LogMatches, []string{"ignoring environments.yaml: using bootstrap config in .*"})
}