From 9665892e02e5afae6b7891cc1b7055ee5683863a Mon Sep 17 00:00:00 2001 From: Steven Presti Date: Wed, 1 Mar 2023 13:06:49 -0500 Subject: [PATCH] *test.go: refactor existing tests --- translate/v32tov31/v32tov31_test.go | 154 ++++++++++------- translate/v33tov32/v33tov32_test.go | 114 ++++++++----- translate/v34tov33/v34tov33_test.go | 251 +++++++++++++++------------- 3 files changed, 305 insertions(+), 214 deletions(-) diff --git a/translate/v32tov31/v32tov31_test.go b/translate/v32tov31/v32tov31_test.go index cecca7c..21fea54 100644 --- a/translate/v32tov31/v32tov31_test.go +++ b/translate/v32tov31/v32tov31_test.go @@ -15,91 +15,127 @@ package v32tov31 import ( + "fmt" "testing" "github.com/coreos/ign-converter/util" + types3_1 "github.com/coreos/ignition/v2/config/v3_1/types" types3_2 "github.com/coreos/ignition/v2/config/v3_2/types" - "github.com/stretchr/testify/assert" ) func TestTranslate3_2to3_1(t *testing.T) { - emptyConfig := types3_2.Config{ - Ignition: types3_2.Ignition{ - Version: "3.2.0", - }, - } - - _, err := Translate(emptyConfig) - if err != nil { - t.Fatalf("Failed translation: %v", err) + type in struct { + data types3_2.Config } - res, err := Translate(util.NonexhaustiveConfig3_2) - if err != nil { - t.Fatalf("Failed translation: %v", err) + type out struct { + data types3_1.Config } - assert.Equal(t, util.NonexhaustiveConfig3_1, res) - _, err = Translate(types3_2.Config{ - Ignition: types3_2.Ignition{ - Version: "3.2.0", - }, - Storage: types3_2.Storage{ - Luks: []types3_2.Luks{ - { - Name: "z", - Device: util.StrP("/dev/z"), + tests := []struct { + in in + out out + err error + }{ + { + in: in{data: types3_2.Config{ + Ignition: types3_2.Ignition{ + Version: "3.2.0", }, - }, + }}, + out: out{data: types3_1.Config{ + Ignition: types3_1.Ignition{ + Version: "3.1.0", + }, + }}, }, - }) - assert.Error(t, err) - _, err = Translate(types3_2.Config{ - Ignition: types3_2.Ignition{ - Version: "3.2.0", + { + in: in{data: util.NonexhaustiveConfig3_2}, + out: out{data: util.NonexhaustiveConfig3_1}, }, - Storage: types3_2.Storage{ - Disks: []types3_2.Disk{ - { - Device: "/dev/a", - Partitions: []types3_2.Partition{ + { + in: in{data: types3_2.Config{ + Ignition: types3_2.Ignition{ + Version: "3.2.0", + }, + Storage: types3_2.Storage{ + Luks: []types3_2.Luks{ { - Label: util.StrP("z"), - Resize: util.BoolP(true), + Name: "z", + Device: util.StrP("/dev/z"), }, }, }, }, + }, + out: out{data: types3_1.Config{}}, + err: fmt.Errorf("LUKS is not supported on 3.1"), }, - }) - assert.Error(t, err) - _, err = Translate(types3_2.Config{ - Ignition: types3_2.Ignition{ - Version: "3.2.0", - }, - Passwd: types3_2.Passwd{ - Users: []types3_2.PasswdUser{ - { - Name: "z", - ShouldExist: util.BoolPStrict(false), + { + in: in{data: types3_2.Config{ + Ignition: types3_2.Ignition{ + Version: "3.2.0", }, + Storage: types3_2.Storage{ + Disks: []types3_2.Disk{ + { + Device: "/dev/a", + Partitions: []types3_2.Partition{ + { + Label: util.StrP("z"), + Resize: util.BoolP(true), + }, + }, + }, + }, + }, + }, }, + out: out{data: types3_1.Config{}}, + err: fmt.Errorf("Resize in Storage.Disks.Partitions is not supported on 3.1"), }, - }) - assert.Error(t, err) - _, err = Translate(types3_2.Config{ - Ignition: types3_2.Ignition{ - Version: "3.2.0", + { + in: in{data: types3_2.Config{ + Ignition: types3_2.Ignition{ + Version: "3.2.0", + }, + Passwd: types3_2.Passwd{ + Users: []types3_2.PasswdUser{ + { + Name: "z", + ShouldExist: util.BoolPStrict(false), + }, + }, + }, + }, + }, + out: out{data: types3_1.Config{}}, + err: fmt.Errorf("ShouldExist in Passwd.Users is not supported on 3.1"), }, - Passwd: types3_2.Passwd{ - Groups: []types3_2.PasswdGroup{ - { - Name: "z", - ShouldExist: util.BoolPStrict(false), + { + in: in{data: types3_2.Config{ + Ignition: types3_2.Ignition{ + Version: "3.2.0", }, + Passwd: types3_2.Passwd{ + Groups: []types3_2.PasswdGroup{ + { + Name: "z", + ShouldExist: util.BoolPStrict(false), + }, + }, + }, + }, }, + out: out{data: types3_1.Config{}}, + err: fmt.Errorf("ShouldExist in Passwd.Groups is not supported on 3.1"), }, - }) - assert.Error(t, err) + } + + for i, test := range tests { + result, err := Translate(test.in.data) + assert.Equal(t, test.err, err, "#%d: bad error: want %v, got %v", i, test.err, err) + assert.Equal(t, test.out.data, result, "#%d: bad result, want %v, got %v", i, test.out.data, result) + } } diff --git a/translate/v33tov32/v33tov32_test.go b/translate/v33tov32/v33tov32_test.go index 353f6a9..08d3118 100644 --- a/translate/v33tov32/v33tov32_test.go +++ b/translate/v33tov32/v33tov32_test.go @@ -15,63 +15,93 @@ package v33tov32 import ( + "fmt" "testing" "github.com/coreos/ign-converter/util" - + types3_2 "github.com/coreos/ignition/v2/config/v3_2/types" types3_3 "github.com/coreos/ignition/v2/config/v3_3/types" - "github.com/stretchr/testify/assert" ) -func TestTranslate3_3to3_2(t *testing.T) { - emptyConfig := types3_3.Config{ - Ignition: types3_3.Ignition{ - Version: "3.3.0", - }, - } - - _, err := Translate(emptyConfig) - if err != nil { - t.Fatalf("Failed translation: %v", err) +func TestTranslate3_3to3_23(t *testing.T) { + type in struct { + data types3_3.Config } - res, err := Translate(util.NonexhaustiveConfig3_3) - if err != nil { - t.Fatalf("Failed translation: %v", err) + type out struct { + data types3_2.Config } - assert.Equal(t, util.NonexhaustiveConfig3_2, res) - _, err = Translate(types3_3.Config{ - Ignition: types3_3.Ignition{ - Version: "3.3.0", - }, - KernelArguments: types3_3.KernelArguments{ - ShouldExist: []types3_3.KernelArgument{"foo"}, + tests := []struct { + in in + out out + err error + }{ + { + in: in{data: types3_3.Config{ + Ignition: types3_3.Ignition{ + Version: "3.3.0", + }, + }}, + out: out{data: types3_2.Config{ + Ignition: types3_2.Ignition{ + Version: "3.2.0", + }, + }}, }, - }) - assert.Error(t, err) - _, err = Translate(types3_3.Config{ - Ignition: types3_3.Ignition{ - Version: "3.3.0", + { + in: in{data: util.NonexhaustiveConfig3_3}, + out: out{data: util.NonexhaustiveConfig3_2}, }, - KernelArguments: types3_3.KernelArguments{ - ShouldNotExist: []types3_3.KernelArgument{"foo"}, + { + in: in{data: types3_3.Config{ + Ignition: types3_3.Ignition{ + Version: "3.3.0", + }, + KernelArguments: types3_3.KernelArguments{ + ShouldExist: []types3_3.KernelArgument{"foo"}, + }, + }}, + out: out{data: types3_2.Config{}}, + err: fmt.Errorf("KernelArguments is not supported on 3.2"), }, - }) - assert.Error(t, err) - _, err = Translate(types3_3.Config{ - Ignition: types3_3.Ignition{ - Version: "3.3.0", + { + + in: in{data: types3_3.Config{ + Ignition: types3_3.Ignition{ + Version: "3.3.0", + }, + KernelArguments: types3_3.KernelArguments{ + ShouldNotExist: []types3_3.KernelArgument{"foo"}, + }, + }}, + out: out{data: types3_2.Config{}}, + err: fmt.Errorf("KernelArguments is not supported on 3.2"), }, - Storage: types3_3.Storage{ - Filesystems: []types3_3.Filesystem{ - { - Device: "/dev/foo", - Format: util.StrP("util.None"), + { + + in: in{data: types3_3.Config{ + Ignition: types3_3.Ignition{ + Version: "3.3.0", }, - }, + Storage: types3_3.Storage{ + Filesystems: []types3_3.Filesystem{ + { + Device: "/dev/foo", + Format: util.StrP("util.None"), + }, + }, + }, + }}, + out: out{data: types3_2.Config{}}, + err: fmt.Errorf("Invalid input config:\nerror at $.storage.filesystems.0.format: invalid filesystem format\n"), }, - }) - assert.Error(t, err) + } + for i, test := range tests { + result, err := Translate(test.in.data) + assert.Equal(t, test.err, err, "#%d: bad error: want %v, got %v", i, test.err, err) + assert.Equal(t, test.out.data, result, "#%d: bad result, want %v, got %v", i, test.out.data, result) + } + } diff --git a/translate/v34tov33/v34tov33_test.go b/translate/v34tov33/v34tov33_test.go index 266e9c0..8d726c0 100644 --- a/translate/v34tov33/v34tov33_test.go +++ b/translate/v34tov33/v34tov33_test.go @@ -15,151 +15,176 @@ package v34tov33 import ( + "fmt" "testing" "github.com/coreos/ign-converter/util" + types3_3 "github.com/coreos/ignition/v2/config/v3_3/types" types3_4 "github.com/coreos/ignition/v2/config/v3_4/types" - "github.com/stretchr/testify/assert" ) func TestTranslate3_4to3_3(t *testing.T) { - emptyConfig := types3_4.Config{ - Ignition: types3_4.Ignition{ - Version: "3.4.0", - }, - } - - _, err := Translate(emptyConfig) - if err != nil { - t.Fatalf("Failed translation: %v", err) + type in struct { + data types3_4.Config } - res, err := Translate(util.NonexhaustiveConfig3_4) - if err != nil { - t.Fatalf("Failed translation: %v", err) + type out struct { + data types3_3.Config } - assert.Equal(t, util.DowntranslateConfig3_3, res) - _, err = Translate(types3_4.Config{ - Ignition: types3_4.Ignition{ - Version: "3.4.0", + tests := []struct { + in in + out out + err error + }{ + { + in: in{data: types3_4.Config{ + Ignition: types3_4.Ignition{ + Version: "3.4.0", + }, + }}, + out: out{data: types3_3.Config{ + Ignition: types3_3.Ignition{ + Version: "3.3.0", + }, + }}, }, - Storage: types3_4.Storage{ - Luks: []types3_4.Luks{ - { - Device: util.StrP("/dev/sda"), - Clevis: types3_4.Clevis{ - Tang: []types3_4.Tang{ - { - URL: "http://example.com", - Thumbprint: util.StrP("z"), - Advertisement: util.StrP("{\"payload\": \"eyJrZXlzIjogW3siYWxnIjogIkVTNTEyIiwgImt0eSI6ICJFQyIsICJjcnYiOiAiUC01MjEiLCAieCI6ICJBRGFNajJmazNob21CWTF5WElSQ21uRk92cmUzOFZjdHMwTnNHeDZ6RWNxdEVXcjh5ekhUMkhfa2hjNGpSa19FQWFLdjNrd2RjZ05sOTBLcGhfMGYyQ190IiwgInkiOiAiQUZ2d1UyeGJ5T1RydWo0V1NtcVlqN2wtcUVTZmhWakdCNTI1Q2d6d0NoZUZRRTBvb1o3STYyamt3NkRKQ05yS3VPUDRsSEhicm8tYXhoUk9MSXNJVExvNCIsICJrZXlfb3BzIjogWyJ2ZXJpZnkiXX0sIHsiYWxnIjogIkVDTVIiLCAia3R5IjogIkVDIiwgImNydiI6ICJQLTUyMSIsICJ4IjogIkFOZDVYcTFvZklUbTdNWG16OUY0VVRSYmRNZFNIMl9XNXczTDVWZ0w3b3hwdmpyM0hkLXNLNUVqd3A1V2swMnJMb3NXVUJjYkZyZEhjZFJTTVJoZlVFTFIiLCAieSI6ICJBRVVaVlVZWkFBY2hVcmdoX3poaTV3SUUzeTEycGwzeWhqUk5LcGpSdW9tUFhKaDhRaFhXRmRWZEtMUlEwX1lwUjNOMjNSUk1pU1lvWlg0Qm42QnlrQVBMIiwgImtleV9vcHMiOiBbImRlcml2ZUtleSJdfV19\", \"protected\": \"eyJhbGciOiJFUzUxMiIsImN0eSI6Imp3ay1zZXQranNvbiJ9\", \"signature\": \"APHfSyVzLwELwG0pMJyIP74gWvhHUvDtv0SESBxA2uOdSXq76IdWHW2xvCZDdlNan8pnqUvEedPZjf_vdKBw9MTXAPMkRxVnu64HepKwlrzzm_zG2R4CHpoCOsGgjH9-acYxg-Vha63oMojv3_bV0VHg-NbzNLaxietgYplstvcNIwkv\"}"), + { + in: in{data: util.NonexhaustiveConfig3_4}, + out: out{data: util.DowntranslateConfig3_3}, + }, + { + in: in{data: types3_4.Config{ + Ignition: types3_4.Ignition{ + Version: "3.4.0", + }, + Storage: types3_4.Storage{ + Luks: []types3_4.Luks{ + { + Device: util.StrP("/dev/sda"), + Clevis: types3_4.Clevis{ + Tang: []types3_4.Tang{ + { + URL: "http://example.com", + Thumbprint: util.StrP("z"), + Advertisement: util.StrP("{\"payload\": \"eyJrZXlzIjogW3siYWxnIjogIkVTNTEyIiwgImt0eSI6ICJFQyIsICJjcnYiOiAiUC01MjEiLCAieCI6ICJBRGFNajJmazNob21CWTF5WElSQ21uRk92cmUzOFZjdHMwTnNHeDZ6RWNxdEVXcjh5ekhUMkhfa2hjNGpSa19FQWFLdjNrd2RjZ05sOTBLcGhfMGYyQ190IiwgInkiOiAiQUZ2d1UyeGJ5T1RydWo0V1NtcVlqN2wtcUVTZmhWakdCNTI1Q2d6d0NoZUZRRTBvb1o3STYyamt3NkRKQ05yS3VPUDRsSEhicm8tYXhoUk9MSXNJVExvNCIsICJrZXlfb3BzIjogWyJ2ZXJpZnkiXX0sIHsiYWxnIjogIkVDTVIiLCAia3R5IjogIkVDIiwgImNydiI6ICJQLTUyMSIsICJ4IjogIkFOZDVYcTFvZklUbTdNWG16OUY0VVRSYmRNZFNIMl9XNXczTDVWZ0w3b3hwdmpyM0hkLXNLNUVqd3A1V2swMnJMb3NXVUJjYkZyZEhjZFJTTVJoZlVFTFIiLCAieSI6ICJBRVVaVlVZWkFBY2hVcmdoX3poaTV3SUUzeTEycGwzeWhqUk5LcGpSdW9tUFhKaDhRaFhXRmRWZEtMUlEwX1lwUjNOMjNSUk1pU1lvWlg0Qm42QnlrQVBMIiwgImtleV9vcHMiOiBbImRlcml2ZUtleSJdfV19\", \"protected\": \"eyJhbGciOiJFUzUxMiIsImN0eSI6Imp3ay1zZXQranNvbiJ9\", \"signature\": \"APHfSyVzLwELwG0pMJyIP74gWvhHUvDtv0SESBxA2uOdSXq76IdWHW2xvCZDdlNan8pnqUvEedPZjf_vdKBw9MTXAPMkRxVnu64HepKwlrzzm_zG2R4CHpoCOsGgjH9-acYxg-Vha63oMojv3_bV0VHg-NbzNLaxietgYplstvcNIwkv\"}"), + }, + }, }, }, }, }, - }, + }}, + err: fmt.Errorf("Invalid input config: tang offline provisioning is not supported in spec v3.3"), }, - }) - assert.Error(t, err) - - _, err = Translate(types3_4.Config{ - Ignition: types3_4.Ignition{ - Version: "3.4.0", - }, - Storage: types3_4.Storage{ - Luks: []types3_4.Luks{ - { - Device: util.StrP("/dev/sda"), - Discard: util.BoolP(true), + { + in: in{data: types3_4.Config{ + Ignition: types3_4.Ignition{ + Version: "3.4.0", }, - }, - }, - }) - assert.Error(t, err) - - _, err = Translate(types3_4.Config{ - Ignition: types3_4.Ignition{ - Version: "3.4.0", - }, - Storage: types3_4.Storage{ - Luks: []types3_4.Luks{ - { - Device: util.StrP("/dev/sda"), - OpenOptions: []types3_4.OpenOption{"foo"}, + Storage: types3_4.Storage{ + Luks: []types3_4.Luks{ + { + Device: util.StrP("/dev/sda"), + Discard: util.BoolP(true), + }, + }, }, - }, - }, - }) - assert.Error(t, err) - - _, err = Translate(types3_4.Config{ - Ignition: types3_4.Ignition{ - Version: "3.4.0", + }}, + err: fmt.Errorf("Invalid input config: luks discard is not supported in spec v3.3"), }, - Storage: types3_4.Storage{ - Files: []types3_4.File{ - { - Node: types3_4.Node{ - Path: "/root/file.txt", - }, - - FileEmbedded1: types3_4.FileEmbedded1{ - Mode: util.IntP(01777), + { + in: in{data: types3_4.Config{ + Ignition: types3_4.Ignition{ + Version: "3.4.0", + }, + Storage: types3_4.Storage{ + Luks: []types3_4.Luks{ + { + Device: util.StrP("/dev/sda"), + OpenOptions: []types3_4.OpenOption{"foo"}, + }, }, }, - }, + }}, + err: fmt.Errorf("Invalid input config: luks openOptions is not supported in spec v3.3"), }, - }) - - assert.Error(t, err) + { + in: in{data: types3_4.Config{ + Ignition: types3_4.Ignition{ + Version: "3.4.0", + }, + Storage: types3_4.Storage{ + Files: []types3_4.File{ + { + Node: types3_4.Node{ + Path: "/root/file.txt", + }, - _, err = Translate(types3_4.Config{ - Ignition: types3_4.Ignition{ - Version: "3.4.0", - }, - Storage: types3_4.Storage{ - Directories: []types3_4.Directory{ - { - Node: types3_4.Node{ - Path: "/rootdir", - Overwrite: util.BoolP(true), - User: types3_4.NodeUser{ - ID: util.IntP(1000), - }, - Group: types3_4.NodeGroup{ - Name: util.StrP("groupname"), + FileEmbedded1: types3_4.FileEmbedded1{ + Mode: util.IntP(01777), + }, }, }, - DirectoryEmbedded1: types3_4.DirectoryEmbedded1{ - Mode: util.IntP(01777), - }, }, - }, + }}, + err: fmt.Errorf("Invalid input config: special mode bits are not supported in spec v3.3"), }, - }) - - assert.Error(t, err) - - _, err = Translate(types3_4.Config{ - Ignition: types3_4.Ignition{ - Version: "3.4.0", - }, - Storage: types3_4.Storage{ - Files: []types3_4.File{ - { - Node: types3_4.Node{ - Path: "/path", + { + in: in{data: types3_4.Config{ + Ignition: types3_4.Ignition{ + Version: "3.4.0", + }, + Storage: types3_4.Storage{ + Directories: []types3_4.Directory{ + { + Node: types3_4.Node{ + Path: "/rootdir", + Overwrite: util.BoolP(true), + User: types3_4.NodeUser{ + ID: util.IntP(1000), + }, + Group: types3_4.NodeGroup{ + Name: util.StrP("groupname"), + }, + }, + DirectoryEmbedded1: types3_4.DirectoryEmbedded1{ + Mode: util.IntP(01777), + }, + }, }, - FileEmbedded1: types3_4.FileEmbedded1{ - Contents: types3_4.Resource{ - Source: util.StrP("arn:aws:s3:us-west-1:123456789012:accesspoint/test/object/some/path"), + }, + }}, + err: fmt.Errorf("Invalid input config: special mode bits are not supported in spec v3.3"), + }, + { + in: in{data: types3_4.Config{ + Ignition: types3_4.Ignition{ + Version: "3.4.0", + }, + Storage: types3_4.Storage{ + Files: []types3_4.File{ + { + Node: types3_4.Node{ + Path: "/path", + }, + FileEmbedded1: types3_4.FileEmbedded1{ + Contents: types3_4.Resource{ + Source: util.StrP("arn:aws:s3:us-west-1:123456789012:accesspoint/test/object/some/path"), + }, + }, }, }, }, - }, + }}, + err: fmt.Errorf("Invalid input config: arn: scheme for s3 is not supported in spec v3.3"), }, - }) - assert.Error(t, err) + } + + for i, test := range tests { + result, err := Translate(test.in.data) + assert.Equal(t, test.err, err, "#%d: bad error: want %v, got %v", i, test.err, err) + assert.Equal(t, test.out.data, result, "#%d: bad result, want %v, got %v", i, test.out.data, result) + } }