Skip to content

Commit

Permalink
Updated to add test-coverage of orphaned nodes.
Browse files Browse the repository at this point in the history
We've updated how we add our fake data, such that we now add a single
orphaned node.  We do this by inserting as usual, then faking the
created_at date as being 300 seconds past the epoch.

This increases the test-coverage of db.go from 83.1% to 87.3%.
  • Loading branch information
skx committed Oct 26, 2017
1 parent 8d34fef commit 3e425cb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
4 changes: 2 additions & 2 deletions cmd_prune_test.go
Expand Up @@ -21,8 +21,8 @@ func TestPruneCommand(t *testing.T) {
if err != nil {
t.Errorf("Error counting reports")
}
if old != 10 {
t.Errorf("We have %d reports, not 10", old)
if old != 30 {
t.Errorf("We have %d reports, not 30", old)
}

tmp := pruneCmd{days: 5, verbose: false}
Expand Down
49 changes: 42 additions & 7 deletions db_test.go
Expand Up @@ -5,6 +5,8 @@
package main

import (
"database/sql"
"fmt"
"io/ioutil"
"os"
"regexp"
Expand Down Expand Up @@ -44,20 +46,21 @@ func addFakeReports() {

//
// Add some records
stmt, err := tx.Prepare("INSERT INTO reports(yaml_file,executed_at) values(?,?)")
stmt, err := tx.Prepare("INSERT INTO reports(fqdn,yaml_file,executed_at) values(?,?,?)")
if err != nil {
panic(err)
}
defer stmt.Close()

count := 0

for count < 10 {
for count < 30 {
now := time.Now().Unix()
days := int64(60 * 60 * 24 * count)

fqdn := fmt.Sprintf("node%d.example.com", count)
now -= days
stmt.Exec("/../data/valid.yaml", now)
stmt.Exec(fqdn, "/../data/valid.yaml", now)
count++
}
tx.Commit()
Expand Down Expand Up @@ -96,6 +99,38 @@ func addFakeNodes() {
n.Skipped = "3"
addDB(n, "")

//
// Here we're trying to fake an orphaned node.
//
// When a report is added the exected_at field is set to
// "time.Now().Unix()". To make an orphaned record we need
// to change that to some time >24 days ago.
//
// We do that by finding the last report-ID, and then editing
// the field.
//
var max_id string
row := db.QueryRow("SELECT MAX(id) FROM reports")
err := row.Scan(&max_id)

switch {
case err == sql.ErrNoRows:
case err != nil:
panic("failed to find max report ID")
default:
}

//
// Now we can change the executed_at field of that last
// addition
//
sqlStmt := fmt.Sprintf("UPDATE reports SET executed_at=300 WHERE id=%s",
max_id)
_, err = db.Exec(sqlStmt)
if err != nil {
panic("Failed to change report ")
}

}

//
Expand Down Expand Up @@ -198,8 +233,8 @@ func TestPrune(t *testing.T) {
if err != nil {
t.Errorf("Error counting reports")
}
if old != 10 {
t.Errorf("We have %d reports, not 10", old)
if old != 30 {
t.Errorf("We have %d reports, not 30", old)
}

//
Expand Down Expand Up @@ -356,9 +391,9 @@ func TestHistory(t *testing.T) {
}

//
// Should have 1 run, becase we have only one unique date..
// Should have 2 runs, becase we have only one unique date..
//
if len(runs) != 1 {
if len(runs) != 2 {
t.Errorf("getReports returned wrong number of results: %d", len(runs))
}

Expand Down

0 comments on commit 3e425cb

Please sign in to comment.