mock_resources_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package k8sclusterreceiver
  4. import (
  5. "context"
  6. "fmt"
  7. "strconv"
  8. "testing"
  9. "time"
  10. quotav1 "github.com/openshift/api/quota/v1"
  11. fakeQuota "github.com/openshift/client-go/quota/clientset/versioned/fake"
  12. "github.com/stretchr/testify/require"
  13. corev1 "k8s.io/api/core/v1"
  14. "k8s.io/apimachinery/pkg/api/resource"
  15. v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  16. "k8s.io/apimachinery/pkg/types"
  17. "k8s.io/client-go/kubernetes/fake"
  18. )
  19. func createPods(t *testing.T, client *fake.Clientset, numPods int) []*corev1.Pod {
  20. out := make([]*corev1.Pod, 0, numPods)
  21. for i := 0; i < numPods; i++ {
  22. p := &corev1.Pod{
  23. ObjectMeta: v1.ObjectMeta{
  24. UID: types.UID("pod" + strconv.Itoa(i)),
  25. Name: strconv.Itoa(i),
  26. Namespace: "test",
  27. },
  28. }
  29. createdPod, err := client.CoreV1().Pods(p.Namespace).Create(context.Background(), p, v1.CreateOptions{})
  30. require.NoError(t, err, "error creating node")
  31. out = append(out, createdPod)
  32. time.Sleep(2 * time.Millisecond)
  33. }
  34. return out
  35. }
  36. func deletePods(t *testing.T, client *fake.Clientset, numPods int) {
  37. for i := 0; i < numPods; i++ {
  38. err := client.CoreV1().Pods("test").Delete(context.Background(), strconv.Itoa(i), v1.DeleteOptions{})
  39. require.NoError(t, err, "error creating node")
  40. }
  41. time.Sleep(2 * time.Millisecond)
  42. }
  43. func createNodes(t *testing.T, client *fake.Clientset, numNodes int) {
  44. for i := 0; i < numNodes; i++ {
  45. n := &corev1.Node{
  46. ObjectMeta: v1.ObjectMeta{
  47. UID: types.UID("node" + strconv.Itoa(i)),
  48. Name: strconv.Itoa(i),
  49. },
  50. }
  51. _, err := client.CoreV1().Nodes().Create(context.Background(), n, v1.CreateOptions{})
  52. require.NoError(t, err, "error creating node")
  53. time.Sleep(2 * time.Millisecond)
  54. }
  55. }
  56. func createClusterQuota(t *testing.T, client *fakeQuota.Clientset, numQuotas int) {
  57. for i := 0; i < numQuotas; i++ {
  58. q := &quotav1.ClusterResourceQuota{
  59. ObjectMeta: v1.ObjectMeta{
  60. Name: fmt.Sprintf("test-clusterquota-%d", i),
  61. UID: types.UID(fmt.Sprintf("test-clusterquota-%d-uid", i)),
  62. },
  63. Status: quotav1.ClusterResourceQuotaStatus{
  64. Total: corev1.ResourceQuotaStatus{
  65. Hard: corev1.ResourceList{
  66. "requests.cpu": *resource.NewQuantity(5, resource.DecimalSI),
  67. },
  68. Used: corev1.ResourceList{
  69. "requests.cpu": *resource.NewQuantity(4, resource.DecimalSI),
  70. },
  71. },
  72. Namespaces: quotav1.ResourceQuotasStatusByNamespace{
  73. {
  74. Namespace: fmt.Sprintf("ns%d", i),
  75. Status: corev1.ResourceQuotaStatus{
  76. Hard: corev1.ResourceList{
  77. "requests.cpu": *resource.NewQuantity(5, resource.DecimalSI),
  78. },
  79. Used: corev1.ResourceList{
  80. "requests.cpu": *resource.NewQuantity(4, resource.DecimalSI),
  81. },
  82. },
  83. },
  84. },
  85. },
  86. }
  87. _, err := client.QuotaV1().ClusterResourceQuotas().Create(context.Background(), q, v1.CreateOptions{})
  88. require.NoError(t, err, "error creating node")
  89. time.Sleep(2 * time.Millisecond)
  90. }
  91. }