Example #1
0
// Check if the revision field avoid data concurrency. Is better to fail than to store the
// wrong state
func scanConcurrency(scanDAO dao.ScanDAO) {
	scan := newScan()

	// Create scan
	if err := scanDAO.Save(&scan); err != nil {
		utils.Fatalln("Couldn't save scan in database", err)
	}

	scan1, err := scanDAO.FindByStartedAt(scan.StartedAt)
	if err != nil {
		utils.Fatalln("Couldn't find created scan in database", err)
	}

	scan2, err := scanDAO.FindByStartedAt(scan.StartedAt)
	if err != nil {
		utils.Fatalln("Couldn't find created scan in database", err)
	}

	if err := scanDAO.Save(&scan1); err != nil {
		utils.Fatalln("Couldn't save scan in database", err)
	}

	if err := scanDAO.Save(&scan2); err == nil {
		utils.Fatalln("Not controlling scan concurrency", nil)
	}

	// Remove scan
	if err := scanDAO.RemoveByStartedAt(scan.StartedAt); err != nil {
		utils.Fatalln("Error while trying to remove a scan", err)
	}
}
Example #2
0
func deleteScan(database *mgo.Database, startedAt time.Time) {
	scanDAO := dao.ScanDAO{
		Database: database,
	}

	if err := scanDAO.RemoveByStartedAt(startedAt); err != nil {
		utils.Fatalln("Error removing scan", err)
	}
}
Example #3
0
// Test all phases of the scan life cycle
func scanLifeCycle(scanDAO dao.ScanDAO) {
	scan := newScan()

	// Create scan
	if err := scanDAO.Save(&scan); err != nil {
		utils.Fatalln("Couldn't save scan in database", err)
	}

	// Search and compare created scan
	if scanRetrieved, err := scanDAO.FindByStartedAt(scan.StartedAt); err != nil {
		utils.Fatalln("Couldn't find created scan in database", err)

	} else if !utils.CompareScan(scan, scanRetrieved) {
		utils.Fatalln("Scan created in being persisted wrongly", nil)
	}

	// Update scan
	scan.DomainsScanned = 100
	if err := scanDAO.Save(&scan); err != nil {
		utils.Fatalln("Couldn't save scan in database", err)
	}

	// Search and compare updated scan
	if scanRetrieved, err := scanDAO.FindByStartedAt(scan.StartedAt); err != nil {
		utils.Fatalln("Couldn't find updated scan in database", err)

	} else if !utils.CompareScan(scan, scanRetrieved) {
		utils.Fatalln("Scan updated in being persisted wrongly", nil)
	}

	// Remove scan
	if err := scanDAO.RemoveByStartedAt(scan.StartedAt); err != nil {
		utils.Fatalln("Error while trying to remove a scan", err)
	}

	// Check removal
	if _, err := scanDAO.FindByStartedAt(scan.StartedAt); err == nil {
		utils.Fatalln("Scan was not removed from database", nil)
	}
}