Skip to content

Commit

Permalink
refactor: generalize the export cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
b1zzu committed Feb 3, 2022
1 parent 7336cc2 commit 8dd2a7b
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 43 deletions.
32 changes: 2 additions & 30 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,9 @@ var (
if err != nil {
return err
}

r := rpdac.NewReportPortal(c)

// retrieve the Dashboard and Widgets in a single reusable object
d, err := r.Dashboard.GetDashboard(exportProject, exportDashboardID)
if err != nil {
return err
}

// write the Dashboard object to file in YAML
err = rpdac.WriteToFile(d, exportFile)
if err != nil {
return err
}

log.Printf("Dashboard \"%s\" exported to \"%s\"", d.Name, exportFile)
return nil
return r.Export(rpdac.DashboardKind, exportProject, exportDashboardID, exportFile)
},
}

Expand All @@ -66,23 +52,9 @@ var (
if err != nil {
return err
}

r := rpdac.NewReportPortal(c)

// retrieve the Filter object
f, err := r.Filter.GetFilter(exportProject, exportFilterID)
if err != nil {
return err
}

// write the Filter object to file in YAML
err = rpdac.WriteToFile(f, exportFile)
if err != nil {
return err
}

log.Printf("Filter \"%s\" exported to \"%s\"", f.Name, exportFile)
return nil
return r.Export(rpdac.DashboardKind, exportProject, exportFilterID, exportFile)
},
}
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/reportportal/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestFilterGetByID(t *testing.T) {
ID: 2,
Name: "mk-e2e-test-suite",
Description: "test",
Type: "Launch",
Conditions: []FilterCondition{
{
FilteringField: "name",
Expand All @@ -71,7 +72,6 @@ func TestFilterGetByID(t *testing.T) {
IsAsc: false,
},
},
Type: "Launch",
}

if !cmp.Equal(filter, want) {
Expand Down
6 changes: 6 additions & 0 deletions pkg/rpdac/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
)

type IDashboardService interface {
Get(project string, id int) (Object, error)

GetDashboard(project string, id int) (*Dashboard, error)
GetDashboardByName(project, name string) (*Dashboard, error)
CreateDashboard(project string, d *Dashboard) error
Expand Down Expand Up @@ -61,6 +63,10 @@ type WidgetContentParameters struct {
WidgetOptions map[string]interface{} `json:"widgetOptions"`
}

func (s *DashboardService) Get(project string, id int) (Object, error) {
return s.GetDashboard(project, id)
}

func (s *DashboardService) GetDashboard(project string, id int) (*Dashboard, error) {

// retireve the dashboard defintion
Expand Down
6 changes: 6 additions & 0 deletions pkg/rpdac/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
)

type IFilterService interface {
Get(project string, id int) (Object, error)

GetFilter(project string, id int) (*Filter, error)
GetFilterByName(project, name string) (*Filter, error)
CreateFilter(project string, f *Filter) error
Expand Down Expand Up @@ -40,6 +42,10 @@ type FilterOrder struct {
IsAsc bool `json:"isAsc"`
}

func (s *FilterService) Get(project string, id int) (Object, error) {
return s.GetFilter(project, id)
}

func (s *FilterService) GetFilter(project string, id int) (*Filter, error) {

// retireve the filter defintion
Expand Down
6 changes: 5 additions & 1 deletion pkg/rpdac/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ var kinds = map[ObjectKind]string{
}

func (k ObjectKind) String() string {
return kinds[k]
v, ok := kinds[k]
if !ok {
return "Unknown"
}
return v
}

// MarshalJSON marshals the enum as a quoted json string
Expand Down
51 changes: 51 additions & 0 deletions pkg/rpdac/moks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package rpdac

type MockDashboardService struct {
GetDashboardM func(project string, id int) (*Dashboard, error)
GetDashboardByNameM func(project, name string) (*Dashboard, error)
CreateDashboardM func(project string, d *Dashboard) error
ApplyDashboardM func(project string, d *Dashboard) error
DeleteDashboardM func(project, name string) error
}

func (s *MockDashboardService) Get(project string, id int) (Object, error) {
return s.GetDashboard(project, id)
}
func (s *MockDashboardService) GetDashboard(project string, id int) (*Dashboard, error) {
return s.GetDashboardM(project, id)
}
func (s *MockDashboardService) GetDashboardByName(project, name string) (*Dashboard, error) {
return s.GetDashboardByNameM(project, name)
}
func (s *MockDashboardService) CreateDashboard(project string, d *Dashboard) error {
return s.CreateDashboardM(project, d)
}
func (s *MockDashboardService) ApplyDashboard(project string, d *Dashboard) error {
return s.ApplyDashboardM(project, d)
}
func (s *MockDashboardService) DeleteDashboard(project, name string) error {
return s.DeleteDashboardM(project, name)
}

type MockFilterService struct {
GetFilterM func(project string, id int) (*Filter, error)
GetFilterByNameM func(project, name string) (*Filter, error)
CreateFilterM func(project string, f *Filter) error
ApplyFilterM func(project string, f *Filter) error
}

func (s *MockFilterService) Get(project string, id int) (Object, error) {
return s.GetFilter(project, id)
}
func (s *MockFilterService) GetFilter(project string, id int) (*Filter, error) {
return s.GetFilterM(project, id)
}
func (s *MockFilterService) GetFilterByName(project, name string) (*Filter, error) {
return s.GetFilterByNameM(project, name)
}
func (s *MockFilterService) CreateFilter(project string, f *Filter) error {
return s.CreateFilterM(project, f)
}
func (s *MockFilterService) ApplyFilter(project string, f *Filter) error {
return s.ApplyFilterM(project, f)
}
43 changes: 32 additions & 11 deletions pkg/rpdac/reportportal.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ type service struct {
client *reportportal.Client
}

type GetInterface interface {
Get(project string, id int) (Object, error)
}

func NewReportPortal(c *reportportal.Client) *ReportPortal {
r := &ReportPortal{client: c}
r.common.client = c
Expand All @@ -40,25 +44,42 @@ func NewReportPortal(c *reportportal.Client) *ReportPortal {
return r
}

func ToYaml(o Object) ([]byte, error) {
b, err := yaml.Marshal(o)
if err != nil {
return []byte{}, fmt.Errorf("error marshal %s %s: %w", o.GetKind(), o.GetName(), err)
// Export the ObjectKind with the passed id from the passed project to the passed file.
//
// The file can be a relative or absoulte path to the file that will be written with the
// full content of the exported Object.
//
func (r *ReportPortal) Export(k ObjectKind, project string, id int, file string) error {

var s GetInterface
switch k {
case DashboardKind:
s = r.Dashboard
case FilterKind:
s = r.Filter
default:
return fmt.Errorf("error: object kind '%s' is not suppoerted from the export method", k.String())
}
return b, nil
}

func WriteToFile(o Object, file string) error {
// retrieve object from reportportal
o, err := s.Get(project, id)
if err != nil {
return fmt.Errorf("error retrieving '%s' with id '%d' in project '%s': %w", k.String(), id, project, err)
}

y, err := ToYaml(o)
// convert object to YAML
b, err := yaml.Marshal(o)
if err != nil {
return err
return fmt.Errorf("error marshal (encoding) '%s' with id '%d' in project '%s' to YAML: %w", k.String(), id, project, err)
}

err = ioutil.WriteFile(file, y, 0660)
// write object to file
err = ioutil.WriteFile(file, b, 0660)
if err != nil {
return fmt.Errorf("error writing yaml %s %s to file %s: %w", o.GetKind(), o.GetName(), file, err)
return fmt.Errorf("error writing '%s' with id '%d' in project '%s' to file '%s': %w", k.String(), id, project, file, err)
}

log.Printf("%s with id '%d' in project '%s' exported to '%s'", k.String(), id, project, file)
return nil
}

Expand Down
118 changes: 118 additions & 0 deletions pkg/rpdac/reportportal_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package rpdac

import (
"io/ioutil"
"os"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -25,3 +27,119 @@ func testEqual(t *testing.T, got, want interface{}) {
t.Errorf("Want \"%s\" but got \"%s\"", want, got)
}
}

func testFileContains(t *testing.T, file string, want string) {
t.Helper()

b, err := ioutil.ReadFile(file)
if err != nil {
t.Errorf("failed to read file '%s': %s", file, err)
}
got := string(b)

if got != want {
t.Errorf("Want '''%s''' from file '%s' but got '''%s'''", want, file, got)
}
}

func tmpFile(t *testing.T, patter string) (name string, clean func()) {
t.Helper()

file, err := ioutil.TempFile("", "dashboard")
if err != nil {
t.Errorf("failed to create tmp file: %s", err)
return "", func() {}
}

name = file.Name()
clean = func() {
err := os.Remove(name)
if err != nil {
t.Errorf("failed to clean tm file '%s': %s", name, err)
}
}

return name, clean
}

func TestExportDashboard(t *testing.T) {
file, cleanFile := tmpFile(t, "dashboard")
defer cleanFile()

r := NewReportPortal(nil)

r.Dashboard = &MockDashboardService{
GetDashboardM: func(project string, id int) (*Dashboard, error) {
testEqual(t, project, "test_project")
testEqual(t, id, 3)
return &Dashboard{
Kind: DashboardKind,
Name: "MK E2E Tests Overview",
Description: "",
Widgets: []*Widget{},
}, nil
},
}

err := r.Export(DashboardKind, "test_project", 3, file)
if err != nil {
t.Errorf("Export returned error: %s", err)
}

want := `kind: Dashboard
name: MK E2E Tests Overview
description: ""
widgets: []
`

testFileContains(t, file, want)
}

func TestExportFilter(t *testing.T) {
file, cleanFile := tmpFile(t, "filter")
defer cleanFile()

r := NewReportPortal(nil)

r.Filter = &MockFilterService{
GetFilterM: func(project string, id int) (*Filter, error) {
testEqual(t, project, "test_project")
testEqual(t, id, 3)
return &Filter{
Kind: FilterKind,
Name: "mk-e2e-test-suite",
Type: "Launch",
Description: "",
Conditions: []FilterCondition{
{FilteringField: "name", Condition: "eq", Value: "mk-e2e-test-suite"},
},
Orders: []FilterOrder{
{SortingColumn: "startTime", IsAsc: false},
{SortingColumn: "number", IsAsc: false},
},
}, nil
},
}

err := r.Export(FilterKind, "test_project", 3, file)
if err != nil {
t.Errorf("Export returned error: %s", err)
}

want := `kind: Filter
name: mk-e2e-test-suite
type: Launch
description: ""
conditions:
- filteringfield: name
condition: eq
value: mk-e2e-test-suite
orders:
- sortingcolumn: startTime
isasc: false
- sortingcolumn: number
isasc: false
`

testFileContains(t, file, want)
}

0 comments on commit 8dd2a7b

Please sign in to comment.