ignition_filesystem: explicit option to create the filesystem (Fix #12152) (#12980)

This commit is contained in:
Máximo Cuadros 2017-03-23 10:02:54 +01:00 committed by Paul Stack
parent 74cf9ac5e1
commit b31ff955b6
4 changed files with 79 additions and 6 deletions

View File

@ -3,6 +3,7 @@ package ignition
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"regexp"
"testing" "testing"
"github.com/coreos/ignition/config/types" "github.com/coreos/ignition/config/types"
@ -67,6 +68,18 @@ func TestIngnitionFileAppend(t *testing.T) {
}) })
} }
func testIgnitionError(t *testing.T, input string, expectedErr *regexp.Regexp) {
resource.Test(t, resource.TestCase{
Providers: testProviders,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testTemplate, input),
ExpectError: expectedErr,
},
},
})
}
func testIgnition(t *testing.T, input string, assert func(*types.Config) error) { func testIgnition(t *testing.T, input string, assert func(*types.Config) error) {
check := func(s *terraform.State) error { check := func(s *terraform.State) error {
got := s.RootModule().Outputs["rendered"].Value.(string) got := s.RootModule().Outputs["rendered"].Value.(string)

View File

@ -34,6 +34,11 @@ func resourceFilesystem() *schema.Resource {
Required: true, Required: true,
ForceNew: true, ForceNew: true,
}, },
"create": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"force": &schema.Schema{ "force": &schema.Schema{
Type: schema.TypeBool, Type: schema.TypeBool,
Optional: true, Optional: true,
@ -84,14 +89,19 @@ func buildFilesystem(d *schema.ResourceData, c *cache) (string, error) {
Format: types.FilesystemFormat(d.Get("mount.0.format").(string)), Format: types.FilesystemFormat(d.Get("mount.0.format").(string)),
} }
create, hasCreate := d.GetOk("mount.0.create")
force, hasForce := d.GetOk("mount.0.force") force, hasForce := d.GetOk("mount.0.force")
options, hasOptions := d.GetOk("mount.0.options") options, hasOptions := d.GetOk("mount.0.options")
if hasOptions || hasForce { if hasCreate || hasOptions || hasForce {
mount.Create = &types.FilesystemCreate{ mount.Create = &types.FilesystemCreate{
Force: force.(bool), Force: force.(bool),
Options: castSliceInterface(options.([]interface{})), Options: castSliceInterface(options.([]interface{})),
} }
} }
if !create.(bool) && (hasForce || hasOptions) {
return "", fmt.Errorf("create should be true when force or options is used")
}
} }
var path *types.Path var path *types.Path

View File

@ -2,6 +2,7 @@ package ignition
import ( import (
"fmt" "fmt"
"regexp"
"testing" "testing"
"github.com/coreos/ignition/config/types" "github.com/coreos/ignition/config/types"
@ -22,11 +23,21 @@ func TestIngnitionFilesystem(t *testing.T) {
} }
} }
data "ignition_filesystem" "baz" {
name = "baz"
mount {
device = "/baz"
format = "ext4"
create = true
}
}
data "ignition_filesystem" "bar" { data "ignition_filesystem" "bar" {
name = "bar" name = "bar"
mount { mount {
device = "/bar" device = "/bar"
format = "ext4" format = "ext4"
create = true
force = true force = true
options = ["rw"] options = ["rw"]
} }
@ -36,11 +47,12 @@ func TestIngnitionFilesystem(t *testing.T) {
filesystems = [ filesystems = [
"${data.ignition_filesystem.foo.id}", "${data.ignition_filesystem.foo.id}",
"${data.ignition_filesystem.qux.id}", "${data.ignition_filesystem.qux.id}",
"${data.ignition_filesystem.baz.id}",
"${data.ignition_filesystem.bar.id}", "${data.ignition_filesystem.bar.id}",
] ]
} }
`, func(c *types.Config) error { `, func(c *types.Config) error {
if len(c.Storage.Filesystems) != 3 { if len(c.Storage.Filesystems) != 4 {
return fmt.Errorf("disks, found %d", len(c.Storage.Filesystems)) return fmt.Errorf("disks, found %d", len(c.Storage.Filesystems))
} }
@ -75,6 +87,23 @@ func TestIngnitionFilesystem(t *testing.T) {
} }
f = c.Storage.Filesystems[2] f = c.Storage.Filesystems[2]
if f.Name != "baz" {
return fmt.Errorf("name, found %q", f.Name)
}
if f.Mount.Device != "/baz" {
return fmt.Errorf("mount.0.device, found %q", f.Mount.Device)
}
if f.Mount.Format != "ext4" {
return fmt.Errorf("mount.0.format, found %q", f.Mount.Format)
}
if f.Mount.Create.Force != false {
return fmt.Errorf("mount.0.force, found %t", f.Mount.Create.Force)
}
f = c.Storage.Filesystems[3]
if f.Name != "bar" { if f.Name != "bar" {
return fmt.Errorf("name, found %q", f.Name) return fmt.Errorf("name, found %q", f.Name)
} }
@ -98,3 +127,22 @@ func TestIngnitionFilesystem(t *testing.T) {
return nil return nil
}) })
} }
func TestIngnitionFilesystemMissingCreate(t *testing.T) {
testIgnitionError(t, `
data "ignition_filesystem" "bar" {
name = "bar"
mount {
device = "/bar"
format = "ext4"
force = true
}
}
data "ignition_config" "test" {
filesystems = [
"${data.ignition_filesystem.bar.id}",
]
}
`, regexp.MustCompile("create should be true when force or options is used"))
}

View File

@ -18,7 +18,7 @@ data "ignition_filesystem" "foo" {
mount { mount {
device = "/dev/disk/by-label/ROOT" device = "/dev/disk/by-label/ROOT"
format = "xfs" format = "xfs"
force = true create = true
options = ["-L", "ROOT"] options = ["-L", "ROOT"]
} }
} }
@ -41,9 +41,11 @@ The `mount` block supports:
* `format` - (Required) The filesystem format (ext4, btrfs, or xfs). * `format` - (Required) The filesystem format (ext4, btrfs, or xfs).
* `force` - (Optional) Whether or not the create operation shall overwrite an existing filesystem. * `create` - (Optional) Indicates if the filesystem shall be created.
* `options` - (Optional) Any additional options to be passed to the format-specific mkfs utility. * `force` - (Optional) Whether or not the create operation shall overwrite an existing filesystem. Only allowed if the filesystem is being created.
* `options` - (Optional) Any additional options to be passed to the format-specific mkfs utility. Only allowed if the filesystem is being created
## Attributes Reference ## Attributes Reference