@@ -34,7 +34,6 @@ import (
34
34
"os/user"
35
35
"strconv"
36
36
"strings"
37
- "sync"
38
37
"testing"
39
38
"time"
40
39
@@ -2121,7 +2120,9 @@ func testKubeJoin(t *testing.T, suite *KubeSuite) {
2121
2120
2122
2121
// fooey
2123
2122
hostUsername := suite .me .Username
2124
- participantUsername := suite .me .Username + "-participant"
2123
+ peerUsername := suite .me .Username + "-peer"
2124
+ observer1Username := suite .me .Username + "-observer1"
2125
+ observer2Username := suite .me .Username + "-observer2"
2125
2126
kubeGroups := []string {kube .TestImpersonationGroup }
2126
2127
kubeUsers := []string {"alice@example.com" }
2127
2128
role , err := types .NewRole ("kubemaster" , types.RoleSpecV6 {
@@ -2152,7 +2153,9 @@ func testKubeJoin(t *testing.T, suite *KubeSuite) {
2152
2153
})
2153
2154
require .NoError (t , err )
2154
2155
teleport .AddUserWithRole (hostUsername , role )
2155
- teleport .AddUserWithRole (participantUsername , joinRole )
2156
+ teleport .AddUserWithRole (peerUsername , joinRole )
2157
+ teleport .AddUserWithRole (observer1Username , joinRole )
2158
+ teleport .AddUserWithRole (observer2Username , joinRole )
2156
2159
2157
2160
err = teleport .CreateEx (t , nil , tconf )
2158
2161
require .NoError (t , err )
@@ -2200,30 +2203,32 @@ func testKubeJoin(t *testing.T, suite *KubeSuite) {
2200
2203
// We need to wait for the exec request to be handled here for the session to be
2201
2204
// created. Sadly though the k8s API doesn't give us much indication of when that is.
2202
2205
var session types.SessionTracker
2203
- require .Eventually (t , func () bool {
2206
+ require .EventuallyWithT (t , func (t * assert. CollectT ) {
2204
2207
// We need to wait for the session to be created here. We can't use the
2205
2208
// session manager's WaitUntilExists method because it doesn't work for
2206
2209
// kubernetes sessions.
2207
2210
sessions , err := teleport .Process .GetAuthServer ().GetActiveSessionTrackers (context .Background ())
2208
- if err != nil || len (sessions ) == 0 {
2209
- return false
2211
+ assert .NoError (t , err )
2212
+ if assert .Len (t , sessions , 1 ) {
2213
+ session = sessions [0 ]
2210
2214
}
2211
-
2212
- session = sessions [0 ]
2213
- return true
2214
2215
}, 10 * time .Second , time .Second )
2215
2216
2216
2217
participantStdinR , participantStdinW , err := os .Pipe ()
2217
2218
require .NoError (t , err )
2218
2219
participantStdoutR , participantStdoutW , err := os .Pipe ()
2219
2220
require .NoError (t , err )
2220
- streamsMu := & sync.Mutex {}
2221
- streams := make ([]* client.KubeSession , 0 , 3 )
2222
- observerCaptures := make ([]* bytes.Buffer , 0 , 2 )
2221
+
2222
+ observerCaptures := make ([]* bytes.Buffer , 2 )
2223
2223
albProxy := helpers .MustStartMockALBProxy (t , teleport .Config .Proxy .WebAddr .Addr )
2224
2224
2225
2225
// join peer by KubeProxyAddr
2226
2226
group .Go (func () error {
2227
+ defer func () {
2228
+ // close participant stdout so that we can read it after till EOF
2229
+ participantStdoutW .Close ()
2230
+ }()
2231
+
2227
2232
tc , err := teleport .NewClient (helpers.ClientConfig {
2228
2233
Login : hostUsername ,
2229
2234
Cluster : helpers .Site ,
@@ -2238,50 +2243,52 @@ func testKubeJoin(t *testing.T, suite *KubeSuite) {
2238
2243
2239
2244
stream , err := kubeJoin (kube.ProxyConfig {
2240
2245
T : teleport ,
2241
- Username : participantUsername ,
2246
+ Username : peerUsername ,
2242
2247
KubeUsers : kubeUsers ,
2243
2248
KubeGroups : kubeGroups ,
2244
2249
}, tc , session , types .SessionPeerMode )
2245
2250
if err != nil {
2246
2251
return trace .Wrap (err )
2247
2252
}
2248
- streamsMu .Lock ()
2249
- streams = append (streams , stream )
2250
- streamsMu .Unlock ()
2253
+
2251
2254
stream .Wait ()
2252
- // close participant stdout so that we can read it after till EOF
2253
- participantStdoutW .Close ()
2255
+
2256
+ t .Cleanup (func () { _ = stream .Close () })
2257
+
2254
2258
return nil
2255
2259
})
2256
2260
2257
2261
// join observer by WebProxyAddr
2258
2262
group .Go (func () error {
2259
- stream , capture := kubeJoinByWebAddr (t , teleport , participantUsername , kubeUsers , kubeGroups )
2260
- streamsMu .Lock ()
2261
- streams = append (streams , stream )
2262
- observerCaptures = append (observerCaptures , capture )
2263
- streamsMu .Unlock ()
2263
+ stream , capture := kubeJoinByWebAddr (t , teleport , observer1Username , kubeUsers , kubeGroups )
2264
+ observerCaptures [0 ] = capture
2264
2265
stream .Wait ()
2266
+
2267
+ t .Cleanup (func () { _ = stream .Close () })
2265
2268
return nil
2266
2269
})
2267
2270
2268
2271
// join observer with ALPN conn upgrade
2269
2272
group .Go (func () error {
2270
- stream , capture := kubeJoinByALBAddr (t , teleport , participantUsername , kubeUsers , kubeGroups , albProxy .Addr ().String ())
2271
- streamsMu .Lock ()
2272
- streams = append (streams , stream )
2273
- observerCaptures = append (observerCaptures , capture )
2274
- streamsMu .Unlock ()
2273
+ stream , capture := kubeJoinByALBAddr (t , teleport , observer2Username , kubeUsers , kubeGroups , albProxy .Addr ().String ())
2274
+ observerCaptures [1 ] = capture
2275
2275
stream .Wait ()
2276
+
2277
+ t .Cleanup (func () { _ = stream .Close () })
2276
2278
return nil
2277
2279
})
2278
2280
2279
- // We wait again for the second user to finish joining the session.
2280
- // We allow a bit of time to pass here to give the session manager time to recognize the
2281
- // new IO streams of the second client.
2282
- time .Sleep (time .Second * 5 )
2281
+ // Wait for all users to finish joining the session.
2282
+ require .EventuallyWithT (t , func (t * assert.CollectT ) {
2283
+ session , err := teleport .Process .GetAuthServer ().GetSessionTracker (context .Background (), session .GetName ())
2284
+ if ! assert .NoError (t , err ) {
2285
+ return
2286
+ }
2287
+
2288
+ assert .Len (t , session .GetParticipants (), 4 )
2289
+ }, 30 * time .Second , 500 * time .Millisecond )
2283
2290
2284
- // sent a test message from the participant
2291
+ // send a test message from the participant
2285
2292
participantStdinW .Write ([]byte ("\a hi from peer\n \r " ))
2286
2293
2287
2294
// lets type "echo hi" followed by "enter" and then "exit" + "enter":
@@ -2306,8 +2313,8 @@ func testKubeJoin(t *testing.T, suite *KubeSuite) {
2306
2313
2307
2314
// Verify observers.
2308
2315
for _ , capture := range observerCaptures {
2309
- require .Contains (t , capture .String (), "hi from peer" )
2310
- require .Contains (t , capture .String (), "hi from term" )
2316
+ assert .Contains (t , capture .String (), "hi from peer" )
2317
+ assert .Contains (t , capture .String (), "hi from term" )
2311
2318
}
2312
2319
}
2313
2320
0 commit comments