From 6730cb7227889d3795e16d91703b6e03e61ec67e Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Sun, 21 Sep 2014 12:14:00 -0700 Subject: [PATCH] oem: refactor the config - Seperate the config from File() - Add YAML tags for the fields --- config/oem.go | 9 ++++++ initialize/config.go | 4 +-- initialize/oem.go | 41 --------------------------- initialize/oem_test.go | 63 ------------------------------------------ system/oem.go | 32 +++++++++++++++++++++ system/oem_test.go | 47 +++++++++++++++++++++++++++++++ 6 files changed, 90 insertions(+), 106 deletions(-) create mode 100644 config/oem.go delete mode 100644 initialize/oem.go delete mode 100644 initialize/oem_test.go create mode 100644 system/oem.go create mode 100644 system/oem_test.go diff --git a/config/oem.go b/config/oem.go new file mode 100644 index 0000000..61d0ae9 --- /dev/null +++ b/config/oem.go @@ -0,0 +1,9 @@ +package config + +type OEM struct { + ID string `yaml:"id"` + Name string `yaml:"name"` + VersionID string `yaml:"version-id"` + HomeURL string `yaml:"home-url"` + BugReportURL string `yaml:"bug-report-url"` +} diff --git a/initialize/config.go b/initialize/config.go index 957abfc..4e28579 100644 --- a/initialize/config.go +++ b/initialize/config.go @@ -33,7 +33,7 @@ type CloudConfig struct { Coreos struct { Etcd config.Etcd Fleet config.Fleet - OEM OEMRelease + OEM config.OEM Update UpdateConfig Units []system.Unit } @@ -217,7 +217,7 @@ func Apply(cfg CloudConfig, env *Environment) error { } } - for _, ccf := range []CloudConfigFile{cfg.Coreos.OEM, cfg.Coreos.Update, cfg.ManageEtcHosts} { + for _, ccf := range []CloudConfigFile{system.OEM{cfg.Coreos.OEM}, cfg.Coreos.Update, cfg.ManageEtcHosts} { f, err := ccf.File(env.Root()) if err != nil { return err diff --git a/initialize/oem.go b/initialize/oem.go deleted file mode 100644 index aea43bc..0000000 --- a/initialize/oem.go +++ /dev/null @@ -1,41 +0,0 @@ -package initialize - -import ( - "fmt" - "path" - "strings" - - "github.com/coreos/coreos-cloudinit/system" -) - -type OEMRelease struct { - ID string `yaml:"id"` - Name string `yaml:"name"` - VersionID string `yaml:"version-id"` - HomeURL string `yaml:"home-url"` - BugReportURL string `yaml:"bug-report-url"` -} - -func (oem OEMRelease) String() string { - fields := []string{ - fmt.Sprintf("ID=%s", oem.ID), - fmt.Sprintf("VERSION_ID=%s", oem.VersionID), - fmt.Sprintf("NAME=%q", oem.Name), - fmt.Sprintf("HOME_URL=%q", oem.HomeURL), - fmt.Sprintf("BUG_REPORT_URL=%q", oem.BugReportURL), - } - - return strings.Join(fields, "\n") + "\n" -} - -func (oem OEMRelease) File(root string) (*system.File, error) { - if oem.ID == "" { - return nil, nil - } - - return &system.File{ - Path: path.Join("etc", "oem-release"), - RawFilePermissions: "0644", - Content: oem.String(), - }, nil -} diff --git a/initialize/oem_test.go b/initialize/oem_test.go deleted file mode 100644 index a2eae46..0000000 --- a/initialize/oem_test.go +++ /dev/null @@ -1,63 +0,0 @@ -package initialize - -import ( - "io/ioutil" - "os" - "path" - "testing" - - "github.com/coreos/coreos-cloudinit/system" -) - -func TestOEMReleaseWrittenToDisk(t *testing.T) { - oem := OEMRelease{ - ID: "rackspace", - Name: "Rackspace Cloud Servers", - VersionID: "168.0.0", - HomeURL: "https://www.rackspace.com/cloud/servers/", - BugReportURL: "https://github.com/coreos/coreos-overlay", - } - dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-") - if err != nil { - t.Fatalf("Unable to create tempdir: %v", err) - } - defer os.RemoveAll(dir) - - f, err := oem.File(dir) - if err != nil { - t.Fatalf("Processing of OEMRelease failed: %v", err) - } - if f == nil { - t.Fatalf("OEMRelease returned nil file unexpectedly") - } - - if _, err := system.WriteFile(f, dir); err != nil { - t.Fatalf("Writing of OEMRelease failed: %v", err) - } - - fullPath := path.Join(dir, "etc", "oem-release") - - 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) - } - - expect := `ID=rackspace -VERSION_ID=168.0.0 -NAME="Rackspace Cloud Servers" -HOME_URL="https://www.rackspace.com/cloud/servers/" -BUG_REPORT_URL="https://github.com/coreos/coreos-overlay" -` - if string(contents) != expect { - t.Fatalf("File has incorrect contents") - } -} diff --git a/system/oem.go b/system/oem.go new file mode 100644 index 0000000..ba3634f --- /dev/null +++ b/system/oem.go @@ -0,0 +1,32 @@ +package system + +import ( + "fmt" + "path" + + "github.com/coreos/coreos-cloudinit/config" +) + +// OEM is a top-level structure which embeds its underlying configuration, +// config.OEM, and provides the system-specific File(). +type OEM struct { + config.OEM +} + +func (oem OEM) File(_ string) (*File, error) { + if oem.ID == "" { + return nil, nil + } + + content := fmt.Sprintf("ID=%s\n", oem.ID) + content += fmt.Sprintf("VERSION_ID=%s\n", oem.VersionID) + content += fmt.Sprintf("NAME=%q\n", oem.Name) + content += fmt.Sprintf("HOME_URL=%q\n", oem.HomeURL) + content += fmt.Sprintf("BUG_REPORT_URL=%q\n", oem.BugReportURL) + + return &File{ + Path: path.Join("etc", "oem-release"), + RawFilePermissions: "0644", + Content: content, + }, nil +} diff --git a/system/oem_test.go b/system/oem_test.go new file mode 100644 index 0000000..4a8029e --- /dev/null +++ b/system/oem_test.go @@ -0,0 +1,47 @@ +package system + +import ( + "reflect" + "testing" + + "github.com/coreos/coreos-cloudinit/config" +) + +func TestOEMFile(t *testing.T) { + for _, tt := range []struct { + config config.OEM + file *File + }{ + { + config.OEM{}, + nil, + }, + { + config.OEM{ + ID: "rackspace", + Name: "Rackspace Cloud Servers", + VersionID: "168.0.0", + HomeURL: "https://www.rackspace.com/cloud/servers/", + BugReportURL: "https://github.com/coreos/coreos-overlay", + }, + &File{ + Path: "etc/oem-release", + RawFilePermissions: "0644", + Content: `ID=rackspace +VERSION_ID=168.0.0 +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("") + if err != nil { + t.Errorf("bad error (%q): want %q, got %q", tt.config, nil, err) + } + if !reflect.DeepEqual(tt.file, file) { + t.Errorf("bad file (%q): want %#v, got %#v", tt.config, tt.file, file) + } + } +}