forked from hashicorp/nomad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
149 lines (129 loc) · 3.23 KB
/
util_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package api
import (
"crypto/rand"
"fmt"
"testing"
"github.com/shoenig/test/must"
)
func assertQueryMeta(t *testing.T, qm *QueryMeta) {
t.Helper()
must.NotEq(t, 0, qm.LastIndex, must.Sprint("bad index"))
must.True(t, qm.KnownLeader, must.Sprint("expected a known leader but gone none"))
}
func assertWriteMeta(t *testing.T, wm *WriteMeta) {
t.Helper()
must.Positive(t, wm.LastIndex, must.Sprint("expected WriteMeta.LastIndex to be > 0"))
}
func testJob() *Job {
task := NewTask("task1", "raw_exec").
SetConfig("command", "/bin/sleep").
Require(&Resources{
CPU: pointerOf(100),
MemoryMB: pointerOf(256),
}).
SetLogConfig(&LogConfig{
MaxFiles: pointerOf(1),
MaxFileSizeMB: pointerOf(2),
})
group := NewTaskGroup("group1", 1).
AddTask(task).
RequireDisk(&EphemeralDisk{
SizeMB: pointerOf(25),
})
job := NewBatchJob("job1", "redis", "global", 1).
AddDatacenter("dc1").
AddTaskGroup(group)
return job
}
func testServiceJob() *Job {
// Create a job of type service
task := NewTask("dummy-task", "exec").SetConfig("command", "/bin/sleep")
group1 := NewTaskGroup("dummy-group", 1).AddTask(task)
job := NewServiceJob("dummy-service", "dummy-service", "global", 5).AddTaskGroup(group1)
return job
}
func testJobWithScalingPolicy() *Job {
job := testJob()
job.TaskGroups[0].Scaling = &ScalingPolicy{
Policy: map[string]interface{}{},
Min: pointerOf(int64(1)),
Max: pointerOf(int64(5)),
Enabled: pointerOf(true),
}
return job
}
func testPeriodicJob() *Job {
job := testJob().AddPeriodicConfig(&PeriodicConfig{
Enabled: pointerOf(true),
Spec: pointerOf("*/30 * * * *"),
SpecType: pointerOf("cron"),
})
return job
}
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
}
func testNamespace() *Namespace {
return &Namespace{
Name: "test-namespace",
Description: "Testing namespaces",
}
}
func testQuotaSpec() *QuotaSpec {
return &QuotaSpec{
Name: "test-quota",
Description: "Testing namespaces",
Limits: []*QuotaLimit{
{
Region: "global",
RegionLimit: &Resources{
CPU: pointerOf(2000),
MemoryMB: pointerOf(2000),
},
},
},
}
}
// 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
}
// generateUUID generates a uuid useful for testing only
func generateUUID() string {
buf := make([]byte, 16)
if _, err := rand.Read(buf); err != nil {
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])
}