This repository has been archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathproxy_test.go
939 lines (757 loc) · 25 KB
/
proxy_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
// Copyright (c) 2016,2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"encoding/json"
"net"
"os"
"strings"
"sync"
"syscall"
"testing"
"time"
"github.com/clearcontainers/proxy/api"
goapi "github.com/clearcontainers/proxy/client"
"github.com/containers/virtcontainers/pkg/hyperstart"
"github.com/containers/virtcontainers/pkg/hyperstart/mock"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
)
const unixURLPrefix = "unix:"
type testRig struct {
t *testing.T
wg sync.WaitGroup
// hyperstart mocking
runHyperstart bool
Hyperstart *mock.Hyperstart
// proxy, in process
proxy *proxy
protocol *protocol
proxyConns []net.Conn // sockets used by proxy to communicate with Client
// client
Client *goapi.Client
// fd leak detection
detector *FdLeakDetector
startFds, stopFds *FdSnapshot
}
func newTestRig(t *testing.T) *testRig {
proto := newProtocol()
proto.HandleCommand(api.CmdRegisterVM, registerVM)
proto.HandleCommand(api.CmdAttachVM, attachVM)
proto.HandleCommand(api.CmdUnregisterVM, unregisterVM)
proto.HandleCommand(api.CmdHyper, hyper)
proto.HandleCommand(api.CmdConnectShim, connectShim)
proto.HandleCommand(api.CmdDisconnectShim, disconnectShim)
proto.HandleCommand(api.CmdSignal, cmdSignal)
proto.HandleStream(api.StreamStdin, forwardStdin)
proto.HandleStream(api.StreamLog, handleLogEntry)
return &testRig{
t: t,
protocol: proto,
proxy: newProxy(),
runHyperstart: true,
detector: NewFdLeadDetector(),
}
}
func (rig *testRig) SetRunHyperstart(run bool) {
rig.runHyperstart = run
}
func (rig *testRig) Start() {
var err error
rig.startFds, err = rig.detector.Snapshot()
assert.Nil(rig.t, err)
// Start hyperstart go routine
if rig.runHyperstart {
rig.Hyperstart = mock.NewHyperstart(rig.t)
rig.Hyperstart.Start()
// Explicitly send READY message from hyperstart mock
rig.wg.Add(1)
go func() {
rig.Hyperstart.SendMessage(int(hyperstart.ReadyCode), []byte{})
rig.wg.Done()
}()
}
// Client object that can be used to issue proxy commands
clientConn := rig.ServeNewClient()
rig.Client = goapi.NewClient(clientConn)
}
func (rig *testRig) Stop() {
var err error
rig.Client.Close()
for _, conn := range rig.proxyConns {
conn.Close()
}
if rig.runHyperstart {
rig.Hyperstart.Stop()
}
rig.wg.Wait()
if rig.proxy != nil {
rig.proxy.wg.Wait()
}
// We shouldn't have leaked a fd between the beginning of Start() and
// the end of Stop().
rig.stopFds, err = rig.detector.Snapshot()
assert.Nil(rig.t, err)
assert.True(rig.t,
rig.detector.Compare(os.Stdout, rig.startFds, rig.stopFds))
}
// SyncWithProxy can be used to make sure the goroutine serving the proxy has
// processed all frames sent up to this point.
func SyncWithProxy(client *goapi.Client) {
_, _ = client.AttachVM("non-existing-id", nil)
}
// RegisterVM registers a new VM, returning 1 token that can be used by a shim.
func (rig *testRig) RegisterVM() (token string) {
ctlSocketPath, ioSocketPath := rig.Hyperstart.GetSocketPaths()
ret, err := rig.Client.RegisterVM(testContainerID, ctlSocketPath, ioSocketPath,
&goapi.RegisterVMOptions{NumIOStreams: 1})
assert.Nil(rig.t, err)
assert.Equal(rig.t, 1, len(ret.IO.Tokens))
return ret.IO.Tokens[0]
}
// ServeNewClient simulate a new client connecting to the proxy. It returns the
// net.Conn that represents client-side connection to the proxy.
func (rig *testRig) ServeNewClient() net.Conn {
clientConn, proxyConn, err := Socketpair()
assert.Nil(rig.t, err)
rig.proxyConns = append(rig.proxyConns, proxyConn)
rig.wg.Add(1)
go func() {
rig.proxy.serveNewClient(rig.protocol, proxyConn)
rig.wg.Done()
}()
return clientConn
}
// ServeNewShim is a specialization of ServerNewClient that returns a mock shim
// already connected to the proxy.
func (rig *testRig) ServeNewShim(token string) *shimRig {
shimConn := rig.ServeNewClient()
shim := newShimRig(rig.t, shimConn, token)
err := shim.connect()
assert.Nil(rig.t, err)
return shim
}
const testContainerID = "0987654321"
func TestRegisterVM(t *testing.T) {
rig := newTestRig(t)
rig.Start()
// Register new VM.
ctlSocketPath, ioSocketPath := rig.Hyperstart.GetSocketPaths()
ret, err := rig.Client.RegisterVM(testContainerID, ctlSocketPath, ioSocketPath, nil)
assert.Nil(t, err)
assert.NotNil(t, ret)
// We haven't asked for I/O tokens
assert.Equal(t, 0, len(ret.IO.Tokens))
// A new RegisterVM message with the same containerID should error out.
_, err = rig.Client.RegisterVM(testContainerID, "fooCtl", "fooIo", nil)
assert.NotNil(t, err)
// RegisterVM should register a new vm object.
proxy := rig.proxy
proxy.Lock()
vm := proxy.vms[testContainerID]
proxy.Unlock()
assert.NotNil(t, vm)
assert.Equal(t, testContainerID, vm.containerID)
// This test shouldn't send anything to hyperstart.
msgs := rig.Hyperstart.GetLastMessages()
assert.Equal(t, 0, len(msgs))
rig.Stop()
}
func TestUnregisterVM(t *testing.T) {
rig := newTestRig(t)
rig.Start()
// Register new VM
ctlSocketPath, ioSocketPath := rig.Hyperstart.GetSocketPaths()
_, err := rig.Client.RegisterVM(testContainerID, ctlSocketPath, ioSocketPath, nil)
assert.Nil(t, err)
// UnregisterVM with a bad containerID.
err = rig.Client.UnregisterVM("foo")
assert.NotNil(t, err)
// Bye!
err = rig.Client.UnregisterVM(testContainerID)
assert.Nil(t, err)
// A second UnregisterVM (client not attached anymore) should return an
// error.
err = rig.Client.UnregisterVM(testContainerID)
assert.NotNil(t, err)
// UnregisterVM should unregister the vm object
proxy := rig.proxy
proxy.Lock()
vm := proxy.vms[testContainerID]
proxy.Unlock()
assert.Nil(t, vm)
// This test shouldn't send anything to hyperstart
msgs := rig.Hyperstart.GetLastMessages()
assert.Equal(t, 0, len(msgs))
rig.Stop()
}
func TestAttachVM(t *testing.T) {
rig := newTestRig(t)
rig.Start()
// Register new VM
ctlSocketPath, ioSocketPath := rig.Hyperstart.GetSocketPaths()
_, err := rig.Client.RegisterVM(testContainerID, ctlSocketPath, ioSocketPath, nil)
assert.Nil(t, err)
// Attaching to an unknown VM should return an error
_, err = rig.Client.AttachVM("foo", nil)
assert.NotNil(t, err)
// Attaching to an existing VM should work. To test we are effectively
// attached, we issue an UnregisterVM that would error out if not
// attached.
ret, err := rig.Client.AttachVM(testContainerID, nil)
assert.Nil(t, err)
// We haven't asked for I/O tokens
assert.Equal(t, 0, len(ret.IO.Tokens))
err = rig.Client.UnregisterVM(testContainerID)
assert.Nil(t, err)
// This test shouldn't send anything with hyperstart
msgs := rig.Hyperstart.GetLastMessages()
assert.Equal(t, 0, len(msgs))
rig.Stop()
}
func TestHyperPing(t *testing.T) {
rig := newTestRig(t)
rig.Start()
ctlSocketPath, ioSocketPath := rig.Hyperstart.GetSocketPaths()
_, err := rig.Client.RegisterVM(testContainerID, ctlSocketPath, ioSocketPath, nil)
assert.Nil(t, err)
// Send ping and verify we have indeed received the message on the
// hyperstart side. Ping is somewhat interesting because it's a case of
// an hyper message without data.
_, err = rig.Client.Hyper("ping", nil)
assert.Nil(t, err)
msgs := rig.Hyperstart.GetLastMessages()
assert.Equal(t, 1, len(msgs))
msg := msgs[0]
assert.Equal(t, hyperstart.PingCode, int(msg.Code))
assert.Equal(t, 0, len(msg.Message))
rig.Stop()
}
func TestHyperStartpod(t *testing.T) {
rig := newTestRig(t)
rig.Start()
// Register new VM
ctlSocketPath, ioSocketPath := rig.Hyperstart.GetSocketPaths()
_, err := rig.Client.RegisterVM(testContainerID, ctlSocketPath, ioSocketPath, nil)
assert.Nil(t, err)
// Send startopd and verify we have indeed received the message on the
// hyperstart side. startpod is interesting because it's a case of an
// hyper message with JSON data.
startpod := hyperstart.Pod{
Hostname: "testhostname",
ShareDir: "rootfs",
}
_, err = rig.Client.Hyper("startpod", &startpod)
assert.Nil(t, err)
msgs := rig.Hyperstart.GetLastMessages()
assert.Equal(t, 1, len(msgs))
msg := msgs[0]
assert.Equal(t, hyperstart.StartPodCode, int(msg.Code))
received := hyperstart.Pod{}
err = json.Unmarshal(msg.Message, &received)
assert.Nil(t, err)
assert.Equal(t, startpod.Hostname, received.Hostname)
assert.Equal(t, startpod.ShareDir, received.ShareDir)
rig.Stop()
}
func TestRegisterVMAllocateTokens(t *testing.T) {
rig := newTestRig(t)
rig.Start()
// Register new VM, asking for tokens
ctlSocketPath, ioSocketPath := rig.Hyperstart.GetSocketPaths()
ret, err := rig.Client.RegisterVM(testContainerID, ctlSocketPath, ioSocketPath,
&goapi.RegisterVMOptions{NumIOStreams: 2})
assert.Nil(t, err)
assert.NotNil(t, ret)
assert.True(t, strings.HasPrefix(ret.IO.URL, unixURLPrefix))
assert.Equal(t, 2, len(ret.IO.Tokens))
// This test shouldn't send anything to hyperstart.
msgs := rig.Hyperstart.GetLastMessages()
assert.Equal(t, 0, len(msgs))
rig.Stop()
}
func TestAttachVMAllocateTokens(t *testing.T) {
rig := newTestRig(t)
rig.Start()
// Register new VM
ctlSocketPath, ioSocketPath := rig.Hyperstart.GetSocketPaths()
_, err := rig.Client.RegisterVM(testContainerID, ctlSocketPath, ioSocketPath, nil)
assert.Nil(t, err)
// Attach to the VM, asking for tokens
ret, err := rig.Client.AttachVM(testContainerID, &goapi.AttachVMOptions{NumIOStreams: 2})
assert.Nil(t, err)
assert.NotNil(t, ret)
assert.True(t, strings.HasPrefix(ret.IO.URL, unixURLPrefix))
assert.Equal(t, 2, len(ret.IO.Tokens))
// Cleanup
err = rig.Client.UnregisterVM(testContainerID)
assert.Nil(t, err)
// This test shouldn't send anything with hyperstart
msgs := rig.Hyperstart.GetLastMessages()
assert.Equal(t, 0, len(msgs))
rig.Stop()
}
func TestConnectShim(t *testing.T) {
rig := newTestRig(t)
rig.Start()
token := rig.RegisterVM()
// Using a bad token should result in an error
err := rig.Client.ConnectShim("notatoken")
assert.NotNil(t, err)
// Register shim with an existing token, all should be good
err = rig.Client.ConnectShim(token)
assert.Nil(t, err)
// Trying to re-use a token that a process has already claimed should
// result in an error.
err = rig.Client.ConnectShim(token)
assert.NotNil(t, err)
// Cleanup
err = rig.Client.DisconnectShim()
assert.Nil(t, err)
// This test shouldn't send anything to hyperstart.
msgs := rig.Hyperstart.GetLastMessages()
assert.Equal(t, 0, len(msgs))
rig.Stop()
}
// Relocations are thoroughly tested in vm_test.go, this is just to ensure we
// have coverage at a higher level.
func TestHyperSequenceNumberRelocation(t *testing.T) {
rig := newTestRig(t)
rig.Start()
token := rig.RegisterVM()
shim := rig.ServeNewShim(token)
// Send newcontainer hyper command
newcontainer := hyperstart.Container{
ID: testContainerID,
Process: &hyperstart.Process{
Args: []string{"/bin/sh"},
},
}
_, err := rig.Client.HyperWithTokens("newcontainer", []string{token}, &newcontainer)
assert.Nil(t, err)
// Verify hyperstart has received the message with relocation
msgs := rig.Hyperstart.GetLastMessages()
assert.Equal(t, 1, len(msgs))
msg := msgs[0]
assert.Equal(t, uint32(hyperstart.NewContainerCode), msg.Code)
payload := hyperstart.Container{}
err = json.Unmarshal(msg.Message, &payload)
assert.Nil(t, err)
assert.NotEqual(t, 0, payload.Process.Stdio)
assert.NotEqual(t, 0, payload.Process.Stderr)
shim.close()
rig.Stop()
}
type shimRig struct {
t *testing.T
token string
conn net.Conn
client *goapi.Client
}
func newShimRig(t *testing.T, conn net.Conn, token string) *shimRig {
return &shimRig{
t: t,
token: token,
conn: conn,
client: goapi.NewClient(conn.(*net.UnixConn)),
}
}
func (rig *shimRig) connect() error {
return rig.client.ConnectShim(rig.token)
}
func (rig *shimRig) close() {
rig.client.DisconnectShim()
rig.conn.Close()
}
func (rig *shimRig) writeIOString(msg string) {
api.WriteStream(rig.conn, api.StreamStdin, []byte(msg))
}
func (rig *shimRig) readIOStream() *api.Frame {
frame, err := api.ReadFrame(rig.conn)
assert.Nil(rig.t, err)
assert.Equal(rig.t, api.TypeStream, frame.Header.Type)
return frame
}
// peekIOSession returns the ioSession corresponding to token
func peekIOSession(proxy *proxy, tokenStr string) *ioSession {
token := Token(tokenStr)
proxy.Lock()
defer proxy.Unlock()
info := proxy.tokenToVM[token]
if info == nil {
return nil
}
return info.vm.findSessionByToken(token)
}
func TestShimIO(t *testing.T) {
rig := newTestRig(t)
rig.Start()
token := rig.RegisterVM()
shim := rig.ServeNewShim(token)
session := peekIOSession(rig.proxy, token)
// Simulate that a runtime has started the VM process
close(session.processStarted)
// Send stdin data.
stdinData := "stdin\n"
shim.writeIOString(stdinData)
// Check stdin data arrives correctly to hyperstart.
buf := make([]byte, 32)
n, seq := rig.Hyperstart.ReadIo(buf)
assert.Equal(t, session.ioBase, seq)
assert.Equal(t, len(stdinData)+12, n)
assert.Equal(t, stdinData, string(buf[12:n]))
// make hyperstart send something on stdout/stderr and verify we
// receive it.
streams := []struct {
seq uint64
stream api.Stream
data string
}{
{session.ioBase, api.StreamStdout, "stdout\n"},
{session.ioBase + 1, api.StreamStderr, "stderr\n"},
}
for _, stream := range streams {
rig.Hyperstart.SendIoString(stream.seq, stream.data)
frame := shim.readIOStream()
n := len(stream.data)
assert.NotNil(t, frame)
assert.Equal(t, api.TypeStream, frame.Header.Type)
assert.Equal(t, stream.stream, api.Stream(frame.Header.Opcode))
assert.Equal(t, n, len(frame.Payload))
assert.Equal(t, stream.data, string(frame.Payload[:n]))
}
// Make hypertart send an exit status an test we receive it.
rig.Hyperstart.CloseIo(session.ioBase)
rig.Hyperstart.SendExitStatus(session.ioBase, 42)
frame, err := api.ReadFrame(shim.conn)
assert.Nil(t, err)
assert.Equal(t, api.TypeNotification, frame.Header.Type)
assert.Equal(t, api.NotificationProcessExited, frame.Header.Opcode)
assert.Equal(t, 1, frame.Header.PayloadLength)
assert.Equal(t, 1, len(frame.Payload))
assert.Equal(t, byte(42), frame.Payload[0])
// Cleanup
shim.close()
rig.Stop()
}
func TestShimSignal(t *testing.T) {
rig := newTestRig(t)
rig.Start()
token := rig.RegisterVM()
shim := rig.ServeNewShim(token)
session := peekIOSession(rig.proxy, token)
session.containerID = testContainerID
// Simulate that a runtime has started the VM process
close(session.processStarted)
// Send signal and check hyperstart receives the right thing.
shim.client.Kill(syscall.SIGUSR1)
msgs := rig.Hyperstart.GetLastMessages()
assert.Equal(t, 1, len(msgs))
decoded := hyperstart.KillCommand{}
err := json.Unmarshal(msgs[0].Message, &decoded)
assert.Nil(t, err)
assert.Equal(t, syscall.SIGUSR1, decoded.Signal)
assert.Equal(t, testContainerID, decoded.Container)
// Send new window size and check hyperstart receives the right thing.
shim.client.SendTerminalSize(42, 24)
msgs = rig.Hyperstart.GetLastMessages()
assert.Equal(t, 1, len(msgs))
decoded1 := windowSizeMessage07{}
err = json.Unmarshal(msgs[0].Message, &decoded1)
assert.Nil(t, err)
assert.Equal(t, session.ioBase, decoded1.Seq)
assert.Equal(t, uint16(42), decoded1.Column)
assert.Equal(t, uint16(24), decoded1.Row)
// Cleanup
shim.close()
rig.Stop()
}
// smallWaitTimeout overrides the default timeout for the tests
const smallWaitTimeout = 20 * time.Millisecond
// TestShimConnectAfterExeccmd tests we correctly wait for the shim to connect
// before forwarding the execcmd to hyperstart.
func TestShimConnectAfterExeccmd(t *testing.T) {
rig := newTestRig(t)
rig.Start()
token := rig.RegisterVM()
// Send an execcmd. Since we don't have the corresponding shim yet, this
// should time out.
oldTimeout := waitForShimTimeout
waitForShimTimeout = smallWaitTimeout
execcmd := hyperstart.ExecCommand{
Container: testContainerID,
Process: hyperstart.Process{
Args: []string{"/bin/sh"},
},
}
_, err := rig.Client.HyperWithTokens("execcmd", []string{token}, &execcmd)
assert.NotNil(t, err)
assert.True(t, strings.Contains(err.Error(), "timeout"))
waitForShimTimeout = oldTimeout
// Send an execcmd again, but this time will connect the shim just after the command.
shimConn := rig.ServeNewClient()
shim := newShimRig(t, shimConn, token)
var wg sync.WaitGroup
wg.Add(1)
go func() {
time.Sleep(smallWaitTimeout)
err := shim.connect()
assert.Nil(t, err)
wg.Done()
}()
_, err = rig.Client.HyperWithTokens("execcmd", []string{token}, &execcmd)
assert.Nil(t, err)
wg.Wait()
// Cleanup
shim.close()
rig.Stop()
}
// TestShimSendSignalAfterExeccmd tests we correctly wait for the runtime to
// start the executable inside the VM before letting the shim send any signal
// commands.
func TestShimSendSignalAfterExeccmd(t *testing.T) {
rig := newTestRig(t)
rig.Start()
token := rig.RegisterVM()
shim := rig.ServeNewShim(token)
// Send a signal. Since the runtime hasn't launched the workload yet, this
// should time out.
oldTimeout := waitForProcessTimeout
waitForProcessTimeout = smallWaitTimeout
// Kill should time out
err := shim.client.Kill(syscall.SIGUSR1)
assert.NotNil(t, err)
assert.True(t, strings.Contains(err.Error(), "timeout"))
// nothing should have been sent to hyperstart
msgs := rig.Hyperstart.GetLastMessages()
assert.Equal(t, 0, len(msgs))
waitForProcessTimeout = oldTimeout
// Do the same, but now mocking the runtime starting a process.
var wg sync.WaitGroup
wg.Add(1)
go func() {
time.Sleep(smallWaitTimeout)
err := shim.client.Kill(syscall.SIGUSR1)
assert.NotNil(t, err)
wg.Done()
}()
execcmd := hyperstart.ExecCommand{
Container: testContainerID,
Process: hyperstart.Process{
Args: []string{"/bin/sh"},
},
}
_, err = rig.Client.HyperWithTokens("execcmd", []string{token}, &execcmd)
assert.Nil(t, err)
wg.Wait()
// This time (again) the signal cmd has not been forwarded. Hyperstart
// should have received one messages (execcmd)
msgs = rig.Hyperstart.GetLastMessages()
assert.Equal(t, 1, len(msgs))
// Cleanup
shim.close()
rig.Stop()
}
// TestShimSendStdinAfterExeccmd tests we correctly wait for the runtime to
// start the executable inside the VM before letting the shim send stdin data.
func TestShimSendStdinAfterExeccmd(t *testing.T) {
rig := newTestRig(t)
rig.Start()
token := rig.RegisterVM()
shim := rig.ServeNewShim(token)
// Send stdin data. Since the runtime hasn't launched the workload yet, this
// should time out.
waitForProcessTimeout = smallWaitTimeout
// For streams, we don't get a message back from the proxy, so err here will
// be nil even if we timeout waiting for the process to be started.
// This also means that SendStdin isn't blocking. It doesn't have to wait for
// a Response from the proxy.
err := shim.client.SendStdin([]byte("hello1\n"))
assert.Nil(t, err)
shim.close()
// Because SendStdin isn't blocking, wait manually for the timeout.
time.Sleep(2 * smallWaitTimeout)
// Timeout should have fired and the proxy handled the error by closing the
// shim connections.
_, err = shim.conn.Write([]byte{' '})
assert.NotNil(t, err)
// The shim just lost its connection because we timed out waiting for the
// runtime to start the process. Connect again.
shim = rig.ServeNewShim(token)
// Do the same, but now mocking the runtime starting a process.
var wg sync.WaitGroup
stdinData := "hello2\n"
wg.Add(1)
go func() {
time.Sleep(smallWaitTimeout)
err := shim.client.SendStdin([]byte(stdinData))
assert.Nil(t, err)
wg.Done()
}()
execcmd := hyperstart.ExecCommand{
Container: testContainerID,
Process: hyperstart.Process{
Args: []string{"/bin/sh"},
},
}
_, err = rig.Client.HyperWithTokens("execcmd", []string{token}, &execcmd)
assert.Nil(t, err)
wg.Wait()
// check stdin data arrives correctly to hyperstart
buf := make([]byte, 512)
n, _ := rig.Hyperstart.ReadIo(buf)
assert.Equal(t, len(stdinData)+12, n)
assert.Equal(t, stdinData, string(buf[12:n]))
// Cleanup
shim.close()
rig.Stop()
}
// This test ensures that a shim will receive the exit code signal even if
// the process inside the corresponding container has not been started.
// This case occurs when a container has been created and it receives a KILL
// or TERM signal. We don't forward the signal to hyperstart because nothing
// runs inside the VM, but we still need to remove the shim which is seen as
// the container process from any orchestrator.
func testShimSignalProcessNotStarted(t *testing.T, signal syscall.Signal, expectFail bool) {
rig := newTestRig(t)
rig.Start()
token := rig.RegisterVM()
shim := rig.ServeNewShim(token)
session := peekIOSession(rig.proxy, token)
session.containerID = testContainerID
// Don't close session.processStarted because we want to simulate
// a case where the container has been created but not started.
// Send signal and check the shim receives an exit code.
err := shim.client.Kill(signal)
if expectFail {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
}
// Cleanup
shim.close()
rig.Stop()
}
func TestShimKillSignalProcessNotStarted(t *testing.T) {
testShimSignalProcessNotStarted(t, syscall.SIGKILL, false)
}
func TestShimTermSignalProcessNotStarted(t *testing.T) {
testShimSignalProcessNotStarted(t, syscall.SIGTERM, false)
}
func TestShimUsr1SignalProcessNotStarted(t *testing.T) {
testShimSignalProcessNotStarted(t, syscall.SIGUSR1, true)
}
func TestValidateLogEntry(t *testing.T) {
tests := []struct {
payload api.LogEntry
valid bool
}{
// Invalid source.
{api.LogEntry{Source: "foo"}, false},
// Can't log with hyperstart/proxy/qemu sources from client API
{api.LogEntry{Source: "hyperstart"}, false},
{api.LogEntry{Source: "proxy"}, false},
{api.LogEntry{Source: "qemu"}, false},
// No empty messages.
{api.LogEntry{Source: "shim", Level: "warn"}, false},
{api.LogEntry{Source: "shim", Level: "warn"}, false},
// Invalid Levels.
{api.LogEntry{Source: "shim", Level: "garbage", Message: "foo"}, false},
{api.LogEntry{Source: "shim", Level: "fatal", Message: "foo"}, false},
{api.LogEntry{Source: "shim", Level: "panic", Message: "foo"}, false},
// One that works!
{api.LogEntry{Source: "shim", Level: "warn", Message: "Something bad happened"}, true},
}
for _, test := range tests {
err := validateLogEntry(&test.payload)
t.Log(test)
if test.valid {
assert.Nil(t, err)
continue
}
assert.NotNil(t, err)
}
}
func TestLog(t *testing.T) {
rig := newTestRig(t)
rig.SetRunHyperstart(false)
rig.Start()
// Test Log()
const warningMessage = "A warning!"
hook := test.NewGlobal()
rig.Client.Log(goapi.LogLevelWarn, goapi.LogSourceShim, testContainerID, warningMessage)
SyncWithProxy(rig.Client)
entry := hook.LastEntry()
assert.NotNil(t, entry)
assert.Equal(t, logrus.WarnLevel, entry.Level)
assert.Equal(t, "shim", entry.Data["source"])
assert.Equal(t, testContainerID, entry.Data["container"])
assert.Equal(t, warningMessage, entry.Message)
// Test Logf()
rig.Client.Logf(goapi.LogLevelError, goapi.LogSourceRuntime, testContainerID, "foo %s", "bar")
SyncWithProxy(rig.Client)
entry = hook.LastEntry()
assert.NotNil(t, entry)
assert.Equal(t, logrus.ErrorLevel, entry.Level)
assert.Equal(t, "runtime", entry.Data["source"])
assert.Equal(t, "foo bar", entry.Message)
rig.Stop()
}
func TestHyperstartResponse(t *testing.T) {
rig := newTestRig(t)
rig.Start()
ctlSocketPath, ioSocketPath := rig.Hyperstart.GetSocketPaths()
_, err := rig.Client.RegisterVM(testContainerID, ctlSocketPath, ioSocketPath, nil)
assert.Nil(t, err)
// Pre-fill what hyper will return.
const testMsg = "Foo Foo!"
msgData := []byte(testMsg)
rig.Hyperstart.SendMessage(hyperstart.AckCode, msgData)
// Trigger and message + response.
data, err := rig.Client.Hyper("ping", msgData)
assert.Nil(t, err)
assert.Equal(t, msgData, data)
rig.Stop()
}
func TestGetSocketPath(t *testing.T) {
p, err := getSocketPath("")
assert.Nil(t, err)
assert.Equal(t, p, legacySocketPath)
// Test for default socket path
DefaultSocketPath = "/proxy/socket/path"
p, err = getSocketPath("")
assert.Nil(t, err, err)
assert.Equal(t, p, DefaultSocketPath)
// Test that a passed socket path takes precedence
var cliSocketPath = "/cli/proxy/socket/path"
ArgSocketPath = &cliSocketPath
p, err = getSocketPath("")
assert.Nil(t, err, err)
assert.Equal(t, p, cliSocketPath)
// Test for too long socket paths
longPath := make([]byte, socketPathMaxLength+1)
cliSocketPath = string(longPath)
ArgSocketPath = &cliSocketPath
p, err = getSocketPath("")
assert.NotNil(t, err, err)
assert.Equal(t, p, "")
// reset
*ArgSocketPath = ""
// Ensure specified socket used
sp := "/je/suis/un/chemin/de/socket"
p, err = getSocketPath(sp)
assert.NoError(t, err)
assert.Equal(t, sp, p)
}