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

Improve Unit Test Experience for Functionality using Azure CLI Configuration #25796

Closed
PramodValavala-MSFT opened this issue Mar 13, 2023 · 8 comments
Assignees
Labels
ARM az resource/group/lock/tag/deployment/policy/managementapp/account management-group Azure CLI Team The command of the issue is owned by Azure CLI team feature-request Test Framework
Milestone

Comments

@PramodValavala-MSFT
Copy link
Contributor

PramodValavala-MSFT commented Mar 13, 2023

Is your feature request related to a problem? Please describe.

When writing unit test for functionality that leverages Azure CLI Configuration, the config is persisted across tests leading to misleading results.

Most recently, new functionality in the az bicep sub-command leveraged Azure CLI Configuration but a bug slipped through the tests due to the config value not being reset with each test, causing numerous build pipeline failures for customers.

Describe the solution you'd like

Better unit testing experience for such functionality. Based on this last experience, here are some thoughts I had:

  • Reset config for each test or In-Memory Config for DummyCLI
  • Should Config API throw error for No Section
  • Lint Rule for cli_ctx.config.get() without default value. Warning at the minimum.
@yonzhan yonzhan added the ARM az resource/group/lock/tag/deployment/policy/managementapp/account management-group label Mar 13, 2023
@yonzhan yonzhan added Azure CLI Team The command of the issue is owned by Azure CLI team feature-request labels Mar 13, 2023
@yonzhan yonzhan added this to the Backlog milestone Mar 13, 2023
@yonzhan
Copy link
Collaborator

yonzhan commented Mar 13, 2023

@wangzelin007 for awareness

@wangzelin007
Copy link
Member

Hi @PramodValavala-MSFT,

  • Reset config for each test or In-Memory Config for DummyCLI
    Our test framework already supports setUp and tearDown for each test
  • Should Config API throw error for No Section
    @jiasli for awareness
  • Lint Rule for cli_ctx.config.get() without default value. Warning at the minimum.
    @jiasli for awareness

@PramodValavala-MSFT
Copy link
Contributor Author

@wangzelin007 Thanks for the update!

While the setUp and tearDown methods are supported, there is still no way to clear the config I believe. Should we just remove the config file manually?

I suppose this functionality is more in the knack framework than Azure CLI, perhaps this should be implemented there?

@jiasli
Copy link
Member

jiasli commented Mar 23, 2023

Sorry but I didn't really get the questions here. The behavior of cli_ctx.config.get() is internally implemented by the configparser built-in library.

  • Should Config API throw error for No Section

Is this a question or conditional?

The current behavior of cli_ctx.config.get('aaa', 'bbb') is to raise configparser.NoSectionError:

  File "D:\cli\py310\lib\site-packages\knack\config.py", line 208, in get
    return self.config_parser.get(section, option)
  File "C:\Users\jiasli\AppData\Local\Programs\Python\Python310\lib\configparser.py", line 783, in get
    d = self._unify_values(section, vars)
  File "C:\Users\jiasli\AppData\Local\Programs\Python\Python310\lib\configparser.py", line 1154, in _unify_values
    raise NoSectionError(section) from None
configparser.NoSectionError: No section: 'aaa'
  • Lint Rule for cli_ctx.config.get() without default value. Warning at the minimum.

Without specifying fallback kwarg, cli_ctx.config.get('core', 'bbb') errs:

  File "D:\cli\py310\lib\site-packages\knack\config.py", line 208, in get
    return self.config_parser.get(section, option)
  File "C:\Users\jiasli\AppData\Local\Programs\Python\Python310\lib\configparser.py", line 794, in get
    raise NoOptionError(option, section)
configparser.NoOptionError: No option 'bbb' in section: 'core'

It can't simply warns since it doesn't know which value to use.

@bebound
Copy link
Contributor

bebound commented Mar 23, 2023

After this PR is merged, each test can use random config dir.
#25689

@PramodValavala-MSFT
Copy link
Contributor Author

@jiasli Thanks for the insights! This behavior is difficult to catch in tests, which led to the issue here, because the config file is re-used across tests, and in our case, we never got the NoSectionError.

Since this is the expected behavior and I assume authors either must ensure a fallback value is present or handle the NoSectionError exception, I was thinking it would be good to have a linter rule to highlight this as a warning that this line might throw this exception without the fallback value provided.

Since this was the first time I was using this API, it wasn't obvious to me that this line could raise an exception and testing it missed as well since the config file was being shared.

@bebound Yes. I think that should help. So, will we be able to change the config dir for each test as part of the setUp method or would it be automatic as part of the test framework?

Also, considering there is a performance impact, wouldn't it make sense to make the config file in-memory and have an API to clear it before each test to speed things up?

@bebound
Copy link
Contributor

bebound commented Mar 24, 2023

IMHO, most test only read config. Modifying specific tests is more efficient than using dedicate config directory for each test. We need to add random_config_dir param in __init__ method in tests class.

I don't know if there is an easy and cross-platform way to create in-memory folders.

@PramodValavala-MSFT
Copy link
Contributor Author

@bebound Thanks for the work here! I've noticed the PR for this has been merged but is not extended to LiveScenarioTests though.

As part of this PR (#26374), I've included the change for that as well. Could you please review?

Closing this issue since the initial ask has been completed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ARM az resource/group/lock/tag/deployment/policy/managementapp/account management-group Azure CLI Team The command of the issue is owned by Azure CLI team feature-request Test Framework
Projects
None yet
Development

No branches or pull requests

6 participants