Skip to content

Commit

Permalink
*test.go: refactor existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prestist committed Mar 1, 2023
1 parent 4d0aa39 commit 9665892
Show file tree
Hide file tree
Showing 3 changed files with 305 additions and 214 deletions.
154 changes: 95 additions & 59 deletions translate/v32tov31/v32tov31_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
114 changes: 72 additions & 42 deletions translate/v33tov32/v33tov32_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
Loading

0 comments on commit 9665892

Please sign in to comment.