Skip to content

Conversation

mrajagopal
Copy link
Collaborator

Proposed changes

This PR address Issue 40 - Create Unit-tests.

Checklist

Before creating a PR, run through this checklist and mark each as complete.

  • I have read the CONTRIBUTING guide
  • I have proven my fix is effective or that my feature works
  • I have checked that all unit tests pass after adding my changes
  • I have ensured the README is up to date
  • I have rebased my branch onto main
  • I will ensure my PR is targeting the main branch and pulling from my branch on my own fork

* This is a exploratory exercise on writing unit-test for data_collector.go and nic_job_list.go
* The nic_job_list_test.go works but data_collector_test.go does not
* The tests themselves are basic to establish a familiarity in writing tests
@mrajagopal mrajagopal self-assigned this Sep 4, 2025
Copilot

This comment was marked as outdated.

mrajagopal and others added 2 commits September 4, 2025 16:36
Remove redundant TODO.

Co-authored-by: Copilot <[email protected]>
@mrajagopal mrajagopal requested a review from Copilot September 4, 2025 04:39
Copilot

This comment was marked as outdated.

mrajagopal and others added 3 commits September 4, 2025 16:42
Remove redundant comment

Co-authored-by: Copilot <[email protected]>
Removed custom fake types; use fake.NewSimpleClientset instead

Co-authored-by: Copilot <[email protected]>
Remove redundant mock-logger code.

Co-authored-by: Copilot <[email protected]>
@mrajagopal mrajagopal requested a review from Copilot September 4, 2025 05:14
Copilot

This comment was marked as outdated.

Remove irrelevant comment

Co-authored-by: Copilot <[email protected]>
@mrajagopal mrajagopal requested a review from Copilot September 4, 2025 05:27
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Implements comprehensive unit tests for the nginx-supportpkg-for-k8s codebase, covering job execution, data collection, and CRD functionality.

  • Unit tests for all job lists (NIM, NIC, NGX, NGF, Common)
  • Data collector functionality and client initialization tests
  • CRD package validation tests

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
pkg/data_collector/data_collector.go Refactors client interface type and method accessibility for testability
pkg/data_collector/data_collector_test.go Tests DataCollector initialization, tarball creation, and namespace validation
pkg/jobs/common_job_list.go Updates deprecated API calls to use Discovery() method
pkg/jobs/common_job_list_test.go Tests common Kubernetes resource collection jobs
pkg/jobs/job_test.go Tests core job execution patterns and edge cases
pkg/jobs/nic_job_list_test.go Tests NGINX Ingress Controller specific job execution
pkg/jobs/ngf_job_list_test.go Tests NGINX Gateway Fabric job functionality
pkg/jobs/ngx_job_list_test.go Tests NGINX specific job execution
pkg/jobs/nim_job_list_test.go Tests NGINX Instance Manager job execution with exclude flags
pkg/crds/crd_test.go Validates CRD list structure and content

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

mrajagopal and others added 7 commits September 4, 2025 17:31
Taking the address of a slice element from a function call creates a pointer to a temporary value. Store the slice in a variable first, then take the address: jobs := CommonJobList(); podListJob = &jobs[i]

Co-authored-by: Copilot <[email protected]>
* Introduce a mock form helm, metrics clients
* Also update go.mod and go.sum
* update Makefile for clean and test targets
* Fix product_info job which was timing out due to absence of nginx-ingress container
@mrajagopal mrajagopal requested a review from Copilot October 13, 2025 04:25
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 13 out of 14 changed files in this pull request and generated 3 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@dareste
Copy link
Collaborator

dareste commented Oct 13, 2025

Great approach with the mock_data_collector as a foundation for testing the different jobs!

@mrajagopal mrajagopal requested a review from dareste October 14, 2025 02:21
* delete commented out code
* delete CRD test as it was not useful
},
}

client := fake.NewSimpleClientset(objs...)
Copy link
Collaborator

Choose a reason for hiding this comment

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

NewSimpleClientset is mark as deprecated.

Suggested change
client := fake.NewSimpleClientset(objs...)
client := fake.NewClientset(objs...)

Copy link
Collaborator

Choose a reason for hiding this comment

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

From what I see, we should iterate over all jobs and verify that:

  1. The response is not an error
  2. The job does not time out
  3. The job returns more than 0 files
  4. Number of files and names are expected
  5. File content format (for those that are JSON) is valid

The test TestCommonJobList_SelectedJobsProduceFiles already covers 1 to 3. To cover 4 and 5, I would expand that same test and verify via a switch case statement for each job (compare names) that we get the right files and formats.

Also, we could include a default selector in the switch case statement to catch those cases where the job name is not known. In that case, the test should fail, forcing us to set a new case statement that contemplates the job that is not being covered in the test.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same review than in common_job_list_test.go apply here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same review than in common_job_list_test.go apply here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same review than in common_job_list_test.go apply here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same review than in common_job_list_test.go apply here.

mrajagopal and others added 4 commits October 17, 2025 12:48
Docs say the return of NewMockClient is always non-nil.
Therefore, removing redundant check.

Co-authored-by: Daniel Aresté <[email protected]>
NewSimpleClientset is mark as deprecated, therefore replacing.

Co-authored-by: Daniel Aresté <[email protected]>
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.

2 participants