Skip to content

Commit f458feb

Browse files
committed
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.
1 parent 02b3fd2 commit f458feb

3 files changed

Lines changed: 41 additions & 9 deletions

File tree

builtin/providers/ignition/resource_ignition_config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func testIgnition(t *testing.T, input string, assert func(*types.Config) error)
8383
resource.Test(t, resource.TestCase{
8484
Providers: testProviders,
8585
Steps: []resource.TestStep{
86-
resource.TestStep{
86+
{
8787
Config: fmt.Sprintf(testTemplate, input),
8888
Check: check,
8989
},

builtin/providers/ignition/resource_ignition_systemd_unit.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,39 @@ func resourceSystemdUnit() *schema.Resource {
1212
Exists: resourceSystemdUnitExists,
1313
Read: resourceSystemdUnitRead,
1414
Schema: map[string]*schema.Schema{
15-
"name": &schema.Schema{
15+
"name": {
1616
Type: schema.TypeString,
1717
Required: true,
1818
ForceNew: true,
1919
},
20-
"enable": &schema.Schema{
20+
"enable": {
2121
Type: schema.TypeBool,
2222
Optional: true,
2323
Default: true,
2424
ForceNew: true,
2525
},
26-
"mask": &schema.Schema{
26+
"mask": {
2727
Type: schema.TypeBool,
2828
Optional: true,
2929
ForceNew: true,
3030
},
31-
"content": &schema.Schema{
31+
"content": {
3232
Type: schema.TypeString,
3333
Optional: true,
3434
ForceNew: true,
3535
},
36-
"dropin": &schema.Schema{
36+
"dropin": {
3737
Type: schema.TypeList,
3838
Optional: true,
3939
ForceNew: true,
4040
Elem: &schema.Resource{
4141
Schema: map[string]*schema.Schema{
42-
"name": &schema.Schema{
42+
"name": {
4343
Type: schema.TypeString,
4444
Required: true,
4545
ForceNew: true,
4646
},
47-
"content": &schema.Schema{
47+
"content": {
4848
Type: schema.TypeString,
4949
Optional: true,
5050
ForceNew: true,
@@ -100,7 +100,7 @@ func buildSystemdUnit(d *schema.ResourceData, c *cache) (string, error) {
100100
}
101101

102102
if err := validateUnitContent(d.Get("content").(string)); err != nil {
103-
if err != errEmptyUnit || (err == errEmptyUnit && len(dropins) == 0) {
103+
if err != errEmptyUnit {
104104
return "", err
105105
}
106106
}

builtin/providers/ignition/resource_ignition_systemd_unit_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,35 @@ func TestIngnitionSystemdUnitEmptyContentWithDropIn(t *testing.T) {
9494
return nil
9595
})
9696
}
97+
98+
// #11325
99+
func TestIgnitionSystemdUnit_emptyContent(t *testing.T) {
100+
testIgnition(t, `
101+
resource "ignition_systemd_unit" "foo" {
102+
name = "foo.service"
103+
enable = true
104+
}
105+
106+
resource "ignition_config" "test" {
107+
systemd = [
108+
"${ignition_systemd_unit.foo.id}",
109+
]
110+
}
111+
`, func(c *types.Config) error {
112+
if len(c.Systemd.Units) != 1 {
113+
return fmt.Errorf("systemd, found %d", len(c.Systemd.Units))
114+
}
115+
116+
u := c.Systemd.Units[0]
117+
if u.Name != "foo.service" {
118+
return fmt.Errorf("name, expected 'foo.service', found %q", u.Name)
119+
}
120+
if u.Contents != "" {
121+
return fmt.Errorf("expected empty content, found %q", u.Contents)
122+
}
123+
if len(u.DropIns) != 0 {
124+
return fmt.Errorf("expected 0 dropins, found %q", u.DropIns)
125+
}
126+
return nil
127+
})
128+
}

0 commit comments

Comments
 (0)