DaanHoogland commented on a change in pull request #2674: Create unit test 
cases for 'ConfigDriveBuilder' class
URL: https://github.com/apache/cloudstack/pull/2674#discussion_r190553310
 
 

 ##########
 File path: 
engine/storage/configdrive/src/org/apache/cloudstack/storage/configdrive/ConfigDriveBuilder.java
 ##########
 @@ -51,30 +47,45 @@
 
     public static final Logger LOG = 
Logger.getLogger(ConfigDriveBuilder.class);
 
-    private static void writeFile(final File folder, final String file, final 
String content) {
-        if (folder == null || Strings.isNullOrEmpty(file)) {
-            return;
-        }
-        final File vendorDataFile = new File(folder, file);
-        try (final FileWriter fw = new FileWriter(vendorDataFile); final 
BufferedWriter bw = new BufferedWriter(fw)) {
-            bw.write(content);
+    /**
+     * Writes a content {@link String} to a file that is going to be created 
in a folder. We will not append to the file if it already exists. Therefore, 
its content will be overwritten.
+     * Moreover, the charset used is {@link 
com.cloud.utils.StringUtils#getPreferredCharset()}.
+     *
+     * We expect the folder object and the file not to be null/empty.
+     */
+    static void writeFile(File folder, String file, String content) {
+        try {
+            FileUtils.write(new File(folder, file), content, 
com.cloud.utils.StringUtils.getPreferredCharset(), false);
         } catch (IOException ex) {
             throw new CloudRuntimeException("Failed to create config drive 
file " + file, ex);
         }
     }
 
-    public static String fileToBase64String(final File isoFile) throws 
IOException {
+    /**
+     *  Read the content of a {@link File} and convert it to a String in base 
64.
+     *  We expect the content of the file to be encoded using {@link 
com.cloud.utils.StringUtils#getPreferredCharset()}
+     */
+    public static String fileToBase64String(File isoFile) throws IOException {
         byte[] encoded = 
Base64.encodeBase64(FileUtils.readFileToByteArray(isoFile));
-        return new String(encoded, StandardCharsets.US_ASCII);
+        return new String(encoded, 
com.cloud.utils.StringUtils.getPreferredCharset());
 
 Review comment:
   the default charset might be different on the MS and the host. something 
that we might want to force in a test.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to