@@ -37,18 +37,26 @@ func TestComposeCancel(t *testing.T) {
3737 c := NewParallelCLI (t )
3838
3939 t .Run ("metrics on cancel Compose build" , func (t * testing.T ) {
40- c .RunDockerComposeCmd (t , "ls" )
41- buildProjectPath := "fixtures/build-infinite/compose.yaml"
40+ const buildProjectPath = "fixtures/build-infinite/compose.yaml"
41+
42+ ctx , cancel := context .WithCancel (context .Background ())
43+ defer cancel ()
4244
4345 // require a separate groupID from the process running tests, in order to simulate ctrl+C from a terminal.
4446 // sending kill signal
45- stdout := & utils.SafeBuffer {}
46- stderr := & utils. SafeBuffer {}
47- cmd , err := StartWithNewGroupID ( context . Background () ,
47+ var stdout , stderr utils.SafeBuffer
48+ cmd , err := StartWithNewGroupID (
49+ ctx ,
4850 c .NewDockerComposeCmd (t , "-f" , buildProjectPath , "build" , "--progress" , "plain" ),
49- stdout ,
50- stderr )
51+ & stdout ,
52+ & stderr ,
53+ )
5154 assert .NilError (t , err )
55+ processDone := make (chan error , 1 )
56+ go func () {
57+ defer close (processDone )
58+ processDone <- cmd .Wait ()
59+ }()
5260
5361 c .WaitForCondition (t , func () (bool , string ) {
5462 out := stdout .String ()
@@ -58,15 +66,21 @@ func TestComposeCancel(t *testing.T) {
5866 errors )
5967 }, 30 * time .Second , 1 * time .Second )
6068
61- err = syscall .Kill (- cmd .Process .Pid , syscall .SIGINT ) // simulate Ctrl-C : send signal to processGroup, children will have same groupId by default
69+ // simulate Ctrl-C : send signal to processGroup, children will have same groupId by default
70+ err = syscall .Kill (- cmd .Process .Pid , syscall .SIGINT )
6271 assert .NilError (t , err )
6372
64- c .WaitForCondition (t , func () (bool , string ) {
65- out := stdout .String ()
66- errors := stderr .String ()
67- return strings .Contains (out , "CANCELED" ), fmt .Sprintf ("'CANCELED' not found in : \n %s\n Stderr: \n %s\n " , out ,
68- errors )
69- }, 10 * time .Second , 1 * time .Second )
73+ select {
74+ case <- ctx .Done ():
75+ t .Fatal ("test context canceled" )
76+ case err := <- processDone :
77+ // TODO(milas): Compose should really not return exit code 130 here,
78+ // this is an old hack for the compose-cli wrapper
79+ assert .Error (t , err , "exit status 130" ,
80+ "STDOUT:\n %s\n STDERR:\n %s\n " , stdout .String (), stderr .String ())
81+ case <- time .After (10 * time .Second ):
82+ t .Fatal ("timeout waiting for Compose exit" )
83+ }
7084 })
7185}
7286
0 commit comments