Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
R
re6stnet
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
zhifan huang
re6stnet
Commits
5100b8e3
Commit
5100b8e3
authored
Apr 20, 2022
by
zhifan huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
2 node connect direct test
parent
f2d64ed7
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
148 additions
and
24 deletions
+148
-24
re6st/tests/test_network/my_net.py
re6st/tests/test_network/my_net.py
+119
-12
re6st/tests/test_network/re6st_wrap.py
re6st/tests/test_network/re6st_wrap.py
+20
-3
re6st/tests/test_network/test_ping.py
re6st/tests/test_network/test_ping.py
+8
-8
setup.py
setup.py
+1
-1
No files found.
re6st/tests/test_network/my_net.py
View file @
5100b8e3
import
subprocess
import
time
import
random
import
weakref
import
sys
class
NetManager
(
object
):
"""contain all the nemu object created, so they can live more time"""
def
__init__
(
self
):
self
.
object
=
[]
self
.
registrys
=
{}
class
Device
(
object
):
app1
=
subprocess
.
Popen
([
'unshare'
,
'-n'
],
stdin
=
subprocess
.
PIPE
)
app2
=
subprocess
.
Popen
([
'unshare'
,
'-n'
],
stdin
=
subprocess
.
PIPE
)
_id
=
0
print
(
"pid, 1:{}, 2:{}"
.
format
(
app1
.
pid
,
app2
.
pid
))
subprocess
.
check_call
([
'ip'
,
'link'
,
'add'
,
'veth0'
,
'netns'
,
str
(
app1
.
pid
)
,
'type'
,
'veth'
,
'peer'
,
'veth1'
,
'netns'
,
str
(
app2
.
pid
)])
@
classmethod
def
get_id
(
cls
):
cls
.
_id
+=
1
return
cls
.
_id
subprocess
.
check_call
([
'nsenter'
,
'-t'
,
str
(
app1
.
pid
),
'-n'
]
+
[
'ip'
,
'link'
,
'set'
,
'up'
,
'veth0'
])
def
__init__
(
self
,
type
,
name
=
None
):
"""
type: device type, str
name: device name, str. if not set, generate one name
"""
# name if name else .....
self
.
type
=
type
self
.
name
=
name
or
"{}-{}"
.
format
(
type
,
self
.
get_id
())
self
.
ips
=
[]
self
.
_up
=
False
subprocess
.
check_call
([
'nsenter'
,
'-t'
,
str
(
app2
.
pid
),
'-n'
]
+
[
'ip'
,
'link'
,
'set'
,
'up'
,
'veth1'
])
subprocess
.
check_call
([
'nsenter'
,
'-t'
,
str
(
app1
.
pid
),
'-n'
]
+
[
'ip'
,
'addr'
,
'add'
,
'dev'
,
'veth0'
,
'10.1.1.1/24'
])
subprocess
.
check_call
([
'nsenter'
,
'-t'
,
str
(
app2
.
pid
),
'-n'
]
+
[
'ip'
,
'addr'
,
'add'
,
'dev'
,
'veth1'
,
'10.1.1.2/24'
])
# subprocess.check_call(['nsenter', '-t', str(app1.pid), '-n','ip', 'route', 'add', '10.1.1.0/24', 'via', '10.1.1.1'])
# subprocess.check_call(['ip', 'route', 'add', '10.1.1.0/24', 'via', '10.1.1.2'])
subprocess
.
check_call
([
'nsenter'
,
'-t'
,
str
(
app2
.
pid
),
'-n'
]
+
[
"ping"
,
"10.1.1.1"
])
@
property
def
up
(
self
):
"bool value control device up or not"
return
self
.
_up
print
(
"good bye!"
)
\ No newline at end of file
@
up
.
setter
def
up
(
self
,
value
):
value
=
"up"
if
value
else
"down"
self
.
net
.
run
([
'ip'
,
'link'
,
'set'
,
'up'
,
self
.
name
])
def
add_ip4
(
self
,
address
,
prefix
):
ip
=
"{}/{}"
.
format
(
address
,
prefix
)
self
.
ips
.
append
(
address
)
self
.
net
.
run
([
'ip'
,
'addr'
,
'add'
,
ip
,
'dev'
,
self
.
name
])
class
Netns
(
object
):
"""a network namespace"""
def
__init__
(
self
):
self
.
devices
=
[]
self
.
app
=
subprocess
.
Popen
([
'unshare'
,
'-n'
],
stdin
=
subprocess
.
PIPE
)
self
.
pid
=
self
.
app
.
pid
lo
=
Device
(
"lo"
,
name
=
"lo"
)
self
.
add_device
(
lo
)
lo
.
up
=
True
self
.
run
([
'sysctl'
,
'-w'
,
'net.ipv4.ip_forward=1'
])
self
.
run
([
'sysctl'
,
'-w'
,
'net.ipv6.conf.default.forwarding=1'
],
stderr
=
sys
.
stderr
)
def
Popen
(
self
,
cmd
,
stdout
=
sys
.
stdout
,
stderr
=
sys
.
stderr
,
**
kw
):
""" a wrapper for subprocess.Popen"""
return
subprocess
.
Popen
([
'nsenter'
,
'-t'
,
str
(
self
.
pid
),
'-n'
]
+
cmd
,
stdout
=
stdout
,
stderr
=
stderr
,
**
kw
)
def
run
(
self
,
cmd
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
,
**
kw
):
""" wrapper for subprocess.checkout"""
subprocess
.
check_call
([
'nsenter'
,
'-t'
,
str
(
self
.
pid
),
'-n'
]
+
cmd
,
stdout
=
stdout
,
stderr
=
stderr
,
**
kw
)
def
add_device
(
self
,
dev
):
self
.
devices
.
append
(
dev
)
dev
.
net
=
weakref
.
proxy
(
self
)
@
staticmethod
def
connect_direct
(
node1
,
node2
):
"""connect 2 node by veth"""
dev1
=
Device
(
"veth"
)
dev2
=
Device
(
"veth"
)
node1
.
add_device
(
dev1
)
node2
.
add_device
(
dev2
)
subprocess
.
check_call
([
'ip'
,
'link'
,
'add'
,
dev1
.
name
,
'netns'
,
str
(
node1
.
pid
)
,
'type'
,
'veth'
,
'peer'
,
dev2
.
name
,
'netns'
,
str
(
node2
.
pid
)])
dev1
.
add_ip4
(
"10.1.1.1"
,
prefix
=
24
)
dev2
.
add_ip4
(
"10.1.1.2"
,
prefix
=
24
)
dev1
.
up
=
dev2
.
up
=
True
@
property
def
ip
(
self
):
return
self
.
out
.
ips
[
-
1
]
@
property
def
out
(
self
):
return
self
.
devices
[
-
1
]
def
main
():
app1
=
Netns
()
app2
=
Netns
()
print
(
"pid, 1:{}, 2:{}"
.
format
(
app1
.
pid
,
app2
.
pid
))
Netns
.
connect_direct
(
app1
,
app2
)
# subprocess.check_call(['nsenter', '-t', str(app1.pid), '-n','ip', 'route', 'add', '10.1.1.0/24', 'via', '10.1.1.1'])
# subprocess.check_call(['ip', 'route', 'add', '10.1.1.0/24', 'via', '10.1.1.2'])
subprocess
.
check_call
([
'nsenter'
,
'-t'
,
str
(
app2
.
pid
),
'-n'
]
+
[
"ping"
,
"10.1.1.1"
])
def
net_simple
():
nm
=
NetManager
()
node1
=
Netns
()
node2
=
Netns
()
Netns
.
connect_direct
(
node1
,
node2
)
# subprocess.check_call(['nsenter', '-t', str(node2.pid), '-n'] + ["ping", '-c', '1', "10.1.1.1"])
nm
.
registrys
[
node1
]
=
[
node2
]
return
nm
if
__name__
==
"__main__"
:
main
()
print
(
"good bye!"
)
re6st/tests/test_network/re6st_wrap.py
View file @
5100b8e3
...
...
@@ -16,7 +16,8 @@ WORK_DIR = Path(__file__).parent.resolve() / "temp_net_test"
DH_FILE
=
WORK_DIR
/
"dh2048.pem"
RE6STNET
=
"re6stnet"
RE6ST_REGISTRY
=
"re6st-registry"
#RE6ST_REGISTRY = "re6st-registry"
RE6ST_REGISTRY
=
"python -m re6st.cli.registry"
RE6ST_CONF
=
"re6st-conf"
def
initial
():
...
...
@@ -67,6 +68,10 @@ class Re6stRegistry(object):
self
.
run
()
# wait the servcice started
while
(
not
self
.
log
.
exists
()):
time
.
sleep
(
0.1
)
@
classmethod
def
generate_name
(
cls
):
cls
.
registry_seq
+=
1
...
...
@@ -94,6 +99,12 @@ class Re6stRegistry(object):
self
.
proc
=
self
.
node
.
Popen
(
cmd
,
stdout
=
PIPE
,
stderr
=
PIPE
)
def
__del__
(
self
):
try
:
self
.
proc
.
terminate
()
except
:
pass
class
Re6stNode
(
object
):
"""class run a re6stnet service on a namespace"""
node_seq
=
-
1
...
...
@@ -149,7 +160,7 @@ class Re6stNode(object):
def
create_node
(
self
):
"""create necessary file for node"""
sys
.
stderr
.
write
(
"
create file for node {}
"
.
format
(
self
.
name
))
sys
.
stderr
.
write
(
"
---create file for node {}---
\
n
"
.
format
(
self
.
name
))
cmd
=
"{script} --registry {registry_url} --email {email}"
cmd
=
cmd
.
format
(
script
=
RE6ST_CONF
,
registry_url
=
self
.
registry
.
url
,
email
=
self
.
email
).
split
()
...
...
@@ -162,7 +173,7 @@ class Re6stNode(object):
while
not
token
:
time
.
sleep
(.
1
)
token
=
db
.
execute
(
"SELECT token FROM token WHERE email=?"
,
(
self
.
email
,)).
fetchone
()
(
self
.
email
,)).
fetchone
()
count
+=
1
if
count
>
100
:
raise
Exception
(
"can't connect to the Register"
)
...
...
@@ -186,3 +197,9 @@ class Re6stNode(object):
console
=
self
.
console
).
split
()
cmd
+=
args
self
.
proc
=
self
.
node
.
Popen
(
cmd
,
stdout
=
PIPE
,
stderr
=
PIPE
)
def
__del__
(
self
):
try
:
self
.
proc
.
terminate
()
except
:
pass
re6st/tests/test_network/test_ping.py
View file @
5100b8e3
...
...
@@ -7,9 +7,9 @@ import sys
import
os
from
pathlib2
import
Path
from
network_build
import
*
import
my_net
PING_PATH
=
Path
(
__file__
).
parent
.
resolve
()
/
"ping.py"
PING_PATH
=
str
(
Path
(
__file__
).
parent
.
resolve
()
/
"ping.py"
)
def
wait_stable
(
nodes
,
timeout
=
180
):
sys
.
stderr
.
write
(
"wait stalbe
\
n
"
)
...
...
@@ -19,7 +19,7 @@ def wait_stable(nodes, timeout=180):
for
node
in
nodes
:
sub_ips
=
set
(
ips
)
-
{
node
.
ip6
}
node
.
ping_proc
=
node
.
node
.
Popen
(
"python {} --retry -a {} "
.
format
(
PING_PATH
,
' '
.
join
(
sub_ips
)),
shell
=
True
)
[
"python"
,
PING_PATH
,
'--retry'
,
'-a'
]
+
list
(
sub_ips
)
)
unfinished
=
list
(
nodes
)
# check all the node network can ping each other
while
len
(
unfinished
):
...
...
@@ -41,8 +41,7 @@ def ping_test(nodes):
for
node
in
nodes
:
sub_ips
=
set
(
ips
)
-
{
node
.
ip6
}
node
.
ping_proc
=
node
.
node
.
Popen
(
"python {} -a {} "
.
format
(
PING_PATH
,
' '
.
join
(
sub_ips
)),
shell
=
True
,
stdout
=
subprocess
.
PIPE
)
[
"python"
,
PING_PATH
,
'--retry'
,
'-a'
]
+
list
(
sub_ips
),
stdout
=
subprocess
.
PIPE
)
out
,
_
=
node
.
ping_proc
.
communicate
()
unreached
=
[
ips
[
addr
]
for
addr
in
out
.
split
()]
if
unreached
:
...
...
@@ -78,19 +77,20 @@ class TestPing(unittest.TestCase):
"""create a network demo, test the connectivity by ping
wait the network stable then ping 3 times
"""
nm
=
network_direct
()
nm
=
my_net
.
net_simple
()
net
=
nm
.
registrys
nodes
=
[]
registrys
=
[]
for
registry
in
net
:
reg
=
re6st_wrap
.
Re6stRegistry
(
registry
,
"2001:db8:42::"
,
recreate
=
Fals
e
)
reg
=
re6st_wrap
.
Re6stRegistry
(
registry
,
"2001:db8:42::"
,
recreate
=
Tru
e
)
reg_node
=
re6st_wrap
.
Re6stNode
(
registry
,
reg
,
name
=
reg
.
name
)
registrys
.
append
(
registry
)
reg_node
.
run
(
"--gateway"
,
"--disable-proto"
,
"none"
,
"--ip"
,
registry
.
ip
)
nodes
.
append
(
reg_node
)
for
m
in
net
[
registry
]:
print
(
"add nodes"
)
node
=
re6st_wrap
.
Re6stNode
(
m
,
reg
)
node
.
run
(
"-i"
+
m
.
iface
.
name
)
node
.
run
(
"-i"
+
m
.
out
.
name
)
nodes
.
append
(
node
)
wait_stable
(
nodes
)
...
...
setup.py
View file @
5100b8e3
...
...
@@ -95,7 +95,7 @@ setup(
install_requires
=
[
'pyOpenSSL >= 0.13'
,
'miniupnpc'
],
extras_require
=
{
'geoip'
:
[
'geoip2'
],
'test'
:
[
'mock'
,
'pathlib2'
,
'
nemu'
,
'python-unshare'
,
'passfd
'
]
'test'
:
[
'mock'
,
'pathlib2'
,
'
multiping
'
]
},
#dependency_links = [
# "http://miniupnp.free.fr/files/download.php?file=miniupnpc-1.7.20120714.tar.gz#egg=miniupnpc-1.7",
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment