refactor: propagate time through context values #5858
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
In Trivy,
time.Now()
has been traditionally handled through two methods:While Functional Options offer advantages in unit testing, they present challenges in integration testing, especially when multiple instances of
time.Now()
need to be mocked with a consistent, fixed time. This approach often requires repetitive coding in multiple places. Conversely, global variables, though convenient, complicate ensuring thread safety.To address these issues, this PR introduces a new approach: storing and passing
time.Time
within the context. As per the documentation guidelines, context values should be used cautiously and are not meant for every situation.However, leveraging context values is a viable solution for data that is integral throughout the entire process flow.
This change allows us to maintain a uniform, fixed time reference throughout a single process while ensuring thread safety. We are open to future improvements and will consider any better methods that may emerge.
This modification enhances our integration testing capabilities. By enabling the fixation of time in tests, we can prevent failures due to End-Of-Life (EOL) related time issues, as was previously encountered in our project, like this.
Checklist