2018-08-05 02:03:18 -07:00
// Copyright 2018 The Prometheus Authors
// 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 main
import (
2023-03-11 15:18:33 -08:00
"bytes"
"context"
2022-07-01 05:38:49 -07:00
"errors"
2018-08-05 02:03:18 -07:00
"fmt"
"net/http"
"net/http/httptest"
2020-12-23 10:52:04 -08:00
"net/url"
2022-01-07 13:58:28 -08:00
"os"
2022-07-01 05:38:49 -07:00
"os/exec"
2023-03-11 15:18:33 -08:00
"path/filepath"
2021-11-30 08:11:48 -08:00
"runtime"
"strings"
2022-07-01 05:38:49 -07:00
"syscall"
2018-08-05 02:03:18 -07:00
"testing"
"time"
2020-08-25 03:32:25 -07:00
2021-10-22 01:19:38 -07:00
"github.com/stretchr/testify/require"
2021-11-08 06:23:17 -08:00
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/rulefmt"
2018-08-05 02:03:18 -07:00
)
2022-07-01 05:38:49 -07:00
var promtoolPath = os . Args [ 0 ]
func TestMain ( m * testing . M ) {
for i , arg := range os . Args {
if arg == "-test.main" {
os . Args = append ( os . Args [ : i ] , os . Args [ i + 1 : ] ... )
main ( )
return
}
}
exitCode := m . Run ( )
os . Exit ( exitCode )
}
2018-08-05 02:03:18 -07:00
func TestQueryRange ( t * testing . T ) {
2019-05-17 09:09:47 -07:00
s , getRequest := mockServer ( 200 , ` { "status": "success", "data": { "resultType": "matrix", "result": []}} ` )
2018-08-05 02:03:18 -07:00
defer s . Close ( )
2020-12-23 10:52:04 -08:00
urlObject , err := url . Parse ( s . URL )
require . Equal ( t , nil , err )
2018-11-14 09:40:07 -08:00
p := & promqlPrinter { }
2022-10-24 16:12:30 -07:00
exitCode := QueryRange ( urlObject , http . DefaultTransport , map [ string ] string { } , "up" , "0" , "300" , 0 , p )
2020-10-29 02:43:23 -07:00
require . Equal ( t , "/api/v1/query_range" , getRequest ( ) . URL . Path )
2019-05-17 09:09:47 -07:00
form := getRequest ( ) . Form
2020-10-29 02:43:23 -07:00
require . Equal ( t , "up" , form . Get ( "query" ) )
require . Equal ( t , "1" , form . Get ( "step" ) )
require . Equal ( t , 0 , exitCode )
2018-08-05 02:03:18 -07:00
2022-10-24 16:12:30 -07:00
exitCode = QueryRange ( urlObject , http . DefaultTransport , map [ string ] string { } , "up" , "0" , "300" , 10 * time . Millisecond , p )
2020-10-29 02:43:23 -07:00
require . Equal ( t , "/api/v1/query_range" , getRequest ( ) . URL . Path )
2019-05-17 09:09:47 -07:00
form = getRequest ( ) . Form
2020-10-29 02:43:23 -07:00
require . Equal ( t , "up" , form . Get ( "query" ) )
require . Equal ( t , "0.01" , form . Get ( "step" ) )
require . Equal ( t , 0 , exitCode )
2020-08-25 03:32:25 -07:00
}
func TestQueryInstant ( t * testing . T ) {
s , getRequest := mockServer ( 200 , ` { "status": "success", "data": { "resultType": "vector", "result": []}} ` )
defer s . Close ( )
2020-12-23 10:52:04 -08:00
urlObject , err := url . Parse ( s . URL )
require . Equal ( t , nil , err )
2020-08-25 03:32:25 -07:00
p := & promqlPrinter { }
2022-10-24 16:12:30 -07:00
exitCode := QueryInstant ( urlObject , http . DefaultTransport , "up" , "300" , p )
2020-10-29 02:43:23 -07:00
require . Equal ( t , "/api/v1/query" , getRequest ( ) . URL . Path )
2020-08-25 03:32:25 -07:00
form := getRequest ( ) . Form
2020-10-29 02:43:23 -07:00
require . Equal ( t , "up" , form . Get ( "query" ) )
require . Equal ( t , "300" , form . Get ( "time" ) )
require . Equal ( t , 0 , exitCode )
2018-08-05 02:03:18 -07:00
}
2019-05-17 09:09:47 -07:00
func mockServer ( code int , body string ) ( * httptest . Server , func ( ) * http . Request ) {
var req * http . Request
2018-08-05 02:03:18 -07:00
server := httptest . NewServer ( http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
2019-05-17 09:09:47 -07:00
r . ParseForm ( )
req = r
2018-08-05 02:03:18 -07:00
w . WriteHeader ( code )
fmt . Fprintln ( w , body )
} ) )
2019-05-17 09:09:47 -07:00
f := func ( ) * http . Request {
return req
2018-08-05 02:03:18 -07:00
}
return server , f
}
2021-06-29 08:32:59 -07:00
func TestCheckSDFile ( t * testing . T ) {
cases := [ ] struct {
name string
file string
err string
} {
{
name : "good .yml" ,
file : "./testdata/good-sd-file.yml" ,
} ,
{
name : "good .yaml" ,
file : "./testdata/good-sd-file.yaml" ,
} ,
{
name : "good .json" ,
file : "./testdata/good-sd-file.json" ,
} ,
{
name : "bad file extension" ,
file : "./testdata/bad-sd-file-extension.nonexistant" ,
err : "invalid file extension: \".nonexistant\"" ,
} ,
{
name : "bad format" ,
file : "./testdata/bad-sd-file-format.yml" ,
err : "yaml: unmarshal errors:\n line 1: field targats not found in type struct { Targets []string \"yaml:\\\"targets\\\"\"; Labels model.LabelSet \"yaml:\\\"labels\\\"\" }" ,
} ,
}
for _ , test := range cases {
t . Run ( test . name , func ( t * testing . T ) {
2021-10-27 17:01:28 -07:00
_ , err := checkSDFile ( test . file )
2021-06-29 08:32:59 -07:00
if test . err != "" {
require . Equalf ( t , test . err , err . Error ( ) , "Expected error %q, got %q" , test . err , err . Error ( ) )
return
}
require . NoError ( t , err )
} )
}
}
2021-08-26 22:44:46 -07:00
func TestCheckDuplicates ( t * testing . T ) {
cases := [ ] struct {
name string
ruleFile string
expectedDups [ ] compareRuleType
} {
{
name : "no duplicates" ,
ruleFile : "./testdata/rules.yml" ,
} ,
{
name : "duplicate in other group" ,
ruleFile : "./testdata/rules_duplicates.yml" ,
expectedDups : [ ] compareRuleType {
{
metric : "job:test:count_over_time1m" ,
2021-08-22 09:03:42 -07:00
label : labels . New ( ) ,
2021-08-26 22:44:46 -07:00
} ,
} ,
} ,
}
for _ , test := range cases {
c := test
t . Run ( c . name , func ( t * testing . T ) {
rgs , err := rulefmt . ParseFile ( c . ruleFile )
require . Empty ( t , err )
dups := checkDuplicates ( rgs . Groups )
require . Equal ( t , c . expectedDups , dups )
} )
}
}
2021-08-26 23:22:34 -07:00
func BenchmarkCheckDuplicates ( b * testing . B ) {
rgs , err := rulefmt . ParseFile ( "./testdata/rules_large.yml" )
require . Empty ( b , err )
b . ResetTimer ( )
for i := 0 ; i < b . N ; i ++ {
checkDuplicates ( rgs . Groups )
}
}
2021-10-27 17:01:28 -07:00
func TestCheckTargetConfig ( t * testing . T ) {
cases := [ ] struct {
name string
file string
err string
} {
{
name : "url_in_scrape_targetgroup_with_relabel_config.good" ,
file : "url_in_scrape_targetgroup_with_relabel_config.good.yml" ,
err : "" ,
} ,
{
name : "url_in_alert_targetgroup_with_relabel_config.good" ,
file : "url_in_alert_targetgroup_with_relabel_config.good.yml" ,
err : "" ,
} ,
{
name : "url_in_scrape_targetgroup_with_relabel_config.bad" ,
file : "url_in_scrape_targetgroup_with_relabel_config.bad.yml" ,
err : "instance 0 in group 0: \"http://bad\" is not a valid hostname" ,
} ,
{
name : "url_in_alert_targetgroup_with_relabel_config.bad" ,
file : "url_in_alert_targetgroup_with_relabel_config.bad.yml" ,
err : "\"http://bad\" is not a valid hostname" ,
} ,
}
for _ , test := range cases {
t . Run ( test . name , func ( t * testing . T ) {
2021-11-30 08:11:48 -08:00
_ , err := checkConfig ( false , "testdata/" + test . file , false )
2021-10-27 17:01:28 -07:00
if test . err != "" {
require . Equalf ( t , test . err , err . Error ( ) , "Expected error %q, got %q" , test . err , err . Error ( ) )
return
}
require . NoError ( t , err )
} )
}
}
2021-11-29 20:02:07 -08:00
2021-11-30 08:11:48 -08:00
func TestCheckConfigSyntax ( t * testing . T ) {
cases := [ ] struct {
name string
file string
syntaxOnly bool
err string
errWindows string
} {
{
name : "check with syntax only succeeds with nonexistent rule files" ,
file : "config_with_rule_files.yml" ,
syntaxOnly : true ,
err : "" ,
errWindows : "" ,
} ,
{
name : "check without syntax only fails with nonexistent rule files" ,
file : "config_with_rule_files.yml" ,
syntaxOnly : false ,
err : "\"testdata/non-existent-file.yml\" does not point to an existing file" ,
errWindows : "\"testdata\\\\non-existent-file.yml\" does not point to an existing file" ,
} ,
{
name : "check with syntax only succeeds with nonexistent service discovery files" ,
file : "config_with_service_discovery_files.yml" ,
syntaxOnly : true ,
err : "" ,
errWindows : "" ,
} ,
// The test below doesn't fail because the file verification for ServiceDiscoveryConfigs doesn't fail the check if
// file isn't found; it only outputs a warning message.
{
name : "check without syntax only succeeds with nonexistent service discovery files" ,
file : "config_with_service_discovery_files.yml" ,
syntaxOnly : false ,
err : "" ,
errWindows : "" ,
} ,
{
name : "check with syntax only succeeds with nonexistent TLS files" ,
file : "config_with_tls_files.yml" ,
syntaxOnly : true ,
err : "" ,
errWindows : "" ,
} ,
{
name : "check without syntax only fails with nonexistent TLS files" ,
file : "config_with_tls_files.yml" ,
syntaxOnly : false ,
err : "error checking client cert file \"testdata/nonexistent_cert_file.yml\": " +
"stat testdata/nonexistent_cert_file.yml: no such file or directory" ,
errWindows : "error checking client cert file \"testdata\\\\nonexistent_cert_file.yml\": " +
"CreateFile testdata\\nonexistent_cert_file.yml: The system cannot find the file specified." ,
} ,
{
name : "check with syntax only succeeds with nonexistent credentials file" ,
file : "authorization_credentials_file.bad.yml" ,
syntaxOnly : true ,
err : "" ,
errWindows : "" ,
} ,
{
name : "check without syntax only fails with nonexistent credentials file" ,
file : "authorization_credentials_file.bad.yml" ,
syntaxOnly : false ,
err : "error checking authorization credentials or bearer token file \"/random/file/which/does/not/exist.yml\": " +
"stat /random/file/which/does/not/exist.yml: no such file or directory" ,
errWindows : "error checking authorization credentials or bearer token file \"testdata\\\\random\\\\file\\\\which\\\\does\\\\not\\\\exist.yml\": " +
"CreateFile testdata\\random\\file\\which\\does\\not\\exist.yml: The system cannot find the path specified." ,
} ,
}
for _ , test := range cases {
t . Run ( test . name , func ( t * testing . T ) {
_ , err := checkConfig ( false , "testdata/" + test . file , test . syntaxOnly )
expectedErrMsg := test . err
if strings . Contains ( runtime . GOOS , "windows" ) {
expectedErrMsg = test . errWindows
}
if expectedErrMsg != "" {
require . Equalf ( t , expectedErrMsg , err . Error ( ) , "Expected error %q, got %q" , test . err , err . Error ( ) )
return
}
require . NoError ( t , err )
} )
}
}
2021-11-29 20:02:07 -08:00
func TestAuthorizationConfig ( t * testing . T ) {
cases := [ ] struct {
name string
file string
err string
} {
{
name : "authorization_credentials_file.bad" ,
file : "authorization_credentials_file.bad.yml" ,
err : "error checking authorization credentials or bearer token file" ,
} ,
{
name : "authorization_credentials_file.good" ,
file : "authorization_credentials_file.good.yml" ,
err : "" ,
} ,
}
for _ , test := range cases {
t . Run ( test . name , func ( t * testing . T ) {
2021-11-30 08:11:48 -08:00
_ , err := checkConfig ( false , "testdata/" + test . file , false )
2021-11-29 20:02:07 -08:00
if test . err != "" {
require . Contains ( t , err . Error ( ) , test . err , "Expected error to contain %q, got %q" , test . err , err . Error ( ) )
return
}
require . NoError ( t , err )
} )
}
}
2022-01-07 13:58:28 -08:00
func TestCheckMetricsExtended ( t * testing . T ) {
if runtime . GOOS == "windows" {
t . Skip ( "Skipping on windows" )
}
f , err := os . Open ( "testdata/metrics-test.prom" )
require . NoError ( t , err )
defer f . Close ( )
stats , total , err := checkMetricsExtended ( f )
require . NoError ( t , err )
require . Equal ( t , 27 , total )
require . Equal ( t , [ ] metricStat {
{
name : "prometheus_tsdb_compaction_chunk_size_bytes" ,
cardinality : 15 ,
percentage : float64 ( 15 ) / float64 ( 27 ) ,
} ,
{
name : "go_gc_duration_seconds" ,
cardinality : 7 ,
percentage : float64 ( 7 ) / float64 ( 27 ) ,
} ,
{
name : "net_conntrack_dialer_conn_attempted_total" ,
cardinality : 4 ,
percentage : float64 ( 4 ) / float64 ( 27 ) ,
} ,
{
name : "go_info" ,
cardinality : 1 ,
percentage : float64 ( 1 ) / float64 ( 27 ) ,
} ,
} , stats )
}
2022-07-01 05:38:49 -07:00
func TestExitCodes ( t * testing . T ) {
if testing . Short ( ) {
t . Skip ( "skipping test in short mode." )
}
for _ , c := range [ ] struct {
file string
exitCode int
lintIssue bool
} {
{
file : "prometheus-config.good.yml" ,
} ,
{
file : "prometheus-config.bad.yml" ,
exitCode : 1 ,
} ,
{
file : "prometheus-config.nonexistent.yml" ,
exitCode : 1 ,
} ,
{
file : "prometheus-config.lint.yml" ,
lintIssue : true ,
exitCode : 3 ,
} ,
} {
t . Run ( c . file , func ( t * testing . T ) {
for _ , lintFatal := range [ ] bool { true , false } {
t . Run ( fmt . Sprintf ( "%t" , lintFatal ) , func ( t * testing . T ) {
args := [ ] string { "-test.main" , "check" , "config" , "testdata/" + c . file }
if lintFatal {
args = append ( args , "--lint-fatal" )
}
tool := exec . Command ( promtoolPath , args ... )
err := tool . Run ( )
if c . exitCode == 0 || ( c . lintIssue && ! lintFatal ) {
require . NoError ( t , err )
return
}
require . Error ( t , err )
var exitError * exec . ExitError
if errors . As ( err , & exitError ) {
status := exitError . Sys ( ) . ( syscall . WaitStatus )
require . Equal ( t , c . exitCode , status . ExitStatus ( ) )
} else {
t . Errorf ( "unable to retrieve the exit status for promtool: %v" , err )
}
} )
}
} )
}
}
2023-03-11 15:18:33 -08:00
func TestDocumentation ( t * testing . T ) {
if runtime . GOOS == "windows" {
t . SkipNow ( )
}
ctx , cancel := context . WithTimeout ( context . Background ( ) , 5 * time . Second )
defer cancel ( )
cmd := exec . CommandContext ( ctx , promtoolPath , "-test.main" , "write-documentation" )
var stdout bytes . Buffer
cmd . Stdout = & stdout
if err := cmd . Run ( ) ; err != nil {
if exitError , ok := err . ( * exec . ExitError ) ; ok {
if exitError . ExitCode ( ) != 0 {
fmt . Println ( "Command failed with non-zero exit code" )
}
}
}
generatedContent := strings . ReplaceAll ( stdout . String ( ) , filepath . Base ( promtoolPath ) , strings . TrimSuffix ( filepath . Base ( promtoolPath ) , ".test" ) )
expectedContent , err := os . ReadFile ( filepath . Join ( ".." , ".." , "docs" , "command-line" , "promtool.md" ) )
require . NoError ( t , err )
require . Equal ( t , string ( expectedContent ) , generatedContent , "Generated content does not match documentation. Hint: run `make cli-documentation`." )
}