@@ -9,26 +9,16 @@ package auth
99import (
1010 "context"
1111 "net"
12- "os"
1312 "path/filepath"
1413 "testing"
1514
1615 "github.com/containerd/containerd/pkg/dialer"
17- runtime_alpha "github.com/containerd/containerd/third_party/k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
1816 "github.com/stretchr/testify/assert"
1917 "google.golang.org/grpc"
2018 "google.golang.org/grpc/credentials/insecure"
2119 runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
2220)
2321
24- type MockAlphaImageService struct {
25- runtime_alpha.UnimplementedImageServiceServer
26- }
27-
28- func (* MockAlphaImageService ) PullImage (_ context.Context , _ * runtime_alpha.PullImageRequest ) (* runtime_alpha.PullImageResponse , error ) {
29- return & runtime_alpha.PullImageResponse {}, nil
30- }
31-
3222type MockImageService struct {
3323 runtime.UnimplementedImageServiceServer
3424}
@@ -39,120 +29,117 @@ func (*MockImageService) PullImage(_ context.Context, _ *runtime.PullImageReques
3929
4030func TestFromImagePull (t * testing.T ) {
4131 var err error
42- assert := assert .New (t )
32+ assertions := assert .New (t )
4333
4434 ctx := context .TODO ()
4535 d := t .TempDir ()
46- defer os .RemoveAll (d )
4736
4837 tagImage := "docker.io/library/busybox:latest"
38+
4939 // should return nil if no proxy
5040 kc , err := FromCRI ("docker.io" , tagImage )
51- assert .Nil (kc )
52- assert .NoError (err )
41+ assertions .Nil (kc )
42+ assertions .NoError (err )
5343
44+ // Mocking the end CRI request consumer.
5445 mockRPC := grpc .NewServer ()
5546 mockSocket := filepath .Join (d , "mock.sock" )
56- lm , err := net .Listen ("unix" , mockSocket )
57- assert .NoError (err )
58-
59- // The server of CRI image service proxy.
60- proxyRPC := grpc .NewServer ()
61- proxySocket := filepath .Join (d , "proxy.sock" )
62- lp , err := net .Listen ("unix" , proxySocket )
63- assert .NoError (err )
47+ listenMock , err := net .Listen ("unix" , mockSocket )
48+ assertions .NoError (err )
6449
6550 // Mocking the end CRI request consumer.
66- serverAlpha := & MockAlphaImageService {}
6751 server := & MockImageService {}
68- runtime_alpha .RegisterImageServiceServer (mockRPC , serverAlpha )
6952 runtime .RegisterImageServiceServer (mockRPC , server )
7053
7154 go func () {
72- err := mockRPC .Serve (lm )
73- assert .NoError (err )
55+ err := mockRPC .Serve (listenMock )
56+ assertions .NoError (err )
7457 }()
7558 defer mockRPC .Stop ()
7659
60+ // The server of CRI image service proxy.
61+ proxyRPC := grpc .NewServer ()
62+ proxySocket := filepath .Join (d , "proxy.sock" )
63+ listenProxy , err := net .Listen ("unix" , proxySocket )
64+ assertions .NoError (err )
7765 AddImageProxy (ctx , proxyRPC , mockSocket )
7866 go func () {
79- err := proxyRPC .Serve (lp )
80- assert .NoError (err )
67+ err := proxyRPC .Serve (listenProxy )
68+ assertions .NoError (err )
8169 }()
8270 defer proxyRPC .Stop ()
8371
84- kc , err = FromCRI ("docker.io" , tagImage )
8572 // should return empty kc before pulling
86- assert .Nil (kc )
87- assert .NoError (err )
73+ kc , err = FromCRI ("docker.io" , tagImage )
74+ assertions .Nil (kc )
75+ assertions .NoError (err )
8876
8977 gopts := []grpc.DialOption {
9078 grpc .WithTransportCredentials (insecure .NewCredentials ()),
9179 grpc .WithContextDialer (dialer .ContextDialer ),
9280 }
9381 conn , err := grpc .Dial (dialer .DialAddress (proxySocket ), gopts ... )
94- assert .NoError (err )
95- criAlphaClient := runtime_alpha .NewImageServiceClient (conn )
96- irAlpha := & runtime_alpha.PullImageRequest {
97- Image : & runtime_alpha.ImageSpec {
82+ assertions .NoError (err )
83+
84+ criClient := runtime .NewImageServiceClient (conn )
85+
86+ _ , err = criClient .PullImage (ctx , & runtime.PullImageRequest {
87+ Image : & runtime.ImageSpec {
9888 Image : tagImage ,
9989 },
100- Auth : & runtime_alpha .AuthConfig {
90+ Auth : & runtime .AuthConfig {
10191 Username : "test" ,
10292 Password : "passwd" ,
10393 },
104- }
105- _ , err = criAlphaClient .PullImage (ctx , irAlpha )
106- assert .NoError (err )
94+ })
95+ assertions .NoError (err )
10796
108- criClient := runtime .NewImageServiceClient (conn )
109-
110- kc , err = FromCRI ("docker.io" , tagImage )
11197 // get correct kc after pulling
112- assert .Equal ("test" , kc .Username )
113- assert .Equal ("passwd" , kc .Password )
114- assert .NoError (err )
98+ kc , err = FromCRI ("docker.io" , tagImage )
99+ assertions .NoError (err )
100+ assertions .Equal ("test" , kc .Username )
101+ assertions .Equal ("passwd" , kc .Password )
115102
116- kc , err = FromCRI ("docker.io" , "docker.io/library/busybox:another" )
117103 // get empty kc with wrong tag
118- assert .Nil (kc )
119- assert .NoError (err )
104+ kc , err = FromCRI ("docker.io" , "docker.io/library/busybox:another" )
105+ assertions .Nil (kc )
106+ assertions .NoError (err )
120107
121108 image2 := "ghcr.io/busybox:latest"
122109
123- ir := & runtime.PullImageRequest {
110+ _ , err = criClient . PullImage ( ctx , & runtime.PullImageRequest {
124111 Image : & runtime.ImageSpec {
125112 Image : image2 ,
126113 },
127114 Auth : & runtime.AuthConfig {
128115 Username : "test_1" ,
129116 Password : "passwd_1" ,
130117 },
131- }
132- _ , err = criClient .PullImage (ctx , ir )
133- assert .NoError (err )
118+ })
119+ assertions .NoError (err )
134120
121+ // get correct kc after pulling
135122 kc , err = FromCRI ("ghcr.io" , image2 )
136- assert . Equal ( kc . Username , "test_1" )
137- assert .Equal (kc .Password , "passwd_1 " )
138- assert . NoError ( err )
123+ assertions . NoError ( err )
124+ assertions .Equal (kc .Username , "test_1 " )
125+ assertions . Equal ( kc . Password , "passwd_1" )
139126
140127 // should work with digest
141128 digestImage := "docker.io/library/busybox@sha256:7cc4b5aefd1d0cadf8d97d4350462ba51c694ebca145b08d7d41b41acc8db5aa"
142- irAlpha = & runtime_alpha .PullImageRequest {
143- Image : & runtime_alpha .ImageSpec {
129+ _ , err = criClient . PullImage ( ctx , & runtime .PullImageRequest {
130+ Image : & runtime .ImageSpec {
144131 Image : digestImage ,
145132 },
146- Auth : & runtime_alpha .AuthConfig {
133+ Auth : & runtime .AuthConfig {
147134 Username : "digest" ,
148135 Password : "dpwd" ,
149136 },
150- }
151- _ , err = criAlphaClient .PullImage (ctx , irAlpha )
152- assert .NoError (err )
137+ })
138+ assertions .NoError (err )
153139
140+ // get correct kc after pulling
154141 kc , err = FromCRI ("docker.io" , digestImage )
155- assert . Equal ( "digest" , kc . Username )
156- assert .Equal ("dpwd " , kc .Password )
157- assert . NoError ( err )
142+ assertions . NoError ( err )
143+ assertions .Equal ("digest " , kc .Username )
144+ assertions . Equal ( "dpwd" , kc . Password )
158145}
0 commit comments