-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
198 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,45 @@ | ||
# PocketIC Golang: A Canister Testing Library | ||
|
||
The client is currently implemented for an unreleased version of the PocketIC server. | ||
The client requires at least version 4 of the PocketIC server. | ||
The client is not yet stable and is subject to change. | ||
|
||
You can download the server [here](https://download.dfinity.systems/ic/136a026d67139ecddbc48db3050e488a3c29bb74/binaries/x86_64-linux/pocket-ic.gz). | ||
|
||
```go | ||
package actor_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/aviate-labs/agent-go/pocketic" | ||
) | ||
|
||
func TestActor(t *testing.T) { | ||
pic, err := pocketic.New(pocketic.DefaultSubnetConfig) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
wasmModule, err := os.ReadFile("actor.wasm") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cID, err := pic.CreateAndInstallCanister(wasmModule, nil, nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Call the actor, it has native support for the idl types of the agent-go library. | ||
var greeting string | ||
if err := pic.QueryCall(*cID, "hello", nil, []any{&greeting}); err != nil { | ||
t.Fatal(err) | ||
} | ||
_ = greeting | ||
} | ||
|
||
``` | ||
|
||
## References | ||
|
||
- [PocketIC](https://github.com/dfinity/pocketic) | ||
- [PocketIC Server](https://github.com/dfinity/ic/tree/master/rs/pocket_ic_server) | ||
|
||
## List of Supported Endpoints | ||
|
||
| Supported | Method | Endpoint | | ||
|-----------|--------|---------------------------------------------------| | ||
| ✅ | GET | /status | | ||
| ✅ | POST | /blobstore | | ||
| ✅ | GET | /blobstore/{id} | | ||
| ❌ | POST | /verify_signature | | ||
| ❌ | GET | /read_graph/{state_label}/{op_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 | | ||
| ✅ | 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/set_time | | ||
| ✅ | 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 | | ||
| ✅ | POST | /instances/{id}/auto_progress | | ||
| ✅ | POST | /instances/{id}/stop_progress | | ||
| ✅ | POST | /http_gateway/ | | ||
| ✅ | POST | /http_gateway/{id}/stop | | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package pocketic | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
// GetBlob retrieves a binary blob from the PocketIC server. | ||
func (pic PocketIC) GetBlob(blobID []byte) ([]byte, error) { | ||
var bytes []byte | ||
if err := pic.do( | ||
http.MethodGet, | ||
fmt.Sprintf("%s/blobstore/%s", pic.server.URL(), hex.EncodeToString(blobID)), | ||
http.StatusOK, | ||
nil, | ||
&bytes, | ||
); err != nil { | ||
return nil, err | ||
} | ||
return bytes, nil | ||
} | ||
|
||
// UploadBlob uploads and stores a binary blob to the PocketIC server. | ||
func (pic PocketIC) UploadBlob(bytes []byte) ([]byte, error) { | ||
method := http.MethodPost | ||
url := fmt.Sprintf("%s/blobstore", pic.server.URL()) | ||
pic.logger.Printf("[POCKETIC] %s %s %+v", method, url, bytes) | ||
req, err := newRequest(method, url, bytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Set("content-type", "application/octet-stream") | ||
resp, err := pic.client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
hexBlobID, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
} | ||
blobID, err := hex.DecodeString(string(hexBlobID)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return blobID, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package pocketic_test | ||
|
||
import ( | ||
"github.com/aviate-labs/agent-go/pocketic" | ||
"testing" | ||
) | ||
|
||
func TestEndpoints(t *testing.T) { | ||
pic, err := pocketic.New( | ||
pocketic.WithLogger(new(testLogger)), | ||
pocketic.WithNNSSubnet(), | ||
pocketic.WithApplicationSubnet(), | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer func() { | ||
if err := pic.Close(); err != nil { | ||
t.Fatal(err) | ||
} | ||
}() | ||
|
||
t.Run("status", func(t *testing.T) { | ||
if err := pic.Status(); err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
t.Run("blobstore", func(t *testing.T) { | ||
id, err := pic.UploadBlob([]byte{0, 1, 2, 3}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
bytes, err := pic.GetBlob(id) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(bytes) != 4 { | ||
t.Fatalf("unexpected blob size: %d", len(bytes)) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters