Skip to content

Commit

Permalink
Add mgmt canister test.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed Mar 25, 2024
1 parent 4990206 commit 8436dc4
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 22 deletions.
7 changes: 3 additions & 4 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (a Agent) Call(canisterID principal.Principal, methodName string, args []an
return err
}
canisterID = effectiveCanisterID(canisterID, args)
a.logger.Printf("[AGENT] CALL %s %s", canisterID, methodName)
a.logger.Printf("[AGENT] CALL %s %s (%x)", canisterID, methodName, *requestID)
if _, err := a.call(canisterID, data); err != nil {
return err
}
Expand Down Expand Up @@ -256,7 +256,7 @@ func (a Agent) Query(canisterID principal.Principal, methodName string, args []a

// RequestStatus returns the status of the request with the given ID.
func (a Agent) RequestStatus(canisterID principal.Principal, requestID RequestID) ([]byte, hashtree.Node, error) {
a.logger.Printf("[AGENT] REQUEST STATUS %s", requestID)
a.logger.Printf("[AGENT] REQUEST STATUS %s %x", canisterID, requestID)
path := []hashtree.Label{hashtree.Label("request_status"), requestID[:]}
c, err := a.readStateCertificate(canisterID, [][]hashtree.Label{path})
if err != nil {
Expand Down Expand Up @@ -303,7 +303,7 @@ func (a Agent) poll(canisterID principal.Principal, requestID RequestID, delay,
for {
select {
case <-ticker.C:
a.logger.Printf("[AGENT] POLL %s", requestID)
a.logger.Printf("[AGENT] POLL %s %x", canisterID, requestID)
data, node, err := a.RequestStatus(canisterID, requestID)
if err != nil {
return nil, err
Expand All @@ -323,7 +323,6 @@ func (a Agent) poll(canisterID principal.Principal, requestID RequestID, delay,
}
return nil, fmt.Errorf("(%d) %s", uint64FromBytes(code), string(message))
case "replied":
fmt.Println(node)
replied, err := hashtree.NewHashTree(node).Lookup(append(path, hashtree.Label("reply"))...)
if err != nil {
return nil, fmt.Errorf("no reply found")
Expand Down
2 changes: 1 addition & 1 deletion agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,6 @@ func TestICPLedger_queryBlocks(t *testing.T) {

type testLogger struct{}

func (t testLogger) Printf(format string, v ...interface{}) {
func (t testLogger) Printf(format string, v ...any) {
fmt.Printf("[TEST]"+format+"\n", v...)
}
4 changes: 4 additions & 0 deletions ic/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"assetstorage": {
"type": "motoko",
"main": "assetstorage/actor.mo"
},
"ic0": {
"type": "motoko",
"main": "ic/actor.mo"
}
}
}
63 changes: 57 additions & 6 deletions ic/ic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"encoding/json"
"fmt"
"github.com/aviate-labs/agent-go"
"github.com/aviate-labs/agent-go/ic"
"github.com/aviate-labs/agent-go/ic/assetstorage"
ic0 "github.com/aviate-labs/agent-go/ic/ic"
"github.com/aviate-labs/agent-go/principal"
"net/url"
"os"
Expand All @@ -19,11 +21,11 @@ func TestModules(t *testing.T) {
if err != nil {
t.Skip(err)
}
var networksConfig map[string]map[string]string
var networksConfig networkConfig
if err := json.Unmarshal(rawNetworksConfig, &networksConfig); err != nil {
t.Fatal(err)
}
host, err := url.Parse(fmt.Sprintf("http://%s", networksConfig["local"]["bind"]))
host, err := url.Parse(fmt.Sprintf("http://%s", networksConfig.Local.Bind))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -60,19 +62,56 @@ func TestModules(t *testing.T) {
t.Fatal(err)
}

config := agent.Config{
ClientConfig: &agent.ClientConfig{Host: host},
FetchRootKey: true,
Logger: new(localLogger),
}

t.Run("assetstorage", func(t *testing.T) {
cId, _ := principal.Decode(m["assetstorage"]["local"])
a, err := assetstorage.NewAgent(cId, agent.Config{
ClientConfig: &agent.ClientConfig{Host: host},
FetchRootKey: true,
})
a, err := assetstorage.NewAgent(cId, config)
if err != nil {
t.Fatal(err)
}
if _, err := a.ApiVersion(); err != nil {
t.Error(err)
}
})

t.Run("management canister", func(t *testing.T) {
controller := principal.AnonymousID
addController := exec.Command(dfxPath, "canister", "update-settings", "--add-controller", controller.String(), "ic0")
if out, err := addController.CombinedOutput(); err != nil {
t.Fatal(sanitizeOutput(out))
}

getContollers := exec.Command(dfxPath, "canister", "info", "ic0")
out, err := getContollers.CombinedOutput()
if err != nil {
t.Fatal(sanitizeOutput(out))
}
if !strings.Contains(string(out), controller.String()) {
t.Error("controller not added")
}

cId, _ := principal.Decode(m["ic0"]["local"])
a, err := ic0.NewAgent(ic.MANAGEMENT_CANISTER_PRINCIPAL, config)
if err != nil {
t.Fatal(err)
}

if err := a.UpdateSettings(ic0.UpdateSettingsArgs{
CanisterId: cId,
Settings: ic0.CanisterSettings{
Controllers: &[]principal.Principal{
principal.AnonymousID,
},
},
}); err != nil {
t.Error(err)
}
})
}

func sanitizeOutput(out []byte) string {
Expand All @@ -83,3 +122,15 @@ func sanitizeOutput(out []byte) string {
}
return s
}

type localLogger struct{}

func (l localLogger) Printf(format string, v ...any) {
fmt.Printf("[LOCAL]"+format+"\n", v...)
}

type networkConfig struct {
Local struct {
Bind string `json:"bind"`
} `json:"local"`
}
6 changes: 5 additions & 1 deletion ic/testdata/networks.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"local": {
"bind": "127.0.0.1:8080"
"bind": "127.0.0.1:8080",
"type": "ephemeral",
"replica": {
"subnet_type": "system"
}
}
}
4 changes: 2 additions & 2 deletions logger.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package agent

type Logger interface {
Printf(format string, v ...interface{})
Printf(format string, v ...any)
}

type defaultLogger struct{}

func (l defaultLogger) Printf(format string, v ...interface{}) {}
func (l defaultLogger) Printf(format string, v ...any) {}
16 changes: 8 additions & 8 deletions mock/replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ func (r *Replica) handleCanister(writer http.ResponseWriter, canisterId, typ str
_, _ = writer.Write([]byte("expected call request"))
return
}
requestId := agent.NewRequestID(req)
requestIdHex := hex.EncodeToString(requestId[:])
r.Requests[requestIdHex] = req
requestID := agent.NewRequestID(req)
requestIDHex := hex.EncodeToString(requestID[:])
r.Requests[requestIDHex] = req
writer.WriteHeader(http.StatusAccepted)
case "query":
if req.Type != agent.RequestTypeQuery {
Expand Down Expand Up @@ -161,12 +161,12 @@ func (r *Replica) handleCanister(writer http.ResponseWriter, canisterId, typ str
_, _ = writer.Write([]byte("expected request_status"))
return
}
requestId := req.Paths[0][1]
requestIdHex := hex.EncodeToString(requestId)
req, ok := r.Requests[requestIdHex]
requestID := req.Paths[0][1]
requestIDHex := hex.EncodeToString(requestID)
req, ok := r.Requests[requestIDHex]
if !ok {
writer.WriteHeader(http.StatusNotFound)
_, _ = writer.Write([]byte("request not found: " + requestIdHex))
_, _ = writer.Write([]byte("request not found: " + requestIDHex))
return
}

Expand Down Expand Up @@ -195,7 +195,7 @@ func (r *Replica) handleCanister(writer http.ResponseWriter, canisterId, typ str
LeftTree: hashtree.Labeled{
Label: []byte("request_status"),
Tree: hashtree.Labeled{
Label: requestId,
Label: requestID,
Tree: hashtree.Fork{
LeftTree: hashtree.Labeled{
Label: []byte("reply"),
Expand Down

0 comments on commit 8436dc4

Please sign in to comment.