Skip to content

Conversation

@siavashs
Copy link
Contributor

@siavashs siavashs commented Jan 2, 2018

This PR adds support for more logind methods, specially for session management.
Closes #238

@squeed
Copy link
Contributor

squeed commented Jan 4, 2018

Looks good. Any idea on how to add tests that aren't fragile?

@siavashs
Copy link
Contributor Author

siavashs commented Jan 6, 2018

I think we can run loginctl and compare the result for some of the methods.

login1/dbus.go Outdated
}

func sessionFromInterfaces(session []interface{}) (*Session, error) {
id := session[0].(string)
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

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
Copy link
Contributor

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{}

Copy link
Contributor

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{}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spurious newline.

@lucab
Copy link
Contributor

lucab commented Jan 8, 2018

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.

@siavashs
Copy link
Contributor Author

I'll look into tests.

uid := session[1].(uint32)
user := session[2].(string)
seat := session[3].(string)
path, ok := session[4].(dbus.ObjectPath)
Copy link
Contributor

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)
Copy link
Contributor

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.

@lucab
Copy link
Contributor

lucab commented Jan 25, 2018

Bump on this. It looks like some comments in my previous review were not precise enough, apologies for that.

@siavashs
Copy link
Contributor Author

No problem, I'll fix it.
I'll try to work on the tests this weekend.

@lucab
Copy link
Contributor

lucab commented Feb 1, 2018

Just dropping a quick note, I'm planning to tag v16 in the next few days (once #248 and #249 are in). Unless you need this quickly in a release, we can pick this post-release once done, without hurrying.

@lucab lucab changed the title Add logind session management methods login1: add support for session management Feb 2, 2018
@siavashs
Copy link
Contributor Author

siavashs commented Feb 3, 2018

As I checked there are no active user sessions inside a docker container so logind responds with empty lists of users and sessions.
One solution is to create a test user inside the container and do a login:

useradd test
login -f test

Let me know if you have a better solution to handle this test case.

@siavashs
Copy link
Contributor Author

siavashs commented Feb 3, 2018

So I implemented the tests by creating and activating a test user and running the tests with the same user.
The results from login1 are compared to values from os/user.Current().

@siavashs
Copy link
Contributor Author

siavashs commented Feb 3, 2018

It seems running sdjournal tests as the new test user fails, while it works fine for me locally as an unprivileged user.
Any idea what might cause that error on the CI?

@siavashs
Copy link
Contributor Author

siavashs commented Feb 4, 2018

These latest changes on my branch work on Ubuntu but fail on Debian!
But it fails for both in the PR tests.

}

func sessionFromInterfaces(session []interface{}) (*Session, error) {
id, ok := session[0].(string)
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor

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)
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, yes.

@lucab
Copy link
Contributor

lucab commented Feb 9, 2018

@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 ListUsers and ListSessions don't panic or return errors, and if there are actual users/sessions let's loop through them and ensure fields are populated.

@siavashs
Copy link
Contributor Author

siavashs commented Feb 9, 2018

Maybe an unrelated question, but why do I see a higher coverage % when running tests locally?

...
ok  	github.com/coreos/go-systemd/login1	0.009s	coverage: 53.7% of statements
...

Is this related to go version?

@lucab
Copy link
Contributor

lucab commented Feb 14, 2018

@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 *FromInterface are only called inside a range iterator thus are not triggered on empty arrays.

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?

@siavashs
Copy link
Contributor Author

siavashs commented Feb 14, 2018

@lucab it is ready 😄
Update: Fixed the commit message

@lucab
Copy link
Contributor

lucab commented Feb 14, 2018

LGTM 💚 . Thanks for your contribution (and patience)!

@lucab lucab merged commit 25fe332 into coreos:master Feb 14, 2018
@siavashs siavashs deleted the logind branch February 14, 2018 14:01
@lucab lucab added this to the v17 milestone May 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants