...

Source file src/cloud.google.com/go/logging/logadmin/metrics_test.go

Documentation: cloud.google.com/go/logging/logadmin

     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  
    15  package logadmin
    16  
    17  import (
    18  	"context"
    19  	"log"
    20  	"testing"
    21  	"time"
    22  
    23  	"cloud.google.com/go/internal/testutil"
    24  	"cloud.google.com/go/internal/uid"
    25  	"google.golang.org/api/iterator"
    26  )
    27  
    28  var metricIDs = uid.NewSpace("GO-CLIENT-TEST-METRIC", nil)
    29  
    30  // Initializes the tests before they run.
    31  func initMetrics(ctx context.Context) {
    32  	// Clean up from aborted tests.
    33  	it := client.Metrics(ctx)
    34  loop:
    35  	for {
    36  		m, err := it.Next()
    37  		switch err {
    38  		case nil:
    39  			if metricIDs.Older(m.ID, 24*time.Hour) {
    40  				client.DeleteMetric(ctx, m.ID)
    41  			}
    42  		case iterator.Done:
    43  			break loop
    44  		default:
    45  			log.Printf("cleanupMetrics: %v", err)
    46  			return
    47  		}
    48  	}
    49  }
    50  
    51  func TestCreateDeleteMetric(t *testing.T) {
    52  	ctx := context.Background()
    53  	metric := &Metric{
    54  		ID:          metricIDs.New(),
    55  		Description: "DESC",
    56  		Filter:      "FILTER",
    57  	}
    58  	if err := client.CreateMetric(ctx, metric); err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	defer client.DeleteMetric(ctx, metric.ID)
    62  
    63  	got, err := client.Metric(ctx, metric.ID)
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	if want := metric; !testutil.Equal(got, want) {
    68  		t.Errorf("got %+v, want %+v", got, want)
    69  	}
    70  
    71  	if err := client.DeleteMetric(ctx, metric.ID); err != nil {
    72  		t.Fatal(err)
    73  	}
    74  
    75  	if _, err := client.Metric(ctx, metric.ID); err == nil {
    76  		t.Fatal("got no error, expected one")
    77  	}
    78  }
    79  
    80  func TestUpdateMetric(t *testing.T) {
    81  	ctx := context.Background()
    82  	metric := &Metric{
    83  		ID:          metricIDs.New(),
    84  		Description: "DESC",
    85  		Filter:      "FILTER",
    86  	}
    87  
    88  	// Updating a non-existent metric creates a new one.
    89  	if err := client.UpdateMetric(ctx, metric); err != nil {
    90  		t.Fatal(err)
    91  	}
    92  	defer client.DeleteMetric(ctx, metric.ID)
    93  	got, err := client.Metric(ctx, metric.ID)
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  	if want := metric; !testutil.Equal(got, want) {
    98  		t.Errorf("got %+v, want %+v", got, want)
    99  	}
   100  
   101  	// Updating an existing metric changes it.
   102  	metric.Description = "CHANGED"
   103  	if err := client.UpdateMetric(ctx, metric); err != nil {
   104  		t.Fatal(err)
   105  	}
   106  	got, err = client.Metric(ctx, metric.ID)
   107  	if err != nil {
   108  		t.Fatal(err)
   109  	}
   110  	if want := metric; !testutil.Equal(got, want) {
   111  		t.Errorf("got %+v, want %+v", got, want)
   112  	}
   113  }
   114  
   115  func TestListMetrics(t *testing.T) {
   116  	ctx := context.Background()
   117  
   118  	var metrics []*Metric
   119  	want := map[string]*Metric{}
   120  	for i := 0; i < 10; i++ {
   121  		m := &Metric{
   122  			ID:          metricIDs.New(),
   123  			Description: "DESC",
   124  			Filter:      "FILTER",
   125  		}
   126  		metrics = append(metrics, m)
   127  		want[m.ID] = m
   128  	}
   129  	for _, m := range metrics {
   130  		if err := client.CreateMetric(ctx, m); err != nil {
   131  			t.Fatalf("Create(%q): %v", m.ID, err)
   132  		}
   133  		defer client.DeleteMetric(ctx, m.ID)
   134  	}
   135  
   136  	got := map[string]*Metric{}
   137  	it := client.Metrics(ctx)
   138  	for {
   139  		m, err := it.Next()
   140  		if err == iterator.Done {
   141  			break
   142  		}
   143  		if err != nil {
   144  			t.Fatal(err)
   145  		}
   146  		// If tests run simultaneously, we may have more metrics than we
   147  		// created. So only check for our own.
   148  		if _, ok := want[m.ID]; ok {
   149  			got[m.ID] = m
   150  		}
   151  	}
   152  	if !testutil.Equal(got, want) {
   153  		t.Errorf("got %+v, want %+v", got, want)
   154  	}
   155  }
   156  

View as plain text