Skip to content

Commit

Permalink
truncate files if append is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Asirian Aleksandr committed Mar 21, 2024
1 parent e06f4c9 commit b0bc35d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions agent/cloudinit/file_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func (w FileWriter) WriteToFile(file *Files) error {
flag := os.O_WRONLY | os.O_CREATE
if file.Append {
flag |= os.O_APPEND
} else {
flag |= os.O_TRUNC
}

f, err := os.OpenFile(file.Path, flag, initPermission)
Expand Down
25 changes: 25 additions & 0 deletions agent/cloudinit/file_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,31 @@ var _ = Describe("FileWriter", func() {

})

It("Should overwrite content of file when append mode is disabled", func() {
fileOriginContent := "very long long message"
file := cloudinit.Files{
Path: path.Join(workDir, "file3.txt"),
Encoding: "",
Owner: "",
Permissions: "",
Content: "short message",
Append: false,
}

err := cloudinit.FileWriter{}.MkdirIfNotExists(workDir)
Expect(err).NotTo(HaveOccurred())

err = os.WriteFile(file.Path, []byte(fileOriginContent), 0644)
Expect(err).NotTo(HaveOccurred())

err = cloudinit.FileWriter{}.WriteToFile(&file)
Expect(err).NotTo(HaveOccurred())

buffer, err := os.ReadFile(file.Path)
Expect(err).NotTo(HaveOccurred())
Expect(string(buffer)).To(Equal(file.Content))
})

It("should return error with invalid owner format", func() {
file := cloudinit.Files{
Path: path.Join(workDir, "file1.txt"),
Expand Down

0 comments on commit b0bc35d

Please sign in to comment.