2015-09-09 01:42:34 +00:00
|
|
|
package api
|
|
|
|
|
2017-02-06 19:48:28 +00:00
|
|
|
import (
|
2022-12-16 18:25:28 +00:00
|
|
|
"crypto/rand"
|
2019-11-12 14:47:18 +00:00
|
|
|
"fmt"
|
2017-02-06 19:48:28 +00:00
|
|
|
"testing"
|
2022-12-16 18:25:28 +00:00
|
|
|
|
|
|
|
"github.com/shoenig/test/must"
|
2017-02-06 19:48:28 +00:00
|
|
|
)
|
2015-09-09 01:42:34 +00:00
|
|
|
|
|
|
|
func assertQueryMeta(t *testing.T, qm *QueryMeta) {
|
2018-03-21 17:13:26 +00:00
|
|
|
t.Helper()
|
2022-12-16 18:25:28 +00:00
|
|
|
|
|
|
|
must.NotEq(t, 0, qm.LastIndex, must.Sprint("bad index"))
|
|
|
|
must.True(t, qm.KnownLeader, must.Sprint("expected a known leader but gone none"))
|
2015-09-09 01:42:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func assertWriteMeta(t *testing.T, wm *WriteMeta) {
|
2018-03-21 17:13:26 +00:00
|
|
|
t.Helper()
|
2015-09-09 01:42:34 +00:00
|
|
|
if wm.LastIndex == 0 {
|
|
|
|
t.Fatalf("bad index: %d", wm.LastIndex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testJob() *Job {
|
2020-10-09 05:21:41 +00:00
|
|
|
task := NewTask("task1", "raw_exec").
|
2016-04-13 22:55:46 +00:00
|
|
|
SetConfig("command", "/bin/sleep").
|
2016-02-02 21:26:12 +00:00
|
|
|
Require(&Resources{
|
2022-08-17 16:26:34 +00:00
|
|
|
CPU: pointerOf(100),
|
|
|
|
MemoryMB: pointerOf(256),
|
2016-02-19 23:49:32 +00:00
|
|
|
}).
|
2016-02-10 21:36:47 +00:00
|
|
|
SetLogConfig(&LogConfig{
|
2022-08-17 16:26:34 +00:00
|
|
|
MaxFiles: pointerOf(1),
|
|
|
|
MaxFileSizeMB: pointerOf(2),
|
2016-02-19 23:49:32 +00:00
|
|
|
})
|
2015-09-16 18:42:08 +00:00
|
|
|
|
|
|
|
group := NewTaskGroup("group1", 1).
|
2016-08-26 03:10:25 +00:00
|
|
|
AddTask(task).
|
2016-09-14 22:43:42 +00:00
|
|
|
RequireDisk(&EphemeralDisk{
|
2022-08-17 16:26:34 +00:00
|
|
|
SizeMB: pointerOf(25),
|
2016-08-26 03:10:25 +00:00
|
|
|
})
|
2015-09-16 18:42:08 +00:00
|
|
|
|
2019-05-02 20:00:21 +00:00
|
|
|
job := NewBatchJob("job1", "redis", "global", 1).
|
2015-09-16 18:42:08 +00:00
|
|
|
AddDatacenter("dc1").
|
|
|
|
AddTaskGroup(group)
|
|
|
|
|
|
|
|
return job
|
2015-09-09 01:42:34 +00:00
|
|
|
}
|
2016-01-13 18:19:53 +00:00
|
|
|
|
2020-01-27 22:14:28 +00:00
|
|
|
func testJobWithScalingPolicy() *Job {
|
|
|
|
job := testJob()
|
|
|
|
job.TaskGroups[0].Scaling = &ScalingPolicy{
|
|
|
|
Policy: map[string]interface{}{},
|
2022-08-17 16:26:34 +00:00
|
|
|
Min: pointerOf(int64(1)),
|
|
|
|
Max: pointerOf(int64(5)),
|
|
|
|
Enabled: pointerOf(true),
|
2020-01-27 22:14:28 +00:00
|
|
|
}
|
|
|
|
return job
|
|
|
|
}
|
|
|
|
|
2016-01-13 18:19:53 +00:00
|
|
|
func testPeriodicJob() *Job {
|
|
|
|
job := testJob().AddPeriodicConfig(&PeriodicConfig{
|
2022-08-17 16:26:34 +00:00
|
|
|
Enabled: pointerOf(true),
|
|
|
|
Spec: pointerOf("*/30 * * * *"),
|
|
|
|
SpecType: pointerOf("cron"),
|
2016-01-13 18:19:53 +00:00
|
|
|
})
|
|
|
|
return job
|
|
|
|
}
|
2017-09-07 23:56:15 +00:00
|
|
|
|
2020-09-09 22:30:40 +00:00
|
|
|
func testRecommendation(job *Job) *Recommendation {
|
|
|
|
rec := &Recommendation{
|
|
|
|
ID: "",
|
|
|
|
Region: *job.Region,
|
|
|
|
Namespace: *job.Namespace,
|
|
|
|
JobID: *job.ID,
|
|
|
|
Group: *job.TaskGroups[0].Name,
|
|
|
|
Task: job.TaskGroups[0].Tasks[0].Name,
|
|
|
|
Resource: "CPU",
|
|
|
|
Value: *job.TaskGroups[0].Tasks[0].Resources.CPU * 2,
|
|
|
|
Meta: map[string]interface{}{
|
|
|
|
"testing": true,
|
|
|
|
"mocked": "also true",
|
|
|
|
},
|
|
|
|
Stats: map[string]float64{
|
|
|
|
"median": 50.0,
|
|
|
|
"mean": 51.0,
|
|
|
|
"max": 75.5,
|
|
|
|
"99": 73.0,
|
|
|
|
"min": 0.0,
|
|
|
|
},
|
|
|
|
EnforceVersion: false,
|
|
|
|
}
|
|
|
|
return rec
|
|
|
|
}
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
func testNamespace() *Namespace {
|
|
|
|
return &Namespace{
|
|
|
|
Name: "test-namespace",
|
|
|
|
Description: "Testing namespaces",
|
|
|
|
}
|
|
|
|
}
|
2017-10-13 21:36:02 +00:00
|
|
|
|
|
|
|
func testQuotaSpec() *QuotaSpec {
|
|
|
|
return &QuotaSpec{
|
2023-03-20 09:40:51 +00:00
|
|
|
Name: "test-quota",
|
2017-10-13 21:36:02 +00:00
|
|
|
Description: "Testing namespaces",
|
|
|
|
Limits: []*QuotaLimit{
|
|
|
|
{
|
|
|
|
Region: "global",
|
|
|
|
RegionLimit: &Resources{
|
2022-08-17 16:26:34 +00:00
|
|
|
CPU: pointerOf(2000),
|
|
|
|
MemoryMB: pointerOf(2000),
|
2017-10-13 21:36:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 18:28:35 +00:00
|
|
|
|
|
|
|
// conversions utils only used for testing
|
|
|
|
// added here to avoid linter warning
|
|
|
|
|
|
|
|
// float64ToPtr returns the pointer to an float64
|
|
|
|
func float64ToPtr(f float64) *float64 {
|
|
|
|
return &f
|
|
|
|
}
|
2019-11-12 14:47:18 +00:00
|
|
|
|
|
|
|
// generateUUID generates a uuid useful for testing only
|
|
|
|
func generateUUID() string {
|
|
|
|
buf := make([]byte, 16)
|
2022-12-16 18:25:28 +00:00
|
|
|
if _, err := rand.Read(buf); err != nil {
|
2019-11-12 14:47:18 +00:00
|
|
|
panic(fmt.Errorf("failed to read random bytes: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x",
|
|
|
|
buf[0:4],
|
|
|
|
buf[4:6],
|
|
|
|
buf[6:8],
|
|
|
|
buf[8:10],
|
|
|
|
buf[10:16])
|
|
|
|
}
|