Commit b9803f9c authored by Yann Garcia's avatar Yann Garcia
Browse files

Add TestTrafficMgrCreateTrafficTable test script; Enhance traffic-mgr

parent a3e919f4
Loading
Loading
Loading
Loading
+181 −34
Original line number Diff line number Diff line
@@ -423,6 +423,138 @@ func (tm *TrafficMgr) CreateCategoryLoad(category string, data map[string]int32)
	return nil
}

// GetCategoryLoad - Get POA Load information
func (tm *TrafficMgr) GetCategoryLoad(category string) (categoryLoads *CategoryLoads, err error) {
	if profiling {
		profilingTimers["GetCategoryLoad"] = time.Now()
	}

	// Validate input
	if category == "" {
		err = errors.New("Missing category name")
		return nil, err
	}

	// Get Category Load entry
	var rows *sql.Rows
	rows, err = tm.db.Query(`
		SELECT category, "0000-0300", "0300-0600", "0600-0900", "0900-1200", "1200-1500", "1500-1800", "1800-2100", "2100-2400"
		FROM `+CategoryTable+`
		WHERE category = ($1)`, category)
	if err != nil {
		log.Error(err.Error())
		return nil, err
	}
	defer rows.Close()

	// Scan result
	for rows.Next() {
		categoryLoads = new(CategoryLoads)
		err = rows.Scan(
			&categoryLoads.Category,
			&categoryLoads.ZeroToThree,
			&categoryLoads.ThreeToSix,
			&categoryLoads.SixToNine,
			&categoryLoads.NineToTwelve,
			&categoryLoads.TwelveToFifteen,
			&categoryLoads.FifteenToEighteen,
			&categoryLoads.EighteenToTwentyOne,
			&categoryLoads.TwentyOneToTwentyFour,
		)
		if err != nil {
			log.Error(err.Error())
			return nil, err
		}
	}
	err = rows.Err()
	if err != nil {
		log.Error(err)
	}

	// Return error if not found
	if categoryLoads == nil {
		err = errors.New("Category Load not found: " + category)
		return nil, err
	}

	if profiling {
		now := time.Now()
		log.Debug("GetCategoryLoad: ", now.Sub(profilingTimers["GetCategoryLoad"]))
	}
	return categoryLoads, nil
}

// GetAllCategoryLoad - Get POA Load information
func (tm *TrafficMgr) GetAllCategoryLoad() (categoryLoads map[string]*CategoryLoads, err error) {
	if profiling {
		profilingTimers["GetAllCategoryLoad"] = time.Now()
	}

	// Create Category map
	categoryLoadsMap := make(map[string]*CategoryLoads)

	// Get Category Load entry
	var rows *sql.Rows
	rows, err = tm.db.Query(`
		SELECT category, "0000-0300", "0300-0600", "0600-0900", "0900-1200", "1200-1500", "1500-1800", "1800-2100", "2100-2400"
		FROM ` + CategoryTable)
	if err != nil {
		log.Error(err.Error())
		return nil, err
	}
	defer rows.Close()

	// Scan results
	for rows.Next() {
	
		categoryLoads := new(CategoryLoads)
		err = rows.Scan(
			&categoryLoads.Category,
			&categoryLoads.ZeroToThree,
			&categoryLoads.ThreeToSix,
			&categoryLoads.SixToNine,
			&categoryLoads.NineToTwelve,
			&categoryLoads.TwelveToFifteen,
			&categoryLoads.FifteenToEighteen,
			&categoryLoads.EighteenToTwentyOne,
			&categoryLoads.TwentyOneToTwentyFour,
		)

		// Add POA to map
		categoryLoadsMap[categoryLoads.Category] = categoryLoads
	}
	err = rows.Err()
	if err != nil {
		log.Error(err)
	}

	if profiling {
		now := time.Now()
		log.Debug("GetAllCategoryLoad: ", now.Sub(profilingTimers["GetAllCategoryLoad"]))
	}

	return categoryLoadsMap, nil
}

// DeleteAllCategory - Delete all Category entries
func (tm *TrafficMgr) DeleteAllCategory() (err error) {
	if profiling {
		profilingTimers["DeleteAllCategory"] = time.Now()
	}

	_, err = tm.db.Exec(`DELETE FROM ` + CategoryTable)
	if err != nil {
		log.Error(err.Error())
		return err
	}

	if profiling {
		now := time.Now()
		log.Debug("DeleteAllCategory: ", now.Sub(profilingTimers["DeleteAllCategory"]))
	}
	return nil
}

// CreatePoaLoad - Create new POA Load
func (tm *TrafficMgr) CreatePoaLoad(poaName string, category string) (err error) {
	if profiling {
@@ -457,7 +589,7 @@ func (tm *TrafficMgr) CreatePoaLoad(poaName string, category string) (err error)

	// Create Traffic Load entry
	query := `INSERT INTO ` + TrafficTable +
		` (poa_name, category, "0000-0300", "0300-0600", "0600-0900", "0900-1200", "1200-1500", "1500-1800", "1800-2100", "2100-2400")
		` (poaName, category, "0000-0300", "0300-0600", "0600-0900", "0900-1200", "1200-1500", "1500-1800", "1800-2100", "2100-2400")
			VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)`
	_, err = tm.db.Exec(query, poaName, category, loadTime[0], loadTime[1], loadTime[2], loadTime[3], loadTime[4], loadTime[5], loadTime[6], loadTime[7])
	if err != nil {
@@ -490,9 +622,9 @@ func (tm *TrafficMgr) GetPoaLoad(poaName string) (poaLoads *PoaLoads, err error)
	// Get Poa entry
	var rows *sql.Rows
	rows, err = tm.db.Query(`
		SELECT id, poa_name, category, "0000-0300", "0300-0600", "0600-0900", "0900-1200", "1200-1500", "1500-1800", "1800-2100", "2100-2400"
		SELECT poaName, category, "0000-0300", "0300-0600", "0600-0900", "0900-1200", "1200-1500", "1500-1800", "1800-2100", "2100-2400"
		FROM `+TrafficTable+`
		WHERE name = ($1)`, poaName)
		WHERE poaName = ($1)`, poaName)
	if err != nil {
		log.Error(err.Error())
		return nil, err
@@ -537,65 +669,80 @@ func (tm *TrafficMgr) GetPoaLoad(poaName string) (poaLoads *PoaLoads, err error)
	return poaLoads, nil
}

// GetCategoryLoad - Get POA Load information
func (tm *TrafficMgr) GetCategoryLoad(category string) (categoryLoads *CategoryLoads, err error) {
// GetAllPoaLoad - Get all POA information
func (tm *TrafficMgr) GetAllPoaLoad() (poaLoadMap map[string]*PoaLoads, err error) {
	if profiling {
		profilingTimers["GetCategoryLoad"] = time.Now()
		profilingTimers["GetAllPoaLoad"] = time.Now()
	}

	// Validate input
	if category == "" {
		err = errors.New("Missing category name")
		return nil, err
	}
	// Create PoaLoad map
	poaLoadMap = make(map[string]*PoaLoads)

	// Get Category Load entry
	// Get POA entries
	var rows *sql.Rows
	rows, err = tm.db.Query(`
		SELECT category, "0000-0300", "0300-0600", "0600-0900", "0900-1200", "1200-1500", "1500-1800", "1800-2100", "2100-2400"
		FROM `+CategoryTable+`
		WHERE category = ($1)`, category)
		SELECT poaName, category, "0000-0300", "0300-0600", "0600-0900", "0900-1200", "1200-1500", "1500-1800", "1800-2100", "2100-2400"
		FROM ` + TrafficTable)
	if err != nil {
		log.Error(err.Error())
		return nil, err
		return poaLoadMap, err
	}
	defer rows.Close()

	// Scan result
	// Scan results
	for rows.Next() {
		categoryLoads = new(CategoryLoads)
		poaLoads := new(PoaLoads)

		// Fill POA
		err = rows.Scan(
			&categoryLoads.Category,
			&categoryLoads.ZeroToThree,
			&categoryLoads.ThreeToSix,
			&categoryLoads.SixToNine,
			&categoryLoads.NineToTwelve,
			&categoryLoads.TwelveToFifteen,
			&categoryLoads.FifteenToEighteen,
			&categoryLoads.EighteenToTwentyOne,
			&categoryLoads.TwentyOneToTwentyFour,
			&poaLoads.PoaName,
			&poaLoads.Category,
			&poaLoads.ZeroToThree,
			&poaLoads.ThreeToSix,
			&poaLoads.SixToNine,
			&poaLoads.NineToTwelve,
			&poaLoads.TwelveToFifteen,
			&poaLoads.FifteenToEighteen,
			&poaLoads.EighteenToTwentyOne,
			&poaLoads.TwentyOneToTwentyFour,
		)
		if err != nil {
			log.Error(err.Error())
			return nil, err
			return poaLoadMap, err
		}

		// Add POA to map
		poaLoadMap[poaLoads.PoaName] = poaLoads
	}
	err = rows.Err()
	if err != nil {
		log.Error(err)
	}

	// Return error if not found
	if categoryLoads == nil {
		err = errors.New("Category Load not found: " + category)
		return nil, err
	if profiling {
		now := time.Now()
		log.Debug("GetAllPoaLoad: ", now.Sub(profilingTimers["GetAllPoaLoad"]))
	}
	return poaLoadMap, nil
}

// DeleteAllPoaLoads - Delete all POA entries
func (tm *TrafficMgr) DeleteAllPoaLoad() (err error) {
	if profiling {
		profilingTimers["DeleteAllPoa"] = time.Now()
	}

	_, err = tm.db.Exec(`DELETE FROM ` + TrafficTable)
	if err != nil {
		log.Error(err.Error())
		return err
	}

	if profiling {
		now := time.Now()
		log.Debug("GetCategoryLoad: ", now.Sub(profilingTimers["GetCategoryLoad"]))
		log.Debug("DeleteAllPoa: ", now.Sub(profilingTimers["DeleteAllPoa"]))
	}
	return categoryLoads, nil
	return nil
}

// PopulateCategoryTable - Populate the categories table
+154 −15
Original line number Diff line number Diff line
@@ -76,7 +76,7 @@ func TestNewTrafficMgr(t *testing.T) {
	// t.Fatalf("DONE")
}

func TestTrafficMgrCreateCategory(t *testing.T) {
func TestTrafficMgrCreateCategoryTable(t *testing.T) {
	fmt.Println("--- ", t.Name())
	log.MeepTextLogInit(t.Name())

@@ -98,14 +98,14 @@ func TestTrafficMgrCreateCategory(t *testing.T) {
	}

	// Make sure Category don't exist
	/*fmt.Println("Verify no Category present")
	fmt.Println("Verify no Category present")
	catMap, err := tm.GetAllCategoryLoad()
	if err != nil {
		t.Fatalf("Failed to get all Category")
	}
	if len(catMap) != 0 {
		t.Fatalf("No Category should be present")
	}*/
	}

	// Add Invalid Category
	fmt.Println("Create Invalid Category")
@@ -121,10 +121,10 @@ func TestTrafficMgrCreateCategory(t *testing.T) {
	}
	err = tm.CreateCategoryLoad("", catData) // Invalid category field value
	if err == nil {
		t.Fatalf("POA load creation should have failed")
		t.Fatalf("Category creation should have failed")
	}

	// Add POA load & Validate successfully added
	// Add Category & Validate successfully added
	catData = map[string]int32{
		FieldZeroToThree:           zeroToThree1,
		FieldThreeToSix:            threeToSix1,
@@ -139,26 +139,116 @@ func TestTrafficMgrCreateCategory(t *testing.T) {
	if err != nil {
		t.Fatalf("Failed to create asset: " + err.Error())
	}
	catMap, err := tm.GetCategoryLoad(category1)
	if err != nil || catMap == nil {
		t.Fatalf("Failed to get POA load")
	catLoad, err := tm.GetCategoryLoad(category1)
	if err != nil || catLoad == nil {
		t.Fatalf("Failed to get Category")
	}
	// Validate
	if !validateCategory(catMap, category1, zeroToThree1, threeToSix1, sixToNine1, nineToTwelve1, twelveToFifteen1, fifteenToEighteen1, eighteenToTwentyOne1, twentyOneToTwentyFour1) {
	if !validateCategory(catLoad, category1, zeroToThree1, threeToSix1, sixToNine1, nineToTwelve1, twelveToFifteen1, fifteenToEighteen1, eighteenToTwentyOne1, twentyOneToTwentyFour1) {
		t.Fatalf("Category validation failed")
	}
	// Delete all & validate updatespoaMap

	fmt.Println("Delete all & validate updates")
	// TODO
	/*err = tm.DeleteAllCategory()
	err = tm.DeleteAllCategory()
	if err != nil {
		t.Fatalf("Failed to delete all POA load")
		t.Fatalf("Failed to delete all Category")
	}
	catMap, err = tm.GetAllCategoryLoad()
	if err != nil || len(catMap) != 0 {
		t.Fatalf("Category should no longer exist")
	}

	// t.Fatalf("DONE")
}

func TestTrafficMgrCreateTrafficTable(t *testing.T) {
	fmt.Println("--- ", t.Name())
	log.MeepTextLogInit(t.Name())

	// Create Connector
	fmt.Println("Create valid VIS Asset Manager")
	tm, err := NewTrafficMgr(tmName, tmNamespace, tmDBUser, tmDBPwd, tmDBHost, tmDBPort)
	if err != nil || tm == nil {
		t.Fatalf("Failed to create VIS Asset Manager")
	}

	// Cleanup
	_ = tm.DeleteTables()

	// Create tables
	fmt.Println("Create Tables")
	err = tm.CreateTables()
	if err != nil {
		t.Fatalf("Failed to create tables")
	}

	// Make sure Traffic don't exist
	fmt.Println("Verify no Traffic present")
	poaLoadMap, err := tm.GetAllPoaLoad()
	if err != nil {
		t.Fatalf("Failed to get all Traffic")
	}
	if len(poaLoadMap) != 0 {
		t.Fatalf("No Traffic should be present")
	}

	// Add Invalid Traffic
	fmt.Println("Create Invalid Traffic")
	err = tm.CreatePoaLoad("", category1) // Invalid poaName field value
	if err == nil {
		t.Fatalf("Traffic creation should have failed")
	}
	err = tm.CreatePoaLoad(poaName1, "") // Invalid category field value
	if err == nil {
		t.Fatalf("Traffic creation should have failed")
	}
	err = tm.CreatePoaLoad(poaName1, category1) // Unknown category field value
	if err == nil {
		t.Fatalf("Traffic creation should have failed due to unknown category field")
	}

	// Add Traffic & Validate successfully added
	// 1. Add Category & Validate successfully added
	catData := map[string]int32{
		FieldZeroToThree:           zeroToThree1,
		FieldThreeToSix:            threeToSix1,
		FieldSixToNine:             sixToNine1,
		FieldNineToTwelve:          nineToTwelve1,
		FieldTwelveToFifteen:       twelveToFifteen1,
		FieldFifteenToEighteen:     fifteenToEighteen1,
		FieldEighteenToTwentyOne:   eighteenToTwentyOne1,
		FieldTwentyOneToTwentyFour: twentyOneToTwentyFour1,
	}
	err = tm.CreateCategoryLoad(category1, catData)
	if err != nil {
		t.Fatalf("Failed to create asset: " + err.Error())
	}
	// 2. Add Traffic
	err = tm.CreatePoaLoad(poaName1, category1)
	if err != nil {
		t.Fatalf("Failed to create asset: " + err.Error())
	}
	// 3. Validate successfully added
	trafficMap, err := tm.GetPoaLoad(poaName1)
	if err != nil || trafficMap == nil {
		t.Fatalf("Failed to get Traffic")
	}
	// Validate
	if !validatePoaLoads(trafficMap, poaName1, category1, zeroToThree1, threeToSix1, sixToNine1, nineToTwelve1, twelveToFifteen1, fifteenToEighteen1, eighteenToTwentyOne1, twentyOneToTwentyFour1) {
		t.Fatalf("Category validation failed")
	}
	// Delete all & validate updatespoaMap
	fmt.Println("Delete all & validate updates")
	err = tm.DeleteAllPoaLoad()
	if err != nil {
		t.Fatalf("Failed to delete all Traffic")
	}
	poaLoadMap, err = tm.GetAllPoaLoad()
	if err != nil || len(poaLoadMap) != 0 {
		t.Fatalf("Traffic should no longer exist")
	}
	poaMap, err = tm.GetAllCategory()
	if err != nil || len(poaMap) != 0 {
		t.Fatalf("POA load should no longer exist")
	}*/

	// t.Fatalf("DONE")
}
@@ -207,3 +297,52 @@ func validateCategory(categoryLoads *CategoryLoads, category string, zeroToThree

	return true
}

func validatePoaLoads(poaLoads *PoaLoads, poaName string, category string, zeroToThree int32, threeToSix int32, sixToNine int32, nineToTwelve int32, twelveToFifteen int32, fifteenToEighteen int32, eighteenToTwentyOne int32, twentyOneToTwentyFour int32) bool {
	if poaLoads == nil {
		fmt.Println("poaLoads == nil")
		return false
	}
	if poaLoads.PoaName != poaName {
		fmt.Println("PoaLoads.PoaName != poaName")
		return false
	}
	if poaLoads.Category != category {
		fmt.Println("PoaLoads.Category != category")
		return false
	}
	if poaLoads.ZeroToThree != zeroToThree {
		fmt.Println("PoaLoads.ZeroToThree != zeroToThree")
		return false
	}
	if poaLoads.ThreeToSix != threeToSix {
		fmt.Println("PoaLoads.ThreeToSix != threeToSix")
		return false
	}
	if poaLoads.SixToNine != sixToNine {
		fmt.Println("PoaLoads.SixToNine != sixToNine")
		return false
	}
	if poaLoads.NineToTwelve != nineToTwelve {
		fmt.Println("PoaLoads.NineToTwelve != nineToTwelve")
		return false
	}
	if poaLoads.EighteenToTwentyOne != eighteenToTwentyOne {
		fmt.Println("PoaLoads.EighteenToTwentyOne != eighteenToTwentyOne")
		return false
	}
	if poaLoads.FifteenToEighteen != fifteenToEighteen {
		fmt.Println("PoaLoads.FifteenToEighteen != fifteenToEighteen")
		return false
	}
	if poaLoads.FifteenToEighteen != fifteenToEighteen {
		fmt.Println("PoaLoads.FifteenToEighteen != fifteenToEighteen")
		return false
	}
	if poaLoads.TwentyOneToTwentyFour != twentyOneToTwentyFour {
		fmt.Println("PoaLoads.TwentyOneToTwentyFour != twentyOneToTwentyFour")
		return false
	}

	return true
}