Example #1
0
// InitializeEnvironment creates new transaction for the test
func (env *Environment) InitializeEnvironment() error {
	var err error
	_, file := filepath.Split(env.testFileName)
	env.dbFile, err = ioutil.TempFile(os.TempDir(), file)
	if err != nil {
		return fmt.Errorf("Failed to create a temporary file in %s: %s", os.TempDir(), err.Error())
	}
	env.dbConnection, err = newDBConnection(env.dbFile.Name())
	if err != nil {
		return fmt.Errorf("Failed to connect to database: %s", err.Error())
	}
	env.Environment = gohan_otto.NewEnvironment(env.dbConnection, &middleware.FakeIdentity{}, 30*time.Second)
	env.SetUp()
	env.addTestingAPI()

	script, err := env.VM.Compile(env.testFileName, env.testSource)
	if err != nil {
		return fmt.Errorf("Failed to compile the file '%s': %s", env.testFileName, err.Error())
	}

	env.VM.Run(script)
	err = env.loadSchemas()
	if err != nil {
		schema.ClearManager()
		return fmt.Errorf("Failed to load schema for '%s': %s", env.testFileName, err.Error())
	}

	err = db.InitDBWithSchemas("sqlite3", env.dbFile.Name(), true, false)
	if err != nil {
		schema.ClearManager()
		return fmt.Errorf("Failed to init DB: %s", err.Error())
	}

	return nil
}
Example #2
0
func newEnvironmentWithExtension(extension *schema.Extension) (env extension.Environment) {
	timelimit := time.Duration(1) * time.Second
	extensions := []*schema.Extension{extension}
	env = otto.NewEnvironment("db_test",
		testDB, &middleware.FakeIdentity{}, timelimit)
	Expect(env.LoadExtensionsForPath(extensions, "test_path")).To(Succeed())
	return
}
Example #3
0
func (server *Server) newEnvironment(name string) extension.Environment {
	envs := []extension.Environment{}
	timelimit := time.Duration(server.timelimit) * time.Second
	for _, extension := range server.extensions {
		switch extension {
		case "javascript":
			envs = append(envs, otto.NewEnvironment(name, server.db, server.keystoneIdentity, timelimit))
		case "gohanscript":
			envs = append(envs, gohanscript.NewEnvironment(timelimit))
		case "go":
			envs = append(envs, golang.NewEnvironment())
		}
	}
	return extension.NewEnvironment(envs)
}
Example #4
0
	"github.com/cloudwan/gohan/server/middleware"
)

var _ = Describe("Environment manager", func() {
	const (
		schemaID1 = "Wormtongue"
		schemaID2 = "Dumbledore"
	)
	var (
		env1    extension.Environment
		env2    extension.Environment
		manager *extension.Manager
	)

	BeforeEach(func() {
		env1 = otto.NewEnvironment(testDB1, &middleware.FakeIdentity{})
		env2 = otto.NewEnvironment(testDB2, &middleware.FakeIdentity{})
	})

	JustBeforeEach(func() {
		manager = extension.GetManager()
		Expect(manager.RegisterEnvironment(schemaID1, env1)).To(Succeed())
	})

	AfterEach(func() {
		extension.ClearManager()
	})

	Describe("Registering environments", func() {
		Context("When it isn't registered", func() {
			It("Should register it", func() {
Example #5
0
					"id": "test_extension",
					"code": `
					  gohan_register_handler("test_event", function(context){
					    var tx = context.transaction;
					    context.resp = gohan_db_state_fetch(
					      tx,
					      "test",
					      "resource_id",
					      "tenant0"
					    );
					  });`,
					"path": ".*",
				})
				Expect(err).ToNot(HaveOccurred())
				extensions := []*schema.Extension{extension}
				env := otto.NewEnvironment(testDB, &middleware.FakeIdentity{}, timelimit)
				Expect(env.LoadExtensionsForPath(extensions, "test_path")).To(Succeed())

				manager := schema.GetManager()
				s, ok := manager.Schema("test")
				Expect(ok).To(BeTrue())

				var fakeTx = new(mocks.Transaction)
				fakeTx.On(
					"StateFetch", s, "resource_id", []string{"tenant0"},
				).Return(
					transaction.ResourceState{
						ConfigVersion: 30,
						StateVersion:  29,
						Error:         "e",
						State:         "s",
Example #6
0
	"github.com/cloudwan/gohan/server/middleware"
)

var _ = Describe("Environment manager", func() {
	const (
		schemaID1 = "Wormtongue"
		schemaID2 = "Dumbledore"
	)
	var (
		env1    extension.Environment
		env2    extension.Environment
		manager *extension.Manager
	)

	BeforeEach(func() {
		env1 = otto.NewEnvironment("extension_test1",
			testDB1, &middleware.FakeIdentity{}, time.Second)
		env2 = otto.NewEnvironment("extension_test2",
			testDB2, &middleware.FakeIdentity{}, time.Second)
	})

	JustBeforeEach(func() {
		manager = extension.GetManager()
		Expect(manager.RegisterEnvironment(schemaID1, env1)).To(Succeed())
	})

	AfterEach(func() {
		extension.ClearManager()
	})

	Describe("Registering environments", func() {
		Context("When it isn't registered", func() {
Example #7
0
					"code": `gohan_register_handler("test_event", function(context) {});`,
					"path": ".*",
				})
				Expect(err).ToNot(HaveOccurred())
				goodExtension.URL = "good_extension.js"

				badExtension, err := schema.NewExtension(map[string]interface{}{
					"id":   "bad_extension",
					"code": `gohan_register_handler("test_event", function(context {});`,
					"path": ".*",
				})
				Expect(err).ToNot(HaveOccurred())
				badExtension.URL = "bad_extension.js"

				extensions := []*schema.Extension{goodExtension, badExtension}
				env := otto.NewEnvironment(testDB, &middleware.FakeIdentity{})
				err = env.LoadExtensionsForPath(extensions, "test_path")
				Expect(err).To(HaveOccurred(), "Expected compilation errors.")

				pattern := regexp.MustCompile(`^(?P<file>[^:]+).*Line\s(?P<line>\d+).*`)
				match := pattern.FindStringSubmatch(err.Error())
				Expect(len(match)).To(Equal(3))

				groups := make(map[string]string)
				for i, name := range pattern.SubexpNames() {
					groups[name] = match[i]
				}

				Expect(groups).To(HaveKeyWithValue("file", "bad_extension.js"))
				Expect(groups).To(HaveKeyWithValue("line", "1"))
			})
Example #8
0
func newEnvironment(dataStore db.DB, identity middleware.IdentityService, timelimit int) extension.Environment {
	return otto.NewEnvironment(dataStore, identity, time.Duration(timelimit)*time.Second)
}
Example #9
0
func newEnvironment(dataStore db.DB, identity middleware.IdentityService) extension.Environment {
	return otto.NewEnvironment(dataStore, identity)
}
Example #10
0
	"github.com/cloudwan/gohan/server/middleware"
)

var _ = Describe("Environment manager", func() {
	const (
		schemaID1 = "Wormtongue"
		schemaID2 = "Dumbledore"
	)
	var (
		env1    extension.Environment
		env2    extension.Environment
		manager *extension.Manager
	)

	BeforeEach(func() {
		env1 = otto.NewEnvironment(testDB1, &middleware.FakeIdentity{}, time.Second)
		env2 = otto.NewEnvironment(testDB2, &middleware.FakeIdentity{}, time.Second)
	})

	JustBeforeEach(func() {
		manager = extension.GetManager()
		Expect(manager.RegisterEnvironment(schemaID1, env1)).To(Succeed())
	})

	AfterEach(func() {
		extension.ClearManager()
	})

	Describe("Registering environments", func() {
		Context("When it isn't registered", func() {
			It("Should register it", func() {
Example #11
0
					"code": `gohan_register_handler("test_event", function(context) {});`,
					"path": ".*",
				})
				Expect(err).ToNot(HaveOccurred())
				goodExtension.URL = "good_extension.js"

				badExtension, err := schema.NewExtension(map[string]interface{}{
					"id":   "bad_extension",
					"code": `gohan_register_handler("test_event", function(context {});`,
					"path": ".*",
				})
				Expect(err).ToNot(HaveOccurred())
				badExtension.URL = "bad_extension.js"

				extensions := []*schema.Extension{goodExtension, badExtension}
				env := otto.NewEnvironment(testDB, &middleware.FakeIdentity{}, timelimit)
				err = env.LoadExtensionsForPath(extensions, "test_path")
				Expect(err).To(HaveOccurred(), "Expected compilation errors.")

				pattern := regexp.MustCompile(`^(?P<file>[^:]+).*Line\s(?P<line>\d+).*`)
				match := pattern.FindStringSubmatch(err.Error())
				Expect(len(match)).To(Equal(3))

				groups := make(map[string]string)
				for i, name := range pattern.SubexpNames() {
					groups[name] = match[i]
				}

				Expect(groups).To(HaveKeyWithValue("file", "bad_extension.js"))
				Expect(groups).To(HaveKeyWithValue("line", "1"))
			})