X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/c6c3d3c8748b59ca0e8a9d5cfec0a54bdfd212f0..6f18406060f2ac32d505db14ceab97b08431ce04:/services/keepstore/volume_generic_test.go diff --git a/services/keepstore/volume_generic_test.go b/services/keepstore/volume_generic_test.go index 7e72a8f246..d5a413693f 100644 --- a/services/keepstore/volume_generic_test.go +++ b/services/keepstore/volume_generic_test.go @@ -1,3 +1,7 @@ +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: AGPL-3.0 + package main import ( @@ -14,6 +18,8 @@ import ( "git.curoverse.com/arvados.git/sdk/go/arvados" "git.curoverse.com/arvados.git/sdk/go/arvadostest" + "github.com/prometheus/client_golang/prometheus" + dto "github.com/prometheus/client_model/go" ) type TB interface { @@ -71,6 +77,8 @@ func DoGenericVolumeTests(t TB, factory TestableVolumeFactory) { testStatus(t, factory) + testMetrics(t, factory) + testString(t, factory) testUpdateReadOnly(t, factory) @@ -529,6 +537,84 @@ func testStatus(t TB, factory TestableVolumeFactory) { } } +func getValueFrom(cv *prometheus.CounterVec, lbls prometheus.Labels) float64 { + c, _ := cv.GetMetricWith(lbls) + pb := &dto.Metric{} + c.Write(pb) + return pb.GetCounter().GetValue() +} + +func testMetrics(t TB, factory TestableVolumeFactory) { + var err error + + v := factory(t) + defer v.Teardown() + reg := prometheus.NewRegistry() + vm := newVolumeMetricsVecs(reg) + + err = v.Start(vm) + if err != nil { + t.Error("Failed Start(): ", err) + } + opsC, _, ioC := vm.getCounterVecsFor(prometheus.Labels{"device_id": v.DeviceID()}) + + if ioC == nil { + t.Error("ioBytes CounterVec is nil") + return + } + + if getValueFrom(ioC, prometheus.Labels{"direction": "out"})+ + getValueFrom(ioC, prometheus.Labels{"direction": "in"}) > 0 { + t.Error("ioBytes counter should be zero") + } + + if opsC == nil { + t.Error("opsCounter CounterVec is nil") + return + } + + var c, writeOpCounter, readOpCounter float64 + + readOpType, writeOpType := v.ReadWriteOperationLabelValues() + writeOpCounter = getValueFrom(opsC, prometheus.Labels{"operation": writeOpType}) + readOpCounter = getValueFrom(opsC, prometheus.Labels{"operation": readOpType}) + + // Test Put if volume is writable + if v.Writable() { + err = v.Put(context.Background(), TestHash, TestBlock) + if err != nil { + t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err) + } + // Check that the write operations counter increased + c = getValueFrom(opsC, prometheus.Labels{"operation": writeOpType}) + if c <= writeOpCounter { + t.Error("Operation(s) not counted on Put") + } + // Check that bytes counter is > 0 + if getValueFrom(ioC, prometheus.Labels{"direction": "out"}) == 0 { + t.Error("ioBytes{direction=out} counter shouldn't be zero") + } + } else { + v.PutRaw(TestHash, TestBlock) + } + + buf := make([]byte, BlockSize) + _, err = v.Get(context.Background(), TestHash, buf) + if err != nil { + t.Fatal(err) + } + + // Check that the operations counter increased + c = getValueFrom(opsC, prometheus.Labels{"operation": readOpType}) + if c <= readOpCounter { + t.Error("Operation(s) not counted on Get") + } + // Check that the bytes "in" counter is > 0 + if getValueFrom(ioC, prometheus.Labels{"direction": "in"}) == 0 { + t.Error("ioBytes{direction=in} counter shouldn't be zero") + } +} + // Invoke String for the volume; expect non-empty result // Test should pass for both writable and read-only volumes func testString(t TB, factory TestableVolumeFactory) { @@ -928,7 +1014,7 @@ func testTrashEmptyTrashUntrash(t TB, factory TestableVolumeFactory) { // goes away. // (In Azure volumes, un/trash changes Mtime, so first backdate again) v.TouchWithDate(TestHash, time.Now().Add(-2*theConfig.BlobSignatureTTL.Duration())) - err = v.Trash(TestHash) + _ = v.Trash(TestHash) err = checkGet() if err == nil || !os.IsNotExist(err) { t.Fatalf("os.IsNotExist(%v) should have been true", err)