@@ -303,6 +303,77 @@ func TestMachinePersistRootfsUnmarshalJSON(t *testing.T) {
303303 }
304304}
305305
306+ func TestMachineRootfsJSON (t * testing.T ) {
307+ t .Run ("marshal" , func (t * testing.T ) {
308+ cases := []struct {
309+ name string
310+ input MachineConfig
311+ output string
312+ }{
313+ {
314+ name : "rootfs with persist and size" ,
315+ input : MachineConfig {Rootfs : & MachineRootfs {Persist : MachinePersistRootfsAlways , SizeGB : 10 }},
316+ output : `{"init":{},"rootfs":{"persist":"always","size_gb":10}}` ,
317+ },
318+ {
319+ name : "rootfs with persist only" ,
320+ input : MachineConfig {Rootfs : & MachineRootfs {Persist : MachinePersistRootfsRestart }},
321+ output : `{"init":{},"rootfs":{"persist":"restart"}}` ,
322+ },
323+ {
324+ name : "nil rootfs omitted" ,
325+ input : MachineConfig {},
326+ output : `{"init":{}}` ,
327+ },
328+ }
329+ for _ , tc := range cases {
330+ b , err := json .Marshal (tc .input )
331+ if err != nil {
332+ t .Errorf ("%s: unexpected error: %v" , tc .name , err )
333+ } else if string (b ) != tc .output {
334+ t .Errorf ("%s: got %s, want %s" , tc .name , string (b ), tc .output )
335+ }
336+ }
337+ })
338+
339+ t .Run ("unmarshal" , func (t * testing.T ) {
340+ cases := []struct {
341+ name string
342+ input string
343+ persist MachinePersistRootfs
344+ sizeGB uint64
345+ }{
346+ {"persist and size" , `{"rootfs":{"persist":"always","size_gb":10}}` , MachinePersistRootfsAlways , 10 },
347+ {"persist only" , `{"rootfs":{"persist":"restart"}}` , MachinePersistRootfsRestart , 0 },
348+ {"size only" , `{"rootfs":{"size_gb":5}}` , MachinePersistRootfsNone , 5 },
349+ {"no rootfs" , `{}` , MachinePersistRootfsNone , 0 },
350+ }
351+ for _ , tc := range cases {
352+ var mc MachineConfig
353+ if err := json .Unmarshal ([]byte (tc .input ), & mc ); err != nil {
354+ t .Errorf ("%s: unexpected error: %v" , tc .name , err )
355+ continue
356+ }
357+ if tc .persist == MachinePersistRootfsNone && tc .sizeGB == 0 {
358+ if mc .Rootfs != nil {
359+ t .Errorf ("%s: expected nil rootfs, got %+v" , tc .name , mc .Rootfs )
360+ }
361+ continue
362+ }
363+ if mc .Rootfs == nil {
364+ t .Errorf ("%s: expected non-nil rootfs" , tc .name )
365+ continue
366+ }
367+ if mc .Rootfs .Persist != tc .persist {
368+ t .Errorf ("%s: persist got %v, want %v" , tc .name , mc .Rootfs .Persist , tc .persist )
369+ }
370+ if mc .Rootfs .SizeGB != tc .sizeGB {
371+ t .Errorf ("%s: size_gb got %d, want %d" , tc .name , mc .Rootfs .SizeGB , tc .sizeGB )
372+ }
373+ }
374+ })
375+ }
376+
306377func TestMachineAutostopUnmarshalJSON (t * testing.T ) {
307378 type testcase struct {
308379 input string
0 commit comments