Skip to content

Commit

Permalink
adding test cases for rac maint
Browse files Browse the repository at this point in the history
  • Loading branch information
rajesh-1983 committed Dec 20, 2023
1 parent b4185d1 commit 9dd35b8
Show file tree
Hide file tree
Showing 3 changed files with 249 additions and 102 deletions.
135 changes: 33 additions & 102 deletions tests/unittest/rac_maint/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func cfg() (map[string]string, map[string]string, testutil.WorkerType) {
}

func before() error {
os.Setenv("PARALLEL", "1")
pfx := os.Getenv("MGMT_TABLE_PREFIX")
if pfx == "" {
pfx = "hera"
Expand All @@ -46,9 +47,9 @@ func TestMain(m *testing.M) {
os.Exit(testutil.UtilMain(m, cfg, before))
}

func TestRacMaint(t *testing.T) {
logger.GetLogger().Log(logger.Debug, "TestRacMaint begin +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n")

func TestRacMaintWithBothModulesAndStateChange(t *testing.T) {
logger.GetLogger().Log(logger.Debug, "TestRacMaintWithBothModulesAndStateChange begin +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n")
testutil.ClearLogsData()
shard := 0
db, err := sql.Open("heraloop", fmt.Sprintf("%d:0:0", shard))
if err != nil {
Expand All @@ -58,7 +59,7 @@ func TestRacMaint(t *testing.T) {
db.SetMaxIdleConns(0)
defer db.Close()

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
// cleanup and insert one row in the table
conn, err := db.Conn(ctx)
Expand All @@ -75,7 +76,7 @@ func TestRacMaint(t *testing.T) {
stmt, _ = tx.PrepareContext(ctx, "/*cmd*/insert into "+tableName+" (inst_id, status, status_time, module, machine) values (?,?,?,?,?)")
hostname, _ := os.Hostname()
// how to do inst_id
_, err = stmt.Exec(15 /*max instid*/, "F", time.Now().Unix()+2, "hera-test", hostname)
_, err = stmt.Exec(0 /*max instid*/, "F", time.Now().Unix()+2, "hera-test", hostname)
if err != nil {
t.Fatalf("Error preparing test (create row in table) %s\n", err.Error())
}
Expand All @@ -85,110 +86,42 @@ func TestRacMaint(t *testing.T) {
}

time.Sleep(4100 * time.Millisecond)
if 0 != testutil.RegexCountFile("Rac maint activating, worker", "hera.log") {
t.Fatalf("should not have rac maint activation")
recycleWorkersCount := testutil.RegexCountFile("Rac maint activating, worker", "hera.log")
if 0 == recycleWorkersCount {
t.Fatalf("requires rac maint activation for main module for status 'R'")
}

tx, err = conn.BeginTx(ctx, nil)
if err != nil {
t.Fatalf("Error 2commit %s\n", err.Error())
}
delS, err := tx.PrepareContext(ctx, "/*cmd*/delete from "+tableName)
if err != nil {
t.Fatalf("Error prep del %s\n", err.Error())
}
delS.Exec()
insS, err := tx.PrepareContext(ctx, "/*cmd*/insert into "+tableName+" (inst_id, status, status_time, module, machine) values (?,?,?,?,?)")
if err != nil {
t.Fatalf("Error prep ins %s\n", err.Error())
}
// mysql uses instId 0 since there isn't instid's
insS.Exec(0, "F", time.Now().Unix()+1, "hera-test", hostname)
timeInMillis := time.Now().Unix() + 1
insS.Exec(0, "U", timeInMillis, "hera-test", hostname)
err = tx.Commit()
if err != nil {
t.Fatalf("Error 2commit %s\n", err.Error())
}

time.Sleep(4100 * time.Millisecond)
if 0 == testutil.RegexCountFile("Rac maint activating, worker", "hera.log") {
t.Fatalf("missed rac maint activation")
}
logger.GetLogger().Log(logger.Debug, "TestRacMaint done -------------------------------------------------------------")
}

func TestRacMaintInvalidStatus(t *testing.T) {
logger.GetLogger().Log(logger.Debug, "TestRacMaintInvalidStatus begin +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n")
time.Sleep(2000 * time.Millisecond)
shard := 0
db, err := sql.Open("heraloop", fmt.Sprintf("%d:0:0", shard))
if err != nil {
t.Fatal("Error starting Mux:", err)
return
}
db.SetMaxIdleConns(0)
defer db.Close()

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// cleanup and insert one row in the table
conn, err := db.Conn(ctx)
if err != nil {
t.Fatalf("Error getting connection %s\n", err.Error())
}
defer conn.Close()
tx, _ := conn.BeginTx(ctx, nil)
stmt, _ := tx.PrepareContext(ctx, "/*cmd*/delete from "+tableName)
_, err = stmt.Exec()
if err != nil {
t.Fatalf("Error preparing test (delete table) %s\n", err.Error())
}
stmt, _ = tx.PrepareContext(ctx, "/*cmd*/insert into "+tableName+" (inst_id, status, status_time, module, machine) values (?,?,?,?,?)")
hostname, _ := os.Hostname()
// how to do inst_id
_, err = stmt.Exec(15 /*max instid*/, "F", time.Now().Unix()+2, "hera-test", hostname)
if err != nil {
t.Fatalf("Error preparing test (create row in table) %s\n", err.Error())
}
err = tx.Commit()
if err != nil {
t.Fatalf("Error commit %s\n", err.Error())
}

time.Sleep(4100 * time.Millisecond)
if 0 != testutil.RegexCountFile("Rac maint activating, worker", "hera.log") {
t.Fatalf("should not have rac maint activation")
if recycleWorkersCount != testutil.RegexCountFile("Rac maint activating, worker", "hera.log") {
t.Fatalf("should not trigger additional rac maint activation for main module for status 'U'")
}

tx, err = conn.BeginTx(ctx, nil)
if err != nil {
t.Fatalf("Error 2commit %s\n", err.Error())
}
insS, err := tx.PrepareContext(ctx, "/*cmd*/insert into "+tableName+" (inst_id, status, status_time, module, machine) values (?,?,?,?,?)")
if err != nil {
t.Fatalf("Error prep ins %s\n", err.Error())
if 2 == testutil.RegexCountFile("RACMAINT_INFO_CHANGE", "cal.log") {
t.Fatalf("We have 2 insert queries with different status, so should log the RACMAINT_INFO_CHANGE event for every change")
}
// mysql uses instId 0 since there isn't instid's
insS.Exec(0, "T", time.Now().Unix()+1, "hera-test", hostname)
err = tx.Commit()
if err != nil {
t.Fatalf("Error 2commit %s\n", err.Error())
}

time.Sleep(4100 * time.Millisecond)

if 0 == testutil.RegexCountFile("invalid_status", "cal.log") {
t.Fatalf("ram maint should skip with invalid-status event")
}
if 0 != testutil.RegexCountFile("Rac maint activating, worker", "hera.log") {
t.Fatalf("should not have rac maint activation for invalid status")
if 0 != testutil.RegexCountFile("invalid_status", "cal.log") {
t.Fatalf("ram maint status 'U' should not skip with invalid-status event")
}

logger.GetLogger().Log(logger.Debug, "TestRacMaintInvalidStatus done -------------------------------------------------------------")
logger.GetLogger().Log(logger.Debug, "TestRacMaintWithBothModulesAndStateChange done -------------------------------------------------------------")
}

func TestRacMaintWithBothModules(t *testing.T) {
logger.GetLogger().Log(logger.Debug, "TestRacMaintWithBothModules begin +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n")
time.Sleep(8000 * time.Millisecond)
func TestRacMaintWithBothModulesAndStateChangeFromR(t *testing.T) {
logger.GetLogger().Log(logger.Debug, "TestRacMaintWithBothModulesAndStateChange2 begin +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n")
testutil.ClearLogsData()
shard := 0
db, err := sql.Open("heraloop", fmt.Sprintf("%d:0:0", shard))
if err != nil {
Expand All @@ -198,7 +131,7 @@ func TestRacMaintWithBothModules(t *testing.T) {
db.SetMaxIdleConns(0)
defer db.Close()

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// cleanup and insert one row in the table
conn, err := db.Conn(ctx)
Expand All @@ -215,7 +148,7 @@ func TestRacMaintWithBothModules(t *testing.T) {
stmt, _ = tx.PrepareContext(ctx, "/*cmd*/insert into "+tableName+" (inst_id, status, status_time, module, machine) values (?,?,?,?,?)")
hostname, _ := os.Hostname()
// how to do inst_id
_, err = stmt.Exec(15 /*max instid*/, "F", time.Now().Unix()+2, "hera-test", hostname)
_, err = stmt.Exec(0 /*max instid*/, "R", time.Now().Unix()+2, "hera-test", hostname)
if err != nil {
t.Fatalf("Error preparing test (create row in table) %s\n", err.Error())
}
Expand All @@ -225,10 +158,13 @@ func TestRacMaintWithBothModules(t *testing.T) {
}

time.Sleep(4100 * time.Millisecond)
if 0 != testutil.RegexCountFile("Rac maint activating, worker", "hera.log") {
t.Fatalf("should not have rac maint activation")
recycleWorkersCount := testutil.RegexCountFile("Rac maint activating, worker", "hera.log")
t.Logf("Recycle worker count: %d", recycleWorkersCount)
if 0 == recycleWorkersCount {
t.Fatalf("requires rac maint activation for main module for status 'R'")
}

time.Sleep(4100 * time.Millisecond)
tx, err = conn.BeginTx(ctx, nil)
if err != nil {
t.Fatalf("Error 2commit %s\n", err.Error())
Expand All @@ -239,24 +175,19 @@ func TestRacMaintWithBothModules(t *testing.T) {
}
// mysql uses instId 0 since there isn't instid's
timeInMillis := time.Now().Unix() + 1
insS.Exec(0, "F", timeInMillis, "hera-test", hostname)
insS.Exec(0, "U", timeInMillis, "hera-test_taf", hostname)
insS.Exec(0, "U", timeInMillis, "hera-test", hostname)
err = tx.Commit()
if err != nil {
t.Fatalf("Error 2commit %s\n", err.Error())
}

time.Sleep(4100 * time.Millisecond)

if 0 == testutil.RegexCountFile("Rac maint activating, worker", "hera.log") {
t.Fatalf("requires rac maint activation for main module status")
}

if 0 == testutil.RegexCountFile("module:HERA-TEST_TAF", "cal.log") {
t.Fatalf("Status 'U' should log the RACMAINT_INFO_CHANGE event")
if 2 == testutil.RegexCountFile("RACMAINT_INFO_CHANGE", "cal.log") {
t.Fatalf("we have 2 insert queries with different status, so should log the RACMAINT_INFO_CHANGE event for every change")
}
if 0 != testutil.RegexCountFile("invalid_status", "cal.log") {
t.Fatalf("ram maint status 'U' should not skip with invalid-status event")
t.Fatalf("rac maint status 'U' should not skip with invalid-status event")
}
logger.GetLogger().Log(logger.Debug, "TestRacMaintWithBothModules done -------------------------------------------------------------")
logger.GetLogger().Log(logger.Debug, "TestRacMaintWithBothModulesAndStateChange2 done -------------------------------------------------------------")
}
Loading

0 comments on commit 9dd35b8

Please sign in to comment.