file: refactor config

- Seperate the config from Permissions()
- Add YAML tags for the fields
This commit is contained in:
Alex Crawford
2014-09-21 19:22:27 -07:00
parent 1fbbaaec19
commit 85b8d804c8
16 changed files with 92 additions and 70 deletions

View File

@@ -7,6 +7,8 @@ import (
"strings"
"syscall"
"testing"
"github.com/coreos/coreos-cloudinit/config"
)
const (
@@ -48,9 +50,9 @@ func TestWriteEnvFileUpdate(t *testing.T) {
}
ef := EnvFile{
File: &File{
File: &File{config.File{
Path: name,
},
}},
Vars: valueUpdate,
}
@@ -95,9 +97,9 @@ func TestWriteEnvFileUpdateNoNewline(t *testing.T) {
}
ef := EnvFile{
File: &File{
File: &File{config.File{
Path: name,
},
}},
Vars: valueUpdate,
}
@@ -136,9 +138,9 @@ func TestWriteEnvFileCreate(t *testing.T) {
fullPath := path.Join(dir, name)
ef := EnvFile{
File: &File{
File: &File{config.File{
Path: name,
},
}},
Vars: valueUpdate,
}
@@ -174,9 +176,9 @@ func TestWriteEnvFileNoop(t *testing.T) {
}
ef := EnvFile{
File: &File{
File: &File{config.File{
Path: name,
},
}},
Vars: valueNoop,
}
@@ -221,9 +223,9 @@ func TestWriteEnvFileUpdateDos(t *testing.T) {
}
ef := EnvFile{
File: &File{
File: &File{config.File{
Path: name,
},
}},
Vars: valueUpdate,
}
@@ -270,9 +272,9 @@ func TestWriteEnvFileDos2Unix(t *testing.T) {
}
ef := EnvFile{
File: &File{
File: &File{config.File{
Path: name,
},
}},
Vars: valueNoop,
}
@@ -318,9 +320,9 @@ func TestWriteEnvFileEmpty(t *testing.T) {
}
ef := EnvFile{
File: &File{
File: &File{config.File{
Path: name,
},
}},
Vars: valueEmpty,
}
@@ -360,9 +362,9 @@ func TestWriteEnvFileEmptyNoCreate(t *testing.T) {
fullPath := path.Join(dir, name)
ef := EnvFile{
File: &File{
File: &File{config.File{
Path: name,
},
}},
Vars: valueEmpty,
}
@@ -391,9 +393,9 @@ func TestWriteEnvFilePermFailure(t *testing.T) {
ioutil.WriteFile(fullPath, []byte(base), 0000)
ef := EnvFile{
File: &File{
File: &File{config.File{
Path: name,
},
}},
Vars: valueUpdate,
}
@@ -413,9 +415,9 @@ func TestWriteEnvFileNameFailure(t *testing.T) {
name := "foo.conf"
ef := EnvFile{
File: &File{
File: &File{config.File{
Path: name,
},
}},
Vars: valueInvalid,
}

View File

@@ -40,9 +40,9 @@ func (eh EtcHosts) File() (*File, error) {
return nil, err
}
return &File{
return &File{config.File{
Path: path.Join("etc", "hosts"),
RawFilePermissions: "0644",
Content: etcHosts,
}, nil
}}, nil
}

View File

@@ -27,11 +27,11 @@ func TestEtcdHostsFile(t *testing.T) {
},
{
"localhost",
&File{
&File{config.File{
Content: fmt.Sprintf("127.0.0.1 %s\n", hostname),
Path: "etc/hosts",
RawFilePermissions: "0644",
},
}},
nil,
},
} {

View File

@@ -8,14 +8,14 @@ import (
"os/exec"
"path"
"strconv"
"github.com/coreos/coreos-cloudinit/config"
)
// File is a top-level structure which embeds its underlying configuration,
// config.File, and provides the system-specific Permissions().
type File struct {
Encoding string
Content string
Owner string
Path string
RawFilePermissions string `yaml:"permissions"`
config.File
}
func (f *File) Permissions() (os.FileMode, error) {

View File

@@ -5,6 +5,8 @@ import (
"os"
"path"
"testing"
"github.com/coreos/coreos-cloudinit/config"
)
func TestWriteFileUnencodedContent(t *testing.T) {
@@ -17,11 +19,11 @@ func TestWriteFileUnencodedContent(t *testing.T) {
fn := "foo"
fullPath := path.Join(dir, fn)
wf := File{
wf := File{config.File{
Path: fn,
Content: "bar",
RawFilePermissions: "0644",
}
}}
path, err := WriteFile(&wf, dir)
if err != nil {
@@ -56,11 +58,11 @@ func TestWriteFileInvalidPermission(t *testing.T) {
}
defer os.RemoveAll(dir)
wf := File{
wf := File{config.File{
Path: path.Join(dir, "tmp", "foo"),
Content: "bar",
RawFilePermissions: "pants",
}
}}
if _, err := WriteFile(&wf, dir); err == nil {
t.Fatalf("Expected error to be raised when writing file with invalid permission")
@@ -77,10 +79,10 @@ func TestWriteFilePermissions(t *testing.T) {
fn := "foo"
fullPath := path.Join(dir, fn)
wf := File{
wf := File{config.File{
Path: fn,
RawFilePermissions: "0755",
}
}}
path, err := WriteFile(&wf, dir)
if err != nil {
@@ -106,11 +108,11 @@ func TestWriteFileEncodedContent(t *testing.T) {
}
defer os.RemoveAll(dir)
wf := File{
wf := File{config.File{
Path: path.Join(dir, "tmp", "foo"),
Content: "",
Encoding: "base64",
}
}}
if _, err := WriteFile(&wf, dir); err == nil {
t.Fatalf("Expected error to be raised when writing file with encoding")

View File

@@ -8,6 +8,7 @@ import (
"strings"
"time"
"github.com/coreos/coreos-cloudinit/config"
"github.com/coreos/coreos-cloudinit/network"
"github.com/coreos/coreos-cloudinit/third_party/github.com/dotcloud/docker/pkg/netlink"
)
@@ -108,11 +109,11 @@ func WriteNetworkdConfigs(interfaces []network.InterfaceGenerator) error {
return nil
}
func writeConfig(filename string, config string) error {
if config == "" {
func writeConfig(filename string, content string) error {
if content == "" {
return nil
}
log.Printf("Writing networkd unit %q\n", filename)
_, err := WriteFile(&File{Content: config, Path: filename}, runtimeNetworkPath)
_, err := WriteFile(&File{config.File{Content: content, Path: filename}}, runtimeNetworkPath)
return err
}

View File

@@ -24,9 +24,9 @@ func (oem OEM) File() (*File, error) {
content += fmt.Sprintf("HOME_URL=%q\n", oem.HomeURL)
content += fmt.Sprintf("BUG_REPORT_URL=%q\n", oem.BugReportURL)
return &File{
return &File{config.File{
Path: path.Join("etc", "oem-release"),
RawFilePermissions: "0644",
Content: content,
}, nil
}}, nil
}

View File

@@ -24,7 +24,7 @@ func TestOEMFile(t *testing.T) {
HomeURL: "https://www.rackspace.com/cloud/servers/",
BugReportURL: "https://github.com/coreos/coreos-overlay",
},
&File{
&File{config.File{
Path: "etc/oem-release",
RawFilePermissions: "0644",
Content: `ID=rackspace
@@ -33,7 +33,7 @@ NAME="Rackspace Cloud Servers"
HOME_URL="https://www.rackspace.com/cloud/servers/"
BUG_REPORT_URL="https://github.com/coreos/coreos-overlay"
`,
},
}},
},
} {
file, err := OEM{tt.config}.File()

View File

@@ -10,6 +10,7 @@ import (
"path/filepath"
"strings"
"github.com/coreos/coreos-cloudinit/config"
"github.com/coreos/coreos-cloudinit/third_party/github.com/coreos/go-systemd/dbus"
)
@@ -35,11 +36,11 @@ func (s *systemd) PlaceUnit(u *Unit, dst string) error {
}
}
file := File{
file := File{config.File{
Path: filepath.Base(dst),
Content: u.Content,
RawFilePermissions: "0644",
}
}}
_, err := WriteFile(&file, dir)
if err != nil {

View File

@@ -90,11 +90,11 @@ func (uc Update) File() (*File, error) {
out += "\n"
}
return &File{
return &File{config.File{
Path: path.Join("etc", "coreos", "update.conf"),
RawFilePermissions: "0644",
Content: out,
}, nil
}}, nil
}
// Units generates units for the cloud-init initializer to act on:

View File

@@ -92,52 +92,52 @@ func TestUpdateFile(t *testing.T) {
},
{
config: config.Update{Group: "master", Server: "http://foo.com"},
file: &File{
file: &File{config.File{
Content: "GROUP=master\nSERVER=http://foo.com\n",
Path: "etc/coreos/update.conf",
RawFilePermissions: "0644",
},
}},
},
{
config: config.Update{RebootStrategy: "best-effort"},
file: &File{
file: &File{config.File{
Content: "REBOOT_STRATEGY=best-effort\n",
Path: "etc/coreos/update.conf",
RawFilePermissions: "0644",
},
}},
},
{
config: config.Update{RebootStrategy: "etcd-lock"},
file: &File{
file: &File{config.File{
Content: "REBOOT_STRATEGY=etcd-lock\n",
Path: "etc/coreos/update.conf",
RawFilePermissions: "0644",
},
}},
},
{
config: config.Update{RebootStrategy: "reboot"},
file: &File{
file: &File{config.File{
Content: "REBOOT_STRATEGY=reboot\n",
Path: "etc/coreos/update.conf",
RawFilePermissions: "0644",
},
}},
},
{
config: config.Update{RebootStrategy: "off"},
file: &File{
file: &File{config.File{
Content: "REBOOT_STRATEGY=off\n",
Path: "etc/coreos/update.conf",
RawFilePermissions: "0644",
},
}},
},
{
config: config.Update{RebootStrategy: "etcd-lock"},
orig: "SERVER=https://example.com\nGROUP=thegroupc\nREBOOT_STRATEGY=awesome",
file: &File{
file: &File{config.File{
Content: "SERVER=https://example.com\nGROUP=thegroupc\nREBOOT_STRATEGY=etcd-lock\n",
Path: "etc/coreos/update.conf",
RawFilePermissions: "0644",
},
}},
},
} {
file, err := Update{tt.config, testReadConfig(tt.orig)}.File()