You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

156 lines
3.5 KiB

  1. // Copyright 2016 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package logadmin
  15. import (
  16. "context"
  17. "log"
  18. "testing"
  19. "time"
  20. "cloud.google.com/go/internal/testutil"
  21. "cloud.google.com/go/internal/uid"
  22. "google.golang.org/api/iterator"
  23. )
  24. var metricIDs = uid.NewSpace("GO-CLIENT-TEST-METRIC", nil)
  25. // Initializes the tests before they run.
  26. func initMetrics(ctx context.Context) {
  27. // Clean up from aborted tests.
  28. it := client.Metrics(ctx)
  29. loop:
  30. for {
  31. m, err := it.Next()
  32. switch err {
  33. case nil:
  34. if metricIDs.Older(m.ID, 24*time.Hour) {
  35. client.DeleteMetric(ctx, m.ID)
  36. }
  37. case iterator.Done:
  38. break loop
  39. default:
  40. log.Printf("cleanupMetrics: %v", err)
  41. return
  42. }
  43. }
  44. }
  45. func TestCreateDeleteMetric(t *testing.T) {
  46. ctx := context.Background()
  47. metric := &Metric{
  48. ID: metricIDs.New(),
  49. Description: "DESC",
  50. Filter: "FILTER",
  51. }
  52. if err := client.CreateMetric(ctx, metric); err != nil {
  53. t.Fatal(err)
  54. }
  55. defer client.DeleteMetric(ctx, metric.ID)
  56. got, err := client.Metric(ctx, metric.ID)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. if want := metric; !testutil.Equal(got, want) {
  61. t.Errorf("got %+v, want %+v", got, want)
  62. }
  63. if err := client.DeleteMetric(ctx, metric.ID); err != nil {
  64. t.Fatal(err)
  65. }
  66. if _, err := client.Metric(ctx, metric.ID); err == nil {
  67. t.Fatal("got no error, expected one")
  68. }
  69. }
  70. func TestUpdateMetric(t *testing.T) {
  71. ctx := context.Background()
  72. metric := &Metric{
  73. ID: metricIDs.New(),
  74. Description: "DESC",
  75. Filter: "FILTER",
  76. }
  77. // Updating a non-existent metric creates a new one.
  78. if err := client.UpdateMetric(ctx, metric); err != nil {
  79. t.Fatal(err)
  80. }
  81. defer client.DeleteMetric(ctx, metric.ID)
  82. got, err := client.Metric(ctx, metric.ID)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. if want := metric; !testutil.Equal(got, want) {
  87. t.Errorf("got %+v, want %+v", got, want)
  88. }
  89. // Updating an existing metric changes it.
  90. metric.Description = "CHANGED"
  91. if err := client.UpdateMetric(ctx, metric); err != nil {
  92. t.Fatal(err)
  93. }
  94. got, err = client.Metric(ctx, metric.ID)
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. if want := metric; !testutil.Equal(got, want) {
  99. t.Errorf("got %+v, want %+v", got, want)
  100. }
  101. }
  102. func TestListMetrics(t *testing.T) {
  103. ctx := context.Background()
  104. var metrics []*Metric
  105. want := map[string]*Metric{}
  106. for i := 0; i < 10; i++ {
  107. m := &Metric{
  108. ID: metricIDs.New(),
  109. Description: "DESC",
  110. Filter: "FILTER",
  111. }
  112. metrics = append(metrics, m)
  113. want[m.ID] = m
  114. }
  115. for _, m := range metrics {
  116. if err := client.CreateMetric(ctx, m); err != nil {
  117. t.Fatalf("Create(%q): %v", m.ID, err)
  118. }
  119. defer client.DeleteMetric(ctx, m.ID)
  120. }
  121. got := map[string]*Metric{}
  122. it := client.Metrics(ctx)
  123. for {
  124. m, err := it.Next()
  125. if err == iterator.Done {
  126. break
  127. }
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. // If tests run simultaneously, we may have more metrics than we
  132. // created. So only check for our own.
  133. if _, ok := want[m.ID]; ok {
  134. got[m.ID] = m
  135. }
  136. }
  137. if !testutil.Equal(got, want) {
  138. t.Errorf("got %+v, want %+v", got, want)
  139. }
  140. }