2023-02-23 20:13:19 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2023-02-24 19:01:54 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/client/proto"
|
2023-03-15 07:54:51 +01:00
|
|
|
"github.com/netbirdio/netbird/version"
|
2023-02-23 20:13:19 +01:00
|
|
|
)
|
|
|
|
|
2023-02-24 19:01:54 +01:00
|
|
|
var resp = &proto.StatusResponse{
|
|
|
|
Status: "Connected",
|
|
|
|
FullStatus: &proto.FullStatus{
|
|
|
|
Peers: []*proto.PeerState{
|
|
|
|
{
|
|
|
|
IP: "192.168.178.101",
|
|
|
|
PubKey: "Pubkey1",
|
|
|
|
Fqdn: "peer-1.awesome-domain.com",
|
|
|
|
ConnStatus: "Connected",
|
|
|
|
ConnStatusUpdate: timestamppb.New(time.Date(2001, time.Month(1), 1, 1, 1, 1, 0, time.UTC)),
|
|
|
|
Relayed: false,
|
|
|
|
Direct: true,
|
2023-02-27 15:14:41 +01:00
|
|
|
LocalIceCandidateType: "",
|
|
|
|
RemoteIceCandidateType: "",
|
2023-02-24 19:01:54 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
IP: "192.168.178.102",
|
|
|
|
PubKey: "Pubkey2",
|
|
|
|
Fqdn: "peer-2.awesome-domain.com",
|
|
|
|
ConnStatus: "Connected",
|
|
|
|
ConnStatusUpdate: timestamppb.New(time.Date(2002, time.Month(2), 2, 2, 2, 2, 0, time.UTC)),
|
|
|
|
Relayed: true,
|
|
|
|
Direct: false,
|
2023-02-27 15:14:41 +01:00
|
|
|
LocalIceCandidateType: "relay",
|
|
|
|
RemoteIceCandidateType: "prflx",
|
2023-02-24 19:01:54 +01:00
|
|
|
},
|
2023-02-23 20:13:19 +01:00
|
|
|
},
|
2023-02-24 19:01:54 +01:00
|
|
|
ManagementState: &proto.ManagementState{
|
|
|
|
URL: "my-awesome-management.com:443",
|
|
|
|
Connected: true,
|
|
|
|
},
|
|
|
|
SignalState: &proto.SignalState{
|
|
|
|
URL: "my-awesome-signal.com:443",
|
|
|
|
Connected: true,
|
|
|
|
},
|
|
|
|
LocalPeerState: &proto.LocalPeerState{
|
|
|
|
IP: "192.168.178.100/16",
|
|
|
|
PubKey: "Some-Pub-Key",
|
|
|
|
KernelInterface: true,
|
|
|
|
Fqdn: "some-localhost.awesome-domain.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DaemonVersion: "0.14.1",
|
|
|
|
}
|
|
|
|
|
|
|
|
var overview = statusOutputOverview{
|
|
|
|
Peers: peersStateOutput{
|
|
|
|
Total: 2,
|
|
|
|
Connected: 2,
|
|
|
|
Details: []peerStateDetailOutput{
|
|
|
|
{
|
2023-02-27 17:06:20 +01:00
|
|
|
IP: "192.168.178.101",
|
|
|
|
PubKey: "Pubkey1",
|
|
|
|
FQDN: "peer-1.awesome-domain.com",
|
|
|
|
Status: "Connected",
|
|
|
|
LastStatusUpdate: time.Date(2001, 1, 1, 1, 1, 1, 0, time.UTC),
|
|
|
|
ConnType: "P2P",
|
|
|
|
Direct: true,
|
|
|
|
IceCandidateType: iceCandidateType{
|
|
|
|
Local: "",
|
|
|
|
Remote: "",
|
|
|
|
},
|
2023-02-24 19:01:54 +01:00
|
|
|
},
|
|
|
|
{
|
2023-02-27 17:06:20 +01:00
|
|
|
IP: "192.168.178.102",
|
|
|
|
PubKey: "Pubkey2",
|
|
|
|
FQDN: "peer-2.awesome-domain.com",
|
|
|
|
Status: "Connected",
|
|
|
|
LastStatusUpdate: time.Date(2002, 2, 2, 2, 2, 2, 0, time.UTC),
|
|
|
|
ConnType: "Relayed",
|
|
|
|
Direct: false,
|
|
|
|
IceCandidateType: iceCandidateType{
|
|
|
|
Local: "relay",
|
|
|
|
Remote: "prflx",
|
|
|
|
},
|
2023-02-24 19:01:54 +01:00
|
|
|
},
|
2023-02-23 20:13:19 +01:00
|
|
|
},
|
|
|
|
},
|
2023-03-15 07:54:51 +01:00
|
|
|
CliVersion: version.NetbirdVersion(),
|
2023-02-24 19:01:54 +01:00
|
|
|
DaemonVersion: "0.14.1",
|
|
|
|
ManagementState: managementStateOutput{
|
2023-02-23 20:13:19 +01:00
|
|
|
URL: "my-awesome-management.com:443",
|
|
|
|
Connected: true,
|
|
|
|
},
|
2023-02-24 19:01:54 +01:00
|
|
|
SignalState: signalStateOutput{
|
2023-02-23 20:13:19 +01:00
|
|
|
URL: "my-awesome-signal.com:443",
|
|
|
|
Connected: true,
|
|
|
|
},
|
2023-02-24 19:01:54 +01:00
|
|
|
IP: "192.168.178.100/16",
|
|
|
|
PubKey: "Some-Pub-Key",
|
2023-02-27 15:14:41 +01:00
|
|
|
KernelInterface: true,
|
2023-02-24 19:01:54 +01:00
|
|
|
FQDN: "some-localhost.awesome-domain.com",
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConversionFromFullStatusToOutputOverview(t *testing.T) {
|
|
|
|
convertedResult := convertToStatusOutputOverview(resp)
|
|
|
|
|
|
|
|
assert.Equal(t, overview, convertedResult)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSortingOfPeers(t *testing.T) {
|
|
|
|
peers := []peerStateDetailOutput{
|
|
|
|
{
|
|
|
|
IP: "192.168.178.104",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IP: "192.168.178.102",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IP: "192.168.178.101",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IP: "192.168.178.105",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IP: "192.168.178.103",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-02-27 15:34:17 +01:00
|
|
|
sortPeersByIP(peers)
|
2023-02-24 19:01:54 +01:00
|
|
|
|
|
|
|
assert.Equal(t, peers[3].IP, "192.168.178.104")
|
2023-02-23 20:13:19 +01:00
|
|
|
}
|
|
|
|
|
2023-02-27 15:34:17 +01:00
|
|
|
func TestParsingToJSON(t *testing.T) {
|
|
|
|
json, _ := parseToJSON(overview)
|
2023-02-23 20:13:19 +01:00
|
|
|
|
2023-02-27 17:06:20 +01:00
|
|
|
//@formatter:off
|
|
|
|
expectedJSON := "{\"" +
|
|
|
|
"peers\":" +
|
2023-02-24 19:01:54 +01:00
|
|
|
"{" +
|
|
|
|
"\"total\":2," +
|
|
|
|
"\"connected\":2," +
|
|
|
|
"\"details\":" +
|
2023-02-23 20:13:19 +01:00
|
|
|
"[" +
|
|
|
|
"{" +
|
2023-02-24 19:01:54 +01:00
|
|
|
"\"fqdn\":\"peer-1.awesome-domain.com\"," +
|
2023-02-27 17:06:20 +01:00
|
|
|
"\"netbirdIp\":\"192.168.178.101\"," +
|
|
|
|
"\"publicKey\":\"Pubkey1\"," +
|
|
|
|
"\"status\":\"Connected\"," +
|
|
|
|
"\"lastStatusUpdate\":\"2001-01-01T01:01:01Z\"," +
|
2023-02-24 19:01:54 +01:00
|
|
|
"\"connectionType\":\"P2P\"," +
|
|
|
|
"\"direct\":true," +
|
2023-02-27 17:06:20 +01:00
|
|
|
"\"iceCandidateType\":" +
|
|
|
|
"{" +
|
|
|
|
"\"local\":\"\"," +
|
|
|
|
"\"remote\":\"\"" +
|
|
|
|
"}" +
|
2023-02-23 20:13:19 +01:00
|
|
|
"}," +
|
|
|
|
"{" +
|
2023-02-24 19:01:54 +01:00
|
|
|
"\"fqdn\":\"peer-2.awesome-domain.com\"," +
|
2023-02-27 17:06:20 +01:00
|
|
|
"\"netbirdIp\":\"192.168.178.102\"," +
|
|
|
|
"\"publicKey\":\"Pubkey2\"," +
|
|
|
|
"\"status\":\"Connected\"," +
|
|
|
|
"\"lastStatusUpdate\":\"2002-02-02T02:02:02Z\"," +
|
2023-02-24 19:01:54 +01:00
|
|
|
"\"connectionType\":\"Relayed\"," +
|
|
|
|
"\"direct\":false," +
|
2023-02-27 17:06:20 +01:00
|
|
|
"\"iceCandidateType\":" +
|
|
|
|
"{" +
|
|
|
|
"\"local\":\"relay\"," +
|
|
|
|
"\"remote\":\"prflx\"" +
|
|
|
|
"}" +
|
2023-02-23 20:13:19 +01:00
|
|
|
"}" +
|
2023-02-24 19:01:54 +01:00
|
|
|
"]" +
|
2023-02-23 20:13:19 +01:00
|
|
|
"}," +
|
2023-02-24 19:01:54 +01:00
|
|
|
"\"cliVersion\":\"development\"," +
|
|
|
|
"\"daemonVersion\":\"0.14.1\"," +
|
|
|
|
"\"management\":" +
|
2023-02-23 20:13:19 +01:00
|
|
|
"{" +
|
2023-02-24 19:01:54 +01:00
|
|
|
"\"url\":\"my-awesome-management.com:443\"," +
|
|
|
|
"\"connected\":true" +
|
2023-02-23 20:13:19 +01:00
|
|
|
"}," +
|
2023-02-24 19:01:54 +01:00
|
|
|
"\"signal\":" +
|
2023-02-27 17:06:20 +01:00
|
|
|
"{\"" +
|
|
|
|
"url\":\"my-awesome-signal.com:443\"," +
|
2023-02-24 19:01:54 +01:00
|
|
|
"\"connected\":true" +
|
|
|
|
"}," +
|
2023-02-27 17:06:20 +01:00
|
|
|
"\"netbirdIp\":\"192.168.178.100/16\"," +
|
2023-02-24 19:01:54 +01:00
|
|
|
"\"publicKey\":\"Some-Pub-Key\"," +
|
2023-02-27 15:14:41 +01:00
|
|
|
"\"usesKernelInterface\":true," +
|
2023-02-27 17:06:20 +01:00
|
|
|
"\"fqdn\":\"some-localhost.awesome-domain.com\"" +
|
2023-02-23 20:13:19 +01:00
|
|
|
"}"
|
2023-02-24 19:01:54 +01:00
|
|
|
// @formatter:on
|
|
|
|
|
2023-02-27 15:52:46 +01:00
|
|
|
assert.Equal(t, expectedJSON, json)
|
2023-02-23 20:13:19 +01:00
|
|
|
}
|
|
|
|
|
2023-02-27 15:34:17 +01:00
|
|
|
func TestParsingToYAML(t *testing.T) {
|
|
|
|
yaml, _ := parseToYAML(overview)
|
2023-02-23 20:13:19 +01:00
|
|
|
|
2023-02-27 15:52:46 +01:00
|
|
|
expectedYAML := "peers:\n" +
|
2023-02-27 11:04:53 +01:00
|
|
|
" total: 2\n" +
|
|
|
|
" connected: 2\n" +
|
|
|
|
" details:\n" +
|
2023-02-27 17:06:20 +01:00
|
|
|
" - fqdn: peer-1.awesome-domain.com\n" +
|
|
|
|
" netbirdIp: 192.168.178.101\n" +
|
2023-02-27 11:04:53 +01:00
|
|
|
" publicKey: Pubkey1\n" +
|
2023-02-27 17:06:20 +01:00
|
|
|
" status: Connected\n" +
|
|
|
|
" lastStatusUpdate: 2001-01-01T01:01:01Z\n" +
|
2023-02-27 11:04:53 +01:00
|
|
|
" connectionType: P2P\n" +
|
|
|
|
" direct: true\n" +
|
2023-02-27 17:06:20 +01:00
|
|
|
" iceCandidateType:\n" +
|
|
|
|
" local: \"\"\n" +
|
|
|
|
" remote: \"\"\n" +
|
|
|
|
" - fqdn: peer-2.awesome-domain.com\n" +
|
|
|
|
" netbirdIp: 192.168.178.102\n" +
|
2023-02-27 11:04:53 +01:00
|
|
|
" publicKey: Pubkey2\n" +
|
2023-02-27 17:06:20 +01:00
|
|
|
" status: Connected\n" +
|
|
|
|
" lastStatusUpdate: 2002-02-02T02:02:02Z\n" +
|
2023-02-27 11:04:53 +01:00
|
|
|
" connectionType: Relayed\n" +
|
|
|
|
" direct: false\n" +
|
2023-02-27 17:06:20 +01:00
|
|
|
" iceCandidateType:\n" +
|
|
|
|
" local: relay\n" +
|
|
|
|
" remote: prflx\n" +
|
2023-02-24 19:01:54 +01:00
|
|
|
"cliVersion: development\n" +
|
|
|
|
"daemonVersion: 0.14.1\n" +
|
|
|
|
"management:\n" +
|
2023-02-27 11:04:53 +01:00
|
|
|
" url: my-awesome-management.com:443\n" +
|
|
|
|
" connected: true\n" +
|
2023-02-24 19:01:54 +01:00
|
|
|
"signal:\n" +
|
2023-02-27 11:04:53 +01:00
|
|
|
" url: my-awesome-signal.com:443\n" +
|
|
|
|
" connected: true\n" +
|
2023-02-27 17:06:20 +01:00
|
|
|
"netbirdIp: 192.168.178.100/16\n" +
|
2023-02-24 19:01:54 +01:00
|
|
|
"publicKey: Some-Pub-Key\n" +
|
2023-02-27 15:14:41 +01:00
|
|
|
"usesKernelInterface: true\n" +
|
2023-02-27 17:06:20 +01:00
|
|
|
"fqdn: some-localhost.awesome-domain.com\n"
|
2023-02-24 19:01:54 +01:00
|
|
|
|
2023-02-27 15:52:46 +01:00
|
|
|
assert.Equal(t, expectedYAML, yaml)
|
2023-02-27 11:23:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParsingToDetail(t *testing.T) {
|
|
|
|
detail := parseToFullDetailSummary(overview)
|
|
|
|
|
|
|
|
expectedDetail := "Peers detail:\n" +
|
|
|
|
" peer-1.awesome-domain.com:\n" +
|
|
|
|
" NetBird IP: 192.168.178.101\n" +
|
|
|
|
" Public key: Pubkey1\n" +
|
|
|
|
" Status: Connected\n" +
|
|
|
|
" -- detail --\n" +
|
|
|
|
" Connection type: P2P\n" +
|
|
|
|
" Direct: true\n" +
|
|
|
|
" ICE candidate (Local/Remote): -/-\n" +
|
|
|
|
" Last connection update: 2001-01-01 01:01:01\n" +
|
|
|
|
"\n" +
|
|
|
|
" peer-2.awesome-domain.com:\n" +
|
|
|
|
" NetBird IP: 192.168.178.102\n" +
|
|
|
|
" Public key: Pubkey2\n" +
|
|
|
|
" Status: Connected\n" +
|
|
|
|
" -- detail --\n" +
|
|
|
|
" Connection type: Relayed\n" +
|
|
|
|
" Direct: false\n" +
|
2023-02-27 15:14:41 +01:00
|
|
|
" ICE candidate (Local/Remote): relay/prflx\n" +
|
2023-02-27 11:23:34 +01:00
|
|
|
" Last connection update: 2002-02-02 02:02:02\n" +
|
|
|
|
"\n" +
|
|
|
|
"Daemon version: 0.14.1\n" +
|
|
|
|
"CLI version: development\n" +
|
2023-02-27 17:12:34 +01:00
|
|
|
"Management: Connected to my-awesome-management.com:443\n" +
|
2023-02-27 11:23:34 +01:00
|
|
|
"Signal: Connected to my-awesome-signal.com:443\n" +
|
2023-02-27 17:06:20 +01:00
|
|
|
"FQDN: some-localhost.awesome-domain.com\n" +
|
2023-02-27 11:23:34 +01:00
|
|
|
"NetBird IP: 192.168.178.100/16\n" +
|
|
|
|
"Interface type: Kernel\n" +
|
|
|
|
"Peers count: 2/2 Connected\n"
|
|
|
|
|
2023-02-24 19:01:54 +01:00
|
|
|
assert.Equal(t, expectedDetail, detail)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParsingToShortVersion(t *testing.T) {
|
|
|
|
shortVersion := parseGeneralSummary(overview, false)
|
|
|
|
|
|
|
|
expectedString := "Daemon version: 0.14.1\n" +
|
|
|
|
"CLI version: development\n" +
|
2023-02-27 17:12:34 +01:00
|
|
|
"Management: Connected\n" +
|
2023-02-24 19:01:54 +01:00
|
|
|
"Signal: Connected\n" +
|
2023-02-27 17:06:20 +01:00
|
|
|
"FQDN: some-localhost.awesome-domain.com\n" +
|
2023-02-24 19:01:54 +01:00
|
|
|
"NetBird IP: 192.168.178.100/16\n" +
|
|
|
|
"Interface type: Kernel\n" +
|
|
|
|
"Peers count: 2/2 Connected\n"
|
|
|
|
|
|
|
|
assert.Equal(t, expectedString, shortVersion)
|
|
|
|
}
|
|
|
|
|
2023-02-27 15:34:17 +01:00
|
|
|
func TestParsingOfIP(t *testing.T) {
|
2023-02-27 15:52:46 +01:00
|
|
|
InterfaceIP := "192.168.178.123/16"
|
2023-02-24 19:01:54 +01:00
|
|
|
|
2023-02-27 15:52:46 +01:00
|
|
|
parsedIP := parseInterfaceIP(InterfaceIP)
|
2023-02-24 19:01:54 +01:00
|
|
|
|
2023-02-27 15:34:17 +01:00
|
|
|
assert.Equal(t, "192.168.178.123\n", parsedIP)
|
2023-02-24 19:01:54 +01:00
|
|
|
}
|