talos

Форк
0
115 строк · 2.7 Кб
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 services_test
6

7
import (
8
	_ "embed"
9
	"testing"
10

11
	"github.com/opencontainers/runtime-spec/specs-go"
12
	"github.com/stretchr/testify/assert"
13
	"github.com/stretchr/testify/require"
14
	"gopkg.in/yaml.v3"
15

16
	"github.com/siderolabs/talos/pkg/machinery/extensions/services"
17
	"github.com/siderolabs/talos/pkg/machinery/nethelpers"
18
)
19

20
//go:embed "testdata/hello.yaml"
21
var helloYAML []byte
22

23
func TestUnmarshal(t *testing.T) {
24
	var spec services.Spec
25

26
	require.NoError(t, yaml.Unmarshal(helloYAML, &spec))
27

28
	assert.Equal(t, services.Spec{
29
		Name: "hello",
30
		Container: services.Container{
31
			Entrypoint: "hello-world",
32
			Args:       []string{"--development", "--log=debug"},
33
			Mounts: []specs.Mount{
34
				{
35
					Destination: "/var/lib/example",
36
					Type:        "bind",
37
					Source:      "/var/lib/example",
38
					Options:     []string{"rbind", "ro"},
39
				},
40
			},
41
		},
42
		Depends: []services.Dependency{
43
			{
44
				Service: "cri",
45
			},
46
			{
47
				Path: "/system/run/machined/machined.sock",
48
			},
49
			{
50
				Network: []nethelpers.Status{nethelpers.StatusAddresses},
51
			},
52
		},
53
		Restart: services.RestartNever,
54
	}, spec)
55

56
	assert.NoError(t, spec.Validate())
57
}
58

59
func TestValidate(t *testing.T) {
60
	for _, tt := range []struct {
61
		name          string
62
		spec          services.Spec
63
		expectedError string
64
	}{
65
		{
66
			name:          "empty",
67
			spec:          services.Spec{},
68
			expectedError: "3 errors occurred:\n\t* name \"\" is invalid\n\t* restart kind is invalid: RestartKind(0)\n\t* container endpoint can't be empty\n\n",
69
		},
70
		{
71
			name: "invalid name",
72
			spec: services.Spec{
73
				Name: "FOO",
74
				Container: services.Container{
75
					Entrypoint: "foo",
76
				},
77
				Restart: services.RestartAlways,
78
			},
79
			expectedError: "1 error occurred:\n\t* name \"FOO\" is invalid\n\n",
80
		},
81
		{
82
			name: "invalid deps",
83
			spec: services.Spec{
84
				Name: "foo",
85
				Container: services.Container{
86
					Entrypoint: "foo",
87
				},
88
				Depends: []services.Dependency{
89
					{},
90
					{
91
						Path: "./somefile",
92
					},
93
					{
94
						Network: []nethelpers.Status{
95
							0,
96
						},
97
					},
98
					{
99
						Network: []nethelpers.Status{
100
							nethelpers.StatusAddresses,
101
						},
102
						Path: "/foo",
103
					},
104
				},
105
				Restart: services.RestartAlways,
106
			},
107
			expectedError: "4 errors occurred:\n\t* no dependency specified\n\t* path is not absolute: \"./somefile\"\n\t* invalid network dependency: Status(0)\n\t* more than a single dependency is set\n\n",
108
		},
109
	} {
110
		t.Run(tt.name, func(t *testing.T) {
111
			err := tt.spec.Validate()
112
			assert.EqualError(t, err, tt.expectedError)
113
		})
114
	}
115
}
116

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

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

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

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