Unit testing for def/ module.
Added unit tests to test code in def/ module.
This commit is contained in:
parent
e24b8a08c9
commit
bac60e872a
396 changed files with 83991 additions and 13209 deletions
31
def/sortingCriteria_test.go
Normal file
31
def/sortingCriteria_test.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
// Copyright (C) 2018 spdf
|
||||
//
|
||||
// This file is part of Elektron.
|
||||
//
|
||||
// Elektron is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Elektron is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Elektron. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
package def
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSortingCriteria(t *testing.T) {
|
||||
task := &Task{CPU: 1.0, RAM: 1024.0, Watts: 50.0}
|
||||
assert.Equal(t, 1.0, SortByCPU(task))
|
||||
assert.Equal(t, 1024.0, SortByRAM(task))
|
||||
assert.Equal(t, 50.0, SortByWatts(task))
|
||||
}
|
136
def/taskUtils_test.go
Normal file
136
def/taskUtils_test.go
Normal file
|
@ -0,0 +1,136 @@
|
|||
// Copyright (C) 2018 spdf
|
||||
//
|
||||
// This file is part of Elektron.
|
||||
//
|
||||
// Elektron is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Elektron is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Elektron. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
package def
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var instances = 1
|
||||
var tasks = []Task{
|
||||
{
|
||||
Name: "task1",
|
||||
CPU: 4.0,
|
||||
RAM: 1024,
|
||||
Watts: 50.0,
|
||||
TaskID: "electron-task1-1",
|
||||
Instances: &instances,
|
||||
},
|
||||
{
|
||||
Name: "task2",
|
||||
CPU: 3.0,
|
||||
RAM: 128,
|
||||
Watts: 55.0,
|
||||
TaskID: "electron-task2-1",
|
||||
Instances: &instances,
|
||||
},
|
||||
{
|
||||
Name: "task3",
|
||||
CPU: 2.0,
|
||||
RAM: 64,
|
||||
Watts: 75.0,
|
||||
TaskID: "electron-task3-1",
|
||||
Instances: &instances,
|
||||
},
|
||||
{
|
||||
Name: "task4",
|
||||
CPU: 1.0,
|
||||
RAM: 3072,
|
||||
Watts: 81.0,
|
||||
TaskID: "electron-task4-1",
|
||||
Instances: &instances,
|
||||
},
|
||||
}
|
||||
|
||||
// Test task classification based on Watts requirement.
|
||||
func TestClassifyTasks(t *testing.T) {
|
||||
// Classifying tasks into two clusters.
|
||||
taskClusters := ClassifyTasks(tasks, 2)
|
||||
assert.True(t, len(taskClusters) == 2, "failed to classify tasks into 2 clusters")
|
||||
|
||||
// Test whether the clusters are ordered.
|
||||
firstCluster := taskClusters[0]
|
||||
secondCluster := taskClusters[1]
|
||||
assert.Less(t, firstCluster.SizeScore, secondCluster.SizeScore, "failed to order task clusters")
|
||||
|
||||
// Tasks in the first cluster should be less power-intensive when compared to the tasks in the second cluster.
|
||||
for _, taskFirstCluster := range firstCluster.Tasks {
|
||||
for _, taskSecondCluster := range secondCluster.Tasks {
|
||||
assert.LessOrEqual(t, taskFirstCluster.Watts,
|
||||
taskSecondCluster.Watts, "failed to correctly classify tasks")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSortTasks(t *testing.T) {
|
||||
testSortedOrder := func(sortingCriteria string, sortedValues []float64, value func(i int) float64) {
|
||||
assert.Equal(t, len(sortedValues), len(tasks))
|
||||
for i := 0; i < len(tasks); i++ {
|
||||
assert.Equal(t, sortedValues[i], value(i), "failed to sort tasks by "+sortingCriteria)
|
||||
}
|
||||
}
|
||||
|
||||
// Sorting by CPU.
|
||||
SortTasks(tasks, SortByCPU)
|
||||
testSortedOrder(
|
||||
"CPU",
|
||||
[]float64{1.0, 2.0, 3.0, 4.0},
|
||||
func(i int) float64 {
|
||||
return tasks[i].CPU
|
||||
})
|
||||
|
||||
// Sorting by RAM.
|
||||
SortTasks(tasks, SortByRAM)
|
||||
testSortedOrder(
|
||||
"RAM",
|
||||
[]float64{64, 128, 1024, 3072},
|
||||
func(i int) float64 {
|
||||
return tasks[i].RAM
|
||||
})
|
||||
|
||||
// Sorting by Watts.
|
||||
SortTasks(tasks, SortByWatts)
|
||||
testSortedOrder(
|
||||
"Watts",
|
||||
[]float64{50.0, 55.0, 75.0, 81.0},
|
||||
func(i int) float64 {
|
||||
return tasks[i].Watts
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetResourceRequirement(t *testing.T) {
|
||||
initTaskResourceRequirements(tasks)
|
||||
|
||||
for _, task := range tasks {
|
||||
resources, err := GetResourceRequirement(task.TaskID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, resources.CPU, task.CPU)
|
||||
assert.Equal(t, resources.Ram, task.RAM)
|
||||
assert.Equal(t, resources.Watts, task.Watts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTaskDistributionInWindow(t *testing.T) {
|
||||
// Using a window of size 4.
|
||||
taskDistribution, err := GetTaskDistributionInWindow(4, tasks)
|
||||
assert.NoError(t, err)
|
||||
// The tasks above are evenly distributed hence, task distribution should be 1.0.
|
||||
assert.Equal(t, taskDistribution, 1.0, "task distribution determined is incorrect")
|
||||
}
|
142
def/task_test.go
Normal file
142
def/task_test.go
Normal file
|
@ -0,0 +1,142 @@
|
|||
// Copyright (C) 2018 spdf
|
||||
//
|
||||
// This file is part of Elektron.
|
||||
//
|
||||
// Elektron is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Elektron is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Elektron. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
package def
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gitlab.com/spdf/elektron/constants"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTasksFromJSON(t *testing.T) {
|
||||
tasks, err := TasksFromJSON("../workload_sample.json")
|
||||
assert.Equal(t, len(tasks), 2)
|
||||
assert.NoError(t, err)
|
||||
|
||||
instances := 10
|
||||
minifeTask := Task{
|
||||
Name: "minife",
|
||||
CPU: 3.0,
|
||||
RAM: 4096,
|
||||
Watts: 63.141,
|
||||
Image: "rdelvalle/minife:electron1",
|
||||
CMD: "cd src && mpirun -np 3 miniFE.x -nx 100 -ny 100 -nz 100",
|
||||
Instances: &instances,
|
||||
ClassToWatts: map[string]float64{
|
||||
"A": 93.062,
|
||||
"B": 65.552,
|
||||
"C": 57.897,
|
||||
"D": 60.729,
|
||||
},
|
||||
}
|
||||
|
||||
dgemmTask := Task{
|
||||
Name: "dgemm",
|
||||
CPU: 3.0,
|
||||
RAM: 32,
|
||||
Watts: 85.903,
|
||||
Image: "rdelvalle/dgemm:electron1",
|
||||
CMD: "/./mt-dgemm 1024",
|
||||
Instances: &instances,
|
||||
ClassToWatts: map[string]float64{
|
||||
"A": 114.789,
|
||||
"B": 89.133,
|
||||
"C": 82.672,
|
||||
"D": 81.944,
|
||||
},
|
||||
}
|
||||
|
||||
assert.True(t, reflect.DeepEqual(minifeTask, tasks[0]))
|
||||
assert.True(t, reflect.DeepEqual(dgemmTask, tasks[1]))
|
||||
}
|
||||
|
||||
func TestTask_UpdateHost(t *testing.T) {
|
||||
task := Task{}
|
||||
// UpdateHost should fail if the host is unknown.
|
||||
assert.False(t, task.UpdateHost("unknown-host"))
|
||||
|
||||
constants.Hosts["known-host"] = struct{}{}
|
||||
// UpdateHost should not succeed as hostname (known-host) is known.
|
||||
task.UpdateHost("known-host")
|
||||
assert.Equal(t, task.Host, "known-host")
|
||||
}
|
||||
|
||||
func TestTask_SetTaskID(t *testing.T) {
|
||||
instances := 1
|
||||
task := Task{
|
||||
Name: "test-task",
|
||||
Instances: &instances,
|
||||
}
|
||||
|
||||
taskID := fmt.Sprintf("electron-%s-%d", task.Name, *task.Instances)
|
||||
task.SetTaskID(taskID)
|
||||
assert.Equal(t, taskID, task.TaskID, "failed to set task ID")
|
||||
}
|
||||
|
||||
func TestWattsToConsider(t *testing.T) {
|
||||
task := Task{
|
||||
Name: "minife",
|
||||
Watts: 50.0,
|
||||
ClassToWatts: map[string]float64{
|
||||
"A": 30.2475289996,
|
||||
"B": 35.6491229228,
|
||||
"C": 24.0476734352,
|
||||
},
|
||||
}
|
||||
|
||||
powerClass := "A"
|
||||
classAttribute := "class"
|
||||
offerClassA := &mesos.Offer{
|
||||
Attributes: []*mesos.Attribute{
|
||||
{
|
||||
Name: &classAttribute,
|
||||
Text: &mesos.Value_Text{Value: &powerClass},
|
||||
},
|
||||
},
|
||||
}
|
||||
// Without class to watts mapping.
|
||||
// Should return the Watts value.
|
||||
wattsClassMapWattsDisabled, err := WattsToConsider(task, false, offerClassA)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, task.Watts, wattsClassMapWattsDisabled)
|
||||
|
||||
// with class to watts mapping.
|
||||
// Should return task.ClassToWatts[pc] where pc is the power-class
|
||||
// of the host corresponding to the offer.
|
||||
wattsClassMapWattsEnabled, err := WattsToConsider(task, true, offerClassA)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, task.ClassToWatts["A"], wattsClassMapWattsEnabled)
|
||||
}
|
||||
|
||||
func TestCompare(t *testing.T) {
|
||||
task1 := Task{
|
||||
Name: "test-task1",
|
||||
TaskID: "electron-test-task1-1",
|
||||
}
|
||||
|
||||
task2 := Task{
|
||||
TaskID: "electron-test-task1-1",
|
||||
}
|
||||
|
||||
// If two tasks have the same Task ID they should be the same task.
|
||||
assert.True(t, Compare(&task1, &task2))
|
||||
}
|
Reference in a new issue