pax_global_header00006660000000000000000000000064123510620050014504gustar00rootroot0000000000000052 comment=395eacbaebccccc5f03ed11dc887ea2f1af300a0 etcd-fs-395eacbaebccccc5f03ed11dc887ea2f1af300a0/000077500000000000000000000000001235106200500205765ustar00rootroot00000000000000etcd-fs-395eacbaebccccc5f03ed11dc887ea2f1af300a0/.drone.yml000066400000000000000000000000571235106200500225100ustar00rootroot00000000000000image: jpetazzo/dind script: - docker --help etcd-fs-395eacbaebccccc5f03ed11dc887ea2f1af300a0/.gitignore000066400000000000000000000003741235106200500225720ustar00rootroot00000000000000# Compiled Object files, Static and Dynamic libs (Shared Objects) *.o *.a *.so # Folders _obj _test # Architecture specific extensions/prefixes *.[568vq] [568vq].out *.cgo1.go *.cgo2.c _cgo_defun.c _cgo_gotypes.go _cgo_export.* _testmain.go *.exe etcd-fs-395eacbaebccccc5f03ed11dc887ea2f1af300a0/.travis.yml000066400000000000000000000001251235106200500227050ustar00rootroot00000000000000language: go go: - 1.2 - release - tip install: make install script: - make test etcd-fs-395eacbaebccccc5f03ed11dc887ea2f1af300a0/LICENSE000066400000000000000000000260631235106200500216120ustar00rootroot00000000000000Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2014 Jonathan Leibiusky Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. etcd-fs-395eacbaebccccc5f03ed11dc887ea2f1af300a0/Makefile000066400000000000000000000005051235106200500222360ustar00rootroot00000000000000export GOPATH=$(shell pwd) test: -docker run -d -p 8001:8001 -p 4001:4001 coreos/etcd -name etcd-node1 go get github.com/franela/goblin go get github.com/coreos/go-etcd/etcd go get github.com/hanwen/go-fuse/fuse go test -v etcdfs install: sudo apt-get install -qq fuse sudo modprobe fuse build: go build etcdfs.go etcd-fs-395eacbaebccccc5f03ed11dc887ea2f1af300a0/README.md000066400000000000000000000034351235106200500220620ustar00rootroot00000000000000etcd-fs ======= Use etcd as a filesystem *THis IS STILL WORK IN PROGRESS* Why using etcd as a fileystem? ============================== Because filesystem API is super stable, widely known and supported, and very simple. Also because it seems like this can be useful for lots of companies out there that have already deployed apps that read some configuration file from local filesystem and would love to load these configuration files to something like etcd and ensure a consistent view of across a cluster of nodes. How does it work? ================= Etcd-fs uses [go-fuse](https://github.com/hanwen/go-fuse) and [go-etcd](https://github.com/coreos/go-etcd), two nice modules to create fuse filesystem in go and use etcd from go. Every file maps to a key in etcd. Every directory maps to a directory in etcd. The content of every file maps to the value of the key in etcd. When you mount the filesystem, it will mount the root directory of etcd. How do I install it? ==================== Clone the project and build it. ```bash make build ``` This generates an executable file ```etcdfs```. You can mount etcd as a filesystem by running ```etcdfs MOUNT_PATH ETCD_ENDPOINT```. For example: ```bash ./etcds /tmp/foobar http://localhost:4001 ``` Then you can access ```/tmp/foobar``` and use etcd as a filesystem. To unmount it: ```bash fusermount -u /tmp/foobar ``` What is supported? ================== Basic filesystem operations like: + Reading/Writing files + Creating/Deleting files + Creating/Deleting directories What is missing? ================ + Lots of optimizations + Lots of error handling + Specifiying more options to etcd connection + Mount filesystem to some node in etcd, not necessarily the root + Use watch to get updates from etcd and maybe change file modification time (????) etcd-fs-395eacbaebccccc5f03ed11dc887ea2f1af300a0/etcdfs.go000066400000000000000000000010351235106200500223740ustar00rootroot00000000000000package main import ( "flag" "log" . "etcdfs" "github.com/hanwen/go-fuse/fuse/pathfs" "github.com/hanwen/go-fuse/fuse/nodefs" ) func main() { flag.Parse() if len(flag.Args()) < 2 { log.Fatal("Usage:\n etcd-fs MOUNTPOINT ETCDENDPOINT") } etcdFs := EtcdFs{FileSystem: pathfs.NewDefaultFileSystem(), EtcdEndpoint: flag.Arg(1)} nfs := pathfs.NewPathNodeFs(&etcdFs, nil) server, _, err := nodefs.MountRoot(flag.Arg(0), nfs.Root(), nil) if err != nil { log.Fatalf("Mount fail: %v\n", err) } server.Serve() } etcd-fs-395eacbaebccccc5f03ed11dc887ea2f1af300a0/src/000077500000000000000000000000001235106200500213655ustar00rootroot00000000000000etcd-fs-395eacbaebccccc5f03ed11dc887ea2f1af300a0/src/etcdfs/000077500000000000000000000000001235106200500226355ustar00rootroot00000000000000etcd-fs-395eacbaebccccc5f03ed11dc887ea2f1af300a0/src/etcdfs/file.go000066400000000000000000000051051235106200500241040ustar00rootroot00000000000000package etcdfs import( "github.com/coreos/go-etcd/etcd" "github.com/hanwen/go-fuse/fuse" "github.com/hanwen/go-fuse/fuse/nodefs" "log" "time" "bytes" ) type etcdFile struct { etcdClient *etcd.Client path string } func NewEtcdFile(client *etcd.Client, path string) nodefs.File { file := new(etcdFile) file.etcdClient = client file.path = path return file } func (f *etcdFile) SetInode(*nodefs.Inode) { } func (f *etcdFile) InnerFile() nodefs.File { return nil } func (f *etcdFile) String() string { return "etcdFile" } func (f *etcdFile) Read(buf []byte, off int64) (fuse.ReadResult, fuse.Status) { res, err := f.etcdClient.Get(f.path, false, false) if err != nil { log.Println("Error:", err) return nil, fuse.EIO } end := int(off) + int(len(buf)) if end > len(res.Node.Value) { end = len(res.Node.Value) } data := []byte(res.Node.Value) return fuse.ReadResultData(data[off:end]), fuse.OK } func (f *etcdFile) Write(data []byte, off int64) (uint32, fuse.Status) { res, err := f.etcdClient.Get(f.path, false, false) if err != nil { log.Println("Error:", err) return 0, fuse.EIO } originalValue := []byte(res.Node.Value) leftChunk := originalValue[:off] end := int(off) + int(len(data)) var rightChunk []byte if end > len(res.Node.Value) { rightChunk = []byte{} } else { rightChunk = data[int(off)+int(len(data)):] } newValue := bytes.NewBuffer(leftChunk) newValue.Grow(len(data)+len(rightChunk)) newValue.Write(data) newValue.Write(rightChunk) _, err = f.etcdClient.Set(f.path, newValue.String(), 0) if err != nil { log.Println("Error:", err) return 0, fuse.EIO } return uint32(len(data)), fuse.OK } func (f *etcdFile) Flush() fuse.Status { return fuse.OK } func (f *etcdFile) Release() { } func (f *etcdFile) GetAttr(out *fuse.Attr) fuse.Status { res, err := f.etcdClient.Get(f.path, false, false) if err != nil { log.Println("Error:", err) return fuse.EIO } out.Mode = fuse.S_IFREG | 0666 out.Size = uint64(len(res.Node.Value)) return fuse.OK } func (f *etcdFile) Fsync(flags int) (code fuse.Status) { return fuse.OK } func (f *etcdFile) Utimens(atime *time.Time, mtime *time.Time) fuse.Status { return fuse.ENOSYS } func (f *etcdFile) Truncate(size uint64) fuse.Status { return fuse.OK } func (f *etcdFile) Chown(uid uint32, gid uint32) fuse.Status { return fuse.ENOSYS } func (f *etcdFile) Chmod(perms uint32) fuse.Status { return fuse.ENOSYS } func (f *etcdFile) Allocate(off uint64, size uint64, mode uint32) (code fuse.Status) { return fuse.OK } etcd-fs-395eacbaebccccc5f03ed11dc887ea2f1af300a0/src/etcdfs/file_test.go000066400000000000000000000043711235106200500251470ustar00rootroot00000000000000package etcdfs import( . "github.com/franela/goblin" "testing" etcdm "github.com/coreos/go-etcd/etcd" // "fmt" "os" "io/ioutil" ) func TestNodeFs(t *testing.T) { g := Goblin(t) g.Describe("File", func() { var etcd *etcdm.Client var fs testEtcdFsMount g.Before(func() { etcd = etcdm.NewClient([]string{testEtcdEndpoint}) }) g.BeforeEach(func() { etcd.RawDelete("/test", true, true) etcd.SetDir("/test", 0) fs = NewTestEtcdFsMount() }) g.AfterEach(func() { fs.Unmount() }) g.Describe("Open", func() { g.It("Should be supported", func() { if _, e := etcd.Set("/test/foo", "bar", 0); e != nil { g.Fail(e) } file, err := os.Open(fs.Path() + "/test/foo") if err != nil { g.Fail(err) } file.Close() }) }) g.Describe("Create", func() { g.It("Should be supported", func() { file, err := os.Create(fs.Path() + "/test/bar") if err != nil { g.Fail(err) } file.Close() if _, er := etcd.Get("/test/bar", false, false); er != nil { g.Fail(er) } }) }) g.Describe("Delete", func() { g.It("Should be supported", func() { etcd.Set("/test/barfoo", "lala", 0) err := os.Remove(fs.Path() + "/test/barfoo") if err != nil { g.Fail(err) } if _, er := etcd.Get("/test/barfoo", false, false); er == nil { g.Fail("The key [/test/barfoo] should not exist") } }) }) g.Describe("Read", func() { g.It("Should be supported", func() { etcd.Set("/test/bar", "foo", 0) data, err := ioutil.ReadFile(fs.Path() + "/test/bar") if err != nil { g.Fail(err) } g.Assert(string(data)).Equal("foo") }) }) g.Describe("Write", func() { g.It("Should be supported", func() { if err := ioutil.WriteFile(fs.Path() + "/test/foobar", []byte("hello world"), 0666); err != nil { g.Fail(err) } res, err := etcd.Get("/test/foobar", false, false) if err != nil { g.Fail(err) } g.Assert(res.Node.Value).Equal("hello world") }) }) }) } etcd-fs-395eacbaebccccc5f03ed11dc887ea2f1af300a0/src/etcdfs/path.go000066400000000000000000000056251235106200500241300ustar00rootroot00000000000000package etcdfs import( "log" "strings" "github.com/hanwen/go-fuse/fuse" "github.com/hanwen/go-fuse/fuse/nodefs" "github.com/hanwen/go-fuse/fuse/pathfs" "github.com/coreos/go-etcd/etcd" ) type EtcdFs struct { pathfs.FileSystem EtcdEndpoint string } func (me *EtcdFs) NewEtcdClient() *etcd.Client { return etcd.NewClient([]string{me.EtcdEndpoint}) } func (me *EtcdFs) Unlink(name string, context *fuse.Context) (code fuse.Status) { if name == "" { return fuse.OK } _, err := me.NewEtcdClient().Delete(name, false) if err != nil { log.Println(err) return fuse.ENOENT } return fuse.OK } func (me *EtcdFs) Rmdir(name string, context *fuse.Context) (code fuse.Status) { if name == "" { return fuse.OK } _, err := me.NewEtcdClient().RawDelete(name, true, true) if err != nil { log.Println(err) return fuse.ENOENT } return fuse.OK } func (me *EtcdFs) Create(name string, flags uint32, mode uint32, context *fuse.Context) (file nodefs.File, code fuse.Status) { _, err := me.NewEtcdClient().Set(name, "", 0) if err != nil { log.Println("Create Error:", err) return nil, fuse.ENOENT } return NewEtcdFile(me.NewEtcdClient(), name), fuse.OK } func (me *EtcdFs) Mkdir(name string, mode uint32, context *fuse.Context) fuse.Status { if name == "" { return fuse.OK } _, err := me.NewEtcdClient().CreateDir(name, 0) if err != nil { log.Println(err) return fuse.ENOENT } return fuse.OK } func (me *EtcdFs) GetAttr(name string, context *fuse.Context) (*fuse.Attr, fuse.Status) { if name == "" { return &fuse.Attr{ Mode: fuse.S_IFDIR | 0666, }, fuse.OK } res, err := me.NewEtcdClient().Get(name, false, false) if err != nil { return nil, fuse.ENOENT } var attr fuse.Attr if res.Node.Dir { attr = fuse.Attr{ Mode: fuse.S_IFDIR | 0666, } } else { attr = fuse.Attr{ Mode: fuse.S_IFREG | 0666, Size: uint64(len(res.Node.Value)), } } return &attr, fuse.OK } func (me *EtcdFs) OpenDir(name string, context *fuse.Context) (c []fuse.DirEntry, code fuse.Status) { res, err := me.NewEtcdClient().Get(name, false, false) if err != nil { log.Println("OpenDir Error:", err) return nil, fuse.ENOENT } entries := []fuse.DirEntry{} for _, e := range(res.Node.Nodes) { chunks := strings.Split(e.Key, "/") file := chunks[len(chunks)-1] if e.Dir { entries = append(entries, fuse.DirEntry{Name: file, Mode: fuse.S_IFDIR}) } else { entries = append(entries, fuse.DirEntry{Name: file, Mode: fuse.S_IFREG}) } } return entries, fuse.OK } func (me *EtcdFs) Open(name string, flags uint32, context *fuse.Context) (file nodefs.File, code fuse.Status) { _, err := me.NewEtcdClient().Get(name, false, false) if err != nil { log.Println("Open Error:", err) return nil, fuse.ENOENT } return NewEtcdFile(me.NewEtcdClient(), name), fuse.OK } etcd-fs-395eacbaebccccc5f03ed11dc887ea2f1af300a0/src/etcdfs/path_test.go000066400000000000000000000063761235106200500251730ustar00rootroot00000000000000package etcdfs import( . "github.com/franela/goblin" "testing" etcdm "github.com/coreos/go-etcd/etcd" "fmt" "os" ) func TestPathFs(t *testing.T) { g := Goblin(t) g.Describe("Path", func() { var etcd *etcdm.Client var fs testEtcdFsMount g.Before(func() { etcd = etcdm.NewClient([]string{testEtcdEndpoint}) }) g.BeforeEach(func() { etcd.RawDelete("/test", true, true) etcd.SetDir("/test", 0) fs = NewTestEtcdFsMount() }) g.AfterEach(func() { fs.Unmount() }) g.Describe("ls", func() { g.It("Should be supported", func() { etcd.Set("/test/a", "a", 0) etcd.SetDir("/test/b", 0) f, err1 := os.Open(fs.Path() + "/test") if err1 != nil { g.Fail(err1) } defer f.Close() files, err2 := f.Readdir(0) if err2 != nil { g.Fail(err2) } g.Assert(len(files) == 2).IsTrue() file1 := files[0] file2 := files[1] switch file1.Name() { case "a": g.Assert(file1.IsDir()).IsFalse() g.Assert(file1.Size()).Equal(1) g.Assert(file1.Mode().String()).Equal("-rw-rw-rw-") case "b": g.Assert(file1.IsDir()).IsTrue() g.Assert(file1.Mode().String()).Equal("drw-rw-rw-") default: g.Fail(fmt.Sprintf("Didn't expect file [%s]", file1.Name())) } switch file2.Name() { case "a": g.Assert(file2.IsDir()).IsFalse() g.Assert(file2.Size()).Equal(1) g.Assert(file2.Mode().String()).Equal("-rw-rw-rw-") case "b": g.Assert(file2.IsDir()).IsTrue() g.Assert(file2.Mode().String()).Equal("drw-rw-rw-") default: g.Fail(fmt.Sprintf("Didn't expect file [%s]", file2.Name())) } }) }) g.Describe("mkdir", func() { g.It("Should be supported", func() { if e := os.Mkdir(fs.Path() + "/test/foo", os.ModeDir | 0666); e != nil { g.Fail(e) } res, err := etcd.Get("/test/foo", false, false) if err != nil { g.Fail(err) } g.Assert(res.Node.Dir).IsTrue() }); g.It("Should support creating with parents", func() { if e := os.MkdirAll(fs.Path() + "/test/a/b/c/foo", os.ModeDir | 0666); e != nil { g.Fail(e) } res, err := etcd.Get("/test/a/b", false, false) if err != nil { g.Fail(err) } g.Assert(res.Node.Dir).IsTrue() }); }) g.Describe("rmdir", func() { g.It("Should be supported", func() { etcd.CreateDir("/test/foo", 0) if e := os.Remove(fs.Path() + "/test/foo"); e != nil { g.Fail(e) } _, err := etcd.Get("/test/foo", false, false) if err == nil { g.Fail("/test/foo should not exist in etcd.") } }); g.It("Should support removing with children", func() { etcd.CreateDir("/test/foo/bar", 0) if e := os.RemoveAll(fs.Path() + "/test/foo"); e != nil { g.Fail(e) } _, err := etcd.Get("/test/foo/bar", false, false) if err == nil { g.Fail("/test/foo should not exist in etcd.") } }); }) }) } etcd-fs-395eacbaebccccc5f03ed11dc887ea2f1af300a0/src/etcdfs/testutil.go000066400000000000000000000026051235106200500250440ustar00rootroot00000000000000package etcdfs import( "io/ioutil" "time" "github.com/hanwen/go-fuse/fuse" "github.com/hanwen/go-fuse/fuse/nodefs" "github.com/hanwen/go-fuse/fuse/pathfs" "fmt" "os" ) const( testTtl = 100 * time.Millisecond testVerbose = false testEtcdEndpoint = "http://localhost:4001" ) type testEtcdFsMount struct { path string state *fuse.Server } func (me testEtcdFsMount) Path() string { return me.path } func (me testEtcdFsMount) Unmount() { err := me.state.Unmount() if err != nil { fmt.Printf("Unmount failed: %f\n", err) } os.RemoveAll(me.path) } func NewTestEtcdFsMount() testEtcdFsMount { t := testEtcdFsMount{} var err error t.path, err = ioutil.TempDir("", "etcd-fs") if err != nil { fmt.Printf("Temdir fail: %v\n", err) } etcdFs := EtcdFs{FileSystem: pathfs.NewDefaultFileSystem(), EtcdEndpoint: testEtcdEndpoint} nfs := pathfs.NewPathNodeFs(&etcdFs, nil) connector := nodefs.NewFileSystemConnector(nfs.Root(), &nodefs.Options{ EntryTimeout: testTtl, AttrTimeout: testTtl, NegativeTimeout: 0.0 }) connector.SetDebug(testVerbose) t.state, err = fuse.NewServer(fuse.NewRawFileSystem(connector.RawFS()), t.path, &fuse.MountOptions{SingleThreaded: true}) if err != nil { fmt.Println("NewServer:", err) } t.state.SetDebug(testVerbose) // Unthreaded, but in background. go t.state.Serve() t.state.WaitMount() return t }