func (s *IntSuite) TestTwoSites(c *check.C) {
	username := s.me.Username

	a := NewInstance("site-A", Host, s.getPorts(5), s.priv, s.pub)
	b := NewInstance("site-B", Host, s.getPorts(5), s.priv, s.pub)

	a.AddUser(username, []string{username})
	b.AddUser(username, []string{username})

	c.Assert(b.Create(a.Secrets.AsSlice(), false, nil), check.IsNil)
	c.Assert(a.Create(b.Secrets.AsSlice(), true, nil), check.IsNil)

	c.Assert(b.Start(), check.IsNil)
	c.Assert(a.Start(), check.IsNil)

	// wait for both sites to see each other via their reverse tunnels (for up to 10 seconds)
	abortTime := time.Now().Add(time.Second * 10)
	for len(b.Tunnel.GetSites()) < 2 && len(b.Tunnel.GetSites()) < 2 {
		time.Sleep(time.Millisecond * 200)
		if time.Now().After(abortTime) {
			c.Fatalf("two sites do not see each other: tunnels are not working")
		}
	}

	var (
		outputA bytes.Buffer
		outputB bytes.Buffer
	)

	// if we got here, it means two sites are cross-connected. lets execute SSH commands
	sshPort := a.GetPortSSHInt()
	cmd := []string{"echo", "hello world"}

	// directly:
	tc, err := a.NewClient(username, "site-A", Host, sshPort)
	tc.Stdout = &outputA
	c.Assert(err, check.IsNil)
	err = tc.SSH(cmd, false)
	c.Assert(err, check.IsNil)
	c.Assert(outputA.String(), check.Equals, "hello world\n")

	// via tunnel b->a:
	tc, err = b.NewClient(username, "site-A", Host, sshPort)
	tc.Stdout = &outputB
	c.Assert(err, check.IsNil)
	err = tc.SSH(cmd, false)
	c.Assert(err, check.IsNil)
	c.Assert(outputA, check.DeepEquals, outputB)

	// Stop "site-A" and try to connect to it again via "site-A" (expect a connection error)
	a.Stop(false)
	err = tc.SSH(cmd, false)
	c.Assert(err, check.ErrorMatches, "Failed connecting to cluster site-A: ssh: subsystem request failed")

	// Reset and start "Site-A" again
	a.Reset()
	err = a.Start()
	c.Assert(err, check.IsNil)

	// try to execute an SSH command using the same old client  to Site-B
	// "site-A" and "site-B" reverse tunnels are supposed to reconnect,
	// and 'tc' (client) is also supposed to reconnect
	for i := 0; i < 10; i++ {
		time.Sleep(time.Millisecond * 5)
		err = tc.SSH(cmd, false)
		if err == nil {
			break
		}
	}
	c.Assert(err, check.IsNil)

	// stop both sites for realZ
	c.Assert(b.Stop(true), check.IsNil)
	c.Assert(a.Stop(true), check.IsNil)
}
// TestTwoSites creates two teleport sites: "a" and "b" and
// creates a tunnel from A to B.
//
// Then it executes an SSH command on A by connecting directly
// to A and by connecting to B via B<->A tunnel
TestTwoSites creates two teleport sites: "a" and "b" and
creates a tunnel from A to B.

Then it executes an SSH command on A by connecting directly
to A and by connecting to B via B<->A tunnel
// TestTwoSites creates two teleport sites : " a " and " b " and // creates a tunnel from A to B . // // Then it executes an SSH command on A by connecting directly // to A and by connecting to B via B< - > A tunnel
func (s *IntSuite) TestTwoSites(c *check.C) {
	username := s.me.Username
	a := NewInstance("site-A", Host, s.getPorts(5), s.priv, s.pub)
	b := NewInstance("site-B", Host, s.getPorts(5), s.priv, s.pub)
	a.AddUser(username, []string{username})
	b.AddUser(username, []string{username})
	c.Assert(b.Create(a.Secrets.AsSlice(), false, nil), check.IsNil)
	c.Assert(a.Create(b.Secrets.AsSlice(), true, nil), check.IsNil)
	c.Assert(b.Start(), check.IsNil)
	c.Assert(a.Start(), check.IsNil)
	abortTime := time.Now().Add(time.Second * 10)
	for len(b.Tunnel.GetSites()) < 2 && len(b.Tunnel.GetSites()) < 2 {
		time.Sleep(time.Millisecond * 200)
		if time.Now().After(abortTime) {
			c.Fatalf("two sites do not see each other: tunnels are not working")
		}
	}
	var (
		outputA bytes.Buffer
		outputB bytes.Buffer
	)
	sshPort := a.GetPortSSHInt()
	cmd := []string{"echo", "hello world"}
	tc, err := a.NewClient(username, "site-A", Host, sshPort)
	tc.Stdout = &outputA
	c.Assert(err, check.IsNil)
	err = tc.SSH(cmd, false)
	c.Assert(err, check.IsNil)
	c.Assert(outputA.String(), check.Equals, "hello world\n")
	tc, err = b.NewClient(username, "site-A", Host, sshPort)
	tc.Stdout = &outputB
	c.Assert(err, check.IsNil)
	err = tc.SSH(cmd, false)
	c.Assert(err, check.IsNil)
	c.Assert(outputA, check.DeepEquals, outputB)
	a.Stop(false)
	err = tc.SSH(cmd, false)
	c.Assert(err, check.ErrorMatches, "Failed connecting to cluster site-A: ssh: subsystem request failed")
	a.Reset()
	err = a.Start()
	c.Assert(err, check.IsNil)
	for i := 0; i < 10; i++ {
		time.Sleep(time.Millisecond * 5)
		err = tc.SSH(cmd, false)
		if err == nil {
			break
		}
	}
	c.Assert(err, check.IsNil)
	c.Assert(b.Stop(true), check.IsNil)
	c.Assert(a.Stop(true), check.IsNil)
}
func ( s * IntSuite ) TestTwoSites ( c * check . C ) { username := s . me . Username a := NewInstance ( " " , Host , s . getPorts ( 5 ) , s . priv , s . pub ) b := NewInstance ( " " , Host , s . getPorts ( 5 ) , s . priv , s . pub ) a . AddUser ( username , [ ] string { username } ) b . AddUser ( username , [ ] string { username } ) c . Assert ( b . Create ( a . Secrets . AsSlice ( ) , false , nil ) , check . IsNil ) c . Assert ( a . Create ( b . Secrets . AsSlice ( ) , true , nil ) , check . IsNil ) c . Assert ( b . Start ( ) , check . IsNil ) c . Assert ( a . Start ( ) , check . IsNil ) abortTime := time . Now ( ) . Add ( time . Second * 10 ) for len ( b . Tunnel . GetSites ( ) ) < 2 && len ( b . Tunnel . GetSites ( ) ) < 2 { time . Sleep ( time . Millisecond * 200 ) if time . Now ( ) . After ( abortTime ) { c . Fatalf ( " " ) } } var ( outputA bytes . Buffer outputB bytes . Buffer ) sshPort := a . GetPortSSHInt ( ) cmd := [ ] string { " " , " " } tc , err := a . NewClient ( username , " " , Host , sshPort ) tc . Stdout = & outputA c . Assert ( err , check . IsNil ) err = tc . SSH ( cmd , false ) c . Assert ( err , check . IsNil ) c . Assert ( outputA . String ( ) , check . Equals , " \n " ) tc , err = b . NewClient ( username , " " , Host , sshPort ) tc . Stdout = & outputB c . Assert ( err , check . IsNil ) err = tc . SSH ( cmd , false ) c . Assert ( err , check . IsNil ) c . Assert ( outputA , check . DeepEquals , outputB ) a . Stop ( false ) err = tc . SSH ( cmd , false ) c . Assert ( err , check . ErrorMatches , " " ) a . Reset ( ) err = a . Start ( ) c . Assert ( err , check . IsNil ) for i := 0 ; i < 10 ; i ++ { time . Sleep ( time . Millisecond * 5 ) err = tc . SSH ( cmd , false ) if err == nil { break } } c . Assert ( err , check . IsNil ) c . Assert ( b . Stop ( true ) , check . IsNil ) c . Assert ( a . Stop ( true ) , check . IsNil ) }
TestTwoSites creates two teleport sites: "a" and "b" and
creates a tunnel from A to B.
TestTwoSites creates two teleport sites : " a " and " b " and creates a tunnel from A to B .
// wait for both sites to see each other via their reverse tunnels (for up to 10 seconds) // if we got here, it means two sites are cross-connected. lets execute SSH commands // directly: // via tunnel b->a: // Stop "site-A" and try to connect to it again via "site-A" (expect a connection error) // Reset and start "Site-A" again // try to execute an SSH command using the same old client  to Site-B // "site-A" and "site-B" reverse tunnels are supposed to reconnect, // and 'tc' (client) is also supposed to reconnect // stop both sites for realZ