func TestGetFiles(t *testing.T) { var ( filename = filepath.Join(erisDir, "tset file.toml") content = "test contents" hash = "QmcJdniiSKMp5az3fJvkbJTANd7bFtDoUkov3a8pkByWkv" ) do := definitions.NowDo() do.Name = hash do.Path = filename // Fake IPFS server. os.Setenv("ERIS_IPFS_HOST", "http://127.0.0.1") ipfs := tests.NewServer("127.0.0.1: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())) } if returned := tests.FileContents(filename); content != returned { fatal(t, fmt.Errorf("Returned unexpected content; expected %q, got %q", content, returned)) } }
func TestPutFiles(t *testing.T) { var ( content = "test contents" filename = filepath.Join(erisDir, "test-file.toml") ) tests.FakeDefinitionFile(erisDir, "test-file", content) do := definitions.NowDo() do.Name = filename log.WithField("=>", do.Name).Info("Putting file (from tests)") hash := "QmcJdniiSKMp5az3fJvkbJTANd7bFtDoUkov3a8pkByWkv" // Fake IPFS server. os.Setenv("ERIS_IPFS_HOST", "http://127.0.0.1") ipfs := tests.NewServer("127.0.0.1:8080") ipfs.SetResponse(tests.ServerResponse{ Code: http.StatusOK, Header: map[string][]string{ "Ipfs-Hash": {hash}, }, }) 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") }
func TestExportService(t *testing.T) { do := def.NowDo() do.Name = "ipfs" const hash = "QmQ1LZYPNG4wSb9dojRicWCmM4gFLTPKFUhFnMTR3GKuA2" // Fake IPFS server. os.Setenv("ERIS_IPFS_HOST", "http://127.0.0.1") ipfs := tests.NewServer("127.0.0.1:8080") ipfs.SetResponse(tests.ServerResponse{ Code: http.StatusOK, Header: map[string][]string{ "Ipfs-Hash": {hash}, }, }) defer ipfs.Close() if err := ExportService(do); err != nil { t.Fatalf("expected service to be exported, got %v", err) } if expected := "/ipfs/"; ipfs.Path() != expected { t.Fatalf("called the wrong endpoint; expected %v, got %v", expected, ipfs.Path()) } if expected := "POST"; ipfs.Method() != expected { t.Fatalf("used the wrong HTTP method; expected %v, got %v", expected, ipfs.Method()) } if content := tests.FileContents(FindServiceDefinitionFile(do.Name)); content != ipfs.Body() { t.Fatalf("sent the bad file; expected %v, got %v", content, ipfs.Body()) } if hash != do.Result { t.Fatalf("hash mismatch; expected %v, got %v", hash, do.Result) } }
func TestImportService(t *testing.T) { do := def.NowDo() do.Name = "eth" do.Hash = "QmQ1LZYPNG4wSb9dojRicWCmM4gFLTPKFUhFnMTR3GKuA2" content := `name = "ipfs" [service] name = "ipfs" image = "` + path.Join(ver.ERIS_REG_DEF, ver.ERIS_IMG_IPFS) + `"` // Fake IPFS server. os.Setenv("ERIS_IPFS_HOST", "http://127.0.0.1") ipfs := tests.NewServer("127.0.0.1:8080") ipfs.SetResponse(tests.ServerResponse{ Code: http.StatusOK, Body: content, }) defer ipfs.Close() if err := ImportService(do); err != nil { t.Fatalf("expected service to be imported, got %v", err) } if expected := "/ipfs/" + do.Hash; ipfs.Path() != expected { t.Fatalf("called the wrong endpoint; expected %v, got %v", expected, ipfs.Path()) } if expected := "GET"; ipfs.Method() != expected { t.Fatalf("used the wrong HTTP method; expected %v, got %v\n", expected, ipfs.Method()) } if imported := tests.FileContents(FindServiceDefinitionFile(do.Name)); imported != content { t.Fatalf("returned unexpected content; expected: %v, got %v", content, imported) } }