Skip to content

Commit

Permalink
Adding a nested folder test
Browse files Browse the repository at this point in the history
  • Loading branch information
safaci2000 committed Sep 10, 2024
1 parent 67e3a40 commit 18208dd
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 65 deletions.
6 changes: 2 additions & 4 deletions config/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ context_name: testing
contexts:
testing:
output_path: test/data
enterprise_support: true
connections:
credential_rules:
- rules:
Expand All @@ -21,7 +20,8 @@ contexts:
user_name: admin
organization_name: Main Org.
password: admin
ignore_filters: False # When set to true all Watched filtered folders will be ignored and ALL folders will be acted on
filter_override:
ignore_dashboard_filters: false
dashboard_settings:
nested_folders: false
watched:
Expand All @@ -47,7 +47,6 @@ contexts:
dashboard_settings:
nested_folders: false
organization_name: Your Org
ignore_filters: False # When set to true all Watched filtered folders will be ignored and ALL folders will be acted on
watched:
- General
qa:
Expand All @@ -72,7 +71,6 @@ contexts:
organization_name: Your Org
dashboard_settings:
nested_folders: false
ignore_filters: False # When set to true all Watched filtered folders will be ignored and ALL folders will be acted on
watched:
- Folder1
- Folder2
Expand Down
17 changes: 2 additions & 15 deletions internal/config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,22 +298,9 @@ func InitGdgConfig(override, defaultConfig string) {
log.Fatal("Invalid configuration detected, please fix your configuration and try again.")
}
if configData.viperConfiguration == nil {
configData.viperConfiguration = make(map[string]*viper.Viper, 0)
configData.viperConfiguration = make(map[string]*viper.Viper)
}
configData.viperConfiguration[ViperGdgConfig] = v

// unmarshall struct
//contexts := configData.GetViperConfig(ViperGdgConfig).GetStringMap("contexts")
//contexts = applyEnvOverrides(contexts, "contexts", v)
//
//contextMaps, err := yaml.Marshal(contexts)
//if err != nil {
// log.Fatal("Failed to decode context map, please check your configuration")
//}
//err = yaml.Unmarshal(contextMaps, &configData.gdgConfig.Contexts)
//if err != nil {
// log.Fatal("No valid configuration file has been found")
//}
}

// readViperConfig utilizes the viper library to load the config from the selected paths
Expand All @@ -331,7 +318,7 @@ func readViperConfig[T any](appName string, configDirs []string, object *T) (*vi

err := v.ReadInConfig()
if err == nil {
// Marshall the data read into a app struct
// Marshall the data read into app struct
err = v.Unmarshal(object)
}

Expand Down
13 changes: 6 additions & 7 deletions internal/config/connection_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,20 @@ type CredentialRule struct {

// MatchingRule defines a single matching rule for Grafana Connections
type MatchingRule struct {
Field string `yaml:"field,omitempty"`
Regex string `yaml:"regex,omitempty"`
Inclusive bool `yaml:"inclusive,omitempty"`
Field string `yaml:"field,omitempty" mapstructure:"field,omitempty"`
Regex string `yaml:"regex,omitempty" mapstructure:"regex,omitempty"`
Inclusive bool `yaml:"inclusive,omitempty" mapstructure:"inclusive,omitempty"`
}

// FilterOverrides model wraps filter overrides for grafana
type FilterOverrides struct {
IgnoreDashboardFilters bool `yaml:"ignore_dashboard_filters"`
IgnoreDashboardFilters bool `yaml:"ignore_dashboard_filters" mapstructure:"ignore_dashboard_filters" `
}

// ConnectionFilters model wraps connection filters for grafana
type ConnectionFilters struct {
NameExclusions string `yaml:"name_exclusions"`
ConnectionTypes []string `yaml:"valid_types"`
// pattern *regexp.Regexp
NameExclusions string `yaml:"name_exclusions" mapstructure:"name_exclusions"`
ConnectionTypes []string `yaml:"valid_types" mapstructure:"valid_types"`
}

func (g GrafanaConnection) User() string {
Expand Down
4 changes: 2 additions & 2 deletions internal/service/dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ func (s *DashNGoImpl) UploadDashboards(filterReq filters.Filter) {
if len(invalidCount) > 0 {
log.Fatal("nested folder feature is disabled in GDG but import path contains a nested folder. Please fix the import or configuration. ", slog.String("files", strings.Join(invalidCount, ", ")))
}
// Check duplicates

}

// Delete all dashboards that match prior to import
Expand Down Expand Up @@ -591,7 +591,7 @@ func (s *DashNGoImpl) UploadDashboards(filterReq filters.Filter) {
slog.Warn("unable to determine dashboard folder name, falling back on default")
}

if folderName == "" || folderName == DefaultFolderName {
if folderName == "" {
folderName = DefaultFolderName
}
if !slices.Contains(validFolders, folderName) && !config.Config().GetDefaultGrafanaConfig().GetFilterOverrides().IgnoreDashboardFilters {
Expand Down
1 change: 1 addition & 0 deletions pkg/test_tooling/nested_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
OrgNameOverride = "GDG_CONTEXTS__TESTING__ORGANIZATION_NAME"
EnableNestedBehavior = "GDG_CONTEXTS__TESTING__DASHBOARD_SETTINGS__NESTED_FOLDERS"
grafanaNestedToggle = "GF_FEATURE_TOGGLES_ENABLE"
IgnoreDashFilters = "GDG_CONTEXTS__TESTING__FILTER_OVERRIDE__IGNORE_DASHBOARD_FILTERS"
)

func setupNestedProps(t *testing.T, enterprise bool) map[string]string {
Expand Down
51 changes: 19 additions & 32 deletions test/dashboard_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,65 +12,52 @@ import (
"github.com/esnet/gdg/internal/service"
"github.com/esnet/gdg/internal/service/filters"
"github.com/esnet/gdg/pkg/test_tooling"
"github.com/esnet/gdg/pkg/test_tooling/containers"
"github.com/grafana/grafana-openapi-client-go/models"

"github.com/stretchr/testify/assert"
)

func TestDashboardNestedFolderCRUD(t *testing.T) {
t.Skip()
if os.Getenv(test_tooling.EnableTokenTestsEnv) == "1" {
t.Log("Skipping token test for nested folders")
t.Skip()
t.Skip("skipping token based tests")
}
containerObj, cleanup := test_tooling.InitOrgs(t, false)
assert.NoError(t, os.Setenv(test_tooling.OrgNameOverride, "testing"))
assert.NoError(t, os.Setenv(test_tooling.EnableNestedBehavior, "true"))
assert.NoError(t, os.Setenv(test_tooling.IgnoreDashFilters, "true"))

props := containers.DefaultGrafanaEnv()
props["GF_FEATURE_TOGGLES_ENABLE"] = "nestedFolders"
apiClient, _, _, cleanup := test_tooling.InitTest(t, nil, props)
defer func() {
err := cleanup()
if err != nil {
slog.Warn("Unable to clean up after dashboard tests")
}
os.Unsetenv(test_tooling.OrgNameOverride)
os.Unsetenv(test_tooling.EnableNestedBehavior)
cleanup()
}()

apiClient, _ := test_tooling.CreateSimpleClient(t, nil, containerObj)

filtersEntity := service.NewDashboardFilter("", "", "")
slog.Info("Exporting all dashboards")
apiClient.UploadDashboards(filtersEntity)
slog.Info("Listing all dashboards")
boards := apiClient.ListDashboards(filtersEntity)
slog.Info("Imported dashboards", "count", len(boards))
ignoredSkipped := true
var generalBoard *models.Hit
var otherBoard *models.Hit
var nestedFolder *models.Hit
for ndx, board := range boards {
slog.Info(board.Slug)
if board.Slug == "latency-patterns" {
ignoredSkipped = false
}
if board.Slug == "individual-flows" {
if board.Slug == "rabbitmq-overview" {
generalBoard = boards[ndx]
}
if board.Slug == "flow-information" {
otherBoard = boards[ndx]
if board.Slug == "node-exporter-full" {
nestedFolder = boards[ndx]
}
}
assert.NotNil(t, otherBoard)
assert.NotNil(t, generalBoard)
assert.True(t, ignoredSkipped)
validateGeneralBoard(t, generalBoard)
validateOtherBoard(t, otherBoard)
// Validate filters

filterFolder := service.NewDashboardFilter("Other", "", "")
boards = apiClient.ListDashboards(filterFolder)
assert.Equal(t, 8, len(boards))
dashboardFilter := service.NewDashboardFilter("", "flow-information", "")
boards = apiClient.ListDashboards(dashboardFilter)
assert.Equal(t, 1, len(boards))
assert.NotNil(t, nestedFolder)
nestedPath := service.GetNestedFolder(nestedFolder.FolderTitle, nestedFolder.FolderUID, apiClient)
assert.Equal(t, nestedPath, "Others/dummy")

// Import Dashboards
numBoards := 16
numBoards := 3
slog.Info("Importing Dashboards")
list := apiClient.DownloadDashboards(filtersEntity)
assert.Equal(t, len(list), numBoards)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"fiscalYearStartMonth": 0,
"gnetId": 10991,
"graphTooltip": 1,
"id": 677,
"id": 1,
"iteration": 1659711638455,
"links": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"fiscalYearStartMonth": 0,
"gnetId": 1860,
"graphTooltip": 1,
"id": 678,
"id": 4,
"links": [
{
"icon": "external link",
Expand Down Expand Up @@ -23303,6 +23303,6 @@
"timezone": "browser",
"title": "Node Exporter Full",
"uid": "rYdddlPWk",
"version": 2,
"version": 1,
"weekStart": ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"editable": true,
"gnetId": 11454,
"graphTooltip": 1,
"id": 679,
"id": 6,
"iteration": 1577045029184,
"links": [],
"panels": [
Expand Down Expand Up @@ -2232,5 +2232,5 @@
"timezone": "",
"title": "K8s / Storage / Volumes / Cluster",
"uid": "bdx48n30kejuoa",
"version": 2
"version": 1
}

0 comments on commit 18208dd

Please sign in to comment.