-
Notifications
You must be signed in to change notification settings - Fork 158
/
constraints_test.go
66 lines (54 loc) · 1.41 KB
/
constraints_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
package empire
import (
"encoding/json"
"fmt"
"reflect"
"testing"
. "github.com/remind101/empire/pkg/bytesize"
"github.com/remind101/empire/pkg/constraints"
)
func TestConstraints_UnmarshalJSON(t *testing.T) {
tests := []struct {
in string
out Constraints
err error
}{
{"512:1KB", Constraints{512, 1024, 0}, nil},
{"512:1KB:nproc=512", Constraints{512, 1024, 512}, nil},
{"1025:1KB", Constraints{1025, 1024, 0}, nil},
{"0:1KB", Constraints{}, constraints.ErrInvalidCPUShare},
{"1024", Constraints{}, constraints.ErrInvalidConstraint},
}
for i, tt := range tests {
var c Constraints
err := json.Unmarshal([]byte(fmt.Sprintf(`"%s"`, tt.in)), &c)
if err != tt.err {
t.Fatalf("#%d: err => %v; want %v", i, err, tt.err)
}
if tt.err != nil {
continue
}
if got, want := c, tt.out; !reflect.DeepEqual(got, want) {
t.Fatalf("#%d: Constraints => %v; want %v", i, got, want)
}
}
}
func TestConstraints_String(t *testing.T) {
tests := []struct {
in Constraints
out string
}{
// Named constraints
{Constraints1X, "1X"},
{Constraints2X, "2X"},
{ConstraintsPX, "PX"},
{Constraints{100, constraints.Memory(1 * MB), 0}, "100:1.00mb"},
{Constraints{100, constraints.Memory(1 * MB), 512}, "100:1.00mb:nproc=512"},
}
for _, tt := range tests {
out := tt.in.String()
if got, want := out, tt.out; got != want {
t.Fatalf(".String() => %s; want %s", got, want)
}
}
}