diff --git a/internal/exporter.go b/internal/exporter.go index 6a1cca6..cc5ecc9 100644 --- a/internal/exporter.go +++ b/internal/exporter.go @@ -3,6 +3,7 @@ package internal import ( "context" "crypto/x509/pkix" + "errors" "fmt" "io/fs" "net" @@ -259,15 +260,15 @@ func (exporter *Exporter) getAllMatchingCertificates(pattern string) ([]*certifi walk, doublestar.WithFilesOnly(), doublestar.WithFailOnIOErrors(), + doublestar.WithFailOnPatternNotExist(), // doublestar.WithNoFollow(), - // doublestar.WithFailOnPatternNotExist(), ) if err != nil { - return nil, err - } + if errors.Is(err, doublestar.ErrPatternNotExist) { + return nil, errors.New("no files match \"" + pattern + "\"") + } - if len(output) == 0 { - log.Warnln("no files match pattern \"" + pattern + "\"") + return nil, err } return output, nil diff --git a/internal/exporter_test.go b/internal/exporter_test.go index 56a3d58..b4a85c5 100644 --- a/internal/exporter_test.go +++ b/internal/exporter_test.go @@ -672,6 +672,20 @@ func TestExposeLabels(t *testing.T) { } func TestGlobbing(t *testing.T) { + // no pattern at all + testRequest(t, &Exporter{ + Files: []string{"does-not-exist/toto"}, + }, func(metrics []model.MetricFamily) { + foundMetrics := getMetricsForName(metrics, "x509_cert_expired") + assert.Len(t, foundMetrics, 0) + foundNbMetrics := getMetricsForName(metrics, "x509_cert_not_before") + assert.Len(t, foundNbMetrics, 0) + foundNaMetrics := getMetricsForName(metrics, "x509_cert_not_after") + assert.Len(t, foundNaMetrics, 0) + errMetric := getMetricsForName(metrics, "x509_read_errors") + assert.Equal(t, 1., errMetric[0].GetGauge().GetValue()) + }) + // no matches testRequest(t, &Exporter{ Files: []string{"does-not-exist/**"}, @@ -683,7 +697,7 @@ func TestGlobbing(t *testing.T) { foundNaMetrics := getMetricsForName(metrics, "x509_cert_not_after") assert.Len(t, foundNaMetrics, 0) errMetric := getMetricsForName(metrics, "x509_read_errors") - assert.Equal(t, 0., errMetric[0].GetGauge().GetValue()) + assert.Equal(t, 1., errMetric[0].GetGauge().GetValue()) }) // single star match