-
Notifications
You must be signed in to change notification settings - Fork 321
login1: add support for session management #242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Looks good. Any idea on how to add tests that aren't fragile? |
|
I think we can run |
login1/dbus.go
Outdated
| } | ||
|
|
||
| func sessionFromInterfaces(session []interface{}) (*Session, error) { | ||
| id := session[0].(string) |
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.
Please check that session has at least 5 entries before accessing them.
login1/dbus.go
Outdated
| uid := session[1].(uint32) | ||
| user := session[2].(string) | ||
| seat := session[3].(string) | ||
| path := session[4].(dbus.ObjectPath) |
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.
Please perform checked type assertions here instead. That is, something like:
path, ok := session[4].(dbus.ObjectPath)
if !ok {
return nil, fmt.Errorf("failed to typecast session field 4 to ObjectPath")
}
login1/dbus.go
Outdated
| func userFromInterfaces(user []interface{}) (*User, error) { | ||
| uid := user[0].(uint32) | ||
| name := user[1].(string) | ||
| path := user[2].(dbus.ObjectPath) |
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.
Same as above. Please add a length check and checked type assertions.
login1/dbus.go
Outdated
| return "", err | ||
| } | ||
|
|
||
| return out.(dbus.ObjectPath), 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.
Please add a checked type assertion for out.
login1/dbus.go
Outdated
| } | ||
|
|
||
| ret := []User{} | ||
|
|
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.
Spurious newline.
login1/dbus.go
Outdated
| } | ||
|
|
||
| ret := []Session{} | ||
|
|
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.
Spurious newline.
|
It would be nice to have at least some basic smoke tests calling those new methods and ensuring they return something meaningful without panicking. I think the docker setup on travis already has a working logind to query. |
|
I'll look into tests. |
| uid := session[1].(uint32) | ||
| user := session[2].(string) | ||
| seat := session[3].(string) | ||
| path, ok := session[4].(dbus.ObjectPath) |
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.
Sorry, I wasn't clear enough in my previous review. Please make sure that all type assertions (i.e. also fields 0 to 3) are checked, like you did for the last field.
| func userFromInterfaces(user []interface{}) (*User, error) { | ||
| uid := user[0].(uint32) | ||
| name := user[1].(string) | ||
| path, ok := user[2].(dbus.ObjectPath) |
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.
Same as above, and apologies that my previous review was not precise.
|
Bump on this. It looks like some comments in my previous review were not precise enough, apologies for that. |
|
No problem, I'll fix it. |
|
As I checked there are no active user sessions inside a docker container so logind responds with empty lists of users and sessions. Let me know if you have a better solution to handle this test case. |
|
So I implemented the tests by creating and activating a test user and running the tests with the same user. |
|
It seems running sdjournal tests as the new test user fails, while it works fine for me locally as an unprivileged user. |
|
These latest changes on my branch work on Ubuntu but fail on Debian! |
| } | ||
|
|
||
| func sessionFromInterfaces(session []interface{}) (*Session, error) { | ||
| id, ok := session[0].(string) |
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.
Please check for len(session) >= 5 before starting to index-access the array.
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.
Should we return an error if the number of fields don't match?
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.
Yes. Rationale is that we prefer to return an error earlier to the caller if the input array is too short, instead of panicing in library code.
| } | ||
|
|
||
| func userFromInterfaces(user []interface{}) (*User, error) { | ||
| uid, ok := user[0].(uint32) |
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.
Please check for len(user) >= 3 before starting to index-access the array.
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.
Should we return an error if the number of fields don't match?
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.
Same as above, yes.
|
@siavashs While I understand it would be nice to exact-match the environment in the test, I think that that is too fragile and also requires manipulating the environment first. I would suggest instead to be a bit less strict: just ensure that calls to |
|
Maybe an unrelated question, but why do I see a higher coverage % when running tests locally? Is this related to go version? |
|
@siavashs my guess is that given that there are no sessions/users in the CI run, some codepaths within conditional paths are not reached. For example, both This PR seems complete to me now, is there anything else you planned to add here? If not, can you please squash commits together and adopt the same title as the PR one? |
|
@lucab it is ready 😄 |
|
LGTM 💚 . Thanks for your contribution (and patience)! |
This PR adds support for more logind methods, specially for session management.
Closes #238