provider/ignition: Fix systemd unit errors

According to the coreos [documentation](https://coreos.com/ignition/docs/latest/configuration.html), systemd units only require the name attribute per each unit. This can also be validated with the CoreOS config validator. This change allows the `ignition_systemd_unit` resource to no longer fail if given an empty `content` and `dropin`.

Also adds a test to cover this use case.
This commit is contained in:
Jake Champlin 2017-01-21 00:28:04 -05:00
parent 02b3fd289a
commit f458feb055
No known key found for this signature in database
GPG Key ID: DC31F41958EF4AC2
3 changed files with 41 additions and 9 deletions

View File

@ -83,7 +83,7 @@ func testIgnition(t *testing.T, input string, assert func(*types.Config) error)
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
Providers: testProviders, Providers: testProviders,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ {
Config: fmt.Sprintf(testTemplate, input), Config: fmt.Sprintf(testTemplate, input),
Check: check, Check: check,
}, },

View File

@ -12,39 +12,39 @@ func resourceSystemdUnit() *schema.Resource {
Exists: resourceSystemdUnitExists, Exists: resourceSystemdUnitExists,
Read: resourceSystemdUnitRead, Read: resourceSystemdUnitRead,
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"name": &schema.Schema{ "name": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
ForceNew: true, ForceNew: true,
}, },
"enable": &schema.Schema{ "enable": {
Type: schema.TypeBool, Type: schema.TypeBool,
Optional: true, Optional: true,
Default: true, Default: true,
ForceNew: true, ForceNew: true,
}, },
"mask": &schema.Schema{ "mask": {
Type: schema.TypeBool, Type: schema.TypeBool,
Optional: true, Optional: true,
ForceNew: true, ForceNew: true,
}, },
"content": &schema.Schema{ "content": {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
ForceNew: true, ForceNew: true,
}, },
"dropin": &schema.Schema{ "dropin": {
Type: schema.TypeList, Type: schema.TypeList,
Optional: true, Optional: true,
ForceNew: true, ForceNew: true,
Elem: &schema.Resource{ Elem: &schema.Resource{
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"name": &schema.Schema{ "name": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
ForceNew: true, ForceNew: true,
}, },
"content": &schema.Schema{ "content": {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
ForceNew: true, ForceNew: true,
@ -100,7 +100,7 @@ func buildSystemdUnit(d *schema.ResourceData, c *cache) (string, error) {
} }
if err := validateUnitContent(d.Get("content").(string)); err != nil { if err := validateUnitContent(d.Get("content").(string)); err != nil {
if err != errEmptyUnit || (err == errEmptyUnit && len(dropins) == 0) { if err != errEmptyUnit {
return "", err return "", err
} }
} }

View File

@ -94,3 +94,35 @@ func TestIngnitionSystemdUnitEmptyContentWithDropIn(t *testing.T) {
return nil return nil
}) })
} }
// #11325
func TestIgnitionSystemdUnit_emptyContent(t *testing.T) {
testIgnition(t, `
resource "ignition_systemd_unit" "foo" {
name = "foo.service"
enable = true
}
resource "ignition_config" "test" {
systemd = [
"${ignition_systemd_unit.foo.id}",
]
}
`, func(c *types.Config) error {
if len(c.Systemd.Units) != 1 {
return fmt.Errorf("systemd, found %d", len(c.Systemd.Units))
}
u := c.Systemd.Units[0]
if u.Name != "foo.service" {
return fmt.Errorf("name, expected 'foo.service', found %q", u.Name)
}
if u.Contents != "" {
return fmt.Errorf("expected empty content, found %q", u.Contents)
}
if len(u.DropIns) != 0 {
return fmt.Errorf("expected 0 dropins, found %q", u.DropIns)
}
return nil
})
}