From f458feb055584f81ca7d66127073f407fc10fad9 Mon Sep 17 00:00:00 2001 From: Jake Champlin Date: Sat, 21 Jan 2017 00:28:04 -0500 Subject: [PATCH] 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. --- .../ignition/resource_ignition_config_test.go | 2 +- .../resource_ignition_systemd_unit.go | 16 +++++----- .../resource_ignition_systemd_unit_test.go | 32 +++++++++++++++++++ 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/builtin/providers/ignition/resource_ignition_config_test.go b/builtin/providers/ignition/resource_ignition_config_test.go index 083d37301..cd56e1109 100644 --- a/builtin/providers/ignition/resource_ignition_config_test.go +++ b/builtin/providers/ignition/resource_ignition_config_test.go @@ -83,7 +83,7 @@ func testIgnition(t *testing.T, input string, assert func(*types.Config) error) resource.Test(t, resource.TestCase{ Providers: testProviders, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: fmt.Sprintf(testTemplate, input), Check: check, }, diff --git a/builtin/providers/ignition/resource_ignition_systemd_unit.go b/builtin/providers/ignition/resource_ignition_systemd_unit.go index 819d6331f..c763a5ad2 100644 --- a/builtin/providers/ignition/resource_ignition_systemd_unit.go +++ b/builtin/providers/ignition/resource_ignition_systemd_unit.go @@ -12,39 +12,39 @@ func resourceSystemdUnit() *schema.Resource { Exists: resourceSystemdUnitExists, Read: resourceSystemdUnitRead, Schema: map[string]*schema.Schema{ - "name": &schema.Schema{ + "name": { Type: schema.TypeString, Required: true, ForceNew: true, }, - "enable": &schema.Schema{ + "enable": { Type: schema.TypeBool, Optional: true, Default: true, ForceNew: true, }, - "mask": &schema.Schema{ + "mask": { Type: schema.TypeBool, Optional: true, ForceNew: true, }, - "content": &schema.Schema{ + "content": { Type: schema.TypeString, Optional: true, ForceNew: true, }, - "dropin": &schema.Schema{ + "dropin": { Type: schema.TypeList, Optional: true, ForceNew: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ - "name": &schema.Schema{ + "name": { Type: schema.TypeString, Required: true, ForceNew: true, }, - "content": &schema.Schema{ + "content": { Type: schema.TypeString, Optional: 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 != errEmptyUnit || (err == errEmptyUnit && len(dropins) == 0) { + if err != errEmptyUnit { return "", err } } diff --git a/builtin/providers/ignition/resource_ignition_systemd_unit_test.go b/builtin/providers/ignition/resource_ignition_systemd_unit_test.go index 01b766607..6e604919f 100644 --- a/builtin/providers/ignition/resource_ignition_systemd_unit_test.go +++ b/builtin/providers/ignition/resource_ignition_systemd_unit_test.go @@ -94,3 +94,35 @@ func TestIngnitionSystemdUnitEmptyContentWithDropIn(t *testing.T) { 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 + }) +}