1package cloudwatch
2
3import (
4	"testing"
5
6	"github.com/google/go-cmp/cmp"
7	"github.com/grafana/grafana-aws-sdk/pkg/awsds"
8	"github.com/grafana/grafana-plugin-sdk-go/backend"
9	"github.com/stretchr/testify/require"
10)
11
12func TestNewInstanceSettings(t *testing.T) {
13	tests := []struct {
14		name       string
15		settings   backend.DataSourceInstanceSettings
16		expectedDS datasourceInfo
17		Err        require.ErrorAssertionFunc
18	}{
19		{
20			name: "creates a request",
21			settings: backend.DataSourceInstanceSettings{
22				JSONData: []byte(`{
23					"profile": "foo",
24					"defaultRegion": "us-east2",
25					"assumeRoleArn": "role",
26					"externalId": "id",
27					"endpoint": "bar",
28					"customMetricsNamespaces": "ns",
29					"authType": "keys"
30				}`),
31				DecryptedSecureJSONData: map[string]string{
32					"accessKey": "A123",
33					"secretKey": "secret",
34				},
35			},
36			expectedDS: datasourceInfo{
37				profile:       "foo",
38				region:        "us-east2",
39				assumeRoleARN: "role",
40				externalID:    "id",
41				endpoint:      "bar",
42				namespace:     "ns",
43				authType:      awsds.AuthTypeKeys,
44				accessKey:     "A123",
45				secretKey:     "secret",
46			},
47			Err: require.NoError,
48		},
49	}
50
51	for _, tt := range tests {
52		t.Run(tt.name, func(t *testing.T) {
53			f := NewInstanceSettings()
54			model, err := f(tt.settings)
55			tt.Err(t, err)
56			datasourceComparer := cmp.Comparer(func(d1 datasourceInfo, d2 datasourceInfo) bool {
57				return d1.profile == d2.profile &&
58					d1.region == d2.region &&
59					d1.authType == d2.authType &&
60					d1.assumeRoleARN == d2.assumeRoleARN &&
61					d1.externalID == d2.externalID &&
62					d1.namespace == d2.namespace &&
63					d1.endpoint == d2.endpoint &&
64					d1.accessKey == d2.accessKey &&
65					d1.secretKey == d2.secretKey &&
66					d1.datasourceID == d2.datasourceID
67			})
68			if !cmp.Equal(model.(datasourceInfo), tt.expectedDS, datasourceComparer) {
69				t.Errorf("Unexpected result. Expecting\n%v \nGot:\n%v", model, tt.expectedDS)
70			}
71		})
72	}
73}
74