Debian Codegraph @

api/v1/lib/resourcetest/resourcetest.go

raw symbols SCIP
1 package resourcetest
2
3 // maybe this could eventually be a resourcedsl package, but resource construction rules aren't that strict here yet
4
5 import (
6 "testing"
7
8 "github.com/mesos/mesos-go/api/v1/lib"
9 )
10
11 // Opt is a functional resource modifier
12 type Opt func(*mesos.Resource)
13
14 func Resource(opt ...Opt) (r mesos.Resource) {
15 if len(opt) == 0 {
16 return
17 }
18 for _, f := range opt {
19 f(&r)
20 }
21 return
22 }
23
24 func Name(x string) Opt { return func(r *mesos.Resource) { r.Name = x } }
25 func Role(x string) Opt { return func(r *mesos.Resource) { r.Role = &x } }
26
27 func Revocable() Opt {
28 return func(r *mesos.Resource) { r.Revocable = &mesos.Resource_RevocableInfo{} }
29 }
30
31 func ValueScalar(x float64) Opt {
32 return func(r *mesos.Resource) {
33 r.Type = mesos.SCALAR.Enum()
34 r.Scalar = &mesos.Value_Scalar{Value: x}
35 }
36 }
37
38 func ValueSet(x ...string) Opt {
39 return func(r *mesos.Resource) {
40 r.Type = mesos.SET.Enum()
41 r.Set = &mesos.Value_Set{Item: x}
42 }
43 }
44
45 type RangeOpt func(*mesos.Ranges)
46
47 // Span naively appends a range to a Ranges collection ("range" is a keyword, so I called this func "Span")
48 func Span(bp, ep uint64) RangeOpt {
49 return func(rs *mesos.Ranges) {
50 *rs = append(*rs, mesos.Value_Range{Begin: bp, End: ep})
51 }
52 }
53
54 func ValueRange(p ...RangeOpt) Opt {
55 return func(r *mesos.Resource) {
56 rs := mesos.Ranges(nil)
57 for _, f := range p {
58 f(&rs)
59 }
60 r.Type = mesos.RANGES.Enum()
61 r.Ranges = r.Ranges.Add(&mesos.Value_Ranges{Range: rs})
62 }
63 }
64
65 func Resources(r ...mesos.Resource) (result mesos.Resources) {
66 return result.Add(r...)
67 }
68
69 func Reservation(ri *mesos.Resource_ReservationInfo) Opt {
70 return func(r *mesos.Resource) {
71 r.Reservation = ri
72 }
73 }
74
75 func Disk(persistenceID, containerPath string) Opt {
76 return func(r *mesos.Resource) {
77 r.Disk = &mesos.Resource_DiskInfo{}
78 if containerPath != "" {
79 r.Disk.Volume = &mesos.Volume{ContainerPath: containerPath}
80 }
81 if persistenceID != "" {
82 r.Disk.Persistence = &mesos.Resource_DiskInfo_Persistence{ID: persistenceID}
83 }
84 }
85 }
86
87 func DiskWithSource(persistenceID, containerPath, source string, sourceType mesos.Resource_DiskInfo_Source_Type) Opt {
88 return func(r *mesos.Resource) {
89 r.Disk = &mesos.Resource_DiskInfo{}
90 if containerPath != "" {
91 r.Disk.Volume = &mesos.Volume{ContainerPath: containerPath}
92 }
93 if persistenceID != "" {
94 r.Disk.Persistence = &mesos.Resource_DiskInfo_Persistence{ID: persistenceID}
95 }
96 if source != "" {
97 r.Disk.Source = &mesos.Resource_DiskInfo_Source{Type: sourceType}
98 switch sourceType {
99 case mesos.PATH:
100 r.Disk.Source.Path = &mesos.Resource_DiskInfo_Source_Path{Root: &source}
101 case mesos.MOUNT:
102 r.Disk.Source.Mount = &mesos.Resource_DiskInfo_Source_Mount{Root: &source}
103 }
104 }
105 }
106 }
107
108 func ReservedBy(principal string) *mesos.Resource_ReservationInfo {
109 result := &mesos.Resource_ReservationInfo{}
110 if principal != "" {
111 result.Principal = &principal
112 }
113 return result
114 }
115
116 func Reserve(r mesos.Resources) *mesos.Offer_Operation {
117 return &mesos.Offer_Operation{
118 Type: mesos.Offer_Operation_RESERVE,
119 Reserve: &mesos.Offer_Operation_Reserve{
120 Resources: r,
121 },
122 }
123 }
124
125 func Unreserve(r mesos.Resources) *mesos.Offer_Operation {
126 return &mesos.Offer_Operation{
127 Type: mesos.Offer_Operation_UNRESERVE,
128 Unreserve: &mesos.Offer_Operation_Unreserve{
129 Resources: r,
130 },
131 }
132 }
133
134 func Create(r mesos.Resources) *mesos.Offer_Operation {
135 return &mesos.Offer_Operation{
136 Type: mesos.Offer_Operation_CREATE,
137 Create: &mesos.Offer_Operation_Create{
138 Volumes: r,
139 },
140 }
141 }
142
143 func Expect(t *testing.T, cond bool, msgformat string, args ...interface{}) bool {
144 if !cond {
145 t.Errorf(msgformat, args...)
146 }
147 return cond
148 }