func TestUpdateMetric(t *testing.T) { ctx := context.Background() metric := &Metric{ ID: ltesting.UniqueID(testMetricIDPrefix), 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 TestCreateDeleteSink(t *testing.T) { ctx := context.Background() sink := &Sink{ ID: ltesting.UniqueID(testSinkIDPrefix), Destination: testSinkDestination, Filter: testFilter, } got, err := client.CreateSink(ctx, sink) if err != nil { t.Fatal(err) } defer client.DeleteSink(ctx, sink.ID) if want := sink; !reflect.DeepEqual(got, want) { t.Errorf("got %+v, want %+v", got, want) } got, err = client.Sink(ctx, sink.ID) if err != nil { t.Fatal(err) } if want := sink; !reflect.DeepEqual(got, want) { t.Errorf("got %+v, want %+v", got, want) } if err := client.DeleteSink(ctx, sink.ID); err != nil { t.Fatal(err) } if _, err := client.Sink(ctx, sink.ID); err == nil { t.Fatal("got no error, expected one") } }
func TestCreateDeleteMetric(t *testing.T) { ctx := context.Background() metric := &Metric{ ID: ltesting.UniqueID(testMetricIDPrefix), 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 TestListMetrics(t *testing.T) { ctx := context.Background() var metrics []*Metric for i := 0; i < 10; i++ { metrics = append(metrics, &Metric{ ID: ltesting.UniqueID(testMetricIDPrefix), Description: "DESC", Filter: "FILTER", }) } 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) } it := client.Metrics(ctx) msg, ok := testutil.TestIteratorNext(metrics, iterator.Done, func() (interface{}, error) { return it.Next() }) if !ok { t.Fatal(msg) } // TODO(jba): test exact paging. }
// Called just before TestMain calls m.Run. // Returns a cleanup function to be called after the tests finish. func initSinks(ctx context.Context) func() { // Create a unique GCS bucket so concurrent tests don't interfere with each other. testBucketPrefix := testProjectID + "-log-sink" testBucket := ltesting.UniqueID(testBucketPrefix) testSinkDestination = "storage.googleapis.com/" + testBucket var storageClient *storage.Client if integrationTest { // Create a unique bucket as a sink destination, and give the cloud logging account // owner right. ts := testutil.TokenSource(ctx, storage.ScopeFullControl) var err error storageClient, err = storage.NewClient(ctx, option.WithTokenSource(ts)) if err != nil { log.Fatalf("new storage client: %v", err) } bucket := storageClient.Bucket(testBucket) if err := bucket.Create(ctx, testProjectID, nil); err != nil { log.Fatalf("creating storage bucket %q: %v", testBucket, err) } if err := bucket.ACL().Set(ctx, "*****@*****.**", storage.RoleOwner); err != nil { log.Fatalf("setting owner role: %v", err) } } // Clean up from aborted tests. for _, sID := range ltesting.ExpiredUniqueIDs(sinkIDs(ctx), testSinkIDPrefix) { client.DeleteSink(ctx, sID) // ignore error } if integrationTest { for _, bn := range ltesting.ExpiredUniqueIDs(bucketNames(ctx, storageClient), testBucketPrefix) { storageClient.Bucket(bn).Delete(ctx) // ignore error } return func() { if err := storageClient.Bucket(testBucket).Delete(ctx); err != nil { log.Printf("deleting %q: %v", testBucket, err) } storageClient.Close() } } return func() {} }
func TestUpdateSink(t *testing.T) { ctx := context.Background() sink := &Sink{ ID: ltesting.UniqueID(testSinkIDPrefix), Destination: testSinkDestination, Filter: testFilter, } // Updating a non-existent sink creates a new one. got, err := client.UpdateSink(ctx, sink) if err != nil { t.Fatal(err) } defer client.DeleteSink(ctx, sink.ID) if want := sink; !reflect.DeepEqual(got, want) { t.Errorf("got %+v, want %+v", got, want) } got, err = client.Sink(ctx, sink.ID) if err != nil { t.Fatal(err) } if want := sink; !reflect.DeepEqual(got, want) { t.Errorf("got %+v, want %+v", got, want) } // Updating an existing sink changes it. sink.Filter = "" if _, err := client.UpdateSink(ctx, sink); err != nil { t.Fatal(err) } got, err = client.Sink(ctx, sink.ID) if err != nil { t.Fatal(err) } if want := sink; !reflect.DeepEqual(got, want) { t.Errorf("got %+v, want %+v", got, want) } }
func TestListSinks(t *testing.T) { ctx := context.Background() var sinks []*Sink for i := 0; i < 4; i++ { sinks = append(sinks, &Sink{ ID: ltesting.UniqueID(testSinkIDPrefix), Destination: testSinkDestination, Filter: testFilter, }) } for _, s := range sinks { if _, err := client.CreateSink(ctx, s); err != nil { t.Fatalf("Create(%q): %v", s.ID, err) } defer client.DeleteSink(ctx, s.ID) } it := client.Sinks(ctx) msg, ok := testutil.TestIteratorNext(sinks, iterator.Done, func() (interface{}, error) { return it.Next() }) if !ok { t.Fatal(msg) } // TODO(jba): test exact paging. }
func TestListSinks(t *testing.T) { ctx := context.Background() var sinks []*Sink for i := 0; i < 4; i++ { sinks = append(sinks, &Sink{ ID: ltesting.UniqueID(testSinkIDPrefix), Destination: testSinkDestination, Filter: testFilter, }) } for _, s := range sinks { if _, err := client.CreateSink(ctx, s); err != nil { t.Fatalf("Create(%q): %v", s.ID, err) } defer client.DeleteSink(ctx, s.ID) } msg, ok := itesting.TestIterator(sinks, func() interface{} { return client.Sinks(ctx) }, func(it interface{}) (interface{}, error) { return it.(*SinkIterator).Next() }) if !ok { t.Fatal(msg) } }
func initLogs(ctx context.Context) { testLogID = ltesting.UniqueID(testLogIDPrefix) // TODO(jba): Clean up from previous aborted tests by deleting old logs; requires ListLogs RPC. }
func initLogs(ctx context.Context) { testLogID = ltesting.UniqueID(testLogIDPrefix) testFilter = fmt.Sprintf(`logName = "projects/%s/logs/%s"`, testProjectID, strings.Replace(testLogID, "/", "%2F", -1)) // TODO(jba): Clean up from previous aborted tests by deleting old logs; requires ListLogs RPC. }