2023-06-25 20:20:59 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2023-06-25 22:19:05 -06:00
|
|
|
"time"
|
2023-07-29 11:03:41 -06:00
|
|
|
"fmt"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net"
|
|
|
|
"crypto/tls"
|
|
|
|
"bytes"
|
2023-06-25 20:20:59 -06:00
|
|
|
)
|
|
|
|
|
2023-07-29 00:40:18 -06:00
|
|
|
func TestGQL(t *testing.T) {
|
2023-07-29 11:03:41 -06:00
|
|
|
ctx := logTestContext(t, []string{"test", "gql", "policy"})
|
2023-07-29 00:40:18 -06:00
|
|
|
|
|
|
|
TestNodeType := NodeType("TEST")
|
|
|
|
err := ctx.RegisterNodeType(TestNodeType, []ExtType{LockableExtType, ACLExtType})
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
gql_ext, err := NewGQLExt(ctx, ":0", nil, nil)
|
|
|
|
fatalErr(t, err)
|
|
|
|
listener_ext := NewListenerExt(10)
|
|
|
|
policy := NewAllNodesPolicy(Actions{MakeAction("+")})
|
2023-07-29 11:03:41 -06:00
|
|
|
gql := NewNode(ctx, nil, TestNodeType, 10, nil, NewLockableExt(), NewACLExt(policy), gql_ext)
|
|
|
|
n1 := NewNode(ctx, nil, TestNodeType, 10, nil, NewLockableExt(), NewACLExt(policy), listener_ext)
|
2023-07-29 00:40:18 -06:00
|
|
|
|
2023-07-29 11:03:41 -06:00
|
|
|
ctx.Send(n1.ID, gql.ID, StateSignal{NewDirectSignal(GQLStateSignalType), "start_server"})
|
|
|
|
_, err = WaitForSignal(ctx, listener_ext, 100*time.Millisecond, GQLStateSignalType, func(sig StateSignal) bool {
|
|
|
|
return sig.State == "server_started"
|
2023-07-29 00:40:18 -06:00
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
2023-07-29 11:03:41 -06:00
|
|
|
|
|
|
|
skipVerifyTransport := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
|
|
|
client := &http.Client{Transport: skipVerifyTransport}
|
|
|
|
port := gql_ext.tcp_listener.Addr().(*net.TCPAddr).Port
|
|
|
|
url := fmt.Sprintf("https://localhost:%d/gql", port)
|
|
|
|
|
|
|
|
ser, err := json.MarshalIndent(&GQLPayload{
|
2023-07-29 16:00:01 -06:00
|
|
|
Query: "query Node($id:String) { Node(id:$id) { ID, TypeHash } }",
|
|
|
|
Variables: map[string]interface{}{
|
|
|
|
"id": n1.ID.String(),
|
|
|
|
},
|
2023-07-29 11:03:41 -06:00
|
|
|
}, "", " ")
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
req_data := bytes.NewBuffer(ser)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", url, req_data)
|
|
|
|
req.SetBasicAuth(n1.ID.String(), "BAD_PASSWORD")
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
resp.Body.Close()
|
|
|
|
|
|
|
|
ctx.Log.Logf("test", "TEST_RESP: %s", body)
|
|
|
|
|
|
|
|
ctx.Send(n1.ID, gql.ID, StateSignal{NewDirectSignal(GQLStateSignalType), "stop_server"})
|
|
|
|
_, err = WaitForSignal(ctx, listener_ext, 100*time.Millisecond, GQLStateSignalType, func(sig StateSignal) bool {
|
|
|
|
return sig.State == "server_stopped"
|
|
|
|
})
|
2023-07-29 00:40:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGQLDB(t *testing.T) {
|
2023-07-27 18:08:43 -06:00
|
|
|
ctx := logTestContext(t, []string{})
|
2023-07-26 13:28:03 -06:00
|
|
|
|
|
|
|
TestUserNodeType := NodeType("TEST_USER")
|
2023-07-27 11:33:11 -06:00
|
|
|
err := ctx.RegisterNodeType(TestUserNodeType, []ExtType{})
|
2023-07-26 13:28:03 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-28 15:07:38 -06:00
|
|
|
u1 := NewNode(ctx, nil, TestUserNodeType, 10, nil)
|
2023-07-26 13:28:03 -06:00
|
|
|
|
|
|
|
ctx.Log.Logf("test", "U1_ID: %s", u1.ID)
|
2023-07-01 13:06:39 -06:00
|
|
|
|
2023-07-29 00:40:18 -06:00
|
|
|
gql_ext, err := NewGQLExt(ctx, ":0", nil, nil)
|
|
|
|
fatalErr(t, err)
|
2023-07-27 12:03:39 -06:00
|
|
|
listener_ext := NewListenerExt(10)
|
2023-07-28 15:07:38 -06:00
|
|
|
gql := NewNode(ctx, nil, GQLNodeType, 10, nil,
|
2023-07-27 11:33:11 -06:00
|
|
|
gql_ext,
|
2023-07-27 12:03:39 -06:00
|
|
|
listener_ext,
|
2023-07-27 15:27:14 -06:00
|
|
|
NewACLExt(),
|
2023-07-27 12:03:39 -06:00
|
|
|
NewGroupExt(nil))
|
2023-07-26 11:56:10 -06:00
|
|
|
ctx.Log.Logf("test", "GQL_ID: %s", gql.ID)
|
2023-07-27 00:57:18 -06:00
|
|
|
|
2023-07-27 16:13:43 -06:00
|
|
|
err = ctx.Send(gql.ID, gql.ID, StopSignal)
|
2023-07-09 20:30:19 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-28 19:32:27 -06:00
|
|
|
_, err = WaitForSignal(ctx, listener_ext, 100*time.Millisecond, StatusSignalType, func(sig IDStateSignal) bool {
|
|
|
|
return sig.State == "stopped" && sig.ID == gql.ID
|
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
2023-07-03 13:14:48 -06:00
|
|
|
|
2023-07-27 15:27:14 -06:00
|
|
|
ser1, err := gql.Serialize()
|
|
|
|
ser2, err := u1.Serialize()
|
|
|
|
ctx.Log.Logf("test", "\n%s\n\n", ser1)
|
|
|
|
ctx.Log.Logf("test", "\n%s\n\n", ser2)
|
2023-07-01 13:06:39 -06:00
|
|
|
|
2023-07-26 11:56:10 -06:00
|
|
|
// Clear all loaded nodes from the context so it loads them from the database
|
|
|
|
ctx.Nodes = NodeMap{}
|
|
|
|
gql_loaded, err := LoadNode(ctx, gql.ID)
|
2023-07-01 13:06:39 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-27 15:27:14 -06:00
|
|
|
listener_ext, err = GetExt[*ListenerExt](gql_loaded)
|
2023-07-27 12:20:49 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-27 16:13:43 -06:00
|
|
|
err = ctx.Send(gql_loaded.ID, gql_loaded.ID, StopSignal)
|
2023-07-23 19:04:04 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-28 19:32:27 -06:00
|
|
|
_, err = WaitForSignal(ctx, listener_ext, 100*time.Millisecond, StatusSignalType, func(sig IDStateSignal) bool {
|
|
|
|
return sig.State == "stopped" && sig.ID == gql_loaded.ID
|
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
2023-07-03 13:14:48 -06:00
|
|
|
|
2023-07-01 13:06:39 -06:00
|
|
|
}
|
2023-07-19 14:45:05 -06:00
|
|
|
|