Skip to content

Commit 9f2dd63

Browse files
committed
refactor: api tests
Unify a list of all APIs in Talos to a single place, and use them in associated tests: * the test for one2many specifics * the test for deprecated methods * the test for missing RBAC rules Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
1 parent a907831 commit 9f2dd63

File tree

3 files changed

+76
-51
lines changed

3 files changed

+76
-51
lines changed

internal/app/apid/pkg/backend/apid_test.go

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"errors"
1111
"testing"
1212

13+
"github.com/siderolabs/gen/xslices"
1314
"github.com/siderolabs/go-pointer"
1415
"github.com/stretchr/testify/assert"
1516
"github.com/stretchr/testify/require"
@@ -21,13 +22,8 @@ import (
2122

2223
"github.com/siderolabs/talos/internal/app/apid/pkg/backend"
2324
"github.com/siderolabs/talos/pkg/grpc/middleware/authz"
24-
"github.com/siderolabs/talos/pkg/machinery/api/cluster"
25+
"github.com/siderolabs/talos/pkg/machinery/api"
2526
"github.com/siderolabs/talos/pkg/machinery/api/common"
26-
"github.com/siderolabs/talos/pkg/machinery/api/inspect"
27-
"github.com/siderolabs/talos/pkg/machinery/api/machine"
28-
"github.com/siderolabs/talos/pkg/machinery/api/security"
29-
"github.com/siderolabs/talos/pkg/machinery/api/storage"
30-
"github.com/siderolabs/talos/pkg/machinery/api/time"
3127
"github.com/siderolabs/talos/pkg/machinery/config"
3228
"github.com/siderolabs/talos/pkg/machinery/proto"
3329
"github.com/siderolabs/talos/pkg/machinery/role"
@@ -214,15 +210,11 @@ func TestAPIDSuite(t *testing.T) {
214210
}
215211

216212
func TestAPIIdiosyncrasies(t *testing.T) {
217-
for _, services := range []protoreflect.ServiceDescriptors{
218-
common.File_common_common_proto.Services(),
219-
cluster.File_cluster_cluster_proto.Services(),
220-
inspect.File_inspect_inspect_proto.Services(),
221-
machine.File_machine_machine_proto.Services(),
222-
// security.File_security_security_proto.Services() is different
223-
storage.File_storage_storage_proto.Services(),
224-
time.File_time_time_proto.Services(),
225-
} {
213+
for _, services := range xslices.Map(api.TalosAPIdOne2ManyAPIs(),
214+
func(fd protoreflect.FileDescriptor) protoreflect.ServiceDescriptors {
215+
return fd.Services()
216+
},
217+
) {
226218
for i := range services.Len() {
227219
service := services.Get(i)
228220
methods := service.Methods()
@@ -299,7 +291,8 @@ func getOptions(t *testing.T, descriptor protoreflect.Descriptor) (deprecated bo
299291
deprecated = pointer.SafeDeref(opts.Deprecated)
300292
version = protobuf.GetExtension(opts, common.E_RemoveDeprecatedMethod).(string)
301293
}
302-
294+
case *descriptorpb.OneofOptions:
295+
// OneofOptions do not have deprecated option
303296
default:
304297
t.Fatalf("unhandled %T", opts)
305298
}
@@ -363,15 +356,7 @@ func TestDeprecatedAPIs(t *testing.T) {
363356
currentVersion, err := config.ParseContractFromVersion(version.Tag)
364357
require.NoError(t, err)
365358

366-
for _, file := range []protoreflect.FileDescriptor{
367-
common.File_common_common_proto,
368-
cluster.File_cluster_cluster_proto,
369-
inspect.File_inspect_inspect_proto,
370-
machine.File_machine_machine_proto,
371-
security.File_security_security_proto,
372-
storage.File_storage_storage_proto,
373-
time.File_time_time_proto,
374-
} {
359+
for _, file := range api.AllAPIs() {
375360
enums := file.Enums()
376361
for i := range enums.Len() {
377362
testEnum(t, enums.Get(i), currentVersion)

internal/app/machined/pkg/system/services/machined_test.go

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,26 @@ import (
88
"fmt"
99
"testing"
1010

11-
cosi "github.com/cosi-project/runtime/api/v1alpha1"
1211
"github.com/stretchr/testify/assert"
1312
"github.com/stretchr/testify/require"
14-
"google.golang.org/grpc"
1513

16-
"github.com/siderolabs/talos/pkg/machinery/api/cluster"
17-
"github.com/siderolabs/talos/pkg/machinery/api/inspect"
18-
"github.com/siderolabs/talos/pkg/machinery/api/machine"
19-
"github.com/siderolabs/talos/pkg/machinery/api/storage"
20-
"github.com/siderolabs/talos/pkg/machinery/api/time"
14+
"github.com/siderolabs/talos/pkg/machinery/api"
2115
)
2216

2317
func collectMethods(t *testing.T) map[string]struct{} {
2418
methods := make(map[string]struct{})
2519

26-
for _, service := range []grpc.ServiceDesc{
27-
cosi.State_ServiceDesc,
28-
cluster.ClusterService_ServiceDesc,
29-
inspect.InspectService_ServiceDesc,
30-
machine.ImageService_ServiceDesc,
31-
machine.MachineService_ServiceDesc,
32-
// security.SecurityService_ServiceDesc, - not in machined
33-
storage.StorageService_ServiceDesc,
34-
time.TimeService_ServiceDesc,
35-
} {
36-
for _, method := range service.Methods {
37-
s := fmt.Sprintf("/%s/%s", service.ServiceName, method.MethodName)
38-
require.NotContains(t, methods, s)
39-
methods[s] = struct{}{}
40-
}
20+
for _, service := range api.TalosAPIdAllAPIs() {
21+
for i := range service.Services().Len() {
22+
svc := service.Services().Get(i)
23+
24+
for j := range svc.Methods().Len() {
25+
method := svc.Methods().Get(j)
4126

42-
for _, stream := range service.Streams {
43-
s := fmt.Sprintf("/%s/%s", service.ServiceName, stream.StreamName)
44-
require.NotContains(t, methods, s)
45-
methods[s] = struct{}{}
27+
s := fmt.Sprintf("/%s/%s", svc.FullName(), method.Name())
28+
require.NotContains(t, methods, s)
29+
methods[s] = struct{}{}
30+
}
4631
}
4732
}
4833

pkg/machinery/api/api.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 api contains API definitions for Talos Linux.
6+
//
7+
//nolint:revive
8+
package api
9+
10+
import (
11+
cosi "github.com/cosi-project/runtime/api/v1alpha1"
12+
"google.golang.org/protobuf/reflect/protoreflect"
13+
14+
"github.com/siderolabs/talos/pkg/machinery/api/cluster"
15+
"github.com/siderolabs/talos/pkg/machinery/api/common"
16+
"github.com/siderolabs/talos/pkg/machinery/api/inspect"
17+
"github.com/siderolabs/talos/pkg/machinery/api/machine"
18+
"github.com/siderolabs/talos/pkg/machinery/api/security"
19+
"github.com/siderolabs/talos/pkg/machinery/api/storage"
20+
"github.com/siderolabs/talos/pkg/machinery/api/time"
21+
)
22+
23+
// TalosAPIdOne2ManyAPIs returns a list of API services that support one-to-many
24+
// communication pattern served by apid.
25+
//
26+
// Note: we are moving to one-to-one APIs, so this list should not grow.
27+
func TalosAPIdOne2ManyAPIs() []protoreflect.FileDescriptor {
28+
return []protoreflect.FileDescriptor{
29+
common.File_common_common_proto,
30+
cluster.File_cluster_cluster_proto,
31+
inspect.File_inspect_inspect_proto,
32+
machine.File_machine_machine_proto,
33+
storage.File_storage_storage_proto,
34+
time.File_time_time_proto,
35+
}
36+
}
37+
38+
// TalosAPIdAllAPIs returns a list of all API services served by apid.
39+
//
40+
// This includes legacy one-to-many APIs as well as newer one-to-one APIs.
41+
func TalosAPIdAllAPIs() []protoreflect.FileDescriptor {
42+
return append(TalosAPIdOne2ManyAPIs(),
43+
cosi.File_v1alpha1_state_proto,
44+
machine.File_machine_image_proto,
45+
)
46+
}
47+
48+
// AllAPIs returns a list of all API services served by Talos components.
49+
//
50+
// This includes Talos apid and trustd APIs.
51+
func AllAPIs() []protoreflect.FileDescriptor {
52+
return append(TalosAPIdAllAPIs(),
53+
security.File_security_security_proto,
54+
)
55+
}

0 commit comments

Comments
 (0)