metadata_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package openshift
  4. import (
  5. "context"
  6. "fmt"
  7. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. )
  13. func TestNewProvider(t *testing.T) {
  14. provider1 := NewProvider("127.0.0.1:4444", "abc", nil)
  15. assert.NotNil(t, provider1)
  16. provider2 := NewProvider("", "", nil)
  17. assert.NotNil(t, provider2)
  18. }
  19. func TestQueryEndpointFailed(t *testing.T) {
  20. ts := httptest.NewServer(http.NotFoundHandler())
  21. defer ts.Close()
  22. provider := &openshiftProvider{
  23. address: ts.URL,
  24. token: "test",
  25. client: &http.Client{},
  26. }
  27. _, err := provider.OpenShiftClusterVersion(context.Background())
  28. assert.Error(t, err)
  29. _, err = provider.K8SClusterVersion(context.Background())
  30. assert.Error(t, err)
  31. _, err = provider.Infrastructure(context.Background())
  32. assert.Error(t, err)
  33. }
  34. func TestQueryEndpointMalformed(t *testing.T) {
  35. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  36. _, err := fmt.Fprintln(w, "{")
  37. assert.NoError(t, err)
  38. }))
  39. defer ts.Close()
  40. provider := &openshiftProvider{
  41. address: ts.URL,
  42. token: "",
  43. client: &http.Client{},
  44. }
  45. _, err := provider.Infrastructure(context.Background())
  46. assert.Error(t, err)
  47. }
  48. func TestQueryEndpointCorrectK8SClusterVersion(t *testing.T) {
  49. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  50. _, err := fmt.Fprintf(w, `{
  51. "major": "1",
  52. "minor": "21",
  53. "gitVersion": "v1.21.11+5cc9227",
  54. "gitCommit": "047f86f8e2212f25394de1c8bad35d9426ae0f4c",
  55. "gitTreeState": "clean",
  56. "buildDate": "2022-09-20T16:39:45Z",
  57. "goVersion": "go1.16.12",
  58. "compiler": "gc",
  59. "platform": "linux/amd64"
  60. }`)
  61. assert.NoError(t, err)
  62. }))
  63. defer ts.Close()
  64. provider := &openshiftProvider{
  65. address: ts.URL,
  66. token: "test",
  67. client: &http.Client{},
  68. }
  69. got, err := provider.K8SClusterVersion(context.Background())
  70. require.NoError(t, err)
  71. expect := "v1.21.11"
  72. assert.Equal(t, expect, got)
  73. }
  74. func TestQueryEndpointCorrectOpenShiftClusterVersion(t *testing.T) {
  75. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  76. _, err := fmt.Fprintf(w, `{
  77. "apiVersion": "config.openshift.io/v1",
  78. "kind": "ClusterVersion",
  79. "status": {
  80. "desired": {"version": "4.8.51"}
  81. }
  82. }`)
  83. assert.NoError(t, err)
  84. }))
  85. defer ts.Close()
  86. provider := &openshiftProvider{
  87. address: ts.URL,
  88. token: "test",
  89. client: &http.Client{},
  90. }
  91. got, err := provider.OpenShiftClusterVersion(context.Background())
  92. require.NoError(t, err)
  93. expect := "4.8.51"
  94. assert.Equal(t, expect, got)
  95. }
  96. func TestQueryEndpointCorrectInfrastructureAWS(t *testing.T) {
  97. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  98. _, err := fmt.Fprintf(w, `{
  99. "apiVersion": "config.openshift.io/v1",
  100. "kind": "Infrastructure",
  101. "status": {
  102. "apiServerInternalURI": "https://api.myopenshift.com:4443",
  103. "apiServerURL": "https://api.myopenshift.com:4443",
  104. "controlPlaneTopology": "HighlyAvailable",
  105. "etcdDiscoveryDomain": "",
  106. "infrastructureName": "test-d-bm4rt",
  107. "infrastructureTopology": "HighlyAvailable",
  108. "platform": "AWS",
  109. "platformStatus": {
  110. "type": "AWS",
  111. "aws": {"region": "us-east-1"}
  112. }}}`)
  113. assert.NoError(t, err)
  114. }))
  115. defer ts.Close()
  116. provider := &openshiftProvider{
  117. address: ts.URL,
  118. token: "test",
  119. client: &http.Client{},
  120. }
  121. got, err := provider.Infrastructure(context.Background())
  122. require.NoError(t, err)
  123. expect := InfrastructureAPIResponse{
  124. Status: InfrastructureStatus{
  125. InfrastructureName: "test-d-bm4rt",
  126. ControlPlaneTopology: "HighlyAvailable",
  127. InfrastructureTopology: "HighlyAvailable",
  128. PlatformStatus: InfrastructurePlatformStatus{
  129. Type: "AWS",
  130. Aws: InfrastructureStatusAWS{
  131. Region: "us-east-1",
  132. },
  133. },
  134. },
  135. }
  136. assert.Equal(t, expect, *got)
  137. }
  138. func TestQueryEndpointCorrectInfrastructureAzure(t *testing.T) {
  139. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  140. _, err := fmt.Fprintf(w, `{
  141. "apiVersion": "config.openshift.io/v1",
  142. "kind": "Infrastructure",
  143. "status": {
  144. "apiServerInternalURI": "https://api.myopenshift.com:4443",
  145. "apiServerURL": "https://api.myopenshift.com:4443",
  146. "controlPlaneTopology": "HighlyAvailable",
  147. "etcdDiscoveryDomain": "",
  148. "infrastructureName": "test-d-bm4rt",
  149. "infrastructureTopology": "HighlyAvailable",
  150. "platform": "AZURE",
  151. "platformStatus": {
  152. "type": "AZURE",
  153. "azure": {"cloudName": "us-east-1"}
  154. }}}`)
  155. assert.NoError(t, err)
  156. }))
  157. defer ts.Close()
  158. provider := &openshiftProvider{
  159. address: ts.URL,
  160. token: "test",
  161. client: &http.Client{},
  162. }
  163. got, err := provider.Infrastructure(context.Background())
  164. require.NoError(t, err)
  165. expect := InfrastructureAPIResponse{
  166. Status: InfrastructureStatus{
  167. InfrastructureName: "test-d-bm4rt",
  168. ControlPlaneTopology: "HighlyAvailable",
  169. InfrastructureTopology: "HighlyAvailable",
  170. PlatformStatus: InfrastructurePlatformStatus{
  171. Type: "AZURE",
  172. Azure: InfrastructureStatusAzure{
  173. CloudName: "us-east-1",
  174. },
  175. },
  176. },
  177. }
  178. assert.Equal(t, expect, *got)
  179. }
  180. func TestQueryEndpointCorrectInfrastructureGCP(t *testing.T) {
  181. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  182. _, err := fmt.Fprintf(w, `{
  183. "apiVersion": "config.openshift.io/v1",
  184. "kind": "Infrastructure",
  185. "status": {
  186. "controlPlaneTopology": "HighlyAvailable",
  187. "etcdDiscoveryDomain": "",
  188. "infrastructureName": "test-d-bm4rt",
  189. "infrastructureTopology": "HighlyAvailable",
  190. "platform": "GCP",
  191. "platformStatus": {
  192. "type": "GCP",
  193. "gcp": {"region": "us-east-1"}
  194. }}}`)
  195. assert.NoError(t, err)
  196. }))
  197. defer ts.Close()
  198. provider := &openshiftProvider{
  199. address: ts.URL,
  200. token: "test",
  201. client: &http.Client{},
  202. }
  203. got, err := provider.Infrastructure(context.Background())
  204. require.NoError(t, err)
  205. expect := InfrastructureAPIResponse{
  206. Status: InfrastructureStatus{
  207. InfrastructureName: "test-d-bm4rt",
  208. ControlPlaneTopology: "HighlyAvailable",
  209. InfrastructureTopology: "HighlyAvailable",
  210. PlatformStatus: InfrastructurePlatformStatus{
  211. Type: "GCP",
  212. GCP: InfrastructureStatusGCP{
  213. Region: "us-east-1",
  214. },
  215. },
  216. },
  217. }
  218. assert.Equal(t, expect, *got)
  219. }
  220. func TestQueryEndpointCorrectInfrastructureIBMCloud(t *testing.T) {
  221. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  222. _, err := fmt.Fprintf(w, `{
  223. "apiVersion": "config.openshift.io/v1",
  224. "kind": "Infrastructure",
  225. "status": {
  226. "controlPlaneTopology": "HighlyAvailable",
  227. "etcdDiscoveryDomain": "",
  228. "infrastructureName": "test-d-bm4rt",
  229. "infrastructureTopology": "HighlyAvailable",
  230. "platform": "IBMCloud",
  231. "platformStatus": {
  232. "type": "ibmcloud",
  233. "ibmcloud": {"location": "us-east-1"}
  234. }}}`)
  235. assert.NoError(t, err)
  236. }))
  237. defer ts.Close()
  238. provider := &openshiftProvider{
  239. address: ts.URL,
  240. token: "test",
  241. client: &http.Client{},
  242. }
  243. got, err := provider.Infrastructure(context.Background())
  244. require.NoError(t, err)
  245. expect := InfrastructureAPIResponse{
  246. Status: InfrastructureStatus{
  247. InfrastructureName: "test-d-bm4rt",
  248. ControlPlaneTopology: "HighlyAvailable",
  249. InfrastructureTopology: "HighlyAvailable",
  250. PlatformStatus: InfrastructurePlatformStatus{
  251. Type: "ibmcloud",
  252. IBMCloud: InfrastructureStatusIBMCloud{
  253. Location: "us-east-1",
  254. },
  255. },
  256. },
  257. }
  258. assert.Equal(t, expect, *got)
  259. }
  260. func TestQueryEndpointCorrectInfrastructureOpenstack(t *testing.T) {
  261. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  262. _, err := fmt.Fprintf(w, `{
  263. "apiVersion": "config.openshift.io/v1",
  264. "kind": "Infrastructure",
  265. "status": {
  266. "controlPlaneTopology": "HighlyAvailable",
  267. "etcdDiscoveryDomain": "",
  268. "infrastructureName": "test-d-bm4rt",
  269. "infrastructureTopology": "HighlyAvailable",
  270. "platform": "openstack",
  271. "platformStatus": {
  272. "type": "openstack",
  273. "openstack": {"cloudName": "us-east-1"}
  274. }}}`)
  275. assert.NoError(t, err)
  276. }))
  277. defer ts.Close()
  278. provider := &openshiftProvider{
  279. address: ts.URL,
  280. token: "test",
  281. client: &http.Client{},
  282. }
  283. got, err := provider.Infrastructure(context.Background())
  284. require.NoError(t, err)
  285. expect := InfrastructureAPIResponse{
  286. Status: InfrastructureStatus{
  287. InfrastructureName: "test-d-bm4rt",
  288. ControlPlaneTopology: "HighlyAvailable",
  289. InfrastructureTopology: "HighlyAvailable",
  290. PlatformStatus: InfrastructurePlatformStatus{
  291. Type: "openstack",
  292. OpenStack: InfrastructureStatusOpenStack{
  293. CloudName: "us-east-1",
  294. },
  295. },
  296. },
  297. }
  298. assert.Equal(t, expect, *got)
  299. }