Skip to content

Commit 7ee2c6c

Browse files
vsochzekemorton
authored andcommitted
reapi: enum type prototype for match_ops and test
Problem: we need to have a faux enum type that is shared within the Go bindings, and a more reasonable test setup for arbitrary go_<test>.go files. Solution: add a pkg subdirectory to our go bindings src, the idea being that we can eventually move more shared logic between the cli and modules there. In addition, add an ability to target a single directory with a go_<test>.go file that (although not build) can be tested with go test, which is the standard way to do it with go. We do not (I think) need to worry about having a binary built for associated go modules (akin to types.go) because they will ultimately be included in the reapi_cli or reapi_module that will be tested. This is just a prototype, meaning that I have not added the actual enum to the C code, but just provided the structure of the module that can be imported into each of the cli and module and used when that is done. Signed-off-by: vsoch <[email protected]>
1 parent 7997c12 commit 7ee2c6c

File tree

6 files changed

+155
-12
lines changed

6 files changed

+155
-12
lines changed

cmake/GolangSimple.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ function(BUILD_GO_PROGRAM NAME MAIN_SRC CGO_CFLAGS CGO_LIBRARY_FLAGS)
2424

2525
add_custom_target(${NAME}_all ALL DEPENDS ${NAME})
2626
install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${NAME} DESTINATION bin)
27-
endfunction(BUILD_GO_PROGRAM)
27+
endfunction(BUILD_GO_PROGRAM)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
add_subdirectory( test )
1+
add_subdirectory( test )

resource/reapi/bindings/go/src/fluxcli/reapi_cli.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,28 @@ func (cli *ReapiClient) InitContext(jgf string, options string) (err error) {
8282

8383
// Match matches a jobspec to the "best" resources based on match option.
8484

85-
// The best resources are determined by the selected match policy.
85+
// The best resources are determined by the selected match policy.
86+
//
87+
// \param match_op String to indicate the match type. Options include:
88+
// allocate, allocate_orelse_reserve, satisfiability,
89+
// allocate_with_satsfiability
8690
//
87-
// \param match_op String to indicate the match type. Options include:
88-
// allocate, allocate_orelse_reserve, satisfiability,
89-
// allocate_with_satsfiability
9091
// \param jobspec jobspec string.
9192
// \param jobid jobid of the uint64_t type.
9293
// \param reserved Boolean into which to return true if this job has been
9394
// reserved instead of allocated.
95+
//
9496
// \param R String into which to return the resource set either
95-
// allocated or reserved.
97+
// allocated or reserved.
98+
//
9699
// \param at If allocated, 0 is returned; if reserved, actual time
97-
// at which the job is reserved.
98-
// \param ov Double into which to return performance overhead
99-
// in terms of elapse time needed to complete
100-
// the match operation.
101-
// \return 0 on success; -1 on error.
100+
// at which the job is reserved.
101+
//
102+
// \param ov Double into which to return performance overhead
103+
// in terms of elapse time needed to complete
104+
// the match operation.
105+
//
106+
// \return 0 on success; -1 on error.
102107
func (cli *ReapiClient) Match(
103108
match_op string,
104109
jobspec string,
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package types
2+
3+
/*****************************************************************************\
4+
* Copyright 2024 Lawrence Livermore National Security, LLC
5+
* (c.f. AUTHORS, NOTICE.LLNS, LICENSE)
6+
*
7+
* This file is part of the Flux resource manager framework.
8+
* For details, see https://github.com/flux-framework.
9+
*
10+
* SPDX-License-Identifier: LGPL-3.0
11+
\*****************************************************************************/
12+
13+
/*
14+
#include "reapi_cli.h"
15+
16+
typedef enum {
17+
MATCH_UNKNOWN = 0
18+
MATCH_ALLOCATE = 1
19+
MATCH_ALLOCATE_ORELSE_RESERVE = 2
20+
MATCH_ALLOCATE_W_SATISFIABILITY = 3
21+
MATCH_SATISFIABILITY = 4
22+
} match_op_enum;
23+
24+
/* usage:
25+
mt := MatchType
26+
C.match_op_enum(mt)
27+
*/
28+
29+
import "C"
30+
31+
type MatchType int
32+
33+
// MatchUnknown serves as a sentinal value that it's undefined
34+
const (
35+
MatchUnknown MatchType = iota // unknown
36+
MatchAllocate // allocate
37+
MatchAllocateOrElseReserve // allocate or else reserve
38+
MatchAllocateWithSatisfiability // allocate with satisfiability
39+
MatchSatisfiability // satisfiability
40+
)
41+
42+
// String ensures that the MatchType can be used in string formatting
43+
func (m MatchType) String() string {
44+
switch m {
45+
case MatchAllocate:
46+
return "allocate"
47+
case MatchAllocateOrElseReserve:
48+
return "allocate or else reserve"
49+
case MatchAllocateWithSatisfiability:
50+
return "allocate with satisfiability"
51+
case MatchSatisfiability:
52+
return "satisfiability"
53+
}
54+
return "unknown"
55+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package types
2+
3+
/*****************************************************************************\
4+
* Copyright 2024 Lawrence Livermore National Security, LLC
5+
* (c.f. AUTHORS, NOTICE.LLNS, LICENSE)
6+
*
7+
* This file is part of the Flux resource manager framework.
8+
* For details, see https://github.com/flux-framework.
9+
*
10+
* SPDX-License-Identifier: LGPL-3.0
11+
\*****************************************************************************/
12+
13+
import (
14+
"testing"
15+
)
16+
17+
// List of match types, purposefully out of order
18+
var matchTypes = []MatchType{
19+
MatchAllocateWithSatisfiability,
20+
MatchAllocateOrElseReserve,
21+
MatchAllocate,
22+
MatchSatisfiability,
23+
MatchUnknown,
24+
}
25+
26+
func TestToString(t *testing.T) {
27+
type test struct {
28+
description string
29+
input MatchType
30+
expected string
31+
}
32+
33+
tests := []test{
34+
{description: "unknown", input: MatchUnknown, expected: "unknown"},
35+
{description: "allocate", input: MatchAllocate, expected: "allocate"},
36+
{description: "satisfiability", input: MatchSatisfiability, expected: "satisfiability"},
37+
{description: "allocate or else reserve", input: MatchAllocateOrElseReserve, expected: "allocate or else reserve"},
38+
{description: "allocate with satisfiability", input: MatchAllocateWithSatisfiability, expected: "allocate with satisfiability"},
39+
}
40+
for _, item := range tests {
41+
t.Run(item.description, func(t *testing.T) {
42+
value := item.input.String()
43+
t.Logf("got %s, want %s", value, item.expected)
44+
if item.expected != value {
45+
t.Errorf("got %s, want %s", value, item.expected)
46+
}
47+
})
48+
}
49+
}
50+
51+
func TestAsInt(t *testing.T) {
52+
type test struct {
53+
description string
54+
input MatchType
55+
expected int
56+
}
57+
58+
tests := []test{
59+
{description: "unknown", input: MatchUnknown, expected: 0},
60+
{description: "allocate", input: MatchAllocate, expected: 1},
61+
{description: "satisfiability", input: MatchSatisfiability, expected: 4},
62+
{description: "allocate or else reserve", input: MatchAllocateOrElseReserve, expected: 2},
63+
{description: "allocate with satisfiability", input: MatchAllocateWithSatisfiability, expected: 3},
64+
}
65+
for _, item := range tests {
66+
t.Run(item.description, func(t *testing.T) {
67+
value := int(item.input)
68+
t.Logf("got %d, want %d", value, item.expected)
69+
if item.expected != value {
70+
t.Errorf("got %d, want %d", value, item.expected)
71+
}
72+
})
73+
}
74+
}

t/t9001-golang-basic.t

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jgf="${SHARNESS_TEST_SRCDIR}/data/resource/jgfs/tiny.json"
2121
jobspec1="${SHARNESS_TEST_SRCDIR}/data/resource/jobspecs/basics/test001.yaml"
2222
jobspec2="${SHARNESS_TEST_SRCDIR}/data/resource/jobspecs/basics/test003.yaml"
2323
main="../../resource/reapi/bindings/go/src/test/main"
24+
root="../../resource/reapi/bindings/go/src"
2425

2526
test001_desc="match allocate 1 slot: 1 socket: 1 core (pol=default)"
2627
test_expect_success "${test001_desc}" '
@@ -36,4 +37,12 @@ test_expect_success "${test002_desc}" '
3637
test_cmp 002.R.out ${exp_dir}/002.R.out
3738
'
3839

40+
# Assume running in subshell, don't need to chdir back
41+
test003_desc="test pkg/types/types_test.go"
42+
test_expect_success "${test003_desc}" '
43+
cd ${root}/pkg/types
44+
go test -v
45+
'
46+
47+
3948
test_done

0 commit comments

Comments
 (0)