Skip to content

Commit

Permalink
Add test for /api/v2.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed May 6, 2024
1 parent 2179652 commit b2a42f0
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 25 deletions.
14 changes: 9 additions & 5 deletions pocketic/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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.
42 changes: 42 additions & 0 deletions pocketic/endpoints_test.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pocketic/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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(),
},
Expand All @@ -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,
)
Expand Down
14 changes: 7 additions & 7 deletions pocketic/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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,
)
Expand Down
2 changes: 1 addition & 1 deletion pocketic/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions pocketic/pocketic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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"`
Expand Down
8 changes: 4 additions & 4 deletions pocketic/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b2a42f0

Please sign in to comment.