This is an automated email from the ASF dual-hosted git repository.
achennaka pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git
The following commit(s) were added to refs/heads/master by this push:
new e1e7079e7 [jwt] do not leak temporary test file
e1e7079e7 is described below
commit e1e7079e7541559c34742cb311ba806f35efbd54
Author: Alexey Serbin <[email protected]>
AuthorDate: Tue Dec 10 15:27:54 2024 -0800
[jwt] do not leak temporary test file
Prior to this patch, many temporary files would be left under /tmp
directory after running jwt-util-test. This patch addresses the issue.
Change-Id: I52481670ba4e20a1b33f777eebf8d3fe618f873f
Reviewed-on: http://gerrit.cloudera.org:8080/22194
Tested-by: Kudu Jenkins
Reviewed-by: Zoltan Chovan <[email protected]>
Reviewed-by: Abhishek Chennaka <[email protected]>
---
src/kudu/util/jwt-util-test.cc | 37 +++++++++++++++++++++++++------------
1 file changed, 25 insertions(+), 12 deletions(-)
diff --git a/src/kudu/util/jwt-util-test.cc b/src/kudu/util/jwt-util-test.cc
index a6e1cf341..c5dd93fff 100644
--- a/src/kudu/util/jwt-util-test.cc
+++ b/src/kudu/util/jwt-util-test.cc
@@ -62,10 +62,11 @@ using kudu::MiniOidc;
/// Utility class for creating a file that will be automatically deleted upon
test
/// completion.
-class TempTestDataFile {
+class TempTestDataFile final {
public:
// Creates a temporary file with the specified contents.
explicit TempTestDataFile(const std::string& contents);
+ ~TempTestDataFile();
/// Returns the absolute path to the file.
const std::string& Filename() const { return name_; }
@@ -75,23 +76,35 @@ class TempTestDataFile {
std::unique_ptr<WritableFile> tmp_file_;
};
-TempTestDataFile::TempTestDataFile(const std::string& contents)
- : name_("/tmp/jwks_XXXXXX") {
- string created_filename;
+TempTestDataFile::TempTestDataFile(const std::string& contents) {
+ static const string cNamePattern = "/tmp/jwks_XXXXXX";
WritableFileOptions opts;
opts.is_sensitive = false;
- Status status;
- status = Env::Default()->NewTempWritableFile(opts, &name_[0],
&created_filename, &tmp_file_);
- if (!status.ok()) {
- std::cerr << Substitute("Error creating temp file: $0", created_filename);
+ string fname;
+ if (auto s = Env::Default()->NewTempWritableFile(
+ opts, cNamePattern, &fname, &tmp_file_); !s.ok()) {
+ std::cerr << Substitute("error creating temp file '$0': $1",
+ fname, s.ToString());
}
- status = WriteStringToFile(Env::Default(), contents, created_filename);
- if (!status.ok()) {
- std::cerr << Substitute("Error writing contents to temp file: $0",
created_filename);
+ if (auto s = WriteStringToFile(Env::Default(), contents, fname);
+ !s.ok()) {
+ std::cerr << Substitute("error writing into temp file '$0': $1",
+ fname, s.ToString());
}
- name_ = created_filename;
+ name_ = fname;
+}
+
+TempTestDataFile::~TempTestDataFile() {
+ if (tmp_file_) {
+ // Remove the temporary file from the filesystem. The destructor
+ // of the 'tmp_file_' field takes care of closing the file.
+ if (auto s = Env::Default()->DeleteFile(name_); !s.ok()) {
+ std::cerr << Substitute("error removing temp file '$0': $1",
+ name_, s.ToString());
+ }
+ }
}
TEST(JwtUtilTest, LoadJwksFile) {