From 3e425cb722cf748b871e6420e2c440622df04274 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Thu, 26 Oct 2017 09:00:06 +0300 Subject: [PATCH] Updated to add test-coverage of orphaned nodes. 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%. --- cmd_prune_test.go | 4 ++-- db_test.go | 49 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/cmd_prune_test.go b/cmd_prune_test.go index dbf6800..019a082 100644 --- a/cmd_prune_test.go +++ b/cmd_prune_test.go @@ -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} diff --git a/db_test.go b/db_test.go index a731dd0..2cc84fc 100644 --- a/db_test.go +++ b/db_test.go @@ -5,6 +5,8 @@ package main import ( + "database/sql" + "fmt" "io/ioutil" "os" "regexp" @@ -44,7 +46,7 @@ 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) } @@ -52,12 +54,13 @@ func addFakeReports() { 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() @@ -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 ") + } + } // @@ -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) } // @@ -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)) }