func TestGetInputPath(t *testing.T) { assert.Equal(t, "", GetInputPath(memfs.Create(), venv.Mock())) assert.Equal(t, "aaa", GetInputPath(memfs.Create(), envWith(map[string]string{"TF_STATE": "aaa"}))) assert.Equal(t, "bbb", GetInputPath(memfs.Create(), envWith(map[string]string{"TI_TFSTATE": "bbb"}))) assert.Equal(t, "terraform.tfstate", GetInputPath(fsWithFiles([]string{"terraform.tfstate"}), venv.Mock())) assert.Equal(t, ".terraform/terraform.tfstate", GetInputPath(fsWithFiles([]string{".terraform/terraform.tfstate"}), venv.Mock())) }
func TestDatasource(t *testing.T) { fs := memfs.Create() fs.Mkdir("/tmp", 0777) f, _ := vfs.Create(fs, "/tmp/foo.json") f.Write([]byte(`{"hello":"world"}`)) sources := make(map[string]*Source) sources["foo"] = &Source{ Alias: "foo", URL: &url.URL{ Scheme: "file", Path: "/tmp/foo.json", }, Ext: "json", Type: "application/json", FS: fs, } data := &Data{ Sources: sources, } expected := make(map[string]interface{}) expected["hello"] = "world" actual := data.Datasource("foo") assert.Equal(t, expected["hello"], actual["hello"]) }
func TestMultiProperties(t *testing.T) { mem := memfs.Create() source := &fsRoot{mem, "/source"} target := &fsRoot{mem, "/source"} testWrite(t, source, ".template", "PROPERTY1\nPROPERTY2") testWrite(t, source, "template1", "hello PROPERTY1") testWrite(t, source, "template2", "hello PROPERTY2") err := source.Create(Environment(map[string]string{ "PROPERTY1": "property1", "PROPERTY2": "property2", }), target) if err != nil { t.Fatal(err) } if testRead(t, target, "template1") != "hello property1\n" { t.Fatal("wrong property1") } if testRead(t, target, "template2") != "hello property2\n" { t.Fatal("wrong property2") } }
func TestIOReadFile(t *testing.T) { fs := memfs.Create() fs.Mkdir("/root", 0775) file, err := fs.OpenFile("/root/file", os.O_WRONLY|os.O_CREATE, 0664) if err != nil { t.Fatal("could not create test file", err) } _, err = file.Write([]byte("hello")) if err != nil { t.Fatal("could not write test file", err) } target := fsRoot{fs, "/root"} reader, err := target.Reader("file") if err != nil { t.Fatal("could not create reader") } var buf bytes.Buffer _, err = buf.ReadFrom(reader) if err != nil { t.Fatal("failed reading", err) } if buf.String() != "hello" { t.Log(buf.String()) t.Fatal("expected hello") } }
func TestIOWriteFile(t *testing.T) { fs := memfs.Create() target := fsRoot{fs, "/root"} writer, err := target.Writer("file") if err != nil { t.Fatal("Could not get writer", err) } _, err = writer.Write([]byte("hello")) if err != nil { t.Fatal("Could not write", err) } file, err := fs.OpenFile("/root/file", os.O_RDONLY, 0) if err != nil { t.Fatal("File not created", err) } var buf bytes.Buffer _, err = buf.ReadFrom(file) if err != nil { t.Fatal("Failed reading", err) } if buf.String() != "hello" { t.Log(buf.String()) t.Fatal("expected hello") } }
func TestIOListRead(t *testing.T) { target := &fsRoot{memfs.Create(), "/root"} testWrite(t, target, "sub1", "hello") list, err := target.List(FilterFileAllowAll) if err != nil { t.Fatal("list failed", err) } if len(list) != 1 { t.Fatal("wrong list", list) } reader, err := target.Reader(list[0]) if err != nil { t.Fatal(err) } var bytes bytes.Buffer bytes.ReadFrom(reader) if bytes.String() != "hello" { t.Log(bytes.String()) t.Fatal("wrong result") } }
func TestIOReadMissing(t *testing.T) { target := fsRoot{memfs.Create(), "/root"} _, err := target.Reader("file") if err != notFound { t.Fatal("expected not found", err) } }
func TestIORestrict(t *testing.T) { target := &fsRoot{memfs.Create(), "/"} testWrite(t, target, "sub1/leaf1", "hello") restricted := target.Restrict("sub1") if testRead(t, restricted, "leaf1") != "hello" { t.Fatal("expected hello") } }
func ExampleMemFS() { // Create a fully writable filesystem in memory fs := memfs.Create() // Like every other vfs.Filesytem, it could be wrapped, e.g. read-only: // fs = vfs.ReadOnly(fs) // The memory fs is completely empty, permissions are supported (e.g. Stat()) but have no effect. fs.Mkdir("/tmp", 0777) }
func TestEnvNoArgs(t *testing.T) { source := &fsRoot{memfs.Create(), "/source"} env, err := source.GetEnvironment() if err != nil { t.Fatal(err) } if len(env.Replacements()) != 0 { t.Fatal("wrong replacements", env.Replacements()) } }
func TestEnvMissingArg(t *testing.T) { source := &fsRoot{memfs.Create(), "/source"} testWrite(t, source, ".template", "key") _, err := source.GetEnvironment() if err == nil { t.Fatal("Expected missing property") } if strings.Index(err.Error(), "key") == -1 { t.Fatal("Wrong error, expected missing 'key'", err) } }
func TestCompareSelfNoProperties(t *testing.T) { fs := &fsRoot{memfs.Create(), "/"} testWrite(t, fs, "test", "value") env, err := fs.Compare(nil, fs) if err != nil { t.Fatal(err) } if len(env) != 0 { t.Fatal("Expected no env", env) } }
func ExampleMountFS() { // Create a vfs supporting mounts // The root fs is accessing the filesystem of the underlying OS fs := mountfs.Create(vfs.OS()) // Mount a memfs inside fs.Mount(memfs.Create(), "/memfs") // This will create /testdir inside the memfs fs.Mkdir("/memfs/testdir", 0777) // This will create /tmp/testdir inside your OS fs fs.Mkdir("/tmp/testdir", 0777) }
func TestIOListEmpty(t *testing.T) { fs := memfs.Create() fs.Mkdir("/root", 0775) target := fsRoot{fs, "/root"} list, err := target.List(FilterFileAllowAll) if err != nil { t.Fatal("list failed", err) } if len(list) != 0 { t.Fatal("expected no entries", list) } }
func TestEnvArgWithDefault(t *testing.T) { source := &fsRoot{memfs.Create(), "/source"} testWrite(t, source, ".template", "key [default value]\nother something else") env, err := source.GetEnvironment("other=value") if err != nil { t.Fatal(err) } if !reflect.DeepEqual(env.Replacements(), []string{"key", "value", "other", "value"}) { t.Log(env.Replacements()) t.Fatal("wrong replacements") } }
func TestEnvOneArg(t *testing.T) { source := &fsRoot{memfs.Create(), "/source"} testWrite(t, source, ".template", "key") env, err := source.GetEnvironment("key=value") if err != nil { t.Fatal(err) } if !reflect.DeepEqual(env.Replacements(), []string{"key", "value"}) { t.Log(env.Replacements()) t.Fatal("wrong replacements") } }
func TestIOList(t *testing.T) { target := &fsRoot{memfs.Create(), "/root"} testWrite(t, target, "sub1", "hello1") testWrite(t, target, "sub2", "hello2") list, err := target.List(FilterFileAllowAll) if err != nil { t.Fatal("list failed", err) } if !reflect.DeepEqual(list, []string{"/sub1", "/sub2"}) { t.Fatal("wrong list", list) } }
func TestMultiSimpleCreate(t *testing.T) { mem := memfs.Create() source := &fsRoot{mem, "/source"} target := &fsRoot{mem, "/target"} testWrite(t, source, "template", "hello") err := source.Create(Environment(nil), target) if err != nil { t.Fatal("Template creation failed", err) } if testRead(t, target, "template") != "hello\n" { t.Fatal("Expected hello") } }
func TestWriteFile(t *testing.T) { fs := memfs.Create() vfs.WriteFile(fs, testpath, testdata, testmode) info, err := fs.Stat(testpath) if err != nil { t.Fatalf("File not created") } if info.Size() != int64(len(testdata)) { t.Fatalf("Bad file size: %d bytes (expected %d)", info.Size(), len(testdata)) } if info.Mode() != testmode { t.Fatalf("Bad file mode: %o (expected %o)", info.Mode(), testmode) } }
func TestEnvPropsBasic(t *testing.T) { source := &fsRoot{memfs.Create(), "/source"} testWrite(t, source, ".template", "key") props, err := envReadProps(source) if err != nil { t.Fatal(err) } if len(props) != 1 { t.Fatal("expected one entry", props) } if props["key"] != "" { t.Fatal("expected key=\"\"", props["key"]) } }
func main() { /////////////////////////////////////////////////// //// Here is where we create our API //// We build End Points //// Set routes //// Create functions that serve end points // ToDo add end point to retrieve files, to do add hypermedia for discoverable // Ned to fix references to API framework, so forking doesn't cause any sillyness // Fernando Zavala 2/9/2016 /////////////////////////////////////////////////// osfs.Mkdir("root", 0777) // now, let's create the in memory fs object mfs := memfs.Create() mfs.Mkdir("/root/", 0777) // create a vfs that supports mounts // add conditional check for memfs -- todo fs := mountfs.Create(osfs) fs.Mount(mfs, "/memfs") // create directory inside of the mem file store fs.Mkdir("/memfs/root", 0777) // added stats end point to find response time api := rest.NewApi() MWstats := &rest.StatusMiddleware{} api.Use(MWstats) api.Use(rest.DefaultDevStack...) router, err := rest.MakeRouter( rest.Get("API/.status", func(w rest.ResponseWriter, r *rest.Request) { w.WriteJson(MWstats.GetStatus()) }), // Root is API, Integration is self explanatory, gives api more structure. rest.Post("API/Integration/springxdsink", springxdsink), ) if err != nil { log.Fatal(err) } api.SetApp(router) log.Fatal(http.ListenAndServe(":8080", api.MakeHandler())) }
func TestReadFile(t *testing.T) { fs := memfs.Create() f, _ := fs.OpenFile(testpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, testmode) f.Write(testdata) f.Close() data, err := vfs.ReadFile(fs, testpath) if err != nil { t.Fatalf("ReadFile failed: %s", err) } if len(data) != len(testdata) { t.Fatalf("Bad data length: %d bytes (expected %d)", len(data), len(testdata)) } _, err = vfs.ReadFile(fs, "/doesnt-exist.txt") if err == nil { t.Fatalf("ReadFile failed: expected error") } }
func fsWithFiles(filenames []string) vfs.Filesystem { fs := memfs.Create() var err error for _, fn := range filenames { path := filepath.Dir(fn) if path != "" { err = vfs.MkdirAll(fs, path, 0700) if err != nil { panic(err) } } err = touchFile(fs, fn) if err != nil { panic(err) } } return fs }
func Example() { // Create a vfs accessing the filesystem of the underlying OS var osfs vfs.Filesystem = vfs.OS() osfs.Mkdir("/tmp", 0777) // Make the filesystem read-only: osfs = vfs.ReadOnly(osfs) // Simply wrap filesystems to change its behaviour // os.O_CREATE will fail and return vfs.ErrReadOnly // os.O_RDWR is supported but Write(..) on the file is disabled f, _ := osfs.OpenFile("/tmp/example.txt", os.O_RDWR, 0) // Return vfs.ErrReadOnly _, err := f.Write([]byte("Write on readonly fs?")) if err != nil { fmt.Errorf("Filesystem is read only!\n") } // Create a fully writable filesystem in memory mfs := memfs.Create() mfs.Mkdir("/root", 0777) // Create a vfs supporting mounts // The root fs is accessing the filesystem of the underlying OS fs := mountfs.Create(osfs) // Mount a memfs inside /memfs // /memfs may not exist fs.Mount(mfs, "/memfs") // This will create /testdir inside the memfs fs.Mkdir("/memfs/testdir", 0777) // This would create /tmp/testdir inside your OS fs // But the rootfs `osfs` is read-only fs.Mkdir("/tmp/testdir", 0777) }
func rootfs() vfs.Filesystem { rfs := memfs.Create() rfs.Mkdir(prefixPath, 0777) return rfs }