Files
ziti/tests/addressable_terminators_test.go
Paul Lorenz 86092a8640 Migrate to the sdk-golang v2 module path. For #3884
Bumps the sdk-golang dependency from v1 to the v2 module
(`github.com/openziti/sdk-golang/v2` at v2.0.0-pre1) and updates all
import paths. This is a no-behavior-change precursor that isolates the
dependency migration from the Connect-V2 feature work in #3884.

- Rewrites `github.com/openziti/sdk-golang/...` imports to
  `github.com/openziti/sdk-golang/v2/...` across the main and zititest
  modules.
- Pins both modules to `github.com/openziti/sdk-golang/v2 v2.0.0-pre1`.
- Adapts `edgeXgressConn.AcceptMessage` to the v2 `MsgSink` signature,
  which now takes an `edge.SdkChannel` argument.
- Replaces the removed `edge.Conn.GetRouterId()` with
  `RemoteAddr().String()` in the loop4 traffic-test logging.

For openziti/sdk-golang#936.
2026-06-23 15:43:39 -04:00

222 lines
5.6 KiB
Go

//go:build dataflow
/*
Copyright NetFoundry Inc.
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
https://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 tests
import (
"net"
"strings"
"testing"
"time"
"github.com/openziti/sdk-golang/v2/ziti"
"github.com/openziti/sdk-golang/v2/ziti/edge"
"github.com/openziti/ziti/v2/controller/xt_smartrouting"
"github.com/pkg/errors"
)
func Test_AddressableTerminators(t *testing.T) {
ctx := NewTestContext(t)
defer ctx.Teardown()
ctx.StartServer()
ctx.RequireAdminManagementApiLogin()
service := ctx.AdminManagementSession.RequireNewServiceAccessibleToAll(xt_smartrouting.Name)
ctx.CreateEnrollAndStartEdgeRouter()
type host struct {
id *identity
context ziti.Context
listener edge.Listener
}
var hosts []*host
var err error
for i := 0; i < 2; i++ {
host := &host{}
hosts = append(hosts, host)
host.id, host.context = ctx.AdminManagementSession.RequireCreateSdkContext()
defer host.context.Close()
host.listener, err = host.context.ListenWithOptions(service.Name, &ziti.ListenOptions{
BindUsingEdgeIdentity: true,
WaitForNEstablishedListeners: 1,
})
ctx.Req.NoError(err)
}
type client struct {
id *identity
context ziti.Context
}
var clients []*client
for i := 0; i < 3; i++ {
client := &client{}
clients = append(clients, client)
client.id, client.context = ctx.AdminManagementSession.RequireCreateSdkContext()
defer client.context.Close()
}
waitForConn := func(listener net.Listener, timeout time.Duration) (net.Conn, error) {
connC := make(chan net.Conn, 1)
errC := make(chan error, 1)
go func() {
conn, err := listener.Accept()
if err != nil {
errC <- err
} else {
connC <- conn
}
}()
select {
case conn := <-connC:
return conn, nil
case err := <-errC:
return nil, err
case <-time.After(timeout):
return nil, errors.Errorf("timed out waiting for connection after %v", timeout)
}
}
for _, client := range clients {
for _, host := range hosts {
conn, err := client.context.DialWithOptions(service.Name, &ziti.DialOptions{
Identity: host.id.name,
})
ctx.Req.NoError(err)
hostConn, err := waitForConn(host.listener, time.Second)
ctx.Req.NoError(err)
ctx.Req.True(strings.Contains(hostConn.LocalAddr().String(), client.id.name))
ctx.Req.NoError(conn.Close())
ctx.Req.NoError(hostConn.Close())
}
}
}
func Test_AddressableTerminatorSameIdentity(t *testing.T) {
ctx := NewTestContext(t)
defer ctx.Teardown()
ctx.StartServer()
ctx.RequireAdminManagementApiLogin()
service := ctx.AdminManagementSession.RequireNewServiceAccessibleToAll(xt_smartrouting.Name)
ctx.CreateEnrollAndStartEdgeRouter()
errorC := make(chan error, 1)
errorHandler := func(err error) {
select {
case errorC <- err:
default:
}
}
identity, context := ctx.AdminManagementSession.RequireCreateSdkContext()
defer context.Close()
listener, err := context.ListenWithOptions(service.Name, &ziti.ListenOptions{
BindUsingEdgeIdentity: true,
ConnectTimeout: 5 * time.Second,
})
ctx.Req.NoError(err)
listener.(edge.SessionListener).SetErrorEventHandler(errorHandler)
defer func() { _ = listener.Close() }()
context2, err := ziti.NewContext(identity.config)
ctx.Req.NoError(err)
listener2, err := context2.ListenWithOptions(service.Name, &ziti.ListenOptions{
BindUsingEdgeIdentity: true,
ConnectTimeout: 5 * time.Second,
})
listener2.(edge.SessionListener).SetErrorEventHandler(errorHandler)
ctx.Req.NoError(err)
defer func() { _ = listener2.Close() }()
select {
case err = <-errorC:
case <-time.After(5 * time.Second):
err = nil
}
ctx.Req.NoError(err)
}
func Test_AddressableTerminatorDifferentIdentity(t *testing.T) {
ctx := NewTestContext(t)
defer ctx.Teardown()
ctx.StartServer()
ctx.RequireAdminManagementApiLogin()
service := ctx.AdminManagementSession.RequireNewServiceAccessibleToAll(xt_smartrouting.Name)
ctx.CreateEnrollAndStartEdgeRouter()
errorC := make(chan error, 1)
errorHandler := func(err error) {
select {
case errorC <- err:
default:
}
}
_, context := ctx.AdminManagementSession.RequireCreateSdkContext()
defer context.Close()
listener, err := context.ListenWithOptions(service.Name, &ziti.ListenOptions{
Identity: "foobar",
ConnectTimeout: 5 * time.Second,
})
listener.(edge.SessionListener).SetErrorEventHandler(errorHandler)
ctx.Req.NoError(err)
defer func() { _ = listener.Close() }()
clientIdentity := ctx.AdminManagementSession.RequireNewIdentityWithOtt(false)
clientConfig := ctx.EnrollIdentity(clientIdentity.Id)
clientContext, err := ziti.NewContext(clientConfig)
ctx.Req.NoError(err)
defer clientContext.Close()
listener2, err := clientContext.ListenWithOptions(service.Name, &ziti.ListenOptions{
Identity: "foobar",
ConnectTimeout: 5 * time.Second,
})
ctx.Req.NoError(err)
listener2.(edge.SessionListener).SetErrorEventHandler(errorHandler)
defer func() { _ = listener2.Close() }()
select {
case err = <-errorC:
case <-time.After(10 * time.Second):
err = nil
}
if err == nil {
ctx.Req.True(listener2.IsClosed(), "listener must be closed")
} else {
ctx.Req.Error(err)
ctx.Req.Contains(err.Error(), "shared identity foobar belongs to different identity")
}
}