示例#1
0
文件: audit_test.go 项目: bac/juju
func (*AuditSuite) TestPutAuditEntry_ValidateAuditEntry(c *gc.C) {
	var auditEntry audit.AuditEntry

	// Don't care what the error is; just that it's not valid.
	validationErr := auditEntry.Validate()
	c.Assert(validationErr, gc.NotNil)

	putAuditEntry := stateaudit.PutAuditEntryFn("", nil)
	err := putAuditEntry(auditEntry)
	c.Check(err, gc.ErrorMatches, validationErr.Error())
}
示例#2
0
文件: audit_test.go 项目: bac/juju
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)
}
示例#3
0
文件: audit_test.go 项目: bac/juju
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)
}