-
Notifications
You must be signed in to change notification settings - Fork 423
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
Refactored token filecache #755
Refactored token filecache #755
Conversation
975cca8
to
e313702
Compare
The token filecache used to use a private global function for creating a filelock, and overrode it in tests with a hand-crafted mocks for filesystem and environment variable operations. This change adds adds injectability to the filecache's filesystem and file lock using afero. This change also will simplify future changes when updating the AWS SDK with new credential interfaces. Signed-off-by: Micah Hausler <mhausler@amazon.com>
e313702
to
8464316
Compare
// A mockable flock interface | ||
type filelock interface { | ||
// FileLocker is a subset of the methods exposed by *flock.Flock | ||
type FileLocker interface { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made this interface public since flock.Flock is an external type and we're just abstracting it for our needs here
} | ||
|
||
func (t *testFS) ReadFile(filename string) ([]byte, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All these methods are no longer needed since we're just using an afero in-memory filesystem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pretty cool
if t.err == nil { | ||
return &t.fileinfo, nil | ||
} else { | ||
func (t *testFS) Stat(filename string) (fs.FileInfo, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only override Stat()
because the in-memory filesystem returns all fs.FileInfo as tmpfiles.
@@ -181,21 +145,37 @@ func validateFileCacheProvider(t *testing.T, p FileCacheProvider, err error, c * | |||
} | |||
} | |||
|
|||
// testSetEnv sets an env var, and returns a cleanup func |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than mocking os
Env methods, I just set/reset them
validateFileCacheProvider(t, p, err, c) | ||
if !p.cachedCredential.IsExpired() { | ||
t.Errorf("missing cache file should result in expired cached credential") | ||
} | ||
tf.err = nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're no longer using a global mock, cleanup at the end of every test is no longer necessary
/lgtm /approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: jyotimahapatra, micahhausler The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
What this PR does / why we need it:
The token filecache used to use a private global function for creating a filelock, and overrode it in tests with a hand-crafted mocks for filesystem and environment variable operations.
This change adds adds injectability to the filecache's filesystem and file lock using afero. This change also will simplify future changes when updating the AWS SDK with new credential interfaces.