Skip to content

Commit

Permalink
test: format and play around with peer module
Browse files Browse the repository at this point in the history
  • Loading branch information
verbit committed Jun 27, 2024
1 parent 42ce25e commit 81625ca
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 93 deletions.
166 changes: 73 additions & 93 deletions test/virtuerl_SUITE.erl
Original file line number Diff line number Diff line change
@@ -1,65 +1,35 @@
%%%-------------------------------------------------------------------
%%% @author ilya
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 11. Sep 2023 9:48 PM
%%%-------------------------------------------------------------------
-module(virtuerl_SUITE).
-author("ilya").

%% API
-export([all/0, init_per_suite/1, end_per_suite/1]).
-export([test_domain/1, test_network/1, test_create_domain/1, test_create_domain_dualstack/1]).

all() -> [test_create_domain, test_create_domain_dualstack].
-behaviour(ct_suite).

init_per_suite(Config) ->
{ok, _} = application:ensure_all_started(virtuerl),
Config.

end_per_suite(_Config) ->
application:stop(virtuerl).
-export([all/0, init_per_suite/1, end_per_suite/1]).
-export([test_create_domain/1, test_create_domain_dualstack/1]).

test_network(_Config) ->
NetJson = thoas:encode(#{"network" => #{"cidr4" => "192.168.111.0/24", "cidr6" => "2001:db8::/80"}}),
{ok, {{_, 201, _}, Headers, _}} = httpc:request(post, {"http://localhost:8081/networks", [], "application/json", NetJson}, [], []),
Loc = proplists:get_value("location", Headers),
NetUri = uri_string:resolve(Loc, "http://localhost:8081"),

{ok, {{_, 200, _}, _, NetBody}} = httpc:request(get, {NetUri, []}, [], []),
{ok, #{<<"cidrs">> := [<<"192.168.111.0/24">>, <<"2001:db8::/80">>]}} = thoas:decode(NetBody),
all() -> [test_create_domain, test_create_domain_dualstack].

{ok, {{_, 204, _}, _, _}} = httpc:request(delete, {NetUri, []}, [], []).

test_domain(_Config) ->
NetJson = thoas:encode(#{"network" => #{"cidr4" => "192.168.111.0/24", "cidr6" => "2001:db8::/80"}}),
{ok, {{_, 201, _}, NetHeaders, NetBody}} = httpc:request(post, {"http://localhost:8081/networks", [], "application/json", NetJson}, [], []),
NetLoc = proplists:get_value("location", NetHeaders),
NetUri = uri_string:resolve(NetLoc, "http://localhost:8081"),
{ok, #{<<"id">> := NetId}} = thoas:decode(NetBody),
init_per_suite(Config) ->
{ok, _} = application:ensure_all_started(virtuerl),
Config.

DomainJson = thoas:encode(#{"domain" => #{"network_id" => NetId, "ipv4_addr" => "192.168.111.39"}}),
{ok, {{_, 201, _}, DomainHeaders, DomainBody}} = httpc:request(post, {"http://localhost:8081/domains", [], "application/json", DomainJson}, [], []),
DomainLoc = proplists:get_value("location", DomainHeaders),
DomainUri = uri_string:resolve(DomainLoc, "http://localhost:8081"),
{ok, #{<<"mac_addr">> := MacAddr, <<"ipv4_addr">> := <<"192.168.111.39">>, <<"ipv6_addr">> := <<"2001:db8::8">>}} = thoas:decode(DomainBody),
<<_:6, Laa:2, _/binary>> = binary:decode_hex(MacAddr),
2 = Laa,

%% {ok, {{_, 200, _}, _, DomainBody1}} = httpc:request(DomainUri),
%% {ok, #{<<"mac_addr">> := MacAddr, <<"ipv4_addr">> := <<"192.168.111.39">>, <<"ipv6_addr">> := <<"2001:db8::8">>}} = thoas:decode(DomainBody1),
end_per_suite(_Config) ->
application:stop(virtuerl).

{ok, {{_, 204, _}, _, _}} = httpc:request(delete, {DomainUri, []}, [], []),
{ok, {{_, 204, _}, _, _}} = httpc:request(delete, {NetUri, []}, [], []).

test_create_domain(_Config) ->
{ok, NetID} = virtuerl_ipam:ipam_create_net([{<<192:8,168:8,17:8,0:8>>, 24}]),
{ok, #{id := DomId, ipv4_addr := <<"192.168.17.8">>}} = virtuerl_mgt:domain_create(#{name => "test_domain", vcpu => 1, memory => 512, network_id => NetID,
inbound_rules => [#{protocols => ["tcp"], target_ports => [80]}],
user_data =>
"#cloud-config
{ok, NetID} = virtuerl_ipam:ipam_create_net([{<<192:8, 168:8, 17:8, 0:8>>, 24}]),
{ok, #{id := DomId, ipv4_addr := <<"192.168.17.8">>}} =
virtuerl_mgt:domain_create(
#{
name => "test_domain",
vcpu => 1,
memory => 512,
network_id => NetID,
inbound_rules => [#{protocols => ["tcp"], target_ports => [80]}],
user_data =>
"#cloud-config
users:
- name: tester
Expand All @@ -83,31 +53,39 @@ packages:
runcmd:
- service nginx restart
"
}), % password: asd
{ok, {{_, 200, _}, _, _}} = wait_for_http("http://192.168.17.8/", 5*60*1000),
}), % password: asd
{ok, {{_, 200, _}, _, _}} = wait_for_http("http://192.168.17.8/", 5 * 60 * 1000),

% make sure address is actually released and reused
virtuerl_pubsub:subscribe(),
virtuerl_mgt:domain_delete(#{id => DomId}),
receive
{domain_deleted, DomId} ->ok
end,
{ok, #{id := Dom2Id, ipv4_addr := <<"192.168.17.8">>}} = virtuerl_mgt:domain_create(#{name => "test_domain_2", vcpu => 1, memory => 512, network_id => NetID, user_data => ""}),
virtuerl_mgt:domain_delete(#{id => Dom2Id}),
receive
{domain_deleted, Dom2Id} ->ok
end,
% make sure address is actually released and reused
virtuerl_pubsub:subscribe(),
virtuerl_mgt:domain_delete(#{id => DomId}),
receive
{domain_deleted, DomId} -> ok
end,
{ok, #{id := Dom2Id, ipv4_addr := <<"192.168.17.8">>}} =
virtuerl_mgt:domain_create(#{name => "test_domain_2", vcpu => 1, memory => 512, network_id => NetID, user_data => ""}),
virtuerl_mgt:domain_delete(#{id => Dom2Id}),
receive
{domain_deleted, Dom2Id} -> ok
end,

virtuerl_ipam:ipam_delete_net(NetID),
virtuerl_ipam:ipam_delete_net(NetID),

ok.

ok.

test_create_domain_dualstack(_Config) ->
{ok, NetID} = virtuerl_ipam:ipam_create_net([{<<192:8,168:8,17:8,0:8>>, 24}, {<<16#fd58:16, 16#40cb:16, 16#bddb:16, 0:80>>, 48}]),
{ok, #{id := DomId, ipv4_addr := <<"192.168.17.8">>, ipv6_addr := <<"fd58:40cb:bddb::8">>}} = virtuerl_mgt:domain_create(#{name => "test_domain", vcpu => 1, memory => 512, network_id => NetID,
inbound_rules => [#{protocols => ["tcp"], target_ports => [80]}],
user_data =>
"#cloud-config
{ok, NetID} = virtuerl_ipam:ipam_create_net([{<<192:8, 168:8, 17:8, 0:8>>, 24}, {<<16#fd58:16, 16#40cb:16, 16#bddb:16, 0:80>>, 48}]),
{ok, #{id := DomId, ipv4_addr := <<"192.168.17.8">>, ipv6_addr := <<"fd58:40cb:bddb::8">>}} =
virtuerl_mgt:domain_create(
#{
name => "test_domain",
vcpu => 1,
memory => 512,
network_id => NetID,
inbound_rules => [#{protocols => ["tcp"], target_ports => [80]}],
user_data =>
"#cloud-config
users:
- name: tester
Expand All @@ -131,33 +109,35 @@ packages:
runcmd:
- service nginx restart
"
}), % password: asd
{ok, {{_, 200, _}, _, _}} = wait_for_http("http://[fd58:40cb:bddb::8]/", 5*60*1000),
{ok, {{_, 200, _}, _, _}} = wait_for_http("http://192.168.17.8/", 5*60*1000),
}), % password: asd
{ok, {{_, 200, _}, _, _}} = wait_for_http("http://[fd58:40cb:bddb::8]/", 5 * 60 * 1000),
{ok, {{_, 200, _}, _, _}} = wait_for_http("http://192.168.17.8/", 5 * 60 * 1000),

virtuerl_pubsub:subscribe(),
virtuerl_mgt:domain_delete(#{id => DomId}),
receive
{domain_deleted, DomId} -> ok
end,

virtuerl_pubsub:subscribe(),
virtuerl_mgt:domain_delete(#{id => DomId}),
receive
{domain_deleted, DomId} ->ok
end,
virtuerl_ipam:ipam_delete_net(NetID),

virtuerl_ipam:ipam_delete_net(NetID),
ok.

ok.

wait_for_http(Url, Timeout) ->
Deadline = erlang:system_time(millisecond) + Timeout,
do_wait_for_http(Url, Deadline).
Deadline = erlang:system_time(millisecond) + Timeout,
do_wait_for_http(Url, Deadline).


do_wait_for_http(Url, Deadline) ->
case httpc:request(get, {Url, []}, [{timeout, 1000}], []) of
{error, _} ->
case erlang:system_time(millisecond) > Deadline of
true ->
{error, timeout};
false ->
timer:sleep(2000),
do_wait_for_http(Url, Deadline)
end;
Res -> Res
end.
case httpc:request(get, {Url, []}, [{timeout, 1000}], []) of
{error, _} ->
case erlang:system_time(millisecond) > Deadline of
true ->
{error, timeout};
false ->
timer:sleep(2000),
do_wait_for_http(Url, Deadline)
end;
Res -> Res
end.
114 changes: 114 additions & 0 deletions test/virtuerl_nested_SUITE.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
-module(virtuerl_nested_SUITE).

-behaviour(ct_suite).

-export([all/0, init_per_suite/1, end_per_suite/1]).
-export([test_create_domain/1]).

-include_lib("common_test/include/ct.hrl").


all() -> [test_create_domain].


init_per_suite(Config) ->
{ok, _} = application:ensure_all_started(virtuerl),
Config.


end_per_suite(_Config) ->
application:stop(virtuerl).


test_create_domain(_Config) ->
{ok, NetID} = virtuerl_ipam:ipam_create_net([{<<192:8, 168:8, 17:8, 0:8>>, 24}]),
{ok, #{id := DomId, ipv4_addr := <<"192.168.17.8">>}} =
virtuerl_mgt:domain_create(
#{
name => "test_domain",
vcpu => 1,
memory => 512,
network_id => NetID,
inbound_rules => [#{protocols => ["tcp"], target_ports => [80, 22]}],
user_data =>
"#cloud-config
users:
- name: tester
passwd: $6$Cf1HnaIWk8TunKFs$40sITB7utYJbVL9kkmhVwzCW33vbq55IGSbpLp1AqOufng1qNxf8wyHj4fdp3xMAfr0yrGioiWwtRvbN58rlI.
lock_passwd: false
shell: /bin/bash
sudo: ALL=(ALL) NOPASSWD:ALL
ssh_authorized_keys:
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJMKtCLcW+LcoEWUfYBuDbaaNkiRoa1CsB+2nWNHIhvi
apt:
primary:
- arches: [default]
search:
- http://mirror.ipb.de/debian/
- http://security.debian.org/debian-security
packages:
- erlang-base
- nginx
runcmd:
- service nginx restart
"
}), % password: asd
{ok, {{_, 200, _}, _, _}} = wait_for_http("http://192.168.17.8/", 5 * 60 * 1000),

ok = file:write_file("id_ed25519",
"-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACCTCrQi3Fvi3KBFlH2Abg22mjZIkaGtQrAftp1jRyIb4gAAAJDJNDQ7yTQ0
OwAAAAtzc2gtZWQyNTUxOQAAACCTCrQi3Fvi3KBFlH2Abg22mjZIkaGtQrAftp1jRyIb4g
AAAEDIBLrG0qYlS4tvkqCVC2o4tsrlAbK7DMRmAz8sFxmCJJMKtCLcW+LcoEWUfYBuDbaa
NkiRoa1CsB+2nWNHIhviAAAACmlseWFAdDQ2MHMBAgM=
-----END OPENSSH PRIVATE KEY-----
"),
ok = file:change_mode("id_ed25519", 8#00600),

Ssh = os:find_executable("ssh"),
{ok, Peer, _Node} =
peer:start_link(#{
exec => {Ssh,
["-i", "id_ed25519",
"-o", "StrictHostKeyChecking=no",
"-o", "UserKnownHostsFile=/dev/null",
"[email protected]",
"erl"]},
connection => standard_io
}),
{ok, Ips} = peer:call(Peer, inet, getifaddrs, []),
ct:print("IPs: ~p~n", [Ips]),
peer:stop(Peer),

virtuerl_pubsub:subscribe(),
virtuerl_mgt:domain_delete(#{id => DomId}),
receive
{domain_deleted, DomId} -> ok
end,
virtuerl_ipam:ipam_delete_net(NetID),

ok.


wait_for_http(Url, Timeout) ->
Deadline = erlang:system_time(millisecond) + Timeout,
do_wait_for_http(Url, Deadline).


do_wait_for_http(Url, Deadline) ->
case httpc:request(get, {Url, []}, [{timeout, 1000}], []) of
{error, _} ->
case erlang:system_time(millisecond) > Deadline of
true ->
{error, timeout};
false ->
timer:sleep(2000),
do_wait_for_http(Url, Deadline)
end;
Res -> Res
end.

0 comments on commit 81625ca

Please sign in to comment.