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

[CERTTF-303] Introduce support for file attachments #250

Merged
merged 24 commits into from
Apr 18, 2024

Conversation

boukeas
Copy link
Contributor

@boukeas boukeas commented Apr 10, 2024

Description

This PR introduces functionality that allows a testflinger client (e.g. the testflinger CLI) to submit a job that includes attachments, with these attachments eventually finding their way into the testflinger agent.

Resolved issues

CERTTF-303

  • Removes the need to create configuration files as here documents within the test_cmds field; the configuration files can be attached instead.
  • Bash scripts can be attached and used in the test_cmds field
  • Attached files can also be useful in the provisioning or firmware_update phases
  • Will serve as a step for addressing RTW-282

Changes

Server

  • Extend the Job submission schema so that attachments fields are allowed under test_data, specifying which local files should be packed into an archive and submitted along with a job. For example:
test_data:
  attachments:
    - local: "/var/log/syslog"
      agent: "logs/syslog"
    - local: "data/config.json"
    - local: "data/scripts/script.sh"
      agent: "test.sh"
  • Such fields are also allowed under the provisioning and firmware update data but the current schema doesn't validate against them because too many arbitrary fields are used in these phases, depending on the use case. This validation issue is not relevant to this PR.

  • In the implementation of the endpoint for submitting a job, a new derived attachments status field is stored in the job data, taking the values:

    • none when there are no attachments to the job
    • waiting when there are attachments but the corresponding archive file hasn't been submitted yet and
    • complete when there are attachments and the corresponding archive file has been submitted

    Only the values none or complete allow a job to be removed from a queue.

  • Introduce two endpoints for submitting and retrieving an archive file that contains the attachments for a specific job. The submission of an attachments archive sets the value of the attachments status field for the job to complete.

  • Move database-related code into a separate database module, so that the API implementation is database-agnostic. This has been partly realised: the database interactions that are relevant to this PR have been moved into reusable functions of the database module. If approved, this change can also be applied to the implementation of all API endpoints, eventually removing all references to MongoDB from the API implementation and making the database an injectable dependency into the app.

CLI

The CLI checks if the job submission includes references to attachments in the provisioning, firmware and test phases and, if so, packs the attachments into an archive and follows the job submission with an additional attachments archive submission to the server (using the new endpoint described above)

Agent

The agent checks if the job submission is associated with attachments and, if so, retrieves the attachments archive from the server, unpacks it and optionally moves/renames the attachments. Note that all attachments are placed under an attachments folder in the agents "rundir". They are additionally separated into phase-related folders, e.g. attachments/test for the attachments provided under test_data.

Points for discussion

  • The intention is that the current functionality will be later extended to attaching files that are intended to end up in the device (rather than the host).
  • Efficiency concerns: how will the server behave when large files are submitted and retrieved? This is the main reason that the job submission still only includes the JSON, without the attachments archive (as a multipart-encoded request). If the job submission was bundled with a large attachments archive then it would take much longer to complete. Attachment archives are submitted and retrieved separately. In the future, this might also allow for asynchronous transfers.
  • There is an evident need for tooling modules that are common among the CLI, the server and the agent. There is code that would be (re)usable and is currently isolated and repeated.
  • How to test in a non-local staging environment

Documentation

Currently only inline documentation is provided. If the approach is approved the documentation will be extended under a separate issue/PR.

Tests

  • The test suite has been extended on the CLI, server and agent sides to provide new unit tests for the new functionality. Some additional tests should be added to test for negative scenarios, i.e. behaviour under unexpected circumstances.
  • Manual tests have been performed in a local testflinger deployment, using this job YAML, to verify that the specified files do indeed find their way to the agent, under the specified names.
job_queue: myqueue
provision_data:
  attachments:
    - local: "README.md"
firmware_update_data:
  attachments:
    - local: "CONTRIBUTING.md"
test_data:
  attachments:
    - local: "/var/log/syslog"
      agent: "logs/syslog"
    - local: "data/config.json"
    - local: "data/scripts/script.sh"
      agent: "test.sh"
  test_cmds: |
    ls -alR
    echo "*** These are the contents of the script file"
    cat attachments/test/test.sh
    echo "*** Running the script file"
    echo "(it's supposed to print the contents of the config file)"
    chmod u+x attachments/test/test.sh
    attachments/test/test.sh attachments/test/data/config.json

@boukeas boukeas requested a review from plars April 10, 2024 13:26
Copy link
Contributor

@nadzyah nadzyah 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 your contribution! I’ve left several suggestions to make it more clear

agent/testflinger_agent/agent.py Outdated Show resolved Hide resolved
agent/testflinger_agent/agent.py Outdated Show resolved Hide resolved
agent/testflinger_agent/client.py Outdated Show resolved Hide resolved
cli/testflinger_cli/__init__.py Outdated Show resolved Hide resolved
cli/testflinger_cli/tests/test_cli.py Show resolved Hide resolved
Copy link
Collaborator

@plars plars left a comment

Choose a reason for hiding this comment

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

I really love some of the improvements you made here, great work! I have a few comments below, let me know what you think.

agent/testflinger_agent/agent.py Outdated Show resolved Hide resolved
cli/testflinger_cli/__init__.py Outdated Show resolved Hide resolved
cli/testflinger_cli/__init__.py Outdated Show resolved Hide resolved
Copy link
Contributor

@thp-canonical thp-canonical left a comment

Choose a reason for hiding this comment

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

This is a welcome feature addition, thanks for working on this! See comments.

agent/testflinger_agent/agent.py Outdated Show resolved Hide resolved
agent/testflinger_agent/agent.py Outdated Show resolved Hide resolved
agent/testflinger_agent/agent.py Outdated Show resolved Hide resolved
agent/testflinger_agent/agent.py Outdated Show resolved Hide resolved
agent/testflinger_agent/agent.py Outdated Show resolved Hide resolved
cli/testflinger_cli/client.py Outdated Show resolved Hide resolved
cli/testflinger_cli/tests/test_cli.py Show resolved Hide resolved
server/src/api/schemas.py Outdated Show resolved Hide resolved
server/src/database.py Show resolved Hide resolved
server/src/database.py Outdated Show resolved Hide resolved
@boukeas boukeas requested a review from plars April 12, 2024 11:46
Copy link
Collaborator

@plars plars left a comment

Choose a reason for hiding this comment

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

This looks reasonable to me. I haven't had a chance to try it end-to-end yet, but I know you did and we will also want to do it in staging as a final sanity check. However I had one other thing that I wanted to ask below about how we ensure the attachments are removed once they are uploaded?
I did see that you have an uploadDate timestamp on it to use for TTL. Would be nice if we could delete-on-use, but I'm ok with the TTL, but didn't see where it was getting set in the index data. Did I just miss it?
Update: Nevermind! I realized about 10 seconds after sending this that we were already using uploadDate as the TTL index, so this should be just fine.
If you are happy with your local testing, then I am ok with this, pending further testing in staging.

server/src/database.py Show resolved Hide resolved
plars
plars previously approved these changes Apr 12, 2024
boukeas added 4 commits April 15, 2024 16:51
…tics

- the new name is `attachments_status`, to avoid any confusion with the
  `attachments` field under each phase.
- the `attachments_status` field only exists if the job has attachments
  (instead of having a value of "none" as before)
Copy link
Collaborator

@plars plars left a comment

Choose a reason for hiding this comment

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

One minor suggestion about a test for the secure_filter() update that you just made, otherwise looks good

agent/testflinger_agent/agent.py Show resolved Hide resolved
plars
plars previously approved these changes Apr 16, 2024
Copy link
Collaborator

@plars plars 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 adding that! +1

@plars plars requested a review from nadzyah April 17, 2024 01:04
cli/testflinger_cli/__init__.py Show resolved Hide resolved
server/src/api/v1.py Show resolved Hide resolved
Copy link
Contributor

@nadzyah nadzyah left a comment

Choose a reason for hiding this comment

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

Also want to approve it since I don’t have questions that can be potential blockers. Good PR, well done c:

@boukeas boukeas merged commit 487f1ee into main Apr 18, 2024
9 checks passed
@boukeas boukeas deleted the CERTTF-303-attachments branch April 18, 2024 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants