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

Provide error when no config was found #323

Merged
merged 2 commits into from
May 5, 2023

Conversation

jonkerj
Copy link
Contributor

@jonkerj jonkerj commented Apr 5, 2023

What this PR does / why we need it:
The control loop currently segfaults when none of the config files was found. This commit makes it give an actual error, providing the user with useful feedback.

Which issue(s) this PR fixes
If needed, I'll raise an issue, please let me know.

Special notes for your reviewer (optional):

@jonkerj jonkerj requested a review from dougbtv as a code owner April 5, 2023 14:57
pkg/config/config.go Outdated Show resolved Hide resolved
@jonkerj jonkerj force-pushed the error-for-no-config branch from 28a57b8 to f39b7d9 Compare April 17, 2023 10:55
Copy link
Collaborator

@maiqueb maiqueb left a comment

Choose a reason for hiding this comment

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

Could you also add a couple of unit tests to https://github.com/k8snetworkplumbingwg/whereabouts/blob/master/pkg/config/config_test.go ?

Let me know if you need any help with it.

@jonkerj
Copy link
Contributor Author

jonkerj commented Apr 17, 2023

Sure, if I can bother you to look at #226 😉 (unrelated to this issue, but I think it deserves some attention too)

@jonkerj
Copy link
Contributor Author

jonkerj commented Apr 17, 2023

Could you also add a couple of unit tests to https://github.com/k8snetworkplumbingwg/whereabouts/blob/master/pkg/config/config_test.go ?

Let me know if you need any help with it.

Well I tried to, but then I discovered that a lot (maybe all) tests in config_test.go are failing now, because GetFlamIPAM (which throws an error when no config files are present) is invoked from LoadIPAMConfig. I'm not really sure if LoadIPAMConfig needs a bit of refactoring to detect this type of error, or if the tests need rewriting, or maybe even something else.

Do you have a hint for me?

@jonkerj
Copy link
Contributor Author

jonkerj commented Apr 24, 2023

ping @maiqueb

@maiqueb
Copy link
Collaborator

maiqueb commented Apr 24, 2023

Could you also add a couple of unit tests to https://github.com/k8snetworkplumbingwg/whereabouts/blob/master/pkg/config/config_test.go ?
Let me know if you need any help with it.

Well I tried to, but then I discovered that a lot (maybe all) tests in config_test.go are failing now, because GetFlamIPAM (which throws an error when no config files are present) is invoked from LoadIPAMConfig. I'm not really sure if LoadIPAMConfig needs a bit of refactoring to detect this type of error, or if the tests need rewriting, or maybe even something else.

Do you have a hint for me?

Yeah, so now that you return an error when there's no config present (I agree this should happen), the tests should be re-written to ensure this configuration is available to the tests.

Let's make sure @dougbtv is on board with the conceptual change first if you don't mind.

I can guide you on adapting the tests later on.

Stay tuned.

@maiqueb
Copy link
Collaborator

maiqueb commented Apr 24, 2023

FWIW, the changes you need to do in the tests are quite small.

Here's what you need to do (using as reference the first test, you need to do something similar for all the failing ones):

       var tmpDir string // this must be accessible from the tests
 
       BeforeEach(func() {
               var err error
               tmpDir, err = os.MkdirTemp("", "whereabouts") // must create a tmp dir to store the configs
               Expect(err).NotTo(HaveOccurred())
       })

       AfterEach(func() {
               Expect(os.RemoveAll(tmpDir)).To(Succeed()) // must clean-up the generated configs
       })

      It("can load a basic config", func() {
		conf := `...` // each test will have a different conf; leave the original configuration you have in each test

		confPath := fmt.Sprintf("%s/%s", tmpDir, "whereabouts.conf") 
		Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed()) // must persist the conf in the file-system

		ipamconfig, _, err := LoadIPAMConfig([]byte(conf), "", confPath) // must pass the new conf-path to the func; it only knows about the default paths
		Expect(err).NotTo(HaveOccurred())
		... // further expectations, each test looks for different stuff
	})

@maiqueb
Copy link
Collaborator

maiqueb commented Apr 24, 2023

@jonkerj alright! @dougbtv 's on board with this change.

Please update the failing tests in a similar way to what I'm proposing in this link.

Also please add a unit test that expects an error to be thrown - because the config on disk is not available.

@jonkerj
Copy link
Contributor Author

jonkerj commented Apr 25, 2023

Hi, sounds allright, I will look into that in a week or so.

@jonkerj jonkerj force-pushed the error-for-no-config branch from f39b7d9 to 91907d6 Compare May 1, 2023 11:38
@jonkerj
Copy link
Contributor Author

jonkerj commented May 1, 2023

I've updated my PR with a commit that fixes those tests. All is green on my side. I am recycling the filename whereabouts.conf throughout the tests, because (if I understand everything correct) it gets reinitialized every test (BeforeEach et al) or gets overwritten anyways.

Copy link
Collaborator

@maiqueb maiqueb left a comment

Choose a reason for hiding this comment

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

I only have two minor comments. Other than that, looks good.

pkg/config/config_test.go Outdated Show resolved Hide resolved
pkg/config/config_test.go Outdated Show resolved Hide resolved
The control loop currently segfaults when none of the config files was
found. This commit makes it error, providing the user with useful
feedback.

Also, L260 returned a `nil` disguised as a var named `err`, which could
be done more explicit.

Signed-off-by: Jorik Jonker <jorik.jonker@eu.equinix.com>
@jonkerj jonkerj force-pushed the error-for-no-config branch from 91907d6 to d498b17 Compare May 3, 2023 08:10
@maiqueb
Copy link
Collaborator

maiqueb commented May 3, 2023

You have plenty of failed unit tests in cmd/whereabouts_test.go .

You need to take care of them.

@jonkerj
Copy link
Contributor Author

jonkerj commented May 3, 2023

You are right, I need to fix the unit tests on way more places than I did. I only tested github.com/k8snetworkplumbingwg/whereabouts/pkg/config, previously, my bad.

@jonkerj jonkerj force-pushed the error-for-no-config branch from d498b17 to d802ba7 Compare May 3, 2023 11:23
@jonkerj
Copy link
Contributor Author

jonkerj commented May 3, 2023

OK, we are almost there. There are still 5 unit tests failing in cmd/whereabouts_test.go, and I'm again unsure how to proceed. In one of the tests (allocate and release addresses on ADD/DEL), ipamConfig(..) is called, which in turn calls config.LoadIPAMConfig(..), which now throws an error.

I think simply ignoring ConfigFileNotFoundError in cmd/whereabouts_tests.go around lines 1451-1454 would mimic the previous behavior (as no config found would be ignored anyways), right?

Also: I feel a bit awkward about both passing byte[](conf) and write it to a file and pass that to LoadIPAMConfig (feels like it's not really DRY). How do you think about that?

It is required per my previous commit, which seemed to break a couple of
tests.

Signed-off-by: Jorik Jonker <jorik.jonker@eu.equinix.com>
@jonkerj jonkerj force-pushed the error-for-no-config branch from d802ba7 to 55b24e2 Compare May 3, 2023 11:43
@maiqueb
Copy link
Collaborator

maiqueb commented May 3, 2023

OK, we are almost there. There are still 5 unit tests failing in cmd/whereabouts_test.go, and I'm again unsure how to proceed. In one of the tests (allocate and release addresses on ADD/DEL), ipamConfig(..) is called, which in turn calls config.LoadIPAMConfig(..), which now throws an error.

I think simply ignoring ConfigFileNotFoundError in cmd/whereabouts_tests.go around lines 1451-1454 would mimic the previous behavior (as no config found would be ignored anyways), right?

Yeah, that was the previous behavior.

... but IMO, the goal of this PR is to get rid of the previous behavior and ensure an error is thrown when a config is not provided. This must be reflected in the unit tests.

IMO, you cannot just ignore the error; you should either do a proper setup of the tests, or mock the real component, providing an implementation that does what you want.

Maybe the later is simpler.

Also: I feel a bit awkward about both passing byte[](conf) and write it to a file and pass that to LoadIPAMConfig (feels like it's not really DRY). How do you think about that?

The whole API could be improved to be honest ...

If you choose to pursue that, please do it in a separate PR.

@jonkerj
Copy link
Contributor Author

jonkerj commented May 3, 2023

OK, we are almost there. There are still 5 unit tests failing in cmd/whereabouts_test.go, and I'm again unsure how to proceed. In one of the tests (allocate and release addresses on ADD/DEL), ipamConfig(..) is called, which in turn calls config.LoadIPAMConfig(..), which now throws an error.

I think simply ignoring ConfigFileNotFoundError in cmd/whereabouts_tests.go around lines 1451-1454 would mimic the previous behavior (as no config found would be ignored anyways), right?

I did something else: I os.WriteFile'd the mashalled config to a tempfile, similar to your earlier suggestion. As the function prototype of ipamConfig does not allow for passing errors currently (that needs significant rewrite of the 5 involved tests), it nils out when anything fails (which cause the test to fail). Not the prettiest, but all tests pass now.

EDIT: they pass locally. I'm working on getting them to pass in GH actions

@jonkerj
Copy link
Contributor Author

jonkerj commented May 3, 2023

All is OK, all tests pass now config files are created. I'll save refactoring for DRY for another rainy day ;-)

Copy link
Collaborator

@maiqueb maiqueb left a comment

Choose a reason for hiding this comment

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

Thank you for this change, and your willingness to jump through all the hurdles I kept setting for you.

@maiqueb maiqueb merged commit 8d5d9d1 into k8snetworkplumbingwg:master May 5, 2023
@jonkerj
Copy link
Contributor Author

jonkerj commented May 5, 2023

you're welcome!

@coveralls
Copy link

coveralls commented Oct 7, 2024

Pull Request Test Coverage Report for Build 4871473391

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 5 of 8 (62.5%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.1%) to 71.976%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pkg/config/config.go 5 8 62.5%
Totals Coverage Status
Change from base Build 4721797056: -0.1%
Covered Lines: 976
Relevant Lines: 1356

💛 - Coveralls

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