-
Notifications
You must be signed in to change notification settings - Fork 70
kill: adds support for numeric signals #107
Conversation
kill.go
Outdated
| for _, v := range signals { | ||
| if v == signum { | ||
| // signal is a valid signal | ||
| if err := vc.KillContainer(containerID, podStatus.ContainersStatus[0].ID, signum); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer only a single call to vc.KillContainer() if possible.
Also, note #109 if you want to pick that up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, I'll take this issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation seems good to me but I have two requests:
- Could you please create a separate function doing this processing about the signal:
func processSignal(signal string) (syscall.Signal, error)
and then call it from the current kill() function so that you actually only add
processedSignal, err := processSignal(signal)
if err != nil {
return err
}
before the call to vc.KillContainer()
- And could you add the corresponding unit test to the function you've added.
kill.go
Outdated
| } | ||
|
|
||
| if err := vc.KillContainer(containerID, podStatus.ContainersStatus[0].ID, signals[signal]); err != nil { | ||
| signum, signalOk := signals[signal] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have the same code repeated twice now with some somewhat deep nested indentation.
if err := vc.KillContainer(containerID, podStatus.ContainersStatus[0].ID, signum); err != nil {
return err
}
return nil
- resolve the argument to get a signal number
- do the KillContainer call
For 1. we don't actually have to cycle through the map, we can just test if the signal is between 1 and 31 (or SIGHUP and SIGUNUSED).
897762b to
468bebe
Compare
|
@jodh-intel @sboeuf @dlespiau Fixed |
1 similar comment
| "testing" | ||
| ) | ||
|
|
||
| func TestProcessSignal(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The usual thing here to limit the code to type and maintain is to use list the test cases in an array of structs (often using anonymous structs): https://dave.cheney.net/2013/06/09/writing-table-driven-tests-in-go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
something like
tests := []struct {
input string
valid bool
golden syscall.Signal
} {
/* list of test cases */
}
for _, test := range tests {
.....
}
An example at: https://github.com/dlespiau/x86db/blob/master/db_test.go#L10
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or another way is to actually create each test such as TestProcessSignalInvalidSignal(), TestProcessSignalInvalidShortSignal(), ... into a generic function like testProcessSignalFailure() or testProcessSignalSuccess().
That way each unit test has a name and it is very quick to get which test failed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sboeuf that adds quite a lot of typing and duplication actually. If the input needs some description, it can be added to the entries of the table, especially for very simple functions like that one that just take a string as input and return an int.
sboeuf
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few changes needed, but this is much better :)
| s, err := strconv.Atoi(signal) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("Failed to convert signal %s to int", signal) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a new line please.
kill.go
Outdated
| return 0, fmt.Errorf("Failed to convert signal %s to int", signal) | ||
| } | ||
| signum = syscall.Signal(s) | ||
| // check whether signal is valid or not |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/check/Check/
| // signal is a valid signal | ||
| return signum, nil | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a new line.
kill.go
Outdated
| } | ||
| signum = syscall.Signal(s) | ||
| // check whether signal is valid or not | ||
| for _, v := range signals { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use "sig" instead of "v" because this will be easier to understand.
| "testing" | ||
| ) | ||
|
|
||
| func TestProcessSignal(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or another way is to actually create each test such as TestProcessSignalInvalidSignal(), TestProcessSignalInvalidShortSignal(), ... into a generic function like testProcessSignalFailure() or testProcessSignalSuccess().
That way each unit test has a name and it is very quick to get which test failed.
|
Oh BTW @devimc could you please rebase recent changes from github.com/clearcontainers/runtime:origin/master so that Semaphore CI build can pass. |
kill_test.go
Outdated
| } | ||
|
|
||
| for _, test := range tests { | ||
| signum, _ := processSignal(test.signal) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're missing testing we receive a non nil error on invalid input.
sboeuf
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After @dlespiau comment is addressed, this lgtm
With this commit kill command is able to handle numeric signals as argument Fixes clearcontainers#109 Signed-off-by: Julio Montes <julio.montes@intel.com>
|
\o/ |
With this commit kill command is able to handle numeric
signals as argument
Signed-off-by: Julio Montes julio.montes@intel.com