mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-05-31 03:54:21 -04:00
cli: use custom byte-slice marshalling for state file (#2460)
* custom byte slice marshalling Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * byte slice compatibility Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * other byte slice compat test Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * add missing dep Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * export byte type alias Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * regenerate exported type Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * test marshal and unmarshal together Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> --------- Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com>
This commit is contained in:
parent
c424ec8825
commit
a8605d7294
13 changed files with 144 additions and 16 deletions
|
@ -14,6 +14,8 @@ import (
|
|||
"github.com/siderolabs/talos/pkg/machinery/config/encoder"
|
||||
"github.com/spf13/afero"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
var defaultState = &State{
|
||||
|
@ -326,3 +328,94 @@ func TestMerge(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalHexBytes(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
in HexBytes
|
||||
expected string
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
in: []byte{0xab, 0xcd, 0xef},
|
||||
expected: "abcdef\n",
|
||||
},
|
||||
"empty": {
|
||||
in: []byte{},
|
||||
expected: "\"\"\n",
|
||||
},
|
||||
"nil": {
|
||||
in: nil,
|
||||
expected: "\"\"\n",
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
actual, err := yaml.Marshal(tc.in)
|
||||
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
assert.Equal(tc.expected, string(actual))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalHexBytes(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
in string
|
||||
expected HexBytes
|
||||
wantErr bool
|
||||
}{
|
||||
"success": {
|
||||
in: "abcdef",
|
||||
expected: []byte{0xab, 0xcd, 0xef},
|
||||
},
|
||||
"empty": {
|
||||
in: "",
|
||||
expected: nil,
|
||||
},
|
||||
"byte slice compat": {
|
||||
in: "[0xab, 0xcd, 0xef]",
|
||||
expected: []byte{0xab, 0xcd, 0xef},
|
||||
},
|
||||
"byte slice compat 2": {
|
||||
in: "[00, 12, 34]",
|
||||
expected: []byte{0x00, 0x0c, 0x22},
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
var actual HexBytes
|
||||
err := yaml.Unmarshal([]byte(tc.in), &actual)
|
||||
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
assert.Equal(tc.expected, actual)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalUnmarshalHexBytes(t *testing.T) {
|
||||
in := HexBytes{0xab, 0xcd, 0xef}
|
||||
expected := "abcdef\n"
|
||||
|
||||
actual, err := yaml.Marshal(in)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, expected, string(actual))
|
||||
|
||||
var actual2 HexBytes
|
||||
err = yaml.Unmarshal(actual, &actual2)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, in, actual2)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue