-
Notifications
You must be signed in to change notification settings - Fork 674
/
plugin_test.go
154 lines (136 loc) · 5.24 KB
/
plugin_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package databricks
import (
"context"
"errors"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
pluginsCore "github.com/flyteorg/flyte/flyteplugins/go/tasks/pluginmachinery/core"
pluginCoreMocks "github.com/flyteorg/flyte/flyteplugins/go/tasks/pluginmachinery/core/mocks"
"github.com/flyteorg/flyte/flytestdlib/ioutils"
"github.com/flyteorg/flyte/flytestdlib/promutils"
)
type MockClient struct {
MockDo func(req *http.Request) (*http.Response, error)
}
func (m MockClient) Do(req *http.Request) (*http.Response, error) {
return m.MockDo(req)
}
var (
testInstance = "test-account.cloud.databricks.com"
)
func TestPlugin(t *testing.T) {
fakeSetupContext := pluginCoreMocks.SetupContext{}
fakeSetupContext.OnMetricsScope().Return(promutils.NewScope("test"))
plugin := Plugin{
metricScope: fakeSetupContext.MetricsScope(),
cfg: GetConfig(),
client: &MockClient{func(req *http.Request) (*http.Response, error) {
return nil, nil
}},
}
t.Run("get config", func(t *testing.T) {
cfg := defaultConfig
cfg.WebAPI.Caching.Workers = 1
cfg.WebAPI.Caching.ResyncInterval.Duration = 5 * time.Second
err := SetConfig(&cfg)
assert.NoError(t, err)
assert.Equal(t, cfg.WebAPI, plugin.GetConfig())
})
t.Run("get ResourceRequirements", func(t *testing.T) {
namespace, constraints, err := plugin.ResourceRequirements(context.TODO(), nil)
assert.NoError(t, err)
assert.Equal(t, pluginsCore.ResourceNamespace("default"), namespace)
assert.Equal(t, plugin.cfg.ResourceConstraints, constraints)
})
}
func TestSendRequest(t *testing.T) {
fakeSetupContext := pluginCoreMocks.SetupContext{}
fakeSetupContext.OnMetricsScope().Return(promutils.NewScope("test1"))
databricksJob := map[string]interface{}{"sparkConfig": map[string]interface{}{"sparkVersion": "7.3.x-scala2.12"}}
token := "token"
plugin := Plugin{
metricScope: fakeSetupContext.MetricsScope(),
cfg: GetConfig(),
client: &MockClient{MockDo: func(req *http.Request) (*http.Response, error) {
assert.Equal(t, req.Method, http.MethodPost)
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutils.NewBytesReadCloser([]byte(`{"id":"someID","data":"someData"}`)),
}, nil
}},
}
t.Run("create a Databricks job", func(t *testing.T) {
data, err := plugin.sendRequest(create, databricksJob, token, "")
assert.NotNil(t, data)
assert.Equal(t, "someID", data["id"])
assert.Equal(t, "someData", data["data"])
assert.Nil(t, err)
})
t.Run("failed to create a Databricks job", func(t *testing.T) {
plugin.client = &MockClient{MockDo: func(req *http.Request) (*http.Response, error) {
assert.Equal(t, req.Method, http.MethodPost)
return &http.Response{
StatusCode: http.StatusBadRequest,
Body: ioutils.NewBytesReadCloser([]byte(`{"message":"failed"}`)),
}, nil
}}
data, err := plugin.sendRequest(create, databricksJob, token, "")
assert.Nil(t, data)
assert.Equal(t, err.Error(), "failed to create Databricks job with error [failed]")
})
t.Run("failed to send request to Databricks", func(t *testing.T) {
plugin.client = &MockClient{MockDo: func(req *http.Request) (*http.Response, error) {
assert.Equal(t, req.Method, http.MethodPost)
return nil, errors.New("failed to send request")
}}
data, err := plugin.sendRequest(create, databricksJob, token, "")
assert.Nil(t, data)
assert.Equal(t, err.Error(), "failed to send request to Databricks platform with err: [failed to send request]")
})
t.Run("failed to send request to Databricks", func(t *testing.T) {
plugin.client = &MockClient{MockDo: func(req *http.Request) (*http.Response, error) {
assert.Equal(t, req.Method, http.MethodPost)
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutils.NewBytesReadCloser([]byte(`123`)),
}, nil
}}
data, err := plugin.sendRequest(create, databricksJob, token, "")
assert.Nil(t, data)
assert.Equal(t, err.Error(), "failed to parse response with err: [json: cannot unmarshal number into Go value of type map[string]interface {}]")
})
t.Run("get a Databricks job", func(t *testing.T) {
plugin.client = &MockClient{MockDo: func(req *http.Request) (*http.Response, error) {
assert.Equal(t, req.Method, http.MethodGet)
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutils.NewBytesReadCloser([]byte(`{"message":"ok"}`)),
}, nil
}}
data, err := plugin.sendRequest(get, databricksJob, token, "")
assert.NotNil(t, data)
assert.Nil(t, err)
})
t.Run("cancel a Databricks job", func(t *testing.T) {
plugin.client = &MockClient{MockDo: func(req *http.Request) (*http.Response, error) {
assert.Equal(t, req.Method, http.MethodPost)
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutils.NewBytesReadCloser([]byte(`{"message":"ok"}`)),
}, nil
}}
data, err := plugin.sendRequest(cancel, databricksJob, token, "")
assert.NotNil(t, data)
assert.Nil(t, err)
})
}
func TestCreateTaskInfo(t *testing.T) {
t.Run("create task info", func(t *testing.T) {
taskInfo := createTaskInfo("run-id", "job-id", testInstance)
assert.Equal(t, 1, len(taskInfo.Logs))
assert.Equal(t, taskInfo.Logs[0].GetUri(), "https://test-account.cloud.databricks.com/#job/job-id/run/run-id")
assert.Equal(t, taskInfo.Logs[0].GetName(), "Databricks Console")
})
}