From b2a42f0b0158f309d514d504efd61c6fd24b39d5 Mon Sep 17 00:00:00 2001 From: Quint Daenen Date: Mon, 6 May 2024 12:00:13 +0200 Subject: [PATCH] Add test for /api/v2. --- pocketic/README.md | 14 ++++++++----- pocketic/endpoints_test.go | 42 ++++++++++++++++++++++++++++++++++++++ pocketic/http.go | 6 +++--- pocketic/instances.go | 14 ++++++------- pocketic/management.go | 2 +- pocketic/pocketic.go | 10 ++++----- pocketic/request.go | 8 ++++---- 7 files changed, 71 insertions(+), 25 deletions(-) diff --git a/pocketic/README.md b/pocketic/README.md index 36398f7..51077c1 100644 --- a/pocketic/README.md +++ b/pocketic/README.md @@ -1,7 +1,6 @@ # PocketIC Golang: A Canister Testing Library The client requires at least version 4 of the PocketIC server. -The client is not yet stable and is subject to change. ## References @@ -33,13 +32,18 @@ The client is not yet stable and is subject to change. | ✅ | POST | /instances/{id}/update/add_cycles | | ✅ | POST | /instances/{id}/update/set_stable_memory | | ✅ | POST | /instances/{id}/update/tick | -| ❌ | GET | /instances/{id}/api/v2/status | -| ❌ | POST | /instances/{id}/api/v2/canister/{ecid}/call | -| ❌ | POST | /instances/{id}/api/v2/canister/{ecid}/query | -| ❌ | POST | /instances/{id}/api/v2/canister/{ecid}/read_state | +| ⚠️ | GET | /instances/{id}/api/v2/status | +| ⚠️ | POST | /instances/{id}/api/v2/canister/{ecid}/call | +| ⚠️ | POST | /instances/{id}/api/v2/canister/{ecid}/query | +| ⚠️ | POST | /instances/{id}/api/v2/canister/{ecid}/read_state | | ✅ | POST | /instances/{id}/auto_progress | | ✅ | POST | /instances/{id}/stop_progress | | ✅ | POST | /http_gateway/ | | ✅ | POST | /http_gateway/{id}/stop | +--- +- ✅ Supported +- ✳️ Supported, but only used internally. +- ⚠️ Supported, but only through the agent. +- ❌ Not supported. diff --git a/pocketic/endpoints_test.go b/pocketic/endpoints_test.go index 779f45a..d55f7b3 100644 --- a/pocketic/endpoints_test.go +++ b/pocketic/endpoints_test.go @@ -1,9 +1,11 @@ package pocketic_test import ( + "github.com/aviate-labs/agent-go" "github.com/aviate-labs/agent-go/candid/idl" "github.com/aviate-labs/agent-go/pocketic" "github.com/aviate-labs/agent-go/principal" + "net/url" "testing" ) @@ -185,6 +187,46 @@ func Endpoints(t *testing.T) *pocketic.PocketIC { t.Fatal(err) } }) + + t.Run("api/v2/canister", func(t *testing.T) { + host, err := url.Parse(pic.InstanceURL()) + if err != nil { + t.Fatal(err) + } + a, err := NewAgent(*canisterID, agent.Config{ + ClientConfig: &agent.ClientConfig{Host: host}, + FetchRootKey: true, + Logger: new(testLogger), + }) + if err != nil { + t.Fatal(err) + } + + if _, err := pic.MakeLive(nil); err != nil { + t.Fatal(err) + } + defer func() { + if err := pic.MakeDeterministic(); err != nil { + t.Fatal(err) + } + }() + + q, err := a.HelloQuery("world") + if err != nil { + t.Fatal(err) + } + if *q != "Hello, world!" { + t.Fatalf("unexpected response: %s", *q) + } + + u, err := a.HelloUpdate("world") + if err != nil { + t.Fatal(err) + } + if *u != "Hello, world!" { + t.Fatalf("unexpected response: %s", *u) + } + }) }) return pic diff --git a/pocketic/http.go b/pocketic/http.go index 5d7af5f..aacfd13 100644 --- a/pocketic/http.go +++ b/pocketic/http.go @@ -78,7 +78,7 @@ func (pic PocketIC) AutoProgress() error { } return pic.do( http.MethodPost, - fmt.Sprintf("%s/auto_progress", pic.instanceURL()), + fmt.Sprintf("%s/auto_progress", pic.InstanceURL()), nil, nil, ) @@ -135,7 +135,7 @@ func (pic PocketIC) MakeLive(port *int) (string, error) { func (pic PocketIC) SetTime(time time.Time) error { return pic.do( http.MethodPost, - fmt.Sprintf("%s/update/set_time", pic.instanceURL()), + fmt.Sprintf("%s/update/set_time", pic.InstanceURL()), RawTime{ NanosSinceEpoch: time.UnixNano(), }, @@ -147,7 +147,7 @@ func (pic PocketIC) SetTime(time time.Time) error { func (pic PocketIC) StopProgress() error { return pic.do( http.MethodPost, - fmt.Sprintf("%s/stop_progress", pic.instanceURL()), + fmt.Sprintf("%s/stop_progress", pic.InstanceURL()), nil, nil, ) diff --git a/pocketic/instances.go b/pocketic/instances.go index a1c9f5c..3cbc45e 100644 --- a/pocketic/instances.go +++ b/pocketic/instances.go @@ -39,7 +39,7 @@ func (pic PocketIC) GetCycles(canisterID principal.Principal) (int, error) { var cycles RawCycles if err := pic.do( http.MethodPost, - fmt.Sprintf("%s/read/get_cycles", pic.instanceURL()), + fmt.Sprintf("%s/read/get_cycles", pic.InstanceURL()), &RawCanisterID{CanisterID: canisterID.Raw}, &cycles, ); err != nil { @@ -68,7 +68,7 @@ func (pic PocketIC) GetStableMemory(canisterID principal.Principal) ([]byte, err var data RawStableMemory if err := pic.do( http.MethodPost, - fmt.Sprintf("%s/read/get_stable_memory", pic.instanceURL()), + fmt.Sprintf("%s/read/get_stable_memory", pic.InstanceURL()), &RawCanisterID{CanisterID: canisterID.Raw}, &data, ); err != nil { @@ -82,7 +82,7 @@ func (pic PocketIC) GetSubnet(canisterID principal.Principal) (*principal.Princi var subnetID RawSubnetID if err := pic.do( http.MethodPost, - fmt.Sprintf("%s/read/get_subnet", pic.instanceURL()), + fmt.Sprintf("%s/read/get_subnet", pic.InstanceURL()), &RawCanisterID{CanisterID: canisterID.Raw}, &subnetID, ); err != nil { @@ -96,7 +96,7 @@ func (pic PocketIC) GetTime() (*time.Time, error) { var t RawTime if err := pic.do( http.MethodGet, - fmt.Sprintf("%s/read/get_time", pic.instanceURL()), + fmt.Sprintf("%s/read/get_time", pic.InstanceURL()), nil, &t, ); err != nil { @@ -125,7 +125,7 @@ func (pic PocketIC) RootKey() ([]byte, error) { var key []byte if err := pic.do( http.MethodPost, - fmt.Sprintf("%s/read/pub_key", pic.instanceURL()), + fmt.Sprintf("%s/read/pub_key", pic.InstanceURL()), &RawSubnetID{SubnetID: subnetID.Raw}, &key, ); err != nil { @@ -142,7 +142,7 @@ func (pic PocketIC) SetStableMemory(canisterID principal.Principal, data []byte, } return pic.do( http.MethodPost, - fmt.Sprintf("%s/update/set_stable_memory", pic.instanceURL()), + fmt.Sprintf("%s/update/set_stable_memory", pic.InstanceURL()), RawSetStableMemory{ CanisterID: canisterID.Raw, BlobID: blobID, @@ -154,7 +154,7 @@ func (pic PocketIC) SetStableMemory(canisterID principal.Principal, data []byte, func (pic PocketIC) Tick() error { return pic.do( http.MethodPost, - fmt.Sprintf("%s/update/tick", pic.instanceURL()), + fmt.Sprintf("%s/update/tick", pic.InstanceURL()), nil, nil, ) diff --git a/pocketic/management.go b/pocketic/management.go index 1abeddb..00570eb 100644 --- a/pocketic/management.go +++ b/pocketic/management.go @@ -17,7 +17,7 @@ func (pic PocketIC) AddCycles(canisterID principal.Principal, amount int) (int, } if err := pic.do( http.MethodPost, - fmt.Sprintf("%s/update/add_cycles", pic.instanceURL()), + fmt.Sprintf("%s/update/add_cycles", pic.InstanceURL()), RawAddCycles{ Amount: amount, CanisterID: canisterID.Raw, diff --git a/pocketic/pocketic.go b/pocketic/pocketic.go index 3d12ffe..6b8c06e 100644 --- a/pocketic/pocketic.go +++ b/pocketic/pocketic.go @@ -255,6 +255,11 @@ func (pic *PocketIC) Close() error { return pic.server.Close() } +// InstanceURL returns the URL of the PocketIC instance. +func (pic PocketIC) InstanceURL() string { + return fmt.Sprintf("%s/instances/%d", pic.server.URL(), pic.InstanceID) +} + // Status pings the PocketIC instance. func (pic PocketIC) Status() error { return pic.do( @@ -280,11 +285,6 @@ func (pic PocketIC) VerifySignature(sig RawVerifyCanisterSigArg) error { ) } -// instanceURL returns the URL of the PocketIC instance. -func (pic PocketIC) instanceURL() string { - return fmt.Sprintf("%s/instances/%d", pic.server.URL(), pic.InstanceID) -} - type SubnetConfigSet struct { Application []SubnetSpec `json:"application"` Bitcoin *SubnetSpec `json:"bitcoin,omitempty"` diff --git a/pocketic/request.go b/pocketic/request.go index eb75d35..cce4b04 100644 --- a/pocketic/request.go +++ b/pocketic/request.go @@ -39,7 +39,7 @@ func (pic PocketIC) AwaitCall(messageID RawMessageID) ([]byte, error) { var resp Result[WASMResult[[]byte]] if err := pic.do( http.MethodPost, - fmt.Sprintf("%s/update/await_ingress_message", pic.instanceURL()), + fmt.Sprintf("%s/update/await_ingress_message", pic.InstanceURL()), messageID, &resp, ); err != nil { @@ -65,7 +65,7 @@ func (pic PocketIC) ExecuteCall( var resp RawCanisterResult if err := pic.do( http.MethodPost, - fmt.Sprintf("%s/update/execute_ingress_message", pic.instanceURL()), + fmt.Sprintf("%s/update/execute_ingress_message", pic.InstanceURL()), RawCanisterCall{ CanisterID: canisterID.Raw, EffectivePrincipal: effectivePrincipal, @@ -129,7 +129,7 @@ func (pic PocketIC) SubmitCallWithEP( var resp RawSubmitIngressResult if err := pic.do( http.MethodPost, - fmt.Sprintf("%s/update/submit_ingress_message", pic.instanceURL()), + fmt.Sprintf("%s/update/submit_ingress_message", pic.InstanceURL()), RawCanisterCall{ CanisterID: canisterID.Raw, EffectivePrincipal: effectivePrincipal, @@ -157,7 +157,7 @@ func (pic PocketIC) canisterCall(endpoint string, canisterID principal.Principal var resp RawCanisterResult if err := pic.do( http.MethodPost, - fmt.Sprintf("%s/%s", pic.instanceURL(), endpoint), + fmt.Sprintf("%s/%s", pic.InstanceURL(), endpoint), RawCanisterCall{ CanisterID: canisterID.Raw, EffectivePrincipal: effectivePrincipal,