Example #1
0
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)
}
Example #2
0
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)
}
Example #3
0
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)
}
Example #4
0
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,
	}})
}
Example #5
0
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")
	}
}
Example #6
0
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)

}
Example #7
0
func (s *UserSuite) TestAddUser(c *gc.C) {
	name := "f00-Bar.ram77"
	displayName := "Display"
	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{})
}
Example #8
0
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.
}
Example #9
0
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)
}
Example #10
0
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
}
Example #11
0
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)
}
Example #12
0
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)
}
Example #13
0
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)
}
Example #14
0
// 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)
}
Example #15
0
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:  "******",
		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:      "******",
		Timestamp:     now,
	})
}
Example #16
0
func (s *ResourcesMongoSuite) TestDoc2Resource(c *gc.C) {
	applicationID := "a-application"
	docID := pendingResourceID("spam", "some-unique-ID-001")
	content := "some data\n..."
	fp, err := charmresource.GenerateFingerprint(strings.NewReader(content))
	c.Assert(err, jc.ErrorIsNil)
	now := coretesting.NonZeroTime()

	res, err := doc2resource(resourceDoc{
		DocID:         docID,
		ID:            "a-application/spam",
		PendingID:     "some-unique-ID-001",
		ApplicationID: applicationID,

		Name: "spam",
		Type: "file",
		Path: "spam.tgz",

		Origin:      "upload",
		Fingerprint: fp.Bytes(),
		Size:        int64(len(content)),

		Username:  "******",
		Timestamp: now,

		StoragePath: "application-a-application/resources/spam-some-unique-ID-001",
	})
	c.Assert(err, jc.ErrorIsNil)

	c.Check(res, jc.DeepEquals, storedResource{
		Resource: resource.Resource{
			Resource: charmresource.Resource{
				Meta: charmresource.Meta{
					Name: "spam",
					Type: charmresource.TypeFile,
					Path: "spam.tgz",
				},
				Origin:      charmresource.OriginUpload,
				Fingerprint: fp,
				Size:        int64(len(content)),
			},
			ID:            "a-application/spam",
			PendingID:     "some-unique-ID-001",
			ApplicationID: applicationID,
			Username:      "******",
			Timestamp:     now,
		},
		storagePath: "application-a-application/resources/spam-some-unique-ID-001",
	})
}
Example #17
0
// TestCountMetrics asserts the correct values are returned
// by CountOfUnsentMetrics and CountOfSentMetrics.
func (s *MetricSuite) TestCountMetrics(c *gc.C) {
	now := testing.NonZeroTime()
	m := []state.Metric{{Key: "pings", Value: "123", Time: now}}
	s.Factory.MakeMetric(c, &factory.MetricParams{Unit: s.unit, Sent: false, Time: &now, Metrics: m})
	s.Factory.MakeMetric(c, &factory.MetricParams{Unit: s.unit, Sent: false, Time: &now, Metrics: m})
	s.Factory.MakeMetric(c, &factory.MetricParams{Unit: s.unit, Sent: true, Time: &now, Metrics: m})
	sent, err := s.State.CountOfSentMetrics()
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(sent, gc.Equals, 1)
	unsent, err := s.State.CountOfUnsentMetrics()
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(unsent, gc.Equals, 2)
	c.Assert(unsent+sent, gc.Equals, 3)
}
Example #18
0
func (*AuditSuite) TestPutAuditEntry_PersistAuditEntry(c *gc.C) {

	requested := audit.AuditEntry{
		JujuServerVersion: version.MustParse("1.0.0"),
		ModelUUID:         utils.MustNewUUID().String(),
		Timestamp:         coretesting.NonZeroTime().UTC(),
		RemoteAddress:     "8.8.8.8",
		OriginType:        "user",
		OriginName:        "bob",
		Operation:         "status",
		Data: map[string]interface{}{
			"a": "b",
			"$a.b": map[string]interface{}{
				"b.$a": "c",
			},
		},
	}

	var insertDocsCalled bool
	insertDocs := func(collectionName string, docs ...interface{}) error {
		insertDocsCalled = true
		c.Check(collectionName, gc.Equals, "audit.log")
		c.Assert(docs, gc.HasLen, 1)

		serializedAuditDoc, err := bson.Marshal(docs[0])
		c.Assert(err, jc.ErrorIsNil)

		requestedTimeBlob, err := requested.Timestamp.MarshalText()
		c.Assert(err, jc.ErrorIsNil)

		c.Check(string(serializedAuditDoc), jc.BSONEquals, map[string]interface{}{
			"juju-server-version": requested.JujuServerVersion,
			"model-uuid":          requested.ModelUUID,
			"timestamp":           string(requestedTimeBlob),
			"remote-address":      "8.8.8.8",
			"origin-type":         requested.OriginType,
			"origin-name":         requested.OriginName,
			"operation":           requested.Operation,
			"data":                mongoutils.EscapeKeys(requested.Data),
		})

		return nil
	}

	putAuditEntry := stateaudit.PutAuditEntryFn("audit.log", insertDocs)
	err := putAuditEntry(requested)
	c.Assert(err, jc.ErrorIsNil)

	c.Assert(insertDocsCalled, jc.IsTrue)
}
Example #19
0
func (s *MeterStateSuite) TestMeterStatusWatcherRespondsToMetricsManager(c *gc.C) {
	mm, err := s.State.MetricsManager()
	c.Assert(err, jc.ErrorIsNil)
	watcher := s.unit.WatchMeterStatus()
	assertMeterStatusChanged(c, watcher)
	err = mm.SetLastSuccessfulSend(testing.NonZeroTime())
	c.Assert(err, jc.ErrorIsNil)
	for i := 0; i < 3; i++ {
		err := mm.IncrementConsecutiveErrors()
		c.Assert(err, jc.ErrorIsNil)
	}
	status := mm.MeterStatus()
	c.Assert(status.Code, gc.Equals, state.MeterAmber)
	assertMeterStatusChanged(c, watcher)
}
Example #20
0
func (s *cloudImageMetadataSuite) TestSaveMetadataWithDateCreated(c *gc.C) {
	attrs := cloudimagemetadata.MetadataAttributes{
		Stream:          "stream",
		Region:          "region-test",
		Version:         "14.04",
		Series:          "trusty",
		Arch:            "arch",
		VirtType:        "virtType-test",
		RootStorageType: "rootStorageType-test",
		Source:          "test",
	}
	now := coretesting.NonZeroTime().UnixNano()
	metadata := cloudimagemetadata.Metadata{attrs, 0, "1", now}
	s.assertRecordMetadata(c, metadata)
	s.assertMetadataRecorded(c, cloudimagemetadata.MetadataAttributes{}, metadata)
}
Example #21
0
func newPersistenceResources(c *gc.C, serviceID string, names ...string) (resource.ServiceResources, []resourceDoc) {
	var svcResources resource.ServiceResources
	var docs []resourceDoc
	for _, name := range names {
		res, doc := newPersistenceResource(c, serviceID, name)
		svcResources.Resources = append(svcResources.Resources, res.Resource)
		svcResources.CharmStoreResources = append(svcResources.CharmStoreResources, res.Resource.Resource)
		docs = append(docs, doc)
		csDoc := doc // a copy
		csDoc.DocID += "#charmstore"
		csDoc.Username = ""
		csDoc.Timestamp = coretesting.ZeroTime()
		csDoc.StoragePath = ""
		csDoc.LastPolled = coretesting.NonZeroTime().UTC()
		docs = append(docs, csDoc)
	}
	return svcResources, docs
}
Example #22
0
func (s *MetricSuite) TestSetMetricBatchesSent(c *gc.C) {
	now := testing.NonZeroTime()
	metrics := make([]*state.MetricBatch, 3)
	for i := range metrics {
		m := []state.Metric{{Key: "pings", Value: "123", Time: now}}
		metrics[i] = s.Factory.MakeMetric(c, &factory.MetricParams{Unit: s.unit, Sent: false, Time: &now, Metrics: m})
	}
	uuids := make([]string, len(metrics))
	for i, m := range metrics {
		uuids[i] = m.UUID()
	}
	err := s.State.SetMetricBatchesSent(uuids)
	c.Assert(err, jc.ErrorIsNil)
	sent, err := s.State.CountOfSentMetrics()
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(sent, gc.Equals, 3)

}
Example #23
0
func (s *StorageSuite) TestExpireAt(c *gc.C) {
	store, err := New(s.config)
	c.Assert(err, jc.ErrorIsNil)

	expiryTime := testing.NonZeroTime().Add(24 * time.Hour)
	store = store.ExpireAt(expiryTime)

	err = store.Put("foo", "bar")
	c.Assert(err, jc.ErrorIsNil)
	s.CheckCalls(c, []gitjujutesting.StubCall{
		{"GetCollection", nil},
		{"Writeable", nil},
		{"UpsertId", []interface{}{"foo", storageDoc{
			Location: "foo",
			Item:     "bar",
			ExpireAt: expiryTime.Add(-1 * time.Second),
		}}},
		{"Close", nil},
	})
}
Example #24
0
func (s *MetricSuite) TestSetMetricSent(c *gc.C) {
	now := s.State.NowToTheSecond()
	m := state.Metric{"pings", "5", now}
	added, 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)
	saved, err := s.State.MetricBatch(added.UUID())
	c.Assert(err, jc.ErrorIsNil)
	err = saved.SetSent(testing.NonZeroTime())
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(saved.Sent(), jc.IsTrue)
	saved, err = s.State.MetricBatch(added.UUID())
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(saved.Sent(), jc.IsTrue)
}
Example #25
0
func (r *RestoreSuite) TestNewConnection(c *gc.C) {
	server := &gitjujutesting.MgoInstance{}
	err := server.Start(coretesting.Certs)
	c.Assert(err, jc.ErrorIsNil)
	defer server.DestroyWithLog()

	st := statetesting.InitializeWithArgs(c,
		statetesting.InitializeArgs{
			Owner: names.NewLocalUserTag("test-admin"),
			Clock: gitjujutesting.NewClock(coretesting.NonZeroTime()),
		})
	c.Assert(st.Close(), jc.ErrorIsNil)

	r.PatchValue(&mongoDefaultDialOpts, mongotest.DialOpts)
	r.PatchValue(&environsGetNewPolicyFunc, func(
		func(*state.State) (environs.Environ, error),
	) state.NewPolicyFunc {
		return nil
	})
	st, err = newStateConnection(st.ControllerTag(), st.ModelTag(), statetesting.NewMongoInfo())
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(st.Close(), jc.ErrorIsNil)
}
Example #26
0
func (*AuditSuite) TestPutAuditEntry_PropagatesWriteError(c *gc.C) {
	const errMsg = "my error"
	insertDocs := func(string, ...interface{}) error {
		return errors.New(errMsg)
	}
	putAuditEntry := stateaudit.PutAuditEntryFn("audit.log", insertDocs)

	uuid, err := utils.NewUUID()
	c.Assert(err, jc.ErrorIsNil)

	auditEntry := audit.AuditEntry{
		JujuServerVersion: version.MustParse("1.0.0"),
		ModelUUID:         uuid.String(),
		Timestamp:         coretesting.NonZeroTime().UTC(),
		RemoteAddress:     "8.8.8.8",
		OriginType:        "user",
		OriginName:        "bob",
		Operation:         "status",
	}
	c.Assert(auditEntry.Validate(), jc.ErrorIsNil)

	err = putAuditEntry(auditEntry)
	c.Check(err, gc.ErrorMatches, errMsg)
}
Example #27
0
func (s *MetricSuite) TestMetricValidation(c *gc.C) {
	nonMeteredUnit := s.Factory.MakeUnit(c, &factory.UnitParams{SetCharmURL: true})
	meteredApplication := s.Factory.MakeApplication(c, &factory.ApplicationParams{Name: "metered-service", Charm: s.meteredCharm})
	meteredUnit := s.Factory.MakeUnit(c, &factory.UnitParams{Application: meteredApplication, SetCharmURL: true})
	dyingUnit, err := meteredApplication.AddUnit()
	c.Assert(err, jc.ErrorIsNil)
	err = dyingUnit.SetCharmURL(s.meteredCharm.URL())
	c.Assert(err, jc.ErrorIsNil)
	err = dyingUnit.Destroy()
	c.Assert(err, jc.ErrorIsNil)
	now := testing.NonZeroTime()
	tests := []struct {
		about   string
		metrics []state.Metric
		unit    *state.Unit
		err     string
	}{{
		"assert non metered unit returns an error",
		[]state.Metric{{"metric-key", "1", now}},
		nonMeteredUnit,
		"charm doesn't implement metrics",
	}, {
		"assert metric with no errors and passes validation",
		[]state.Metric{{"pings", "1", now}},
		meteredUnit,
		"",
	}, {
		"assert valid metric fails on dying unit",
		[]state.Metric{{"pings", "1", now}},
		dyingUnit,
		"unit \"metered-service/1\" not found",
	}, {
		"assert charm doesn't implement key returns error",
		[]state.Metric{{"not-implemented", "1", now}},
		meteredUnit,
		`metric "not-implemented" not defined`,
	}, {
		"assert invalid value returns error",
		[]state.Metric{{"pings", "foobar", now}},
		meteredUnit,
		`invalid value type: expected float, got "foobar"`,
	}, {
		"long value returns error",
		[]state.Metric{{"pings", "3.141592653589793238462643383279", now}},
		meteredUnit,
		`metric value is too large`,
	}, {
		"negative value returns error",
		[]state.Metric{{"pings", "-42.0", now}},
		meteredUnit,
		`invalid value: value must be greater or equal to zero, got -42.0`,
	}, {
		"non-float value returns an error",
		[]state.Metric{{"pings", "abcd", now}},
		meteredUnit,
		`invalid value type: expected float, got "abcd"`,
	}}
	for i, t := range tests {
		c.Logf("test %d: %s", i, t.about)
		chURL, ok := t.unit.CharmURL()
		c.Assert(ok, jc.IsTrue)
		_, err := s.State.AddMetrics(
			state.BatchParam{
				UUID:     utils.MustNewUUID().String(),
				Created:  now,
				CharmURL: chURL.String(),
				Metrics:  t.metrics,
				Unit:     t.unit.UnitTag(),
			},
		)
		if t.err == "" {
			c.Assert(err, jc.ErrorIsNil)
		} else {
			c.Assert(err, gc.ErrorMatches, t.err)
		}
	}
}