Example #1
0
// getPluginDataAndHTML returns all data needed to properly render plugins
// for a page. It logs errors but does not return them, as plugin errors
// cannot stop the rendering of the rest of the page
func getPluginDataAndHTML(pluginManager plugin.PanelManager, page plugin.PageScope, ctx plugin.UIContext) pluginData {
	includes, err := pluginManager.Includes(page)
	if err != nil {
		evergreen.Logger.Errorf(
			slogger.ERROR, "error getting include html from plugin manager on %v page: %v",
			page, err)
	}

	panels, err := pluginManager.Panels(page)
	if err != nil {
		evergreen.Logger.Errorf(
			slogger.ERROR, "error getting panel html from plugin manager on %v page: %v",
			page, err)
	}

	data, err := pluginManager.UIData(ctx, page)
	if err != nil {
		evergreen.Logger.Errorf(slogger.ERROR, "error getting plugin data on %v page: %v",
			page, err)
	}

	return pluginData{includes, panels, data}
}
Example #2
0
func TestPanelManagerRetrieval(t *testing.T) {
	var ppm plugin.PanelManager

	Convey("With a simple plugin panel manager", t, func() {
		ppm = &plugin.SimplePanelManager{}

		Convey("and a registered set of test plugins with panels", func() {
			// These 3 plugins exist to check the sort output of the manager.
			// For consistency, plugin panels and includes are ordered by plugin name
			// and then by the order of their declaration in the Panels array.
			// This test asserts that the panels in A come before B which come
			// before C, even though they are not in the plugin array in that order.
			testPlugins := []plugin.Plugin{
				&MockUIPlugin{
					NickName: "A_the_first_letter",
					Conf: &plugin.PanelConfig{
						Panels: []plugin.UIPanel{
							{
								Page:     plugin.TaskPage,
								Position: plugin.PageCenter,
								Includes: []template.HTML{
									"0",
									"1",
								},
								PanelHTML: "0",
								DataFunc: func(context plugin.UIContext) (interface{}, error) {
									return 1000, nil
								},
							},
							{
								Page:     plugin.TaskPage,
								Position: plugin.PageCenter,
								Includes: []template.HTML{
									"2",
									"3",
								},
								PanelHTML: "1",
							},
							{
								Page:     plugin.TaskPage,
								Position: plugin.PageLeft,
								Includes: []template.HTML{
									"4",
								},
								PanelHTML: "X",
							},
						},
					},
				},
				&MockUIPlugin{
					NickName: "C_the_third_letter",
					Conf: &plugin.PanelConfig{
						Panels: []plugin.UIPanel{
							{
								Page:     plugin.TaskPage,
								Position: plugin.PageCenter,
								Includes: []template.HTML{
									"7",
									"8",
								},
								PanelHTML: "3",
								DataFunc: func(context plugin.UIContext) (interface{}, error) {
									return 2112, nil
								},
							},
						},
					},
				},
				&MockUIPlugin{
					NickName: "B_the_middle_letter",
					Conf: &plugin.PanelConfig{
						Panels: []plugin.UIPanel{
							{
								Page:     plugin.TaskPage,
								Position: plugin.PageCenter,
								Includes: []template.HTML{
									"5",
								},
								PanelHTML: "2",
								DataFunc: func(context plugin.UIContext) (interface{}, error) {
									return 1776, nil
								},
							},
							{
								Page:     plugin.TaskPage,
								Position: plugin.PageLeft,
								Includes: []template.HTML{
									"6",
								},
								PanelHTML: "Z",
							},
						},
					},
				},
			}

			err := ppm.RegisterPlugins(testPlugins)
			So(err, ShouldBeNil)

			Convey("retrieved includes for the task page should be in correct "+
				"stable alphabetical order by plugin name", func() {
				includes, err := ppm.Includes(plugin.TaskPage)
				So(err, ShouldBeNil)
				So(includes, ShouldNotBeNil)

				// includes == [0 1 2 ... ]
				for i := 1; i < len(includes); i++ {
					So(includes[i], ShouldBeGreaterThan, includes[i-1])
				}
			})
			Convey("retrieved panel HTML for the task page should be in correct "+
				"stable alphabetical order by plugin name", func() {
				panels, err := ppm.Panels(plugin.TaskPage)
				So(err, ShouldBeNil)
				So(len(panels.Right), ShouldEqual, 0)
				So(len(panels.Left), ShouldBeGreaterThan, 0)
				So(len(panels.Center), ShouldBeGreaterThan, 0)

				// left == [X Z]
				So(panels.Left[0], ShouldBeLessThan, panels.Left[1])

				// center == [0 1 2 3]
				for i := 1; i < len(panels.Center); i++ {
					So(panels.Center[i], ShouldBeGreaterThan, panels.Center[i-1])
				}
			})
			Convey("data functions populate the results map with their return values", func() {
				uiData, err := ppm.UIData(plugin.UIContext{}, plugin.TaskPage)
				So(err, ShouldBeNil)
				So(len(uiData), ShouldBeGreaterThan, 0)
				So(uiData["A_the_first_letter"], ShouldEqual, 1000)
				So(uiData["B_the_middle_letter"], ShouldEqual, 1776)
				So(uiData["C_the_third_letter"], ShouldEqual, 2112)
			})
		})
	})
}