16
16
package containerd
17
17
18
18
import (
19
+ "github.com/containerd/typeurl/v2"
20
+ "google.golang.org/protobuf/types/known/anypb"
19
21
"testing"
20
22
21
- "github.com/containerd/typeurl"
22
23
specs "github.com/opencontainers/runtime-spec/specs-go"
23
24
"github.com/stretchr/testify/assert"
24
25
@@ -42,6 +43,36 @@ func (m *mockedMachineInfo) GetVersionInfo() (*info.VersionInfo, error) {
42
43
return & info.VersionInfo {}, nil
43
44
}
44
45
46
+ // Copied from https://github.com/containerd/containerd/blob/main/pkg/protobuf/any.go
47
+ // FromAny converts typeurl.Any to github.com/containerd/containerd/protobuf/types.Any.
48
+ //
49
+ // TODO: vendor these functions once they are moved to github.com/containerd/typeurl.
50
+ //
51
+ // Currently they are copied over so as not to introduce a dependency on containerd/containerd again
52
+ func FromAny (from typeurl.Any ) * anypb.Any {
53
+ if from == nil {
54
+ return nil
55
+ }
56
+
57
+ if pbany , ok := from .(* anypb.Any ); ok {
58
+ return pbany
59
+ }
60
+
61
+ return & anypb.Any {
62
+ TypeUrl : from .GetTypeUrl (),
63
+ Value : from .GetValue (),
64
+ }
65
+ }
66
+
67
+ // MarshalAnyToProto converts an arbitrary interface to github.com/containerd/containerd/protobuf/types.Any.
68
+ func MarshalAnyToProto (from interface {}) (* anypb.Any , error ) {
69
+ anyType , err := typeurl .MarshalAny (from )
70
+ if err != nil {
71
+ return nil , err
72
+ }
73
+ return FromAny (anyType ), nil
74
+ }
75
+
45
76
func TestHandler (t * testing.T ) {
46
77
as := assert .New (t )
47
78
type testCase struct {
@@ -65,7 +96,7 @@ func TestHandler(t *testing.T) {
65
96
Labels : map [string ]string {"io.cri-containerd.kind" : "sandbox" },
66
97
}
67
98
spec := & specs.Spec {Root : & specs.Root {Path : "/test/" }, Process : & specs.Process {Env : []string {"TEST_REGION=FRA" , "TEST_ZONE=A" , "HELLO=WORLD" }}}
68
- testContainer .Spec , _ = typeurl . MarshalAny (spec )
99
+ testContainer .Spec , _ = MarshalAnyToProto (spec )
69
100
testContainers ["40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9" ] = testContainer
70
101
for _ , ts := range []testCase {
71
102
{
0 commit comments