2017-05-11 16:39:54 +02:00
|
|
|
// Copyright 2016 Google Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package logadmin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2017-07-23 09:51:42 +02:00
|
|
|
"time"
|
2017-05-11 16:39:54 +02:00
|
|
|
|
2017-07-23 09:51:42 +02:00
|
|
|
"cloud.google.com/go/internal/testutil"
|
2017-05-11 16:39:54 +02:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
"google.golang.org/api/iterator"
|
|
|
|
)
|
|
|
|
|
2017-07-23 09:51:42 +02:00
|
|
|
var metricIDs = testutil.NewUIDSpace("GO-CLIENT-TEST-METRIC")
|
2017-05-11 16:39:54 +02:00
|
|
|
|
|
|
|
// Initializes the tests before they run.
|
|
|
|
func initMetrics(ctx context.Context) {
|
|
|
|
// Clean up from aborted tests.
|
|
|
|
it := client.Metrics(ctx)
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
m, err := it.Next()
|
|
|
|
switch err {
|
|
|
|
case nil:
|
2017-07-23 09:51:42 +02:00
|
|
|
if metricIDs.Older(m.ID, 24*time.Hour) {
|
|
|
|
client.DeleteMetric(ctx, m.ID)
|
|
|
|
}
|
2017-05-11 16:39:54 +02:00
|
|
|
case iterator.Done:
|
|
|
|
break loop
|
|
|
|
default:
|
|
|
|
log.Printf("cleanupMetrics: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateDeleteMetric(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
metric := &Metric{
|
2017-07-23 09:51:42 +02:00
|
|
|
ID: metricIDs.New(),
|
2017-05-11 16:39:54 +02:00
|
|
|
Description: "DESC",
|
|
|
|
Filter: "FILTER",
|
|
|
|
}
|
|
|
|
if err := client.CreateMetric(ctx, metric); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer client.DeleteMetric(ctx, metric.ID)
|
|
|
|
|
|
|
|
got, err := client.Metric(ctx, metric.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if want := metric; !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("got %+v, want %+v", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := client.DeleteMetric(ctx, metric.ID); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := client.Metric(ctx, metric.ID); err == nil {
|
|
|
|
t.Fatal("got no error, expected one")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateMetric(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
metric := &Metric{
|
2017-07-23 09:51:42 +02:00
|
|
|
ID: metricIDs.New(),
|
2017-05-11 16:39:54 +02:00
|
|
|
Description: "DESC",
|
|
|
|
Filter: "FILTER",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updating a non-existent metric creates a new one.
|
|
|
|
if err := client.UpdateMetric(ctx, metric); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer client.DeleteMetric(ctx, metric.ID)
|
|
|
|
got, err := client.Metric(ctx, metric.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if want := metric; !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("got %+v, want %+v", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updating an existing metric changes it.
|
|
|
|
metric.Description = "CHANGED"
|
|
|
|
if err := client.UpdateMetric(ctx, metric); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
got, err = client.Metric(ctx, metric.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if want := metric; !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("got %+v, want %+v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListMetrics(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
var metrics []*Metric
|
2017-07-23 09:51:42 +02:00
|
|
|
want := map[string]*Metric{}
|
2017-05-11 16:39:54 +02:00
|
|
|
for i := 0; i < 10; i++ {
|
2017-07-23 09:51:42 +02:00
|
|
|
m := &Metric{
|
|
|
|
ID: metricIDs.New(),
|
2017-05-11 16:39:54 +02:00
|
|
|
Description: "DESC",
|
|
|
|
Filter: "FILTER",
|
2017-07-23 09:51:42 +02:00
|
|
|
}
|
|
|
|
metrics = append(metrics, m)
|
|
|
|
want[m.ID] = m
|
2017-05-11 16:39:54 +02:00
|
|
|
}
|
|
|
|
for _, m := range metrics {
|
|
|
|
if err := client.CreateMetric(ctx, m); err != nil {
|
|
|
|
t.Fatalf("Create(%q): %v", m.ID, err)
|
|
|
|
}
|
|
|
|
defer client.DeleteMetric(ctx, m.ID)
|
|
|
|
}
|
|
|
|
|
2017-07-23 09:51:42 +02:00
|
|
|
got := map[string]*Metric{}
|
|
|
|
it := client.Metrics(ctx)
|
|
|
|
for {
|
|
|
|
m, err := it.Next()
|
|
|
|
if err == iterator.Done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// If tests run simultaneously, we may have more metrics than we
|
|
|
|
// created. So only check for our own.
|
|
|
|
if _, ok := want[m.ID]; ok {
|
|
|
|
got[m.ID] = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("got %+v, want %+v", got, want)
|
2017-05-11 16:39:54 +02:00
|
|
|
}
|
|
|
|
}
|