Debian Codegraph @

api/v0/mesosutil/mesosprotoutil_test.go

raw symbols SCIP
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package mesosutil
20
21 import (
22 "github.com/gogo/protobuf/proto"
23 mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
24 "github.com/stretchr/testify/assert"
25 "testing"
26 )
27
28 func TestFilterResources(t *testing.T) {
29 resources := []*mesos.Resource{
30 NewScalarResource("mem", 200),
31 NewScalarResource("cpu", 4),
32 NewScalarResource("mem", 500),
33 }
34
35 memRes := FilterResources(resources, func(res *mesos.Resource) bool {
36 if res.GetType() == mesos.Value_SCALAR && res.GetName() == "mem" {
37 return true
38 }
39 return false
40 })
41
42 assert.Equal(t, 2, len(memRes))
43 }
44
45 func TestNewValueRange(t *testing.T) {
46 val := NewValueRange(20, 40)
47 if val == nil {
48 t.Fatal("Not creating protobuf object Value_Range.")
49 }
50
51 if (val.GetEnd() - val.GetBegin()) != 20 {
52 t.Fatal("Protobuf object Value_Range not returning expected values.")
53 }
54 }
55
56 func TestNewScalarResource(t *testing.T) {
57 val := NewScalarResource("mem", 200)
58 if val == nil {
59 t.Fatal("Not creating protobuf object Resource properly.")
60 }
61 if val.GetType() != mesos.Value_SCALAR {
62 t.Fatal("Expected type SCALAR for protobuf, got", val.GetType())
63 }
64 if val.GetName() != "mem" && val.GetScalar().GetValue() != 200 {
65 t.Fatal("Protobuf object Resource has wrong name and Scalar values.")
66 }
67 }
68
69 func TestNewRangesResource(t *testing.T) {
70 val := NewRangesResource("quotas", []*mesos.Value_Range{NewValueRange(20, 40)})
71 if val == nil {
72 t.Fatal("Not creating protobuf object Resource properly.")
73 }
74 if val.GetType() != mesos.Value_RANGES {
75 t.Fatal("Expected type SCALAR for protobuf, got", val.GetType())
76 }
77 if len(val.GetRanges().GetRange()) != 1 {
78 t.Fatal("Expected Resource of type RANGES with 1 range, but got", len(val.GetRanges().GetRange()))
79 }
80
81 }
82
83 func TestNewSetResource(t *testing.T) {
84 val := NewSetResource("greeting", []string{"hello", "world"})
85 if val == nil {
86 t.Fatal("Not creating protobuf object Resource properly.")
87 }
88 if val.GetType() != mesos.Value_SET {
89 t.Fatal("Expected type SET for protobuf, got", val.GetType())
90 }
91 if len(val.GetSet().GetItem()) != 2 {
92 t.Fatal("Expected Resource of type SET with 2 items, but got", len(val.GetRanges().GetRange()))
93 }
94 if val.GetSet().GetItem()[0] != "hello" {
95 t.Fatal("Protobuf Resource of type SET got wrong value.")
96 }
97 }
98
99 func TestNewFrameworkID(t *testing.T) {
100 id := NewFrameworkID("test-id")
101 if id == nil {
102 t.Fatal("Not creating protobuf oject FrameworkID.")
103 }
104 if id.GetValue() != "test-id" {
105 t.Fatal("Protobuf object not returning expected value.")
106 }
107 }
108
109 func TestNewFrameworkInfo(t *testing.T) {
110 info := NewFrameworkInfo("test-user", "test-name", NewFrameworkID("test-id"))
111 info.Hostname = proto.String("localhost")
112 if info == nil {
113 t.Fatal("Not creating protobuf object FrameworkInfo")
114 }
115 if info.GetUser() != "test-user" {
116 t.Fatal("Protobuf object FrameworkInfo.User missing value.")
117 }
118 if info.GetName() != "test-name" {
119 t.Fatal("Protobuf object FrameworkInfo.Name missing value.")
120 }
121 if info.GetId() == nil {
122 t.Fatal("Protobuf object FrameowrkInfo.Id missing value.")
123 }
124 if info.GetHostname() != "localhost" {
125 t.Fatal("Protobuf object FrameworkInfo.Hostname missing value.")
126 }
127 }
128
129 func TestNewMasterInfo(t *testing.T) {
130 master := NewMasterInfo("master-1", 1234, 5678)
131 if master == nil {
132 t.Fatal("Not creating protobuf object MasterInfo")
133 }
134 if master.GetId() != "master-1" {
135 t.Fatal("Protobuf object MasterInfo.Id missing.")
136 }
137 if master.GetIp() != 1234 {
138 t.Fatal("Protobuf object MasterInfo.Ip missing.")
139 }
140 if master.GetPort() != 5678 {
141 t.Fatal("Protobuf object MasterInfo.Port missing.")
142 }
143 }
144
145 func TestNewOfferID(t *testing.T) {
146 id := NewOfferID("offer-1")
147 if id == nil {
148 t.Fatal("Not creating protobuf object OfferID")
149 }
150 if id.GetValue() != "offer-1" {
151 t.Fatal("Protobuf object OfferID.Value missing.")
152 }
153 }
154
155 func TestNewOffer(t *testing.T) {
156 offer := NewOffer(NewOfferID("offer-1"), NewFrameworkID("framework-1"), NewSlaveID("slave-1"), "localhost")
157 if offer == nil {
158 t.Fatal("Not creating protobuf object Offer")
159 }
160 if offer.GetId().GetValue() != "offer-1" {
161 t.Fatal("Protobuf object Offer.Id missing")
162 }
163 if offer.GetFrameworkId().GetValue() != "framework-1" {
164 t.Fatal("Protobuf object Offer.FrameworkId missing.")
165 }
166 if offer.GetSlaveId().GetValue() != "slave-1" {
167 t.Fatal("Protobuf object Offer.SlaveId missing.")
168 }
169 if offer.GetHostname() != "localhost" {
170 t.Fatal("Protobuf object offer.Hostname missing.")
171 }
172 }
173
174 func TestNewSlaveID(t *testing.T) {
175 id := NewSlaveID("slave-1")
176 if id == nil {
177 t.Fatal("Not creating protobuf object SlaveID")
178 }
179 if id.GetValue() != "slave-1" {
180 t.Fatal("Protobuf object SlaveID.Value missing.")
181 }
182 }
183
184 func TestNewTaskID(t *testing.T) {
185 id := NewSlaveID("task-1")
186 if id == nil {
187 t.Fatal("Not creating protobuf object TaskID")
188 }
189 if id.GetValue() != "task-1" {
190 t.Fatal("Protobuf object TaskID.Value missing.")
191 }
192 }
193
194 func TestNewTaskInfo(t *testing.T) {
195 info := NewTaskInfo(
196 "simple-task",
197 NewTaskID("simpe-task-1"),
198 NewSlaveID("slave-1"),
199 []*mesos.Resource{NewScalarResource("mem", 400)},
200 )
201 if info == nil {
202 t.Fatal("Not creating protobuf object TaskInfo")
203 }
204 if info.GetName() != "simple-task" {
205 t.Fatal("Protobuf object TaskInfo.Name missing.")
206 }
207 if info.GetTaskId() == nil {
208 t.Fatal("Protobuf object TaskInfo.TaskId missing.")
209 }
210 if info.GetSlaveId() == nil {
211 t.Fatal("Protobuf object TaskInfo.SlaveId missing.")
212 }
213 if len(info.GetResources()) != 1 {
214 t.Fatal("Protobuf object TaskInfo.Resources missing.")
215 }
216 }
217
218 func TestNewTaskStatus(t *testing.T) {
219 status := NewTaskStatus(NewTaskID("task-1"), mesos.TaskState_TASK_RUNNING)
220 if status == nil {
221 t.Fatal("Not creating protobuf object TaskStatus")
222 }
223 if status.GetTaskId().GetValue() != "task-1" {
224 t.Fatal("Protobuf object TaskStatus.TaskId missing.")
225 }
226 if status.GetState() != mesos.TaskState(mesos.TaskState_TASK_RUNNING) {
227 t.Fatal("Protobuf object TaskStatus.State missing.")
228 }
229 }
230
231 func TestNewCommandInfo(t *testing.T) {
232 cmd := NewCommandInfo("echo Hello!")
233 if cmd == nil {
234 t.Fatal("Not creating protobuf object CommandInfo")
235 }
236 if cmd.GetValue() != "echo Hello!" {
237 t.Fatal("Protobuf object CommandInfo.Value missing")
238 }
239 }
240
241 func TestNewExecutorInfo(t *testing.T) {
242 info := NewExecutorInfo(NewExecutorID("exec-1"), NewCommandInfo("ls -l"))
243 if info == nil {
244 t.Fatal("Not creating protobuf object ExecutorInfo")
245 }
246 if info.GetExecutorId().GetValue() != "exec-1" {
247 t.Fatal("Protobuf object ExecutorInfo.ExecutorId missing")
248 }
249 if info.GetCommand().GetValue() != "ls -l" {
250 t.Fatal("Protobuf object ExecutorInfo.Command missing")
251 }
252 }