Skip to content
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

lxd: add all-projects param to the operation API #12078

Closed
wants to merge 7 commits into from

Conversation

AuruTus
Copy link
Contributor

@AuruTus AuruTus commented Jul 26, 2023

Add all-projects query param support for operationsGet API and update its swagger doc (both 1.0/operations and 1.0/operations?recursion=1).

Fix #11808

@github-actions github-actions bot added Documentation Documentation needs updating API Changes to the REST API labels Jul 26, 2023
Copy link
Contributor

@roosterfish roosterfish left a comment

Choose a reason for hiding this comment

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

Hi @AuruTus, can you please add an API extension since this is adding extra functionality? For reference check #12045 and especially this commit 48ace10.

@AuruTus
Copy link
Contributor Author

AuruTus commented Jul 27, 2023

Hi @AuruTus, can you please add an API extension since this is adding extra functionality? For reference check #12045 and especially this commit 48ace10.

hi @roosterfish, thx for the review. I got it a bit more now. And I'm supposed to add r.CheckExtension for client's api call in codes, right? CMIIW 😃

@roosterfish
Copy link
Contributor

Yes please.

lxd/operations.go Outdated Show resolved Hide resolved
@monstermunchkin
Copy link
Contributor

@AuruTus please sign-off all your commits (git commit -s), and rebase your PR as it seems to have conflicts.

@AuruTus
Copy link
Contributor Author

AuruTus commented Jul 27, 2023

@monstermunchkin hi master, all is done and signoffs are ok now.

lxd/operations.go Outdated Show resolved Hide resolved
lxd/operations.go Outdated Show resolved Hide resolved
client/lxd_operations.go Outdated Show resolved Hide resolved
client/lxd_operations.go Show resolved Hide resolved
lxd/operations.go Outdated Show resolved Hide resolved
lxd/operations.go Outdated Show resolved Hide resolved
Copy link
Member

@tomponline tomponline left a comment

Choose a reason for hiding this comment

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

Thanks for this.

We should add a test for this though.

@monstermunchkin do you know of any precedent for testing operation listings that may help here?

@monstermunchkin
Copy link
Contributor

do you know of any precedent for testing operation listings that may help here?

We don't currently test operations, and we don't have a way of creating them via the API. What do you think about adding a new internal API endpoint called internal/testing/operations which can be used for creating operations? That way we could create fake operations and test against them.

lxd/operations.go Outdated Show resolved Hide resolved
@tomponline
Copy link
Member

do you know of any precedent for testing operation listings that may help here?

We don't currently test operations, and we don't have a way of creating them via the API. What do you think about adding a new internal API endpoint called internal/testing/operations which can be used for creating operations? That way we could create fake operations and test against them.

I'm not sure that is needed. It looks like the test_exec test function has a good example of how to inspect operations from lxc exec calls, so as operations hang around for 5s after they have completed, I suggest we use lxc exec as an easy way to create operations in different projects.

Signed-off-by: Aurutus <emslhy@hotmail.com>
Signed-off-by: Aurutus <emslhy@hotmail.com>
@tomponline
Copy link
Member

Please can you rebase so your commits so they tell the final logical set of changes rather than the evolution over the PR review. Thanks

@AuruTus
Copy link
Contributor Author

AuruTus commented Jul 29, 2023

Please can you rebase so your commits so they tell the final logical set of changes rather than the evolution over the PR review. Thanks

@tomponline hi master, sorry I don't understand how to operate such things and those above are already generated by rebasing and force-pushing. Could give me some guides?

@tomponline
Copy link
Member

Hey, so what I meant was that this latest commit f7a2c8d alters a commit earlier on your PR. Because we don't squash merge but instead pull all of your individual commits into the main branch we like that each commit within the pr represent the final version of it. Does that make sense? Thanks

@AuruTus
Copy link
Contributor Author

AuruTus commented Jul 29, 2023

Hey, so what I meant was that this latest commit f7a2c8d alters a commit earlier on your PR. Because we don't squash merge but instead pull all of your individual commits into the main branch we like that each commit within the pr represent the final version of it. Does that make sense? Thanks

Oh I got it! 😄 . Will fix this later with the test sh script together. BTW, have a nice weekend.

Signed-off-by: Aurutus <emslhy@hotmail.com>
Signed-off-by: Aurutus <emslhy@hotmail.com>
Signed-off-by: Aurutus <emslhy@hotmail.com>
@AuruTus
Copy link
Contributor Author

AuruTus commented Jul 30, 2023

@tomponline hi master, sorry I encounter some problems in local test env now, so I have to push the rebased commits first.

The instance starting command has error as:

Creating x1
Starting x1                                
ERROR  [2023-07-30T09:05:27Z] Failed starting instance                      action=start created="2023-07-30 09:05:25.0278038 +0000 UTC" ephemeral=false instance=x1 instanceType=container project=default stateful=false used="1970-01-01 00:00:00 +0000 UTC"
Error: Failed to run: /root/go/bin/lxd forkstart x1 /root/workspace/lxd/test/tmp.617/P15/containers /root/workspace/lxd/test/tmp.617/P15/logs/x1/lxc.conf: exit status 1
Try `lxc info --show-log local:x1` for more info

And this is the log print

Name: x1
Status: STOPPED
Type: container
Architecture: x86_64
Created: 2023/07/30 09:05 UTC
Last Used: 2023/07/30 09:05 UTC

Log:

lxc x1 20230730090527.569 WARN     cgfsng - cgroups/cgfsng.c:fchowmodat:1252 - No such file or directory - Failed to fchownat(42, memory.oom.group, 1000000000, 0, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW )
lxc x1 20230730090527.571 ERROR    start - start.c:print_top_failing_dir:99 - Permission denied - Could not access /root. Please grant it x access, or add an ACL for the container root
lxc x1 20230730090527.571 ERROR    sync - sync.c:sync_wait:34 - An error occurred in another process (expected sequence number 1)
lxc x1 20230730090527.571 ERROR    lxccontainer - lxccontainer.c:wait_on_daemonized_start:877 - Received container state "ABORTING" instead of "RUNNING"
lxc x1 20230730090527.571 ERROR    start - start.c:__lxc_start:2074 - Failed to spawn container "x1"
lxc x1 20230730090527.571 WARN     start - start.c:lxc_abort:1039 - No such process - Failed to send SIGKILL via pidfd 43 for process 29334
lxc 20230730090527.741 ERROR    af_unix - af_unix.c:lxc_abstract_unix_recv_fds_iov:218 - Connection reset by peer - Failed to receive response
lxc 20230730090527.741 ERROR    af_unix - af_unix.c:lxc_abstract_unix_recv_fds_iov:218 - Connection reset by peer - Failed to receive response
lxc 20230730090527.741 ERROR    commands - commands.c:lxc_cmd_rsp_recv_fds:127 - Failed to receive file descriptors for command "get_state"
lxc 20230730090527.741 ERROR    commands - commands.c:lxc_cmd_rsp_recv_fds:127 - Failed to receive file descriptors for command "get_init_pid"

I've been stuck here for a while, and didn't find a way to solve it. 😢

@tomponline
Copy link
Member

Does it run on the main branch, is is just your branch it does not run on?

@AuruTus
Copy link
Contributor Author

AuruTus commented Aug 1, 2023

Does it run on the main branch, is is just your branch it does not run on?

Yeah, tried both main and my own branch. Cannot setup instances with lxc launch testimage x1 in most test scripts. I try to change the /etc/sub{g,u}id for root like what I searched on google, but it doesn't work. (And I'm using WSL2. Alr followed the How to install LXD guide to install deps and can build it)

@monstermunchkin
Copy link
Contributor

@AuruTus looks like a permission issue. Try granting execute permission for /root: chmod o+x /root

@AuruTus
Copy link
Contributor Author

AuruTus commented Aug 1, 2023

@AuruTus looks like a permission issue. Try granting execute permission for /root: chmod o+x /root

Cool! It works now. Thx master. I can continue my test. BTW, is there any guide on integration test result assertion? I noticed that there exists an expression like some command || false, but it won't output failed or error message or even stop the rest test cases.

@MusicDin
Copy link
Member

MusicDin commented Aug 2, 2023

@AuruTus You can use set -e in your tests. It is an option that tells the shell to exit immediately if any command exits with a non-zero status. Test in backup.sh is an example that uses set -e option.

@AuruTus
Copy link
Contributor Author

AuruTus commented Aug 2, 2023

Hi masters, I think these test cases is enough for standalone mode query. (I cannot create a new network successfully on my Ubuntu 22.04 distro when creating a new project, so I directly test with the default project).

And there're still some issues for my cluster testing. I cannot mount /sys/kernel/security. I tried other clustering test cases, but they also cannot build up the cluster successfully. They have the same error when setting up netns.

And this the log printed when I wrote scripts

==> TEST BEGIN: clustering operations get
==> Setup clustering bridge br3987
2023/08/02 09:15:29 auth - running at http://127.0.0.1:56263
==> Setup clustering netns lxd39871
mount: /sys/kernel/security: mount point does not exist.
cat: /root/workspace/lxd/test/tmp.939/ns/lxd39871/PID: No such file or directory
Error: argument "" is wrong: Invalid "netns" value

nsenter: failed to parse pid: ''
==> Spawn bootstrap cluster node in lxd39871 with storage driver dir
==> Setting up directory backend in /root/workspace/lxd/test/tmp.939/qom
==> Spawning lxd in /root/workspace/lxd/test/tmp.939/qom
cat: /root/workspace/lxd/test/tmp.939/ns/lxd39871/PID: No such file or directory
==> print test log
Error: Get "http://unix.socket/1.0": dial unix /root/workspace/lxd/test/tmp.939/qom/unix.socket: connect: no such file or directory
make: *** [Makefile:281: tmp] Error 1

@monstermunchkin
Copy link
Contributor

And there're still some issues for my cluster testing. I cannot mount /sys/kernel/security. I tried other clustering test cases, but they also cannot build up the cluster successfully. They have the same error when setting up netns.

You're on WSL2, right? Perhaps this will fix your problem: microsoft/WSL#8840

If not, you could try setting up an Ubuntu VM and do your tests in there.

@AuruTus
Copy link
Contributor Author

AuruTus commented Aug 3, 2023

And there're still some issues for my cluster testing. I cannot mount /sys/kernel/security. I tried other clustering test cases, but they also cannot build up the cluster successfully. They have the same error when setting up netns.

You're on WSL2, right? Perhaps this will fix your problem: microsoft/WSL#8840

If not, you could try setting up an Ubuntu VM and do your tests in there.

got it! will try this after fixing static analysis.

Signed-off-by: Aurutus <emslhy@hotmail.com>
Signed-off-by: Aurutus <emslhy@hotmail.com>
set -e
echo "==> get all-projects operations normally"
result="$(lxc query "/1.0/operations?all-projects=true" | jq '.success[0]' | wc -l)"
test "${result}" -eq 1 || false
Copy link
Contributor

Choose a reason for hiding this comment

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

You can drop the || false.

Since this tests operations of all projects, limiting it to just one doesn't make sense. Instead you should have assert_get_all_operations take an argument which represents the number of expected operations.

Comment on lines +10 to +11
result="$(lxc query "/1.0/operations?recursion=1&all-projects=true" | jq '.success[0]' | grep -c 'status_code')"
test "${result}" -eq 1 || false
Copy link
Contributor

Choose a reason for hiding this comment

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

You can drop the || false.

Same as above, you should pass the expected number of operations as an argument.

(
set -e
echo "==> get all-projects operations normally"
result="$(lxc query "/1.0/operations?all-projects=true" | jq '.success[0]' | wc -l)"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
result="$(lxc query "/1.0/operations?all-projects=true" | jq '.success[0]' | wc -l)"
result="$(lxc query "/1.0/operations?all-projects=true" | jq '.success | length')"

test "${result}" -eq 1 || false

echo "==> get all-projects operations recursively"
result="$(lxc query "/1.0/operations?recursion=1&all-projects=true" | jq '.success[0]' | grep -c 'status_code')"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
result="$(lxc query "/1.0/operations?recursion=1&all-projects=true" | jq '.success[0]' | grep -c 'status_code')"
result="$(lxc query "/1.0/operations?recursion=1&all-projects=true" | jq '[.success[] | select(.status_code)] | length')"

This will count the number of occurrences of status_code.

Comment on lines +15 to +28
ensure_import_testimage

# create container
name=x1
lxc launch testimage ${name}
lxc list ${name} | grep RUNNING

# get_all_operations
lxc query -X POST -d '{\"command\":[\"touch\",\"/root/foo1\"],\"record-output\":false}' /1.0/instances/${name}/exec
sleep 1
assert_get_all_operations

lxc stop "${name}" --force
lxc delete "${name}"
Copy link
Contributor

Choose a reason for hiding this comment

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

In this function you want to test if all operations of all projects are listed. However, you only create an operation in a single project.

Instead, you need to create a second project, and launch a container there as well. Then, you can run the exec commands for both containers, and count the number of operations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it. Will refactor this snippet with above ones later. I try to test these on VM. Too many constraints on my WSL.

lxc list ${name} | grep RUNNING

# get_all_operations
lxc query -X POST -d '{\"command\":[\"touch\",\"/root/foo1\"],\"record-output\":false}' /1.0/instances/${name}/exec
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
lxc query -X POST -d '{\"command\":[\"touch\",\"/root/foo1\"],\"record-output\":false}' /1.0/instances/${name}/exec
lxc exec -T --project=<whatever> "${name}" true

Comment on lines +31 to +62
test_operations_get_by_project() {
assert_get_project_operations() {
# $1: query param for project name
(
set -e
echo "==> get $1 operations normally"
result="$(lxc query "/1.0/operations?project=$1" | jq '.success[0]' | wc -l)"
test "${result}" -eq 1 || false

echo "==> get $1 operations recursively"
result="$(lxc query "/1.0/operations?recursion=1&project=$1" | jq '.success[0]' | grep -c 'status_code')"
test "${result}" -eq 1 || false
)
}

ensure_import_testimage

project="default"

# create container
name=x1
lxc launch testimage ${name} --project ${project}
lxc list ${name} --project ${project} | grep RUNNING

# get opetaions with project name
lxc query -X POST -d '{\"command\":[\"touch\",\"/root/foo1\"],\"record-output\":false}' /1.0/instances/${name}/exec?project=${project}
sleep 1
assert_get_project_operations ${project}

lxc stop "${name}" --force --project ${project}
lxc delete "${name}" --project ${project}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The same comments as above apply here.

return nil, err
}

// Turn it into just a list of operations
Copy link
Member

@MusicDin MusicDin Aug 1, 2023

Choose a reason for hiding this comment

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

Just a small nit, but can you end comments with a full stop? Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will add this later too. 😄

@tomponline
Copy link
Member

@AuruTus hi, is this something you're still interested in progressing? Thanks

@AuruTus
Copy link
Contributor Author

AuruTus commented Aug 17, 2023

@AuruTus hi, is this something you're still interested in progressing? Thanks

sry, a bit busy recently. may have no enough free time to test it. Maybe can close this PR, for the code is really simple.

@MusicDin
Copy link
Member

@AuruTus Would you mind if I finish this?

@AuruTus
Copy link
Contributor Author

AuruTus commented Aug 18, 2023

@AuruTus Would you mind if I finish this?

Oh that's great then. thx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Changes to the REST API Documentation Documentation needs updating
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add all-projects param to the operation API
5 participants