talos
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
5package services_test
6
7import (
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"
21var helloYAML []byte
22
23func TestUnmarshal(t *testing.T) {
24var spec services.Spec
25
26require.NoError(t, yaml.Unmarshal(helloYAML, &spec))
27
28assert.Equal(t, services.Spec{
29Name: "hello",
30Container: services.Container{
31Entrypoint: "hello-world",
32Args: []string{"--development", "--log=debug"},
33Mounts: []specs.Mount{
34{
35Destination: "/var/lib/example",
36Type: "bind",
37Source: "/var/lib/example",
38Options: []string{"rbind", "ro"},
39},
40},
41},
42Depends: []services.Dependency{
43{
44Service: "cri",
45},
46{
47Path: "/system/run/machined/machined.sock",
48},
49{
50Network: []nethelpers.Status{nethelpers.StatusAddresses},
51},
52},
53Restart: services.RestartNever,
54}, spec)
55
56assert.NoError(t, spec.Validate())
57}
58
59func TestValidate(t *testing.T) {
60for _, tt := range []struct {
61name string
62spec services.Spec
63expectedError string
64}{
65{
66name: "empty",
67spec: services.Spec{},
68expectedError: "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{
71name: "invalid name",
72spec: services.Spec{
73Name: "FOO",
74Container: services.Container{
75Entrypoint: "foo",
76},
77Restart: services.RestartAlways,
78},
79expectedError: "1 error occurred:\n\t* name \"FOO\" is invalid\n\n",
80},
81{
82name: "invalid deps",
83spec: services.Spec{
84Name: "foo",
85Container: services.Container{
86Entrypoint: "foo",
87},
88Depends: []services.Dependency{
89{},
90{
91Path: "./somefile",
92},
93{
94Network: []nethelpers.Status{
950,
96},
97},
98{
99Network: []nethelpers.Status{
100nethelpers.StatusAddresses,
101},
102Path: "/foo",
103},
104},
105Restart: services.RestartAlways,
106},
107expectedError: "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} {
110t.Run(tt.name, func(t *testing.T) {
111err := tt.spec.Validate()
112assert.EqualError(t, err, tt.expectedError)
113})
114}
115}
116