client_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package apachesparkreceiver
  4. import (
  5. "encoding/json"
  6. "net/http"
  7. "net/http/httptest"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "testing"
  12. "github.com/stretchr/testify/require"
  13. "go.opentelemetry.io/collector/component"
  14. "go.opentelemetry.io/collector/component/componenttest"
  15. "go.opentelemetry.io/collector/config/confighttp"
  16. "go.uber.org/zap"
  17. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/apachesparkreceiver/internal/models"
  18. )
  19. const (
  20. testURL = "http://sparkmock.com"
  21. )
  22. func TestNewApacheSparkClient(t *testing.T) {
  23. testCases := []struct {
  24. desc string
  25. cfg *Config
  26. host component.Host
  27. settings component.TelemetrySettings
  28. logger *zap.Logger
  29. expectError error
  30. }{
  31. {
  32. desc: "Valid Configuration",
  33. cfg: &Config{
  34. HTTPClientSettings: confighttp.HTTPClientSettings{
  35. Endpoint: defaultEndpoint,
  36. },
  37. },
  38. host: componenttest.NewNopHost(),
  39. settings: componenttest.NewNopTelemetrySettings(),
  40. logger: zap.NewNop(),
  41. expectError: nil,
  42. },
  43. }
  44. for _, tc := range testCases {
  45. t.Run(tc.desc, func(t *testing.T) {
  46. ac, err := newApacheSparkClient(tc.cfg, tc.host, tc.settings)
  47. if tc.expectError != nil {
  48. require.Nil(t, ac)
  49. require.Contains(t, err.Error(), tc.expectError.Error())
  50. } else {
  51. require.NoError(t, err)
  52. actualClient, ok := ac.(*apacheSparkClient)
  53. require.True(t, ok)
  54. require.Equal(t, tc.logger, actualClient.logger)
  55. require.NotNil(t, actualClient.client)
  56. }
  57. })
  58. }
  59. }
  60. func TestClusterStats(t *testing.T) {
  61. testCases := []struct {
  62. desc string
  63. testFunc func(*testing.T)
  64. }{
  65. {
  66. desc: "Returns an error on Get() failure",
  67. testFunc: func(t *testing.T) {
  68. data := loadAPIResponseData(t, clusterStatsResponseFile)
  69. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  70. if strings.HasSuffix(r.RequestURI, "metrics/json") {
  71. w.WriteHeader(http.StatusUnauthorized)
  72. } else {
  73. _, err := w.Write(data)
  74. require.NoError(t, err)
  75. }
  76. }))
  77. defer ts.Close()
  78. tc := createTestClient(t, testURL)
  79. clusterStats, err := tc.ClusterStats()
  80. require.NotNil(t, err)
  81. require.Nil(t, clusterStats)
  82. },
  83. },
  84. {
  85. desc: "Handles case where result cannot be unmarshalled",
  86. testFunc: func(t *testing.T) {
  87. data := loadAPIResponseData(t, clusterStatsResponseFile)
  88. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  89. var err error
  90. if strings.HasSuffix(r.RequestURI, "/metrics/json") {
  91. _, err = w.Write([]byte("[{}]"))
  92. require.NoError(t, err)
  93. } else {
  94. _, err = w.Write(data)
  95. }
  96. require.NoError(t, err)
  97. }))
  98. defer ts.Close()
  99. tc := createTestClient(t, ts.URL)
  100. clusterStats, err := tc.ClusterStats()
  101. require.Nil(t, clusterStats)
  102. require.NotNil(t, err)
  103. },
  104. },
  105. {
  106. desc: "Success case",
  107. testFunc: func(t *testing.T) {
  108. data := loadAPIResponseData(t, clusterStatsResponseFile)
  109. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  110. var err error
  111. if strings.HasSuffix(r.RequestURI, "/metrics/json") {
  112. _, err = w.Write(data)
  113. require.NoError(t, err)
  114. }
  115. require.NoError(t, err)
  116. }))
  117. defer ts.Close()
  118. tc := createTestClient(t, ts.URL)
  119. var expected *models.ClusterProperties
  120. data = loadAPIResponseData(t, clusterStatsResponseFile)
  121. err := json.Unmarshal(data, &expected)
  122. require.NoError(t, err)
  123. clusterStats, err := tc.ClusterStats()
  124. require.NoError(t, err)
  125. require.NotNil(t, clusterStats)
  126. require.Equal(t, expected, clusterStats)
  127. },
  128. },
  129. }
  130. for _, tc := range testCases {
  131. t.Run(tc.desc, tc.testFunc)
  132. }
  133. }
  134. func TestApplications(t *testing.T) {
  135. testCases := []struct {
  136. desc string
  137. testFunc func(*testing.T)
  138. }{
  139. {
  140. desc: "Returns an error on Get() failure",
  141. testFunc: func(t *testing.T) {
  142. data := loadAPIResponseData(t, appsStatsResponseFile)
  143. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  144. if strings.HasSuffix(r.RequestURI, "applications") {
  145. w.WriteHeader(http.StatusUnauthorized)
  146. } else {
  147. _, err := w.Write(data)
  148. require.NoError(t, err)
  149. }
  150. }))
  151. defer ts.Close()
  152. tc := createTestClient(t, testURL)
  153. apps, err := tc.Applications()
  154. require.NotNil(t, err)
  155. require.Nil(t, apps)
  156. },
  157. },
  158. {
  159. desc: "Handles case where result cannot be unmarshalled",
  160. testFunc: func(t *testing.T) {
  161. data := loadAPIResponseData(t, appsStatsResponseFile)
  162. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  163. var err error
  164. if strings.HasSuffix(r.RequestURI, "applications") {
  165. _, err = w.Write([]byte(""))
  166. require.NoError(t, err)
  167. } else {
  168. _, err = w.Write(data)
  169. }
  170. require.NoError(t, err)
  171. }))
  172. defer ts.Close()
  173. tc := createTestClient(t, ts.URL)
  174. apps, err := tc.Applications()
  175. require.Nil(t, apps)
  176. require.NotNil(t, err)
  177. },
  178. },
  179. {
  180. desc: "Success case",
  181. testFunc: func(t *testing.T) {
  182. data := loadAPIResponseData(t, appsStatsResponseFile)
  183. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  184. var err error
  185. if strings.HasSuffix(r.RequestURI, "applications") {
  186. _, err = w.Write(data)
  187. require.NoError(t, err)
  188. }
  189. require.NoError(t, err)
  190. }))
  191. defer ts.Close()
  192. tc := createTestClient(t, ts.URL)
  193. var expected []models.Application
  194. data = loadAPIResponseData(t, appsStatsResponseFile)
  195. err := json.Unmarshal(data, &expected)
  196. require.NoError(t, err)
  197. apps, err := tc.Applications()
  198. require.NoError(t, err)
  199. require.NotNil(t, apps)
  200. require.Equal(t, expected, apps)
  201. },
  202. },
  203. }
  204. for _, tc := range testCases {
  205. t.Run(tc.desc, tc.testFunc)
  206. }
  207. }
  208. func TestStageStats(t *testing.T) {
  209. testCases := []struct {
  210. desc string
  211. testFunc func(*testing.T)
  212. }{
  213. {
  214. desc: "Returns an error on Get() failure",
  215. testFunc: func(t *testing.T) {
  216. data := loadAPIResponseData(t, stagesStatsResponseFile)
  217. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  218. if strings.HasSuffix(r.RequestURI, "stages") {
  219. w.WriteHeader(http.StatusUnauthorized)
  220. } else {
  221. _, err := w.Write(data)
  222. require.NoError(t, err)
  223. }
  224. }))
  225. defer ts.Close()
  226. tc := createTestClient(t, testURL)
  227. stageStats, err := tc.StageStats("some_app_id")
  228. require.NotNil(t, err)
  229. require.Nil(t, stageStats)
  230. },
  231. },
  232. {
  233. desc: "Handles case where result cannot be unmarshalled",
  234. testFunc: func(t *testing.T) {
  235. data := loadAPIResponseData(t, stagesStatsResponseFile)
  236. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  237. var err error
  238. if strings.HasSuffix(r.RequestURI, "stages") {
  239. _, err = w.Write([]byte(""))
  240. require.NoError(t, err)
  241. } else {
  242. _, err = w.Write(data)
  243. }
  244. require.NoError(t, err)
  245. }))
  246. defer ts.Close()
  247. tc := createTestClient(t, ts.URL)
  248. stageStats, err := tc.StageStats("some_app_id")
  249. require.Nil(t, stageStats)
  250. require.NotNil(t, err)
  251. },
  252. },
  253. {
  254. desc: "Success case",
  255. testFunc: func(t *testing.T) {
  256. data := loadAPIResponseData(t, stagesStatsResponseFile)
  257. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  258. var err error
  259. if strings.HasSuffix(r.RequestURI, "stages") {
  260. _, err = w.Write(data)
  261. require.NoError(t, err)
  262. }
  263. require.NoError(t, err)
  264. }))
  265. defer ts.Close()
  266. tc := createTestClient(t, ts.URL)
  267. var expected []models.Stage
  268. data = loadAPIResponseData(t, stagesStatsResponseFile)
  269. err := json.Unmarshal(data, &expected)
  270. require.NoError(t, err)
  271. stageStats, err := tc.StageStats("some_app_id")
  272. require.NoError(t, err)
  273. require.NotNil(t, stageStats)
  274. require.Equal(t, expected, stageStats)
  275. },
  276. },
  277. }
  278. for _, tc := range testCases {
  279. t.Run(tc.desc, tc.testFunc)
  280. }
  281. }
  282. func TestExecutorStats(t *testing.T) {
  283. testCases := []struct {
  284. desc string
  285. testFunc func(*testing.T)
  286. }{
  287. {
  288. desc: "Returns an error on Get() failure",
  289. testFunc: func(t *testing.T) {
  290. data := loadAPIResponseData(t, executorsStatsResponseFile)
  291. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  292. if strings.HasSuffix(r.RequestURI, "executors") {
  293. w.WriteHeader(http.StatusUnauthorized)
  294. } else {
  295. _, err := w.Write(data)
  296. require.NoError(t, err)
  297. }
  298. }))
  299. defer ts.Close()
  300. tc := createTestClient(t, testURL)
  301. executorStats, err := tc.ExecutorStats("some_app_id")
  302. require.NotNil(t, err)
  303. require.Nil(t, executorStats)
  304. },
  305. },
  306. {
  307. desc: "Handles case where result cannot be unmarshalled",
  308. testFunc: func(t *testing.T) {
  309. data := loadAPIResponseData(t, executorsStatsResponseFile)
  310. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  311. var err error
  312. if strings.HasSuffix(r.RequestURI, "executors") {
  313. _, err = w.Write([]byte(""))
  314. require.NoError(t, err)
  315. } else {
  316. _, err = w.Write(data)
  317. }
  318. require.NoError(t, err)
  319. }))
  320. defer ts.Close()
  321. tc := createTestClient(t, ts.URL)
  322. executorStats, err := tc.ExecutorStats("some_app_id")
  323. require.Nil(t, executorStats)
  324. require.NotNil(t, err)
  325. },
  326. },
  327. {
  328. desc: "Success case",
  329. testFunc: func(t *testing.T) {
  330. data := loadAPIResponseData(t, executorsStatsResponseFile)
  331. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  332. var err error
  333. if strings.HasSuffix(r.RequestURI, "executors") {
  334. _, err = w.Write(data)
  335. require.NoError(t, err)
  336. }
  337. require.NoError(t, err)
  338. }))
  339. defer ts.Close()
  340. tc := createTestClient(t, ts.URL)
  341. var expected []models.Executor
  342. data = loadAPIResponseData(t, executorsStatsResponseFile)
  343. err := json.Unmarshal(data, &expected)
  344. require.NoError(t, err)
  345. executorStats, err := tc.ExecutorStats("some_app_id")
  346. require.NoError(t, err)
  347. require.NotNil(t, executorStats)
  348. require.Equal(t, expected, executorStats)
  349. },
  350. },
  351. }
  352. for _, tc := range testCases {
  353. t.Run(tc.desc, tc.testFunc)
  354. }
  355. }
  356. func TestJobStats(t *testing.T) {
  357. testCases := []struct {
  358. desc string
  359. testFunc func(*testing.T)
  360. }{
  361. {
  362. desc: "Returns an error on Get() failure",
  363. testFunc: func(t *testing.T) {
  364. data := loadAPIResponseData(t, jobsStatsResponseFile)
  365. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  366. if strings.HasSuffix(r.RequestURI, "jobs") {
  367. w.WriteHeader(http.StatusUnauthorized)
  368. } else {
  369. _, err := w.Write(data)
  370. require.NoError(t, err)
  371. }
  372. }))
  373. defer ts.Close()
  374. tc := createTestClient(t, testURL)
  375. jobStats, err := tc.JobStats("some_app_id")
  376. require.NotNil(t, err)
  377. require.Nil(t, jobStats)
  378. },
  379. },
  380. {
  381. desc: "Handles case where result cannot be unmarshalled",
  382. testFunc: func(t *testing.T) {
  383. data := loadAPIResponseData(t, jobsStatsResponseFile)
  384. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  385. var err error
  386. if strings.HasSuffix(r.RequestURI, "jobs") {
  387. _, err = w.Write([]byte(""))
  388. require.NoError(t, err)
  389. } else {
  390. _, err = w.Write(data)
  391. }
  392. require.NoError(t, err)
  393. }))
  394. defer ts.Close()
  395. tc := createTestClient(t, ts.URL)
  396. jobStats, err := tc.JobStats("some_app_id")
  397. require.Nil(t, jobStats)
  398. require.NotNil(t, err)
  399. },
  400. },
  401. {
  402. desc: "Success case",
  403. testFunc: func(t *testing.T) {
  404. data := loadAPIResponseData(t, jobsStatsResponseFile)
  405. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  406. var err error
  407. if strings.HasSuffix(r.RequestURI, "jobs") {
  408. _, err = w.Write(data)
  409. require.NoError(t, err)
  410. }
  411. require.NoError(t, err)
  412. }))
  413. defer ts.Close()
  414. tc := createTestClient(t, ts.URL)
  415. var expected []models.Job
  416. data = loadAPIResponseData(t, jobsStatsResponseFile)
  417. err := json.Unmarshal(data, &expected)
  418. require.NoError(t, err)
  419. jobStats, err := tc.JobStats("some_app_id")
  420. require.NoError(t, err)
  421. require.NotNil(t, jobStats)
  422. require.Equal(t, expected, jobStats)
  423. },
  424. },
  425. }
  426. for _, tc := range testCases {
  427. t.Run(tc.desc, tc.testFunc)
  428. }
  429. }
  430. func createTestClient(t *testing.T, baseEndpoint string) client {
  431. t.Helper()
  432. cfg := createDefaultConfig().(*Config)
  433. cfg.Endpoint = baseEndpoint
  434. testClient, err := newApacheSparkClient(cfg, componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings())
  435. require.NoError(t, err)
  436. return testClient
  437. }
  438. func loadAPIResponseData(t *testing.T, fileName string) []byte {
  439. t.Helper()
  440. fullPath := filepath.Join("testdata", "apiresponses", fileName)
  441. data, err := os.ReadFile(fullPath)
  442. require.NoError(t, err)
  443. return data
  444. }