talos

Форк
0
/
meta_test.go 
106 строк · 2.1 Кб
1
// This Source Code Form is subject to the terms of the Mozilla Public
2
// License, v. 2.0. If a copy of the MPL was not distributed with this
3
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4

5
package meta_test
6

7
import (
8
	"fmt"
9
	"strings"
10
	"testing"
11

12
	"github.com/stretchr/testify/assert"
13
	"github.com/stretchr/testify/require"
14

15
	"github.com/siderolabs/talos/pkg/machinery/meta"
16
)
17

18
func TestValue(t *testing.T) {
19
	t.Parallel()
20

21
	var v meta.Value
22

23
	require.NoError(t, v.Parse("10=foo"))
24

25
	assert.Equal(t, uint8(10), v.Key)
26
	assert.Equal(t, "foo", v.Value)
27

28
	assert.Equal(t, "0xa=foo", v.String())
29

30
	var v2 meta.Value
31

32
	require.NoError(t, v2.Parse(v.String()))
33

34
	assert.Equal(t, v, v2)
35
}
36

37
func TestEncodeDecodeValues(t *testing.T) {
38
	t.Parallel()
39

40
	for _, allowGzip := range []bool{false, true} {
41
		t.Run(fmt.Sprintf("allowGzip=%v", allowGzip), func(t *testing.T) {
42
			t.Parallel()
43

44
			for _, test := range []struct {
45
				name string
46

47
				values []string
48

49
				expectedEncodedSize int
50
				expectedGzippedSize int
51
			}{
52
				{
53
					name: "empty",
54
				},
55
				{
56
					name: "simple",
57
					values: []string{
58
						"10=foo",
59
						"0xb=bar",
60
					},
61

62
					expectedEncodedSize: 20,
63
					expectedGzippedSize: 20,
64
				},
65
				{
66
					name: "huge",
67
					values: []string{
68
						"10=" + strings.Repeat("foobar", 256),
69
						"0xb=" + strings.Repeat("baz", 256),
70
					},
71

72
					expectedEncodedSize: 3084,
73
					expectedGzippedSize: 80,
74
				},
75
			} {
76
				t.Run(test.name, func(t *testing.T) {
77
					t.Parallel()
78

79
					values := make(meta.Values, len(test.values))
80

81
					for i, v := range test.values {
82
						require.NoError(t, values[i].Parse(v))
83
					}
84

85
					if len(values) == 0 {
86
						values = nil
87
					}
88

89
					encoded := values.Encode(allowGzip)
90

91
					switch {
92
					case test.expectedEncodedSize > 0 && !allowGzip:
93
						assert.Equal(t, test.expectedEncodedSize, len(encoded))
94
					case test.expectedGzippedSize > 0 && allowGzip:
95
						assert.Equal(t, test.expectedGzippedSize, len(encoded))
96
					}
97

98
					decoded, err := meta.DecodeValues(encoded)
99
					require.NoError(t, err)
100

101
					assert.Equal(t, values, decoded)
102
				})
103
			}
104
		})
105
	}
106
}
107

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.