Skip to content

Commit 22185cc

Browse files
committed
Minor cleanup
1 parent a94dc62 commit 22185cc

14 files changed

Lines changed: 62 additions & 94 deletions

internal/container/docker.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,6 @@ func (cf *DockerFactory) Create(image string, opts *ContainerOptions) (Container
5656

5757
r, err := cf.cli.ContainerInspect(cf.ctx, id)
5858
log.Printf("Container %s has name %s\n", id, r.Name)
59-
// TODO: remove, only for debug
60-
//containers, err := cf.cli.ContainerList(cf.ctx, types.ContainerListOptions{})
61-
//if err != nil {
62-
// panic(err)
63-
//}
64-
//
65-
//for _, c := range containers {
66-
// fmt.Println(c)
67-
//} // end remove
6859

6960
return id, err
7061
}

internal/function/function.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"encoding/json"
55
"fmt"
66

7-
"github.com/serverledge-faas/serverledge/internal/types"
8-
97
"time"
108

119
"github.com/serverledge-faas/serverledge/internal/cache"
@@ -134,8 +132,7 @@ func (f *Function) Delete() error {
134132
return nil
135133
}
136134

137-
func (f *Function) Equals(cmp types.Comparable) bool {
138-
f2 := cmp.(*Function)
135+
func (f *Function) Equals(f2 *Function) bool {
139136
return (f == nil && f2 == nil) || (f.Name == f2.Name &&
140137
f.CustomImage == f2.CustomImage &&
141138
f.CPUDemand == f2.CPUDemand &&

internal/function/signature.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,10 @@ func (s SignatureBuilder) Build() *Signature {
181181
return &s.signature
182182
}
183183

184-
// TODO: this function might modify its input to force matching of single inputs
185-
func (s *Signature) CheckAllInputs(inputMap map[string]interface{}) error {
184+
// CheckOrMatchInputs performs name and type checking between inputMap and the signature.
185+
// If the signature has a single input that type-checks with inputMap, the name of
186+
// the parameter in inputMap is updated to match with the signature.
187+
func (s *Signature) CheckOrMatchInputs(inputMap map[string]interface{}) error {
186188
errors := ""
187189
if len(inputMap) < len(s.Inputs) {
188190
errors += fmt.Sprintf("type-error: there are %d inputs, but should have been %d\n", len(inputMap), len(s.Inputs))

internal/test/api_test.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"strings"
78
"testing"
89
"time"
910

@@ -177,21 +178,24 @@ func TestDeleteWorkflow(t *testing.T) {
177178
AddOutput("result", function.Int{}).
178179
Build())
179180
utils.AssertNilMsg(t, err, "failed to initialize function")
180-
workflow, err := CreateSequenceWorkflow(fn, db, fn)
181-
workflow.Name = fcName
181+
wflow, err := CreateSequenceWorkflow(fn, db, fn)
182+
wflow.Name = fcName
182183
utils.AssertNil(t, err)
183184

184-
err = workflow.Save()
185+
err = wflow.Save()
185186
utils.AssertNil(t, err)
186187

187188
// the API under test is the following
188-
deleteWorkflowApiTest(t, fcName, HOST, PORT) // TODO: check success
189-
190-
// delete the container when not used
191-
// deleteApiTest(t, fn.Name, HOST, PORT) // the function has already been deleted during composition deletion
189+
deleteWorkflowApiTest(t, fcName, HOST, PORT)
192190

193-
// utils.AssertTrueMsg(t, node.ArePoolsEmptyInThisNode(), "container pools are not empty after the end of test")
194-
// utils.AssertTrueMsg(t, workflow.IsEmptyPartialDataCache(), "partial data cache is not empty")
191+
list, err := workflow.GetAllWorkflows()
192+
found := false
193+
for _, w := range list {
194+
if strings.Compare(w, wflow.Name) == 0 {
195+
found = true
196+
}
197+
}
198+
utils.AssertFalse(t, found)
195199
}
196200

197201
// TestAsyncInvokeWorkflow tests the REST API that executes a given function composition
@@ -240,7 +244,7 @@ func TestAsyncInvokeWorkflow(t *testing.T) {
240244
i++
241245
time.Sleep(200 * time.Millisecond)
242246
} else {
243-
result, err := compExecReport.GetSingleResult()
247+
result, err := GetSingleResult(&compExecReport)
244248
utils.AssertNilMsg(t, err, "failed to get single result")
245249
utils.AssertEquals(t, "4", result)
246250
break

internal/test/aslparser_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"os"
66
"testing"
77

8+
"github.com/lithammer/shortuuid"
89
"github.com/serverledge-faas/serverledge/internal/workflow"
910
"github.com/serverledge-faas/serverledge/utils"
10-
"github.com/lithammer/shortuuid"
1111
)
1212

1313
// / TestParsedWorkflowName verifies that the composition name matches the filename (without extension)
@@ -221,7 +221,7 @@ func TestParsingChoiceWorkflowWithBoolExpr(t *testing.T) {
221221
utils.AssertNil(t, err1)
222222

223223
// checks the result (1+1+1 = 3)
224-
output, err := resultMap.GetIntSingleResult()
224+
output, err := GetIntSingleResult(&resultMap)
225225
utils.AssertNilMsg(t, err, "failed to get int single result")
226226
utils.AssertEquals(t, 3, output)
227227

@@ -234,7 +234,7 @@ func TestParsingChoiceWorkflowWithBoolExpr(t *testing.T) {
234234
utils.AssertNil(t, err2)
235235

236236
// checks the result (20*2+1 = 41)
237-
output2, err := resultMap2.GetIntSingleResult()
237+
output2, err := GetIntSingleResult(&resultMap2)
238238
utils.AssertNilMsg(t, err, "failed to get int single result")
239239
utils.AssertEquals(t, 41, output2)
240240

internal/test/main_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import (
1010
"testing"
1111
"time"
1212

13+
"github.com/labstack/echo/v4"
1314
"github.com/serverledge-faas/serverledge/internal/api"
1415
"github.com/serverledge-faas/serverledge/internal/config"
1516
"github.com/serverledge-faas/serverledge/internal/metrics"
1617
"github.com/serverledge-faas/serverledge/internal/node"
1718
"github.com/serverledge-faas/serverledge/internal/registration"
1819
"github.com/serverledge-faas/serverledge/internal/scheduling"
1920
u "github.com/serverledge-faas/serverledge/utils"
20-
"github.com/labstack/echo/v4"
2121
"google.golang.org/grpc/codes"
2222
)
2323

@@ -53,7 +53,6 @@ func testStartServerledge(isInCloud bool, outboundIp string) (*registration.Regi
5353
registry.Area = AREA
5454
}
5555
// before register checkout other servers into the local area
56-
//todo use this info later on; future work with active remote server selection
5756
_, err := registry.GetAll(true)
5857
if err != nil {
5958
log.Fatal(err)

internal/test/signature_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ func TestSignatureInputOnly(t *testing.T) {
6363
u.AssertNil(t, sig.CheckAllOutputs(m))
6464
// check correct input
6565
m["hello"] = "giacomo"
66-
u.AssertNil(t, sig.CheckAllInputs(m))
66+
u.AssertNil(t, sig.CheckOrMatchInputs(m))
6767
// check wrong input
6868
m["hello"] = []string{"no", "yes"}
69-
u.AssertNonNil(t, sig.CheckAllInputs(m))
69+
u.AssertNonNil(t, sig.CheckOrMatchInputs(m))
7070
// check nil input value
7171
m["hello"] = nil
72-
u.AssertNonNil(t, sig.CheckAllInputs(m))
72+
u.AssertNonNil(t, sig.CheckOrMatchInputs(m))
7373
// u.AssertNil(t, sig.CheckAllOutputs(make(map[string]interface{})))
7474
}
7575

@@ -80,7 +80,7 @@ func TestSignatureOutputOnly(t *testing.T) {
8080
Build()
8181
m := make(map[string]interface{})
8282
// we do not have input
83-
u.AssertNil(t, sig.CheckAllInputs(m))
83+
u.AssertNil(t, sig.CheckOrMatchInputs(m))
8484
// check correct type output
8585
m["len"] = 1
8686
u.AssertNil(t, sig.CheckAllOutputs(m))
@@ -105,21 +105,21 @@ func TestComplexSignature(t *testing.T) {
105105
Build()
106106
m := make(map[string]interface{})
107107
// we do not have all inputs
108-
u.AssertNonNil(t, sig.CheckAllInputs(m))
108+
u.AssertNonNil(t, sig.CheckOrMatchInputs(m))
109109
// we do not have all outputs
110110
u.AssertNonNil(t, sig.CheckAllOutputs(m))
111111
// check partially correct input
112112
m["hello"] = "giacomo"
113-
u.AssertNonNil(t, sig.CheckAllInputs(m))
113+
u.AssertNonNil(t, sig.CheckOrMatchInputs(m))
114114
// check useless input
115115
m["food"] = "pizza"
116-
u.AssertNonNil(t, sig.CheckAllInputs(m))
116+
u.AssertNonNil(t, sig.CheckOrMatchInputs(m))
117117
// check correct input, but also with useless input (should work)
118118
m["age"] = "26"
119-
u.AssertNil(t, sig.CheckAllInputs(m))
119+
u.AssertNil(t, sig.CheckOrMatchInputs(m))
120120
// check totally correct input
121121
delete(m, "food")
122-
u.AssertNil(t, sig.CheckAllInputs(m))
122+
u.AssertNil(t, sig.CheckOrMatchInputs(m))
123123
// check partial output (with conversion)
124124
m["price"] = "2.5"
125125
u.AssertNonNil(t, sig.CheckAllOutputs(m))

internal/test/util.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"os"
99
"testing"
1010

11-
"github.com/cornelk/hashmap"
1211
"github.com/serverledge-faas/serverledge/internal/cli"
1312
"github.com/serverledge-faas/serverledge/internal/client"
1413
"github.com/serverledge-faas/serverledge/internal/function"
@@ -197,8 +196,7 @@ func createApiIfNotExistsTest(t *testing.T, fn *function.Function, host string,
197196

198197
func invokeApiTest(fn string, params map[string]interface{}, host string, port int) error {
199198
request := client.InvocationRequest{
200-
Params: params,
201-
// QoSClass: qosClass,
199+
Params: params,
202200
QoSMaxRespT: 250,
203201
CanDoOffloading: true,
204202
Async: false,
@@ -212,7 +210,6 @@ func invokeApiTest(fn string, params map[string]interface{}, host string, port i
212210
if err2 != nil {
213211
return err2
214212
}
215-
// utils.PrintJsonResponse(resp.Body)
216213
return nil
217214
}
218215

@@ -276,22 +273,11 @@ func pollWorkflowTest(t *testing.T, requestId string, host string, port int) str
276273
return utils.GetJsonResponse(resp.Body)
277274
}
278275

279-
func newWorkflowRequestTest() *workflow.Request {
280-
281-
return &workflow.Request{
282-
Id: "test",
283-
ExecReport: workflow.ExecutionReport{
284-
Reports: hashmap.New[workflow.ExecutionReportId, *function.ExecutionReport](), // make(map[workflow.ExecutionReportId]*function.ExecutionReport),
285-
},
286-
}
287-
}
288-
289276
func IsWindows() bool {
290277
return os.PathSeparator == '\\' && os.PathListSeparator == ';'
291278
}
292279

293280
// CreateSequenceWorkflow if successful, returns a workflow pointer with a sequence of Simple Nodes
294-
// TODO: do we need this utility function?
295281
func CreateSequenceWorkflow(funcs ...*function.Function) (*workflow.Workflow, error) {
296282
builder := workflow.NewBuilder()
297283
for _, f := range funcs {
@@ -301,7 +287,6 @@ func CreateSequenceWorkflow(funcs ...*function.Function) (*workflow.Workflow, er
301287
}
302288

303289
// CreateChoiceWorkflow if successful, returns a workflow with one Choice Node with each branch consisting of the same sub-workflow
304-
// TODO: why is a 'dagger' needed?
305290
func CreateChoiceWorkflow(dagger func() (*workflow.Workflow, error), condArr ...workflow.Condition) (*workflow.Workflow, error) {
306291
return workflow.NewBuilder().
307292
AddChoiceNode(condArr...).
@@ -311,7 +296,6 @@ func CreateChoiceWorkflow(dagger func() (*workflow.Workflow, error), condArr ...
311296

312297
// CreateScatterSingleFunctionWorkflow if successful, returns a workflow with one fan out, N simple node with the same function
313298
// and then a fan in node that merges all the result in an array.
314-
// TODO: This appears to be used only in tests. Move away from the workflow package
315299
func CreateScatterSingleFunctionWorkflow(fun *function.Function, fanOutDegree int) (*workflow.Workflow, error) {
316300
return workflow.NewBuilder().
317301
AddScatterFanOutNode(fanOutDegree).
@@ -322,7 +306,6 @@ func CreateScatterSingleFunctionWorkflow(fun *function.Function, fanOutDegree in
322306

323307
// CreateBroadcastWorkflow if successful, returns a workflow with one fan out node, N simple nodes with different functions and a fan in node
324308
// The number of branches is defined by the number of given functions
325-
// TODO: This appears to be used only in tests. Move away from the workflow package
326309
func CreateBroadcastWorkflow(dagger func() (*workflow.Workflow, error), fanOutDegree int) (*workflow.Workflow, error) {
327310
return workflow.NewBuilder().
328311
AddBroadcastFanOutNode(fanOutDegree).
@@ -333,8 +316,6 @@ func CreateBroadcastWorkflow(dagger func() (*workflow.Workflow, error), fanOutDe
333316

334317
// CreateBroadcastMultiFunctionWorkflow if successful, returns a workflow with one fan out node, each branch chained with a different workflow that run in parallel, and a fan in node.
335318
// The number of branch is defined as the number of dagger functions.
336-
// TODO: why is a 'dagger' needed?
337-
// TODO: This appears to be used only in tests. Move away from the workflow package
338319
func CreateBroadcastMultiFunctionWorkflow(dagger ...func() (*workflow.Workflow, error)) (*workflow.Workflow, error) {
339320
builder := workflow.NewBuilder().
340321
AddBroadcastFanOutNode(len(dagger))
@@ -345,3 +326,25 @@ func CreateBroadcastMultiFunctionWorkflow(dagger ...func() (*workflow.Workflow,
345326
AddFanInNode().
346327
Build()
347328
}
329+
330+
func GetSingleResult(cer *workflow.ExecutionReport) (string, error) {
331+
if len(cer.Result) == 1 {
332+
for _, value := range cer.Result {
333+
return fmt.Sprintf("%v", value), nil
334+
}
335+
}
336+
return "", fmt.Errorf("there is not exactly one result: there are %d result(s)", len(cer.Result))
337+
}
338+
339+
func GetIntSingleResult(cer *workflow.ExecutionReport) (int, error) {
340+
if len(cer.Result) == 1 {
341+
for _, value := range cer.Result {
342+
valueInt, ok := value.(int)
343+
if !ok {
344+
return 0, fmt.Errorf("value %v cannot be casted to int", value)
345+
}
346+
return valueInt, nil
347+
}
348+
}
349+
return 0, fmt.Errorf("there is not exactly one result: there are %d result(s)", len(cer.Result))
350+
}

internal/test/workflow_integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ func TestInvokeWorkflowFailAndSucceed(t *testing.T) {
513513
resultMap, errInvoke1 := wflow.Invoke(request)
514514
u.AssertNilMsg(t, errInvoke1, "error while invoking the branch (succeed)")
515515

516-
result, err := resultMap.GetIntSingleResult()
516+
result, err := GetIntSingleResult(&resultMap)
517517
u.AssertNilMsg(t, err, "Result not found")
518518
u.AssertEquals(t, 1, result)
519519

@@ -560,7 +560,7 @@ func TestInvokeWorkflowPassDoNothing(t *testing.T) {
560560
resultMap, errInvoke1 := wflow.Invoke(request)
561561
u.AssertNilMsg(t, errInvoke1, "error while invoking the composition with pass node")
562562

563-
result, err := resultMap.GetIntSingleResult()
563+
result, err := GetIntSingleResult(&resultMap)
564564
u.AssertNilMsg(t, err, "Result not found")
565565
u.AssertEquals(t, 3, result)
566566
}

0 commit comments

Comments
 (0)