Skip to content

Commit

Permalink
Add tests for grizzly.Pull
Browse files Browse the repository at this point in the history
  • Loading branch information
joanlopez committed Oct 10, 2023
1 parent ac78028 commit 840fa24
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 13 deletions.
5 changes: 3 additions & 2 deletions pkg/grafana/dashboards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import (
"testing"

"github.com/grafana/grizzly/pkg/grizzly"
. "github.com/grafana/grizzly/pkg/internal/testutil"
"github.com/stretchr/testify/require"
)

func TestDashboard(t *testing.T) {
os.Setenv("GRAFANA_URL", getUrl())
os.Setenv("GRAFANA_URL", GetUrl())

grizzly.ConfigureProviderRegistry(
[]grizzly.Provider{
&Provider{},
})

ticker := pingService(getUrl())
ticker := PingService(GetUrl())
defer ticker.Stop()

t.Run("get remote dashboard - success", func(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/grafana/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import (
"testing"

"github.com/grafana/grizzly/pkg/grizzly"
. "github.com/grafana/grizzly/pkg/internal/testutil"
"github.com/stretchr/testify/require"
)

func TestDatasources(t *testing.T) {
os.Setenv("GRAFANA_URL", getUrl())
os.Setenv("GRAFANA_URL", GetUrl())

grizzly.ConfigureProviderRegistry(
[]grizzly.Provider{
&Provider{},
})

ticker := pingService(getUrl())
ticker := PingService(GetUrl())
defer ticker.Stop()

t.Run("get remote datasource - success", func(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/grafana/folders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"testing"

"github.com/grafana/grizzly/pkg/grizzly"
. "github.com/grafana/grizzly/pkg/internal/testutil"
"github.com/stretchr/testify/require"
)

func TestFolders(t *testing.T) {
os.Setenv("GRAFANA_URL", getUrl())
os.Setenv("GRAFANA_URL", GetUrl())

ticker := pingService(getUrl())
ticker := PingService(GetUrl())
defer ticker.Stop()

t.Run("get remote folder - success", func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ deleteDatasources:
orgId: 1

datasources:
- name: AppDynamics
- uid: 392IktgGk
name: AppDynamics
type: dlopes7-appdynamics-datasource
access: proxy
basicAuth: true
Expand Down
16 changes: 14 additions & 2 deletions pkg/grizzly/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ func ListRemote(opts Opts) error {
// Pull pulls remote resources and stores them in the local file system.
// The given resourcePath must be a directory, where all resources will be stored.
func Pull(resourcePath string, opts Opts) error {
stat, err := os.Stat(resourcePath)
isFile, err := isFile(resourcePath)
if err != nil {
return err
}

if !stat.IsDir() {
if isFile {
return fmt.Errorf("pull <resource-path> must be a directory")
}

Expand Down Expand Up @@ -462,3 +462,15 @@ func Export(exportDir string, resources Resources) error {
}
return nil
}

func isFile(resourcePath string) (bool, error) {
stat, err := os.Stat(resourcePath)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}

return !stat.IsDir(), nil
}
70 changes: 70 additions & 0 deletions pkg/grizzly/workflow_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package grizzly_test

import (
"os"
"path/filepath"
"testing"

"github.com/grafana/grizzly/pkg/grafana"
"github.com/grafana/grizzly/pkg/grizzly"
. "github.com/grafana/grizzly/pkg/internal/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPull(t *testing.T) {
os.Setenv("GRAFANA_URL", GetUrl())

grizzly.ConfigureProviderRegistry(
[]grizzly.Provider{
&grafana.Provider{},
})

ticker := PingService(GetUrl())
defer ticker.Stop()

opts := grizzly.Opts{
Targets: []string{
"Datasource/392IktgGk",
},
}

t.Run("with existing file", func(t *testing.T) {
t.Parallel()

path := filepath.Join(t.TempDir(), filepath.Base(t.Name()))
f, err := os.Create(path)
require.NoError(t, err)
require.NoError(t, f.Close())

err = grizzly.Pull(path, opts)
assert.Error(t, err)
assert.ErrorContains(t, err, "pull <resource-path> must be a directory")
})

t.Run("with existing folder", func(t *testing.T) {
t.Parallel()

path := filepath.Join(t.TempDir(), filepath.Base(t.Name()))
err := os.MkdirAll(path, 0755)
require.NoError(t, err)

err = grizzly.Pull(path, opts)
assert.NoError(t, err)
assert.Equal(t, 1, numOfFiles(path))
})

t.Run("with non-existing folder", func(t *testing.T) {
t.Parallel()

path := filepath.Join(t.TempDir(), filepath.Base(t.Name()))
err := grizzly.Pull(path, opts)
assert.NoError(t, err)
assert.Equal(t, 1, numOfFiles(path))
})
}

func numOfFiles(path string) int {
files, _ := os.ReadDir(path)
return len(files)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package grafana
package testutil

import (
"fmt"
Expand All @@ -7,23 +7,23 @@ import (
"time"
)

func getUrl() string {
func GetUrl() string {
if os.Getenv("CI") != "" {
return "http://grizzly-grafana:3000/"
} else {
return "http://localhost:3000/"
}
}

func pingService(url string) *time.Ticker {
func PingService(url string) *time.Ticker {
ticker := time.NewTicker(1 * time.Second)
timeoutExceeded := time.After(120 * time.Second)

success := false
for !success {
select {
case <-timeoutExceeded:
panic("Unable to connect to grizzly-grafana:3000")
panic(fmt.Sprintf("Unable to connect to %s", url))

case <-ticker.C:
resp, _ := http.Get(url)
Expand Down

0 comments on commit 840fa24

Please sign in to comment.