Skip to content

Update credential file path check to current working directory #50

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

Merged
merged 9 commits into from
Dec 18, 2019

Conversation

rmkeezer
Copy link
Contributor

It currently incorrectly checks in the core root directory.

Closes https://github.ibm.com/arf/planning-sdk-squad/issues/1253

@codecov
Copy link

codecov bot commented Dec 16, 2019

Codecov Report

Merging #50 into master will increase coverage by 0.17%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #50      +/-   ##
==========================================
+ Coverage   97.58%   97.75%   +0.17%     
==========================================
  Files          17       17              
  Lines         579      579              
==========================================
+ Hits          565      566       +1     
+ Misses         14       13       -1
Impacted Files Coverage Δ
ibm_cloud_sdk_core/utils.py 99.05% <100%> (+0.94%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ef52165...9c54046. Read the comment docs.

Copy link
Contributor

@mkistler mkistler left a comment

Choose a reason for hiding this comment

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

Suggested a fix for the build failure.

Also -- it would be nice to have a test to verify this behavior.

from os import getenv, environ
from os.path import dirname, isfile, join, expanduser, abspath
from os import getenv, environ, getcwd
from os.path import isfile, join, expanduser, abspath
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
from os.path import isfile, join, expanduser, abspath
from os.path import isfile, join, expanduser

This change will fix the linter error that is causing the build to fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the unused path and I changed a test to load the credentials file from the current working directory instead of the absolute path of ../../file but that doesn't test the exact reason this error happened. It happened because the core was being used one directly higher in the python sdk. This is fixed but I currently dont know an easy was of testing that scenario.

Copy link
Member

Choose a reason for hiding this comment

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

Can you have a test that relies on a file in a specific directory X (e.g. X/ibm-credentials.env) where the test first performs the equivalent of the "cd X" shell command? Then when you trigger the loading of the credentials file, the one in the (now) "current" directory should be found.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, I now modified two tests. For test_iam, I changed it to load the ibm-credentials.env file from the current working directory instead of from the file_path environment variable. For test_configure_services, I did what your comment suggested and changed working directories to the credentials file, then triggered loading the credentials file in the new current directory.

Copy link
Member

@padamstx padamstx left a comment

Choose a reason for hiding this comment

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

I left some comments on the testcase and I think we need to resolve those before we merge this in.

cwd = os.getcwd()
os.chdir(os.path.dirname(file_path))
iam_authenticator = get_authenticator_from_environment('ibm-watson')
os.chdir(cwd)
Copy link
Member

Choose a reason for hiding this comment

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

I don't quite understand why you'd restore the original CWD immediately after loading up an authenticator but before you construct the service instance (which presumably would make a call to BaseService.configure_service(). What scenario are you trying to test exactly? What would make sense to me would be to "cd" to the directory containing the ibm-credentials.env file, then try to construct an instance of the service while passing in a service name of "ibm_watson". This should load up an authenticator via external config AND configure the service instance using the properties contained in that file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mistakenly thought that getting the authenticator was the only thing that needed to be in the current working directory so I moved my cleanup statements to immediately after that so they would be called even if the assert statements failed. I moved it to it's correct location and it's working now, thanks!

temp_env_path = os.getcwd() + '/ibm-credentials.env'
copyfile(file_path, temp_env_path)
iam_authenticator = get_authenticator_from_environment('ibm-watson')
os.remove(temp_env_path)
Copy link
Member

Choose a reason for hiding this comment

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

This is the same as the first scenario, except in the case above you were "cd"'ing back to the original working directory (hence hiding the ibm-credentials.env file) and in this case you're actually deleting the file prior to constructing the service. In both cases, the file will not be "seen" when the service is being constructed.

Copy link
Member

Choose a reason for hiding this comment

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

I think what we need to test (primarily) is a simple scenario like this:

  • change the CWD to the location where the ibm-credentials.env file exists
  • load an authenticator using the factory and pass in "ibm_watson" for the service name; verify that it is the correct type of authenticator (based on the properties defined in ibm-credentials.env)
  • construct an instance of the test service and call its configure_service() method (passing in "ibm_watson" for the service name; verify that the service url field of the service is set to the URL specified in ibm-credentials.env.

Copy link
Contributor Author

@rmkeezer rmkeezer Dec 18, 2019

Choose a reason for hiding this comment

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

Right, I mistakenly put my cleanup statements in the wrong place so the service was defaulting to the default URL. I fixed it as you said and it's working as expected now. Thanks!

@rmkeezer rmkeezer requested a review from padamstx December 18, 2019 16:53
Copy link
Member

@padamstx padamstx left a comment

Choose a reason for hiding this comment

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

LGTM

@rmkeezer rmkeezer merged commit 626b2d7 into master Dec 18, 2019
@ibm-devx-automation
Copy link

🎉 This PR is included in version 1.5.2 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@padamstx padamstx deleted the correct_cwd branch November 10, 2020 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants