Skip to content

Commit

Permalink
Add instance endpoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed May 6, 2024
1 parent e2e2395 commit 32babe5
Show file tree
Hide file tree
Showing 10 changed files with 487 additions and 67 deletions.
24 changes: 12 additions & 12 deletions pocketic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ The client is not yet stable and is subject to change.
|| GET | /status |
|| POST | /blobstore |
|| GET | /blobstore/{id} |
| | POST | /verify_signature |
| | POST | /verify_signature |
|| GET | /read_graph/{state_label}/{op_id} |
| | GET | /instances/ |
| | POST | /instances/ |
| | DELETE | /instances/{id} |
| | GET | /instances/ |
| | POST | /instances/ |
| | DELETE | /instances/{id} |
|| POST | /instances/{id}/read/query |
| | GET | /instances/{id}/read/get_time |
| | POST | /instances/{id}/read/get_cycles |
| | POST | /instances/{id}/read/get_stable_memory |
| | POST | /instances/{id}/read/get_subnet |
| | POST | /instances/{id}/read/pub_key |
| | GET | /instances/{id}/read/get_time |
| | POST | /instances/{id}/read/get_cycles |
| | POST | /instances/{id}/read/get_stable_memory |
| | POST | /instances/{id}/read/get_subnet |
| | POST | /instances/{id}/read/pub_key |
|| POST | /instances/{id}/update/submit_ingress_message |
|| POST | /instances/{id}/update/await_ingress_message |
| | POST | /instances/{id}/update/execute_ingress_message |
| | POST | /instances/{id}/update/execute_ingress_message |
|| POST | /instances/{id}/update/set_time |
|| POST | /instances/{id}/update/add_cycles |
| | POST | /instances/{id}/update/set_stable_memory |
| | POST | /instances/{id}/update/tick |
| | 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 |
Expand Down
5 changes: 4 additions & 1 deletion pocketic/blobstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (pic PocketIC) GetBlob(blobID []byte) ([]byte, error) {
}

// UploadBlob uploads and stores a binary blob to the PocketIC server.
func (pic PocketIC) UploadBlob(bytes []byte) ([]byte, error) {
func (pic PocketIC) UploadBlob(bytes []byte, gzipCompression bool) ([]byte, error) {
method := http.MethodPost
url := fmt.Sprintf("%s/blobstore", pic.server.URL())
pic.logger.Printf("[POCKETIC] %s %s %+v", method, url, bytes)
Expand All @@ -32,6 +32,9 @@ func (pic PocketIC) UploadBlob(bytes []byte) ([]byte, error) {
return nil, err
}
req.Header.Set("content-type", "application/octet-stream")
if gzipCompression {
req.Header.Set("content-encoding", "gzip")
}
resp, err := pic.client.Do(req)
if err != nil {
return nil, err
Expand Down
155 changes: 154 additions & 1 deletion pocketic/endpoints_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package pocketic_test

import (
"github.com/aviate-labs/agent-go/candid/idl"
"github.com/aviate-labs/agent-go/pocketic"
"github.com/aviate-labs/agent-go/principal"
"testing"
)

Expand All @@ -21,7 +23,7 @@ func Endpoints(t *testing.T) *pocketic.PocketIC {
}
})
t.Run("blobstore", func(t *testing.T) {
id, err := pic.UploadBlob([]byte{0, 1, 2, 3})
id, err := pic.UploadBlob([]byte{0, 1, 2, 3}, false)
if err != nil {
t.Fatal(err)
}
Expand All @@ -33,6 +35,157 @@ func Endpoints(t *testing.T) *pocketic.PocketIC {
t.Fatalf("unexpected blob size: %d", len(bytes))
}
})
t.Run("instances", func(t *testing.T) {
var instances []string
t.Run("get", func(t *testing.T) {
instances, err = pic.GetInstances()
if err != nil {
t.Fatal(err)
}
if len(instances) == 0 {
t.Fatal("no instances found")
}
})
var instanceConfig *pocketic.InstanceConfig
t.Run("post", func(t *testing.T) {
instanceConfig, err = pic.CreateInstance(pocketic.DefaultSubnetConfig)
if err != nil {
t.Fatal(err)
}
if instanceConfig == nil {
t.Fatal("instance config is nil")
}
newInstances, err := pic.GetInstances()
if err != nil {
t.Fatal(err)
}
if len(newInstances) != len(instances)+1 {
t.Fatalf("unexpected instances count: %d", len(newInstances))
}
})
t.Run("delete", func(t *testing.T) {
if err := pic.DeleteInstance(instanceConfig.InstanceID); err != nil {
t.Fatal(err)
}
newInstances, err := pic.GetInstances()
if err != nil {
t.Fatal(err)
}
if newInstances[len(newInstances)-1] != "Deleted" {
t.Fatal("instance was not deleted")
}
})

canisterID, err := pic.CreateCanister()
if err != nil {
t.Fatal(err)
}
wasmModule := compileMotoko(t, "testdata/main.mo", "testdata/main.wasm")
if err := pic.InstallCode(*canisterID, wasmModule, nil, nil); err != nil {
t.Fatal(err)
}

t.Run("query", func(t *testing.T) {
if err := pic.QueryCall(*canisterID, principal.AnonymousID, "void", nil, nil); err == nil {
t.Fatal()
}
var resp string
if err := pic.QueryCall(*canisterID, principal.AnonymousID, "helloQuery", []any{"world"}, []any{&resp}); err != nil {
t.Fatal(err)
}
if resp != "Hello, world!" {
t.Fatalf("unexpected response: %s", resp)
}
})

t.Run("get_time", func(t *testing.T) {
dt, err := pic.GetTime()
if err != nil {
t.Fatal(err)
}
if dt == nil {
t.Fatal("time is nil")
}
})

t.Run("get_cycles", func(t *testing.T) {
cycles, err := pic.GetCycles(*canisterID)
if err != nil {
t.Fatal(err)
}
if cycles <= 0 {
t.Fatalf("unexpected cycles: %d", cycles)
}
})

t.Run("stable_memory", func(t *testing.T) {
if err := pic.SetStableMemory(*canisterID, []byte{0, 1, 2, 3}, false); err != nil {
t.Fatal(err)
}
if _, err := pic.GetStableMemory(*canisterID); err != nil {
t.Fatal(err)
}
})

t.Run("get_subnet", func(t *testing.T) {
subnetID, err := pic.GetSubnet(*canisterID)
if err != nil {
t.Fatal(err)
}
if subnetID == nil {
t.Fatal("subnet ID is nil")
}
})

t.Run("pub_key", func(t *testing.T) {
if _, err := pic.RootKey(); err != nil {
t.Fatal(err)
}
})

t.Run("ingress_message", func(t *testing.T) {
payload, err := idl.Marshal([]any{"world"})
if err != nil {
t.Fatal(err)
}
{
msgID, err := pic.SubmitCall(*canisterID, principal.AnonymousID, "helloUpdate", payload)
if err != nil {
t.Fatal(err)
}
raw, err := pic.AwaitCall(*msgID)
if err != nil {
t.Fatal(err)
}
var resp string
if err := idl.Unmarshal(raw, []any{&resp}); err != nil {
t.Fatal(err)
}
if resp != "Hello, world!" {
t.Fatalf("unexpected response: %s", resp)
}
}
{
raw, err := pic.ExecuteCall(*canisterID, new(pocketic.RawEffectivePrincipalNone), principal.AnonymousID, "helloUpdate", payload)
if err != nil {
t.Fatal(err)
}
var resp string
if err := idl.Unmarshal(raw, []any{&resp}); err != nil {
t.Fatal(err)
}
if resp != "Hello, world!" {
t.Fatalf("unexpected response: %s", resp)
}
}
})

t.Run("tick", func(t *testing.T) {
if err := pic.Tick(); err != nil {
t.Fatal(err)
}
})
})

return pic
}
2 changes: 1 addition & 1 deletion pocketic/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (pic PocketIC) SetTime(time time.Time) error {
fmt.Sprintf("%s/update/set_time", pic.instanceURL()),
http.StatusOK,
RawTime{
NanosSinceEpoch: int(time.UnixNano()),
NanosSinceEpoch: time.UnixNano(),
},
nil,
)
Expand Down
Loading

0 comments on commit 32babe5

Please sign in to comment.