Skip to content

Add unit tests for remote.kubernetes components #3770

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ptodev
Copy link
Contributor

@ptodev ptodev commented Jun 4, 2025

PR Description

Adding unit tests for remote.kubernetes components which use a fake K8s to test whether the correct exports are produced.

PR Checklist

  • CHANGELOG.md updated
  • Documentation added
  • Tests updated
  • Config converters updated

@ptodev ptodev requested a review from a team as a code owner June 4, 2025 10:25
@ptodev ptodev force-pushed the ptodev/k8s-attr branch from 63b0d1c to aa584b4 Compare June 17, 2025 16:46
return fakeClientset, nil
}

func Eventually(t *testing.T, min, max time.Duration, retries int, f func() error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason why not using the other implementations we have available of similar function?

Comment on lines +103 to +105
if !reflect.DeepEqual(expect.Data, actual.Data) {
return fmt.Errorf("expected %#v, got %#v", expect, actual)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: there's a slight advantage of using require or assert packages because they will print a nice diff for many data types when they are not equal that helps to spot where the issue is.

Comment on lines +110 to +111
// Do the initial Update() in Run() instead of in New().
// That way New() is stateless and has no side effects.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it because of the initial poll or does the GetKubernetesClient try to talk to k8s on creation and can return errors if misconfigured? Not sure how New() is stateful, seems to me that multiple calls to New() will not modify internal state, but maybe I'm missing something.

By moving this from New to Run, we change the way errors will be handled, no? Errors in New I think generally give a more detailed information back to the user and can stop applying a new config?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, "stateful" is the wrong word. What I meant is that New() won't have side effects. It makes it easier to substitute parts of the component with fake ones. I think it also aligns better with how Alloy components are meant to be designed. Normally you wouldn't want a New function to connect to external services, to create files, etc.

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