本文整理匯總了Golang中github.com/juju/juju/testing.NonZeroTime函數的典型用法代碼示例。如果您正苦於以下問題:Golang NonZeroTime函數的具體用法?Golang NonZeroTime怎麽用?Golang NonZeroTime使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NonZeroTime函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestMetricsAcrossEnvironments
func (s *CrossModelMetricSuite) TestMetricsAcrossEnvironments(c *gc.C) {
now := s.State.NowToTheSecond().Add(-48 * time.Hour)
m := state.Metric{"pings", "5", now}
m1, err := s.models[0].state.AddMetrics(
state.BatchParam{
UUID: utils.MustNewUUID().String(),
Created: now,
CharmURL: s.models[0].meteredCharm.URL().String(),
Metrics: []state.Metric{m},
Unit: s.models[0].unit.UnitTag(),
},
)
c.Assert(err, jc.ErrorIsNil)
m2, err := s.models[1].state.AddMetrics(
state.BatchParam{
UUID: utils.MustNewUUID().String(),
Created: now,
CharmURL: s.models[1].meteredCharm.URL().String(),
Metrics: []state.Metric{m},
Unit: s.models[1].unit.UnitTag(),
},
)
c.Assert(err, jc.ErrorIsNil)
batches, err := s.State.AllMetricBatches()
c.Assert(err, jc.ErrorIsNil)
c.Assert(batches, gc.HasLen, 2)
unsent, err := s.models[0].state.CountOfUnsentMetrics()
c.Assert(err, jc.ErrorIsNil)
c.Assert(unsent, gc.Equals, 1)
toSend, err := s.models[0].state.MetricsToSend(10)
c.Assert(err, jc.ErrorIsNil)
c.Assert(toSend, gc.HasLen, 1)
err = m1.SetSent(testing.NonZeroTime().Add(-25 * time.Hour))
c.Assert(err, jc.ErrorIsNil)
err = m2.SetSent(testing.NonZeroTime().Add(-25 * time.Hour))
c.Assert(err, jc.ErrorIsNil)
sent, err := s.models[0].state.CountOfSentMetrics()
c.Assert(err, jc.ErrorIsNil)
c.Assert(sent, gc.Equals, 1)
err = s.models[0].state.CleanupOldMetrics()
c.Assert(err, jc.ErrorIsNil)
// The metric from model s.models[1] should still be in place.
batches, err = s.State.AllMetricBatches()
c.Assert(err, jc.ErrorIsNil)
c.Assert(batches, gc.HasLen, 1)
}
示例2: TestCleanupMetrics
func (s *MetricSuite) TestCleanupMetrics(c *gc.C) {
oldTime := testing.NonZeroTime().Add(-(time.Hour * 25))
now := testing.NonZeroTime()
m := state.Metric{"pings", "5", oldTime}
oldMetric1, err := s.State.AddMetrics(
state.BatchParam{
UUID: utils.MustNewUUID().String(),
Created: now,
CharmURL: s.meteredCharm.URL().String(),
Metrics: []state.Metric{m},
Unit: s.unit.UnitTag(),
},
)
c.Assert(err, jc.ErrorIsNil)
oldMetric1.SetSent(testing.NonZeroTime().Add(-25 * time.Hour))
oldMetric2, err := s.State.AddMetrics(
state.BatchParam{
UUID: utils.MustNewUUID().String(),
Created: now,
CharmURL: s.meteredCharm.URL().String(),
Metrics: []state.Metric{m},
Unit: s.unit.UnitTag(),
},
)
c.Assert(err, jc.ErrorIsNil)
oldMetric2.SetSent(testing.NonZeroTime().Add(-25 * time.Hour))
m = state.Metric{"pings", "5", now}
newMetric, err := s.State.AddMetrics(
state.BatchParam{
UUID: utils.MustNewUUID().String(),
Created: now,
CharmURL: s.meteredCharm.URL().String(),
Metrics: []state.Metric{m},
Unit: s.unit.UnitTag(),
},
)
c.Assert(err, jc.ErrorIsNil)
newMetric.SetSent(testing.NonZeroTime())
err = s.State.CleanupOldMetrics()
c.Assert(err, jc.ErrorIsNil)
_, err = s.State.MetricBatch(newMetric.UUID())
c.Assert(err, jc.ErrorIsNil)
_, err = s.State.MetricBatch(oldMetric1.UUID())
c.Assert(err, jc.Satisfies, errors.IsNotFound)
_, err = s.State.MetricBatch(oldMetric2.UUID())
c.Assert(err, jc.Satisfies, errors.IsNotFound)
}
示例3: addMetadataDoc
func (s *ImageSuite) addMetadataDoc(c *gc.C, kind, series, arch string, size int64, checksum, path, sourceURL string) {
doc := struct {
Id string `bson:"_id"`
ModelUUID string `bson:"modelUUID"`
Kind string `bson:"kind"`
Series string `bson:"series"`
Arch string `bson:"arch"`
Size int64 `bson:"size"`
SHA256 string `bson:"sha256,omitempty"`
Path string `bson:"path"`
Created time.Time `bson:"created"`
SourceURL string `bson:"sourceurl"`
}{
Id: fmt.Sprintf("my-uuid-%s-%s-%s", kind, series, arch),
ModelUUID: "my-uuid",
Kind: kind,
Series: series,
Arch: arch,
Size: size,
SHA256: checksum,
Path: path,
Created: testing.NonZeroTime(),
SourceURL: sourceURL,
}
err := s.metadataCollection.Insert(&doc)
c.Assert(err, gc.IsNil)
}
示例4: TestSetCharmStoreResourceOkay
func (s *ResourcePersistenceSuite) TestSetCharmStoreResourceOkay(c *gc.C) {
lastPolled := coretesting.NonZeroTime().UTC()
applicationname := "a-application"
res, doc := newPersistenceResource(c, applicationname, "spam")
expected := doc // a copy
expected.DocID += "#charmstore"
expected.Username = ""
expected.Timestamp = coretesting.ZeroTime()
expected.StoragePath = ""
expected.LastPolled = lastPolled
p := NewResourcePersistence(s.base)
ignoredErr := errors.New("<never reached>")
s.stub.SetErrors(nil, nil, nil, ignoredErr)
err := p.SetCharmStoreResource(res.ID, res.ApplicationID, res.Resource.Resource, lastPolled)
c.Assert(err, jc.ErrorIsNil)
s.stub.CheckCallNames(c,
"Run",
"ApplicationExistsOps",
"RunTransaction",
)
s.stub.CheckCall(c, 2, "RunTransaction", []txn.Op{{
C: "resources",
Id: "resource#a-application/spam#charmstore",
Assert: txn.DocMissing,
Insert: &expected,
}, {
C: "application",
Id: "a-application",
Assert: txn.DocExists,
}})
}
示例5: TestPruneLogsByTime
func (s *LogsSuite) TestPruneLogsByTime(c *gc.C) {
dbLogger := state.NewDbLogger(s.State, names.NewMachineTag("22"), jujuversion.Current)
defer dbLogger.Close()
log := func(t time.Time, msg string) {
err := dbLogger.Log(t, "module", "loc", loggo.INFO, msg)
c.Assert(err, jc.ErrorIsNil)
}
now := coretesting.NonZeroTime()
maxLogTime := now.Add(-time.Minute)
log(now, "keep")
log(maxLogTime.Add(time.Second), "keep")
log(maxLogTime, "keep")
log(maxLogTime.Add(-time.Second), "prune")
log(maxLogTime.Add(-(2 * time.Second)), "prune")
noPruneMB := 100
err := state.PruneLogs(s.State, maxLogTime, noPruneMB)
c.Assert(err, jc.ErrorIsNil)
// After pruning there should just be 3 "keep" messages left.
var docs []bson.M
err = s.logsColl.Find(nil).All(&docs)
c.Assert(err, jc.ErrorIsNil)
c.Assert(docs, gc.HasLen, 3)
for _, doc := range docs {
c.Assert(doc["x"], gc.Equals, "keep")
}
}
示例6: TestTimeFiltering
func (s *LogTailerSuite) TestTimeFiltering(c *gc.C) {
// Add 10 logs that shouldn't be returned.
threshT := coretesting.NonZeroTime()
s.writeLogsT(c,
threshT.Add(-5*time.Second), threshT.Add(-time.Millisecond), 5,
logTemplate{Message: "dont want"},
)
// Add 5 logs that should be returned.
want := logTemplate{Message: "want"}
s.writeLogsT(c, threshT, threshT.Add(5*time.Second), 5, want)
tailer, err := state.NewLogTailer(s.otherState, &state.LogTailerParams{
StartTime: threshT,
Oplog: s.oplogColl,
})
c.Assert(err, jc.ErrorIsNil)
defer tailer.Stop()
s.assertTailer(c, tailer, 5, want)
// Write more logs. These will be read from the the oplog.
want2 := logTemplate{Message: "want 2"}
s.writeLogsT(c, threshT.Add(6*time.Second), threshT.Add(10*time.Second), 5, want2)
s.assertTailer(c, tailer, 5, want2)
}
示例7: TestAddUser
func (s *UserSuite) TestAddUser(c *gc.C) {
name := "f00-Bar.ram77"
displayName := "Display"
password := "password"
creator := "admin"
now := testing.NonZeroTime().Round(time.Second).UTC()
user, err := s.State.AddUser(name, displayName, password, creator)
c.Assert(err, jc.ErrorIsNil)
c.Assert(user, gc.NotNil)
c.Assert(user.Name(), gc.Equals, name)
c.Assert(user.DisplayName(), gc.Equals, displayName)
c.Assert(user.PasswordValid(password), jc.IsTrue)
c.Assert(user.CreatedBy(), gc.Equals, creator)
c.Assert(user.DateCreated().After(now) ||
user.DateCreated().Equal(now), jc.IsTrue)
lastLogin, err := user.LastLogin()
c.Assert(err, jc.Satisfies, state.IsNeverLoggedInError)
c.Assert(lastLogin, gc.DeepEquals, time.Time{})
user, err = s.State.User(user.UserTag())
c.Assert(err, jc.ErrorIsNil)
c.Assert(user, gc.NotNil)
c.Assert(user.Name(), gc.Equals, name)
c.Assert(user.DisplayName(), gc.Equals, displayName)
c.Assert(user.PasswordValid(password), jc.IsTrue)
c.Assert(user.CreatedBy(), gc.Equals, creator)
c.Assert(user.DateCreated().After(now) ||
user.DateCreated().Equal(now), jc.IsTrue)
lastLogin, err = user.LastLogin()
c.Assert(err, jc.Satisfies, state.IsNeverLoggedInError)
c.Assert(lastLogin, gc.DeepEquals, time.Time{})
}
示例8: TestFunctional
func (s *ResourcesSuite) TestFunctional(c *gc.C) {
ch := s.ConnSuite.AddTestingCharm(c, "wordpress")
s.ConnSuite.AddTestingService(c, "a-application", ch)
st, err := s.State.Resources()
c.Assert(err, jc.ErrorIsNil)
resources, err := st.ListResources("a-application")
c.Assert(err, jc.ErrorIsNil)
c.Check(resources.Resources, gc.HasLen, 0)
data := "spamspamspam"
res := newResource(c, "spam", data)
file := bytes.NewBufferString(data)
_, err = st.SetResource("a-application", res.Username, res.Resource, file)
c.Assert(err, jc.ErrorIsNil)
csResources := []charmresource.Resource{res.Resource}
err = st.SetCharmStoreResources("a-application", csResources, testing.NonZeroTime())
c.Assert(err, jc.ErrorIsNil)
resources, err = st.ListResources("a-application")
c.Assert(err, jc.ErrorIsNil)
res.Timestamp = resources.Resources[0].Timestamp
c.Check(resources, jc.DeepEquals, resource.ServiceResources{
Resources: []resource.Resource{res},
CharmStoreResources: csResources,
})
// TODO(ericsnow) Add more as state.Resources grows more functionality.
}
示例9: TestPruneStatusHistoryBySize
func (s *StatusHistorySuite) TestPruneStatusHistoryBySize(c *gc.C) {
clock := testing.NewClock(coretesting.NonZeroTime())
err := s.State.SetClockForTesting(clock)
c.Assert(err, jc.ErrorIsNil)
service := s.Factory.MakeApplication(c, nil)
unit := s.Factory.MakeUnit(c, &factory.UnitParams{Application: service})
state.PrimeUnitStatusHistory(c, clock, unit, status.Active, 20000, 1000, nil)
history, err := unit.StatusHistory(status.StatusHistoryFilter{Size: 25000})
c.Assert(err, jc.ErrorIsNil)
c.Logf("%d\n", len(history))
c.Assert(history, gc.HasLen, 20001)
err = state.PruneStatusHistory(s.State, 0, 1)
c.Assert(err, jc.ErrorIsNil)
history, err = unit.StatusHistory(status.StatusHistoryFilter{Size: 25000})
c.Assert(err, jc.ErrorIsNil)
historyLen := len(history)
// When writing this test, the size was 6670 for about 0,00015 MB per entry
// but that is a size that can most likely change so I wont risk a flaky test
// here, enough to say that if this size suddenly is no longer less than
// half its good reason for suspicion.
c.Assert(historyLen, jc.LessThan, 10000)
}
示例10: setStored
func (s *backupsSuite) setStored(id string) *time.Time {
s.Storage.ID = id
s.Storage.Meta = backupstesting.NewMetadataStarted()
s.Storage.Meta.SetID(id)
stored := testing.NonZeroTime().UTC()
s.Storage.Meta.SetStored(&stored)
return &stored
}
示例11: TestPruneLogsBySize
func (s *LogsSuite) TestPruneLogsBySize(c *gc.C) {
// Set up 3 models and generate different amounts of logs
// for them.
now := coretesting.NonZeroTime().Truncate(time.Millisecond)
s0 := s.State
startingLogsS0 := 10
s.generateLogs(c, s0, now, startingLogsS0)
s1 := s.Factory.MakeModel(c, nil)
defer s1.Close()
startingLogsS1 := 10000
s.generateLogs(c, s1, now, startingLogsS1)
s2 := s.Factory.MakeModel(c, nil)
defer s2.Close()
startingLogsS2 := 12000
s.generateLogs(c, s2, now, startingLogsS2)
// Prune logs collection back to 1 MiB.
tsNoPrune := coretesting.NonZeroTime().Add(-3 * 24 * time.Hour)
err := state.PruneLogs(s.State, tsNoPrune, 1)
c.Assert(err, jc.ErrorIsNil)
// Logs for first env should not be touched.
c.Assert(s.countLogs(c, s0), gc.Equals, startingLogsS0)
// Logs for second env should be pruned.
c.Assert(s.countLogs(c, s1), jc.LessThan, startingLogsS1)
// Logs for third env should be pruned to a similar level as
// second env.
c.Assert(s.countLogs(c, s2), jc.LessThan, startingLogsS1)
// Ensure that the latest log records are still there.
assertLatestTs := func(st *state.State) {
var doc bson.M
err := s.logsColl.Find(bson.M{"e": st.ModelUUID()}).Sort("-t").One(&doc)
c.Assert(err, jc.ErrorIsNil)
c.Assert(doc["t"], gc.Equals, now.UnixNano())
}
assertLatestTs(s0)
assertLatestTs(s1)
assertLatestTs(s2)
}
示例12: assertMetricsManagerAmberState
func assertMetricsManagerAmberState(c *gc.C, metricsManager *state.MetricsManager) {
err := metricsManager.SetLastSuccessfulSend(testing.NonZeroTime())
c.Assert(err, jc.ErrorIsNil)
for i := 0; i < 3; i++ {
err := metricsManager.IncrementConsecutiveErrors()
c.Assert(err, jc.ErrorIsNil)
}
status := metricsManager.MeterStatus()
c.Assert(status.Code, gc.Equals, state.MeterAmber)
}
示例13: TestUpdateLastLogin
func (s *UserSuite) TestUpdateLastLogin(c *gc.C) {
now := testing.NonZeroTime().Round(time.Second).UTC()
user := s.Factory.MakeUser(c, nil)
err := user.UpdateLastLogin()
c.Assert(err, jc.ErrorIsNil)
lastLogin, err := user.LastLogin()
c.Assert(err, jc.ErrorIsNil)
c.Assert(lastLogin.After(now) ||
lastLogin.Equal(now), jc.IsTrue)
}
示例14: assertMetricsManagerRedState
// TODO (mattyw) This function could be moved into a metricsmanager testing package.
func assertMetricsManagerRedState(c *gc.C, metricsManager *state.MetricsManager) {
// To enter the red state we need to set a last successful send as over 1 week ago
err := metricsManager.SetLastSuccessfulSend(testing.NonZeroTime().Add(-8 * 24 * time.Hour))
c.Assert(err, jc.ErrorIsNil)
for i := 0; i < 3; i++ {
err := metricsManager.IncrementConsecutiveErrors()
c.Assert(err, jc.ErrorIsNil)
}
status := metricsManager.MeterStatus()
c.Assert(status.Code, gc.Equals, state.MeterRed)
}
示例15: TestDoc2BasicResourceCharmstoreFull
func (s *ResourcesMongoSuite) TestDoc2BasicResourceCharmstoreFull(c *gc.C) {
applicationID := "a-application"
docID := applicationResourceID("spam")
content := "some data\n..."
fp, err := charmresource.GenerateFingerprint(strings.NewReader(content))
c.Assert(err, jc.ErrorIsNil)
now := coretesting.NonZeroTime()
res, err := doc2basicResource(resourceDoc{
DocID: docID,
ID: "a-application/spam",
PendingID: "some-unique-ID",
ApplicationID: applicationID,
Name: "spam",
Type: "file",
Path: "spam.tgz",
Description: "you need this!",
Origin: "store",
Revision: 5,
Fingerprint: fp.Bytes(),
Size: int64(len(content)),
Username: "a-user",
Timestamp: now,
StoragePath: "application-a-application/resources/spam",
})
c.Assert(err, jc.ErrorIsNil)
c.Check(res, jc.DeepEquals, resource.Resource{
Resource: charmresource.Resource{
Meta: charmresource.Meta{
Name: "spam",
Type: charmresource.TypeFile,
Path: "spam.tgz",
Description: "you need this!",
},
Origin: charmresource.OriginStore,
Revision: 5,
Fingerprint: fp,
Size: int64(len(content)),
},
ID: "a-application/spam",
PendingID: "some-unique-ID",
ApplicationID: applicationID,
Username: "a-user",
Timestamp: now,
})
}