示例#1
0
func (s *EditorSuite) TestLoadPreprocessed() {
	s.loadInterfacesConf()
	s.VerifyRpc("Load", objx.Map{"path": "/etc/network/interfaces"}, objx.Map{
		"configPath": "/etc/network/interfaces",
		"content":    objx.MustFromJSON(EXPECTED_INTERFACES_JSON),
		"schema": objx.MustFromJSON(
			strings.Replace(
				s.ReadSourceDataFile("interfaces.schema.json"),
				"_format", "format", -1)),
	})
}
示例#2
0
func (s *EditorSuite) TestMultipleSchemasPerConfig() {
	s.CopyDataFilesToTempDir("sample-extra.schema.json")
	s.Ck("loadSchema()", s.editor.loadSchema("sample-extra.schema.json"))
	s.VerifyRpc("List", objx.Map{}, []objx.Map{
		{
			"configPath":  "/sample.json",
			"schemaPath":  "/sample-extra.schema.json",
			"title":       "Example Config (alt)",
			"description": "Just an example (alt)",
		},
		{
			"configPath":  "/sample.json",
			"schemaPath":  "/sample.schema.json",
			"title":       "Example Config",
			"description": "Just an example",
		},
	})
	content := objx.Map{
		"device_type": "MSU21",
		"name":        "MSU21",
		"id":          "msu21",
		"slave_id":    float64(24),
		"enabled":     true,
	}
	s.VerifyRpc("Load", objx.Map{"path": "/sample.schema.json"}, objx.Map{
		"configPath": "/sample.json",
		"content":    content,
		"schema":     objx.MustFromJSON(EXPECTED_SCHEMA_CONTENT),
	})
	s.VerifyRpc("Load", objx.Map{"path": "/sample-extra.schema.json"}, objx.Map{
		"configPath": "/sample.json",
		"content":    content,
		"schema":     objx.MustFromJSON(EXPECTED_ALT_SCHEMA_CONTENT),
	})

	content["id"] = "msu21xxx"
	s.VerifyRpc("Save", objx.Map{
		"path":    "/sample.schema.json",
		"content": content,
	}, objx.Map{
		"path": "/sample.schema.json",
	})
	s.verifyJSONFile("sample.json", content)

	content["id"] = "msu21yyy"
	s.VerifyRpc("Save", objx.Map{
		"path":    "/sample-extra.schema.json",
		"content": content,
	}, objx.Map{
		"path": "/sample-extra.schema.json",
	})
	s.verifyJSONFile("sample.json", content)
}
示例#3
0
func (s *EditorSuite) TestRemoveSchema() {
	s.verifyInitialSchemaList()
	s.CopyDataFilesToTempDir("another.schema.json", "another.json")
	s.RmFile("sample.schema.json")
	dwc := NewEditorDirWatcherClient(s.editor)
	s.Ck("LiveLoadFile()", dwc.LiveLoadFile(s.DataFilePath("another.schema.json")))
	s.Ck("LiveRemoveFile()", dwc.LiveRemoveFile(s.DataFilePath("sample.schema.json")))
	s.VerifyRpc("List", objx.Map{}, []objx.Map{
		{
			"configPath":  "/another.json",
			"schemaPath":  "/another.schema.json",
			"title":       "Another Example Config",
			"description": "",
		},
	})
	s.VerifyRpcError("Load", objx.Map{"path": "/sample.json"},
		EDITOR_ERROR_FILE_NOT_FOUND, "EditorError", "File not found")
	s.VerifyRpc("Load", objx.Map{"path": "/another.json"}, objx.Map{
		"configPath": "/another.json",
		"content": objx.Map{
			"name": "foobar",
		},
		"schema": objx.MustFromJSON(EXPECTED_ANOTHER_SCHEMA_CONTENT),
	})
}
示例#4
0
func (s *EditorSuite) TestAddSchema() {
	s.verifyInitialSchemaList()
	s.CopyDataFilesToTempDir("another.schema.json", "another.json")
	dwc := NewEditorDirWatcherClient(s.editor)
	s.Ck("LiveLoadFile()", dwc.LiveLoadFile(s.DataFilePath("another.schema.json")))
	s.VerifyRpc("List", objx.Map{}, []objx.Map{
		{
			"configPath":  "/another.json",
			"schemaPath":  "/another.schema.json",
			"title":       "Another Example Config",
			"description": "",
		},
		{
			"configPath":  "/sample.json",
			"schemaPath":  "/sample.schema.json",
			"title":       "Example Config",
			"description": "Just an example",
		},
	})
	s.verifyLoadSampleJson()
	s.VerifyRpc("Load", objx.Map{"path": "/another.json"}, objx.Map{
		"configPath": "/another.json",
		"content": objx.Map{
			"name": "foobar",
		},
		"schema": objx.MustFromJSON(EXPECTED_ANOTHER_SCHEMA_CONTENT),
	})
}
示例#5
0
func (s *EditorSuite) verifyLoadSampleJson() {
	s.VerifyRpc("Load", objx.Map{"path": "/sample.json"}, objx.Map{
		"configPath": "/sample.json",
		"content": objx.Map{
			"device_type": "MSU21",
			"name":        "MSU21",
			"id":          "msu21",
			"slave_id":    float64(24),
			"enabled":     true,
		},
		"schema": objx.MustFromJSON(EXPECTED_SCHEMA_CONTENT),
	})
}
示例#6
0
func TestJSToObjxAndBack(t *testing.T) {
	ctx := newESContext(nil)
	for _, jsonStr := range objTests {
		if r := ctx.PevalString("(" + jsonStr + ")"); r != 0 {
			t.Fatal("failed to evaluate the script")
		}
		object := ctx.GetJSObject(-1)
		ctx.Pop()
		json := objx.MustFromJSON(jsonStr)
		assert.Equal(t, json, object)

		ctx.PushGlobalObject()
		ctx.PushJSObject(object.(objx.Map))
		ctx.PutPropString(-2, "jso")
		if r := ctx.PevalString("JSON.stringify(jso)"); r != 0 {
			t.Fatal("failed to evaluate the script")
		}
		jsonStr1 := ctx.SafeToString(-1)
		ctx.Pop()
		json1 := objx.MustFromJSON(jsonStr1)
		assert.Equal(t, json, json1)
	}
}
示例#7
0
func (s *EditorSuite) TestLoadFile() {
	s.verifyLoadSampleJson()
	s.CopyDataFilesToTempDir("another.schema.json", "another.json")
	s.Ck("loadSchema()", s.editor.loadSchema("another.schema.json"))
	for _, path := range []string{"/another.json", "/another.schema.json"} {
		s.VerifyRpc("Load", objx.Map{"path": path}, objx.Map{
			"configPath": "/another.json",
			"content": objx.Map{
				"name": "foobar",
			},
			"schema": objx.MustFromJSON(EXPECTED_ANOTHER_SCHEMA_CONTENT),
		})
	}
}
示例#8
0
func (s *EditorSuite) verifyJSONFile(path string, expectedContent objx.Map) {
	bs, err := ioutil.ReadFile(s.DataFilePath(path))
	s.Ck("ReadFile()", err)
	s.Equal(expectedContent, objx.MustFromJSON(string(bs)))
}