Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add datasource integration tests #364

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion integration/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,74 @@ import (
)

func TestDatasources(t *testing.T) {
dir := "testdata/datasources"
setupContexts(t, dir)

t.Run("Get datasource - success", func(t *testing.T) {
runTest(t, GrizzlyTest{
TestDir: dir,
RunOnContexts: allContexts,
Commands: []Command{
{
Command: "get Datasource.392IktgGk",
ExpectedCode: 0,
ExpectedOutputFile: "392IktgGk-returned.yaml",
},
},
})
})

t.Run("Get datasource list - success", func(t *testing.T) {
runTest(t, GrizzlyTest{
TestDir: dir,
RunOnContexts: allContexts,
Commands: []Command{
{
Command: "list -r -t Datasource",
ExpectedOutputFile: "list.txt",
},
},
})
})

t.Run("Apply datasource", func(t *testing.T) {
runTest(t, GrizzlyTest{
TestDir: dir,
RunOnContexts: allContexts,
Commands: []Command{
{
Command: "apply appdynamics2.yaml",
ExpectedOutput: "Datasource.appdynamics2 added\n",
},
{
Command: "get Datasource.appdynamics2",
ExpectedOutputFile: "appdynamics2-returned.yaml",
},
},
})
})

t.Run("Apply broken datasource", func(t *testing.T) {
runTest(t, GrizzlyTest{
TestDir: dir,
RunOnContexts: []string{"default", "basic_auth"},
Commands: []Command{
{
Command: "apply datasource-broken.json",
ExpectedOutput: "Datasource.datasource-broken added\n",
},
{
Command: "get -o json Datasource.datasource-broken",
ExpectedOutputFiles: []string{
"datasource-broken-returned.json",
"datasource-broken-returned2.json",
Comment on lines +74 to +75
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand. Why is there the same file twice with a different name?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because the subdir instance returns different data. But for now I've just excluded the subdir target for this test for this reason.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't understand why we don't use
ExpectedOutputFile: "datasource-broken-returned.json"
here 😅

},
},
},
})
})
}
func TestDatasourceHandler(t *testing.T) {
provider, err := grafana.NewProvider(&testutil.TestContext().Grafana)
require.NoError(t, err)
handler := grafana.NewDatasourceHandler(provider)
Expand All @@ -35,7 +103,7 @@ func TestDatasources(t *testing.T) {
require.NoError(t, err)

require.NotNil(t, resources)
require.Len(t, resources, 1)
require.Len(t, resources, 3)
})

t.Run("post remote datasource - success", func(t *testing.T) {
Expand Down
16 changes: 16 additions & 0 deletions integration/testdata/datasources/392IktgGk-returned.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: grizzly.grafana.com/v1alpha1
kind: Datasource
metadata:
name: 392IktgGk
spec:
access: proxy
basicAuth: true
basicAuthUser: mothership2019052316285110@mothership2019052316285110
jsonData: {}
name: AppDynamics
orgId: 1
type: dlopes7-appdynamics-datasource
typeLogoUrl: public/img/icn-datasource.svg
uid: 392IktgGk
url: https://mothership2019052316285110.saas.appdynamics.com
withCredentials: true
18 changes: 18 additions & 0 deletions integration/testdata/datasources/392IktgGk.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# config file version
apiVersion: grizzly.grafana.com/v1alpha1
kind: Datasource
metadata:
name: 392IktgGk
spec:
access: proxy
basicAuth: true
basicAuthUser: mothership2019052316285110@mothership2019052316285110
basicAuthPassword: l9pufv67qmud
jsonData: {}
name: AppDynamics
orgId: 1
type: dlopes7-appdynamics-datasource
typeLogoUrl: public/img/icn-datasource.svg
uid: 392IktgGk
url: https://mothership2019052316285110.saas.appdynamics.com
withCredentials: true
16 changes: 16 additions & 0 deletions integration/testdata/datasources/appdynamics2-returned.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: grizzly.grafana.com/v1alpha1
kind: Datasource
metadata:
name: appdynamics2
spec:
access: proxy
basicAuth: true
basicAuthUser: mothership2019052316285110@mothership2019052316285110
jsonData: {}
name: AppDynamics2
orgId: 1
type: dlopes7-appdynamics-datasource
typeLogoUrl: public/img/icn-datasource.svg
uid: appdynamics2
url: https://mothership2019052316285110.saas.appdynamics.com
withCredentials: true
17 changes: 17 additions & 0 deletions integration/testdata/datasources/appdynamics2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# config file version
apiVersion: grizzly.grafana.com/v1alpha1
kind: Datasource
metadata:
name: appdynamics2
spec:
uid: appdynamics2
name: AppDynamics2
type: dlopes7-appdynamics-datasource
access: proxy
basicAuth: true
basicAuthUser: mothership2019052316285110@mothership2019052316285110
basicAuthPassword: l9pufv67qmud
editable: true
enabled: true
url: https://mothership2019052316285110.saas.appdynamics.com
withCredentials: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"apiVersion": "grizzly.grafana.com/v1alpha1",
"kind": "Datasource",
"metadata": {
"name": "datasource-broken"
},
"spec": {
"access": "proxy",
"jsonData": {},
"name": "InfluxDB",
"orgId": 1,
"type": "influxdb",
"typeLogoUrl": "/public/app/plugins/datasource/influxdb/img/influxdb_logo.svg",
"uid": "datasource-broken"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"apiVersion": "grizzly.grafana.com/v1alpha1",
"kind": "Datasource",
"metadata": {
"name": "datasource-broken"
},
"spec": {
"access": "proxy",
"jsonData": {},
"name": "InfluxDB",
"orgId": 1,
"type": "influxdb",
"typeLogoUrl": "/grafana/public/app/plugins/datasource/influxdb/img/influxdb_logo.svg",
"uid": "datasource-broken"
}
}
18 changes: 18 additions & 0 deletions integration/testdata/datasources/datasource-broken.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"apiVersion": "grizzly.grafana.com/v1alpha1",
"kind": "Datasource",
"metadata": {
"name": "datasource-broken"
},
"spec": {
"access": "proxy",
"id": 107,
"jsonData": {},
"name": "InfluxDB",
"orgId": 1,
"type": "influxdb",
"typeLogoUrl": "/public/app/plugins/datasource/influxdb/img/influxdb_logo.svg",
"uid": "datasource-broken",
"version": 1
}
}
Empty file.
11 changes: 11 additions & 0 deletions integration/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Command struct {
ExpectedLogsContain string
ExpectedOutput string
ExpectedOutputFile string
ExpectedOutputFiles []string
ExpectedOutputContains string
}
type GrizzlyTest struct {
Expand Down Expand Up @@ -61,6 +62,16 @@ func runTest(t *testing.T, test GrizzlyTest) {
require.NoError(t, err)
command.ExpectedOutput = string(bytes)
}
if command.ExpectedOutputFiles != nil {
var expectedOutputs []string
for _, expectedOutputFile := range command.ExpectedOutputFiles {
bytes, err := os.ReadFile(filepath.Join(test.TestDir, expectedOutputFile))
require.NoError(t, err)
expectedOutputs = append(expectedOutputs, strings.TrimSpace(string(bytes)))
}
require.Contains(t, expectedOutputs, strings.TrimSpace(stdout))

}
if command.ExpectedOutput != "" {
require.Equal(t, strings.TrimSpace(command.ExpectedOutput), strings.TrimSpace(stdout))
}
Expand Down
Loading