2014-03-18 20:00:41 +04:00
|
|
|
package system
|
2014-03-12 04:46:58 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWriteFileUnencodedContent(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create tempdir: %v", err)
|
|
|
|
}
|
2014-06-05 23:48:32 +04:00
|
|
|
defer os.RemoveAll(dir)
|
2014-03-12 04:46:58 +04:00
|
|
|
|
2014-06-05 23:48:32 +04:00
|
|
|
fn := "foo"
|
|
|
|
fullPath := path.Join(dir, fn)
|
2014-03-18 20:00:41 +04:00
|
|
|
|
|
|
|
wf := File{
|
2014-06-05 23:48:32 +04:00
|
|
|
Path: fn,
|
|
|
|
Content: "bar",
|
2014-03-18 20:00:41 +04:00
|
|
|
RawFilePermissions: "0644",
|
2014-03-12 04:46:58 +04:00
|
|
|
}
|
|
|
|
|
2014-06-05 23:48:32 +04:00
|
|
|
path, err := WriteFile(&wf, dir)
|
|
|
|
if err != nil {
|
2014-03-18 20:00:41 +04:00
|
|
|
t.Fatalf("Processing of WriteFile failed: %v", err)
|
2014-06-05 23:48:32 +04:00
|
|
|
} else if path != fullPath {
|
|
|
|
t.Fatalf("WriteFile returned bad path: want %s, got %s", fullPath, path)
|
2014-03-18 20:00:41 +04:00
|
|
|
}
|
2014-03-12 04:46:58 +04:00
|
|
|
|
|
|
|
fi, err := os.Stat(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to stat file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.Mode() != os.FileMode(0644) {
|
|
|
|
t.Errorf("File has incorrect mode: %v", fi.Mode())
|
|
|
|
}
|
|
|
|
|
|
|
|
contents, err := ioutil.ReadFile(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to read expected file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(contents) != "bar" {
|
|
|
|
t.Fatalf("File has incorrect contents")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteFileInvalidPermission(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create tempdir: %v", err)
|
|
|
|
}
|
2014-06-05 23:48:32 +04:00
|
|
|
defer os.RemoveAll(dir)
|
2014-03-12 04:46:58 +04:00
|
|
|
|
2014-03-18 20:00:41 +04:00
|
|
|
wf := File{
|
2014-06-05 23:48:32 +04:00
|
|
|
Path: path.Join(dir, "tmp", "foo"),
|
|
|
|
Content: "bar",
|
2014-03-18 20:00:41 +04:00
|
|
|
RawFilePermissions: "pants",
|
|
|
|
}
|
|
|
|
|
2014-06-05 23:48:32 +04:00
|
|
|
if _, err := WriteFile(&wf, dir); err == nil {
|
2014-03-12 04:46:58 +04:00
|
|
|
t.Fatalf("Expected error to be raised when writing file with invalid permission")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-18 04:05:59 +04:00
|
|
|
func TestWriteFilePermissions(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create tempdir: %v", err)
|
|
|
|
}
|
2014-06-05 23:48:32 +04:00
|
|
|
defer os.RemoveAll(dir)
|
2014-03-18 04:05:59 +04:00
|
|
|
|
2014-06-05 23:48:32 +04:00
|
|
|
fn := "foo"
|
|
|
|
fullPath := path.Join(dir, fn)
|
2014-03-18 20:00:41 +04:00
|
|
|
|
|
|
|
wf := File{
|
2014-06-05 23:48:32 +04:00
|
|
|
Path: fn,
|
2014-03-18 20:00:41 +04:00
|
|
|
RawFilePermissions: "0755",
|
2014-03-18 04:05:59 +04:00
|
|
|
}
|
|
|
|
|
2014-06-05 23:48:32 +04:00
|
|
|
path, err := WriteFile(&wf, dir)
|
|
|
|
if err != nil {
|
2014-03-18 20:00:41 +04:00
|
|
|
t.Fatalf("Processing of WriteFile failed: %v", err)
|
2014-06-05 23:48:32 +04:00
|
|
|
} else if path != fullPath {
|
|
|
|
t.Fatalf("WriteFile returned bad path: want %s, got %s", fullPath, path)
|
2014-03-18 20:00:41 +04:00
|
|
|
}
|
2014-03-18 04:05:59 +04:00
|
|
|
|
|
|
|
fi, err := os.Stat(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to stat file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.Mode() != os.FileMode(0755) {
|
|
|
|
t.Errorf("File has incorrect mode: %v", fi.Mode())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 04:46:58 +04:00
|
|
|
func TestWriteFileEncodedContent(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create tempdir: %v", err)
|
|
|
|
}
|
2014-06-05 23:48:32 +04:00
|
|
|
defer os.RemoveAll(dir)
|
2014-03-12 04:46:58 +04:00
|
|
|
|
2014-03-18 20:00:41 +04:00
|
|
|
wf := File{
|
2014-06-05 23:48:32 +04:00
|
|
|
Path: path.Join(dir, "tmp", "foo"),
|
|
|
|
Content: "",
|
2014-03-18 20:00:41 +04:00
|
|
|
Encoding: "base64",
|
|
|
|
}
|
|
|
|
|
2014-06-05 23:48:32 +04:00
|
|
|
if _, err := WriteFile(&wf, dir); err == nil {
|
2014-03-12 04:46:58 +04:00
|
|
|
t.Fatalf("Expected error to be raised when writing file with encoding")
|
|
|
|
}
|
|
|
|
}
|