Exemplo n.º 1
0
func TestMain(m *testing.M) {
	var err error

	c := config.NewConfig()
	c.ContainerName = TEST_CONTAINER_NAME

	client = NewSwift(c)
	if err = client.Auth(); err != nil {
		log.Fatalf("%v", err)
		os.Exit(1)
	}

	client.CreateContainer()
	code := m.Run()
	client.DeleteContainer()

	defer os.Exit(code)
}
Exemplo n.º 2
0
func initSwift() error {
	c := config.NewConfig()
	c.ContainerName = TEST_CONTAINER

	// initialize swift
	var err error
	swift = openstack.NewSwift(c)
	if err = swift.Auth(); err != nil {
		return err
	}
	if err = swift.DeleteContainer(); err != nil {
		// 404
	}

	if err = swift.CreateContainer(); err != nil {
		return err
	}
	return nil
}
Exemplo n.º 3
0
func TestSetup(t *testing.T) {
	var err error

	c := config.NewConfig()
	c.MountPoint = TEST_MOUNTPOINT
	c.ContainerName = TEST_CONTAINER_NAME
	c.CreateContainer = true
	c.Debug = true
	c.NoDaemon = true

	// initialize swift and uplaod testdata
	swift := openstack.NewSwift(c)
	if err = swift.Auth(); err != nil {
		t.Fatalf("%v", err)
	}

	swift.DeleteContainer()
	swift.CreateContainer()

	// mapper
	mp, err := mapper.NewObjectMapper(c)
	if err != nil {
		t.Fatalf("%v", err)
	}
	obj, err := mp.Create(TEST_OBJECT_NAME)
	if err != nil {
		t.Fatalf("%v", err)
	}
	file, err := obj.Open(os.O_CREATE|os.O_WRONLY, 0666)
	if err != nil {
		t.Fatalf("%v", err)
	}

	_, err = file.WriteString(TEST_DATA)
	if err != nil {
		t.Fatalf("%v", err)
	}
	file.Close()

	// initialize ObjectFile
	objfile = NewObjectFile(TEST_OBJECT_NAME, obj)
}
Exemplo n.º 4
0
func TestNewObjectMapper(t *testing.T) {
	// if we need debug messages, please comment it out.
	// logrus.SetLevel(logrus.DebugLevel)

	// init swift
	if err := initSwift(); err != nil {
		t.Fatalf("%v", err)
	}
	swift.DeleteContainer()
	swift.CreateContainer()

	// Upload test directory and test data before initialize mapper
	dirname := "test-directory"
	swift.Upload(TEST_OBJECT, strings.NewReader(TEST_DATA))
	swift.MakeDirectory(dirname)

	// init mapper
	c := config.NewConfig()
	c.ContainerName = TEST_CONTAINER
	mapper, _ = NewObjectMapper(c)

	// test to exist local file or directory by syncObject()
	obj, ok := mapper.objects[dirname]
	if !ok {
		t.Fatalf("Directory %s not found", dirname)
	} else if obj.Type != DIRECTORY {
		t.Fatalf("invalid object type %s.(not directory)", dirname)
	}

	obj, ok = mapper.objects[TEST_OBJECT]
	if !ok {
		t.Fatalf("Object %s not found", TEST_OBJECT)
	} else if obj.Type != FILE {
		t.Fatalf("invalid object type %s.(not file)", TEST_OBJECT)
	}
}
Exemplo n.º 5
0
func Run() {
	app := cli.NewApp()

	app.Name = config.APP_NAME
	app.Usage = "The file system to mount OpenStack Swift via FUSE."
	app.Version = config.APP_VERSION
	app.HideHelp = true
	app.Author = "Hironobu Saitoh"
	app.Email = "*****@*****.**"
	app.ArgsUsage = "container-name mountpoint"

	conf := config.NewConfig()
	defer conf.Logfile.Close()

	app.Flags = conf.GetFlags()

	app.Action = func(c *cli.Context) {
		if c.Bool("help") || len(c.Args()) < 2 {
			cli.ShowAppHelp(c)
			return
		}

		app.Flags = append(app.Flags, cli.BoolFlag{
			Name:  "child",
			Usage: "internal use only",
		})

		var err error
		if err = conf.SetConfigFromContext(c); err != nil {
			log.Warnf("%v", err)
			return
		}

		if !conf.ChildProcess {
			if err = daemonize(c, conf); err != nil {
				log.Warnf("%v", err)
				return
			}
		}

		log.Debug("Create mapper")
		mapper, err := mapper.NewObjectMapper(conf)
		if err != nil {
			log.Warnf("%v", err)
			afterDaemonize(err)
			return
		}

		log.Debug("Create filesystem")
		objectFs := fs.NewObjectFileSystem(conf, mapper)

		log.Debug("Mount filesystem")
		server, err := mount(objectFs, conf.MountPoint)
		if err != nil {
			log.Warnf("%v", err)
			afterDaemonize(err)
			return
		}

		if conf.ChildProcess {
			afterDaemonize(nil)
		}

		// main loop
		log.Debugf("Swiftfs process with pid %d started", syscall.Getpid())
		server.Serve()

		log.Debug("Shutdown")
	}

	// In daemonizing process, "--child" flag wil be used to recognize myelf as child process.
	// Remove this flag before the Apps parsing the arguments.
	args := make([]string, 0, len(os.Args))
	for _, p := range os.Args {
		if p == "--child" {
			conf.ChildProcess = true
		} else {
			args = append(args, p)
		}
	}

	if err := app.Run(args); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}