mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-25 20:37:53 +08:00
This is intended as a replacement for sharness. These are vanilla Go tests which can be run in your IDE for quick iteration on end-to-end CLI tests. This also removes IPTB by duplicating its functionality in the test harness. This isn't a big deal...IPTB's complexity is mostly around the fact that its state needs to be saved to disk in between `iptb` command invocations, and that it uses Go plugins to inject functionality, neither of which are relevant here. If we merge this, we'll have to live with bifurcated tests for a while until they are all migrated. I'd recommend we self-enforce a rule that, if we need to touch a sharness test, we migrate it and one more test over to Go tests first. Then eventually we will have migrated everything.
81 lines
2.2 KiB
Go
81 lines
2.2 KiB
Go
package harness
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"reflect"
|
|
"strings"
|
|
|
|
. "github.com/ipfs/kubo/test/cli/testutils"
|
|
)
|
|
|
|
func (n *Node) IPFSCommands() []string {
|
|
res := n.IPFS("commands").Stdout.String()
|
|
res = strings.TrimSpace(res)
|
|
split := SplitLines(res)
|
|
var cmds []string
|
|
for _, line := range split {
|
|
trimmed := strings.TrimSpace(line)
|
|
if trimmed == "ipfs" {
|
|
continue
|
|
}
|
|
cmds = append(cmds, trimmed)
|
|
}
|
|
return cmds
|
|
}
|
|
|
|
func (n *Node) SetIPFSConfig(key string, val interface{}, flags ...string) {
|
|
valBytes, err := json.Marshal(val)
|
|
if err != nil {
|
|
log.Panicf("marshling config for key '%s': %s", key, err)
|
|
}
|
|
valStr := string(valBytes)
|
|
|
|
args := []string{"config", "--json"}
|
|
args = append(args, flags...)
|
|
args = append(args, key, valStr)
|
|
n.IPFS(args...)
|
|
|
|
// validate the config was set correctly
|
|
var newVal string
|
|
n.GetIPFSConfig(key, &newVal)
|
|
if val != newVal {
|
|
log.Panicf("key '%s' did not retain value '%s' after it was set, got '%s'", key, val, newVal)
|
|
}
|
|
}
|
|
|
|
func (n *Node) GetIPFSConfig(key string, val interface{}) {
|
|
res := n.IPFS("config", key)
|
|
valStr := strings.TrimSpace(res.Stdout.String())
|
|
// only when the result is a string is the result not well-formed JSON,
|
|
// so check the value type and add quotes if it's expected to be a string
|
|
reflectVal := reflect.ValueOf(val)
|
|
if reflectVal.Kind() == reflect.Ptr && reflectVal.Elem().Kind() == reflect.String {
|
|
valStr = fmt.Sprintf(`"%s"`, valStr)
|
|
}
|
|
err := json.Unmarshal([]byte(valStr), val)
|
|
if err != nil {
|
|
log.Fatalf("unmarshaling config for key '%s', value '%s': %s", key, valStr, err)
|
|
}
|
|
}
|
|
|
|
func (n *Node) IPFSAddStr(content string, args ...string) string {
|
|
log.Debugf("node %d adding content '%s' with args: %v", n.ID, PreviewStr(content), args)
|
|
return n.IPFSAdd(strings.NewReader(content), args...)
|
|
}
|
|
|
|
func (n *Node) IPFSAdd(content io.Reader, args ...string) string {
|
|
log.Debugf("node %d adding with args: %v", n.ID, args)
|
|
fullArgs := []string{"add", "-q"}
|
|
fullArgs = append(fullArgs, args...)
|
|
res := n.Runner.MustRun(RunRequest{
|
|
Path: n.IPFSBin,
|
|
Args: fullArgs,
|
|
CmdOpts: []CmdOpt{RunWithStdin(content)},
|
|
})
|
|
out := strings.TrimSpace(res.Stdout.String())
|
|
log.Debugf("add result: %q", out)
|
|
return out
|
|
}
|