Merge pull request #8 from bcwaldon/cloud-config-header

feat(serialization): Add #cloud-config header
This commit is contained in:
Brian Waldon 2014-03-12 17:12:25 -07:00
commit 6a6897831a
2 changed files with 17 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package cloudinit package cloudinit
import ( import (
"fmt"
"log" "log"
"launchpad.net/goyaml" "launchpad.net/goyaml"
@ -26,11 +27,14 @@ func NewCloudConfig(contents []byte) (*CloudConfig, error) {
func (cc CloudConfig) String() string { func (cc CloudConfig) String() string {
bytes, err := goyaml.Marshal(cc) bytes, err := goyaml.Marshal(cc)
if err == nil { if err != nil {
return string(bytes)
} else {
return "" return ""
} }
stringified := string(bytes)
stringified = fmt.Sprintf("#cloud-config\n%s", stringified)
return stringified
} }
func ApplyCloudConfig(cfg CloudConfig, sshKeyName string) error { func ApplyCloudConfig(cfg CloudConfig, sshKeyName string) error {

View File

@ -1,6 +1,7 @@
package cloudinit package cloudinit
import ( import (
"strings"
"testing" "testing"
) )
@ -146,3 +147,12 @@ ssh_authorized_keys:
t.Error("Parsed incorrect number of SSH keys") t.Error("Parsed incorrect number of SSH keys")
} }
} }
func TestCloudConfigSerializationHeader(t *testing.T) {
cfg, _ := NewCloudConfig([]byte{})
contents := cfg.String()
header := strings.SplitN(contents, "\n", 2)[0]
if header != "#cloud-config" {
t.Fatalf("Serialized config did not have expected header")
}
}