-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure*PKI function indicates if one or more certificate files were c…
…hanged (#308) Co-authored-by: Angelos Kolaitis <[email protected]>
- Loading branch information
1 parent
7eff7df
commit ab2ff4f
Showing
6 changed files
with
242 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package setup | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestEnsureFile(t *testing.T) { | ||
t.Run("CreateFile", func(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
tempDir := t.TempDir() | ||
fname := path.Join(tempDir, "test") | ||
updated, err := ensureFile(fname, "test", os.Getuid(), os.Getgid(), 0777) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(updated).To(BeTrue()) | ||
|
||
createdFile, _ := os.ReadFile(fname) | ||
g.Expect(string(createdFile) == "test").To(BeTrue()) | ||
}) | ||
|
||
t.Run("DeleteFile", func(t *testing.T) { | ||
g := NewWithT(t) | ||
tempDir := t.TempDir() | ||
fname := path.Join(tempDir, "test") | ||
|
||
// Create a file with some content. | ||
updated, err := ensureFile(fname, "test", os.Getuid(), os.Getgid(), 0777) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(updated).To(BeTrue()) | ||
|
||
// Delete the file. | ||
updated, err = ensureFile(fname, "", os.Getuid(), os.Getgid(), 0777) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(updated).To(BeTrue()) | ||
|
||
_, err = os.Stat(fname) | ||
g.Expect(os.IsNotExist(err)).To(BeTrue()) | ||
}) | ||
|
||
t.Run("ChangeContent", func(t *testing.T) { | ||
g := NewWithT(t) | ||
tempDir := t.TempDir() | ||
fname := path.Join(tempDir, "test") | ||
|
||
// Create a file with some content. | ||
updated, err := ensureFile(fname, "test", os.Getuid(), os.Getgid(), 0777) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(updated).To(BeTrue()) | ||
|
||
// ensureFile with same content should return that the file was not updated. | ||
updated, err = ensureFile(fname, "test", os.Getuid(), os.Getgid(), 0777) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(updated).To(BeFalse()) | ||
|
||
// Change the content and ensureFile should return that the file was updated. | ||
updated, err = ensureFile(fname, "new content", os.Getuid(), os.Getgid(), 0777) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(updated).To(BeTrue()) | ||
|
||
createdFile, _ := os.ReadFile(fname) | ||
g.Expect(string(createdFile) == "new content").To(BeTrue()) | ||
|
||
// Change permissions and ensureFile should return that the file was not updated. | ||
updated, err = ensureFile(fname, "new content", os.Getuid(), os.Getgid(), 0666) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(updated).To(BeFalse()) | ||
}) | ||
|
||
t.Run("NotExist", func(t *testing.T) { | ||
g := NewWithT(t) | ||
tempDir := t.TempDir() | ||
fname := path.Join(tempDir, "test") | ||
|
||
// ensureFile on inexistent file with empty content should return that the file was not updated. | ||
updated, err := ensureFile(fname, "", os.Getuid(), os.Getgid(), 0777) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(updated).To(BeFalse()) | ||
}) | ||
} |
Oops, something went wrong.