Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions machine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,9 @@ func (mpr *MachinePersistRootfs) UnmarshalJSON(raw []byte) error {
}

type MachineRootfs struct {
Persist MachinePersistRootfs `toml:"persist,omitempty" json:"persist,omitempty" enums:"never,always,restart"`
SizeGB uint64 `toml:"size_gb,omitempty" json:"size_gb,omitempty"`
Persist MachinePersistRootfs `toml:"persist,omitempty" json:"persist,omitempty" enums:"never,always,restart"`
SizeGB uint64 `toml:"size_gb,omitempty" json:"size_gb,omitempty"`
FsSizeGB uint64 `toml:"fs_size_gb,omitempty" json:"fs_size_gb,omitempty"`
}

// @description The Machine restart policy defines whether and how flyd restarts a Machine after its main process exits. See https://fly.io/docs/machines/guides-examples/machine-restart-policy/.
Expand Down
34 changes: 25 additions & 9 deletions machine_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,16 @@ func TestMachineRootfsJSON(t *testing.T) {
input: MachineConfig{Rootfs: &MachineRootfs{Persist: MachinePersistRootfsRestart}},
output: `{"init":{},"rootfs":{"persist":"restart"}}`,
},
{
name: "rootfs with fs_size_gb",
input: MachineConfig{Rootfs: &MachineRootfs{Persist: MachinePersistRootfsAlways, SizeGB: 10, FsSizeGB: 8}},
output: `{"init":{},"rootfs":{"persist":"always","size_gb":10,"fs_size_gb":8}}`,
},
{
name: "rootfs with fs_size_gb only",
input: MachineConfig{Rootfs: &MachineRootfs{FsSizeGB: 5}},
output: `{"init":{},"rootfs":{"fs_size_gb":5}}`,
},
{
name: "nil rootfs omitted",
input: MachineConfig{},
Expand All @@ -338,23 +348,26 @@ func TestMachineRootfsJSON(t *testing.T) {

t.Run("unmarshal", func(t *testing.T) {
cases := []struct {
name string
input string
persist MachinePersistRootfs
sizeGB uint64
name string
input string
persist MachinePersistRootfs
sizeGB uint64
fsSizeGB uint64
}{
{"persist and size", `{"rootfs":{"persist":"always","size_gb":10}}`, MachinePersistRootfsAlways, 10},
{"persist only", `{"rootfs":{"persist":"restart"}}`, MachinePersistRootfsRestart, 0},
{"size only", `{"rootfs":{"size_gb":5}}`, MachinePersistRootfsNone, 5},
{"no rootfs", `{}`, MachinePersistRootfsNone, 0},
{"persist and size", `{"rootfs":{"persist":"always","size_gb":10}}`, MachinePersistRootfsAlways, 10, 0},
{"persist only", `{"rootfs":{"persist":"restart"}}`, MachinePersistRootfsRestart, 0, 0},
{"size only", `{"rootfs":{"size_gb":5}}`, MachinePersistRootfsNone, 5, 0},
{"fs_size_gb only", `{"rootfs":{"fs_size_gb":5}}`, MachinePersistRootfsNone, 0, 5},
{"all fields", `{"rootfs":{"persist":"always","size_gb":10,"fs_size_gb":8}}`, MachinePersistRootfsAlways, 10, 8},
{"no rootfs", `{}`, MachinePersistRootfsNone, 0, 0},
}
for _, tc := range cases {
var mc MachineConfig
if err := json.Unmarshal([]byte(tc.input), &mc); err != nil {
t.Errorf("%s: unexpected error: %v", tc.name, err)
continue
}
if tc.persist == MachinePersistRootfsNone && tc.sizeGB == 0 {
if tc.persist == MachinePersistRootfsNone && tc.sizeGB == 0 && tc.fsSizeGB == 0 {
if mc.Rootfs != nil {
t.Errorf("%s: expected nil rootfs, got %+v", tc.name, mc.Rootfs)
}
Expand All @@ -370,6 +383,9 @@ func TestMachineRootfsJSON(t *testing.T) {
if mc.Rootfs.SizeGB != tc.sizeGB {
t.Errorf("%s: size_gb got %d, want %d", tc.name, mc.Rootfs.SizeGB, tc.sizeGB)
}
if mc.Rootfs.FsSizeGB != tc.fsSizeGB {
t.Errorf("%s: fs_size_gb got %d, want %d", tc.name, mc.Rootfs.FsSizeGB, tc.fsSizeGB)
}
}
})
}
Expand Down