示例#1
0
func TestGetFiles(t *testing.T) {
	fileName := strings.Replace(file, "temp", "pmet", 1)
	hash := "QmcJdniiSKMp5az3fJvkbJTANd7bFtDoUkov3a8pkByWkv"
	do := definitions.NowDo()
	do.Name = hash
	do.Path = fileName

	// Fake IPFS server.
	os.Setenv("ERIS_IPFS_HOST", "http://0.0.0.0")
	ipfs := tests.NewServer("0.0.0.0:8080")
	ipfs.SetResponse(tests.ServerResponse{
		Code: http.StatusOK,
		Body: content,
	})
	defer ipfs.Close()

	if err := GetFiles(do); err != nil {
		fatal(t, err)
	}

	if expected := "/ipfs/" + hash; ipfs.Path() != expected {
		fatal(t, fmt.Errorf("Called the wrong endpoint; expected %v, got %v\n", expected, ipfs.Path()))
	}

	if expected := "GET"; ipfs.Method() != expected {
		fatal(t, fmt.Errorf("Used the wrong HTTP method; expected %v, got %v\n", expected, ipfs.Method()))
	}

	f, err := os.Open(fileName)
	if err != nil {
		fatal(t, err)
	}
	defer f.Close()

	contentPuted, err := ioutil.ReadAll(f)
	if err != nil {
		fatal(t, err)
	}

	if string(contentPuted) != content {
		fatal(t, fmt.Errorf("Returned unexpected content; expected: %q, got %q", content, string(contentPuted)))
	}
}
示例#2
0
func TestPutFiles(t *testing.T) {
	do := definitions.NowDo()
	do.Name = file
	log.WithField("=>", do.Name).Info("Putting file (from tests)")

	hash := "QmcJdniiSKMp5az3fJvkbJTANd7bFtDoUkov3a8pkByWkv"

	// Fake IPFS server.
	os.Setenv("ERIS_IPFS_HOST", "http://0.0.0.0")
	ipfs := tests.NewServer("0.0.0.0:8080")
	log.Warn("Server turned on.")
	ipfs.SetResponse(tests.ServerResponse{
		Code: http.StatusOK,
		Header: map[string][]string{
			"Ipfs-Hash": {hash},
		},
	})
	log.Warn("Waiting on server response.")
	defer ipfs.Close()

	if err := PutFiles(do); err != nil {
		fatal(t, err)
	}

	if expected := "/ipfs/"; ipfs.Path() != expected {
		fatal(t, fmt.Errorf("Called the wrong endpoint; expected %v, got %v\n", expected, ipfs.Path()))
	}

	if expected := "POST"; ipfs.Method() != expected {
		fatal(t, fmt.Errorf("Used the wrong HTTP method; expected %v, got %v\n", expected, ipfs.Method()))
	}

	if ipfs.Body() != content {
		fatal(t, fmt.Errorf("Put the bad file; expected %q, got %q\n", content, ipfs.Body()))
	}

	if hash != do.Result {
		fatal(t, fmt.Errorf("Hash mismatch; expected %q, got %q\n", hash, do.Result))
	}

	log.WithField("result", do.Result).Debug("Finished putting a file")
}
示例#3
0
func TestImportService(t *testing.T) {
	do := def.NowDo()
	do.Name = "eth"
	do.Hash = "QmQ1LZYPNG4wSb9dojRicWCmM4gFLTPKFUhFnMTR3GKuA2"
	log.WithFields(log.Fields{
		"=>":   do.Name,
		"hash": do.Hash,
	}).Debug("Importing service (from tests)")

	content := `name = "ipfs"

[service]
name = "ipfs"
image = "quay.io/eris/ipfs"`

	// Fake IPFS server.
	os.Setenv("ERIS_IPFS_HOST", "http://localhost")
	ipfs := tests.NewServer("localhost:8080")
	ipfs.SetResponse(tests.ServerResponse{
		Code: http.StatusOK,
		Body: content,
	})
	defer ipfs.Close()

	if err := ImportService(do); err != nil {
		tests.IfExit(err)
	}

	if expected := "/ipfs/" + do.Hash; ipfs.Path() != expected {
		tests.IfExit(fmt.Errorf("Called the wrong endpoint; expected %v, got %v\n", expected, ipfs.Path()))
	}

	if expected := "GET"; ipfs.Method() != expected {
		tests.IfExit(fmt.Errorf("Used the wrong HTTP method; expected %v, got %v\n", expected, ipfs.Method()))
	}

	if imported := tests.FileContents(FindServiceDefinitionFile(do.Name)); imported != content {
		tests.IfExit(fmt.Errorf("Returned unexpected content; expected: %q, got %q", content, imported))
	}
}
示例#4
0
func TestExportService(t *testing.T) {
	do := def.NowDo()
	do.Name = "ipfs"

	hash := "QmQ1LZYPNG4wSb9dojRicWCmM4gFLTPKFUhFnMTR3GKuA2"

	// Fake IPFS server.
	os.Setenv("ERIS_IPFS_HOST", "http://localhost")
	ipfs := tests.NewServer("localhost:8080")
	ipfs.SetResponse(tests.ServerResponse{
		Code: http.StatusOK,
		Header: map[string][]string{
			"Ipfs-Hash": {hash},
		},
	})
	defer ipfs.Close()

	if err := ExportService(do); err != nil {
		tests.IfExit(err)
	}

	if expected := "/ipfs/"; ipfs.Path() != expected {
		tests.IfExit(fmt.Errorf("Called the wrong endpoint; expected %v, got %v\n", expected, ipfs.Path()))
	}

	if expected := "POST"; ipfs.Method() != expected {
		tests.IfExit(fmt.Errorf("Used the wrong HTTP method; expected %v, got %v\n", expected, ipfs.Method()))
	}

	if content := tests.FileContents(FindServiceDefinitionFile(do.Name)); content != ipfs.Body() {
		tests.IfExit(fmt.Errorf("Sent the bad file; expected %q, got %q\n", content, ipfs.Body()))
	}

	if hash != do.Result {
		tests.IfExit(fmt.Errorf("Hash mismatch; expected %q, got %q\n", hash, do.Result))
	}
}