github.com/bigcommerce/nomad@v0.9.3-bc/e2e/metrics/metrics_test.go (about)

     1  package metrics
     2  
     3  import (
     4  	"testing"
     5  
     6  	"flag"
     7  	"fmt"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  
    12  	"github.com/hashicorp/nomad/api"
    13  	"github.com/hashicorp/nomad/e2e/e2eutil"
    14  	"github.com/hashicorp/nomad/helper/uuid"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  var metrics = flag.Bool("metrics", false, "run metrics tests")
    19  
    20  func WaitForCluster(t *testing.T, nomadClient *api.Client) {
    21  	// Ensure cluster has leader before running tests
    22  	e2eutil.WaitForLeader(t, nomadClient)
    23  	// Ensure that we have four client nodes in ready state
    24  	e2eutil.WaitForNodesReady(t, nomadClient, 1)
    25  }
    26  
    27  // TestMetrics runs fabio/prometheus and waits for those to succeed
    28  // After that a series of jobs added to the input directory are executed
    29  // Unlike other e2e tests this test does not clean up after itself.
    30  // This test is meant for AWS environments and will not work locally
    31  func TestMetrics(t *testing.T) {
    32  	if !*metrics {
    33  		t.Skip("skipping test in non-metrics mode.")
    34  	}
    35  	require := require.New(t)
    36  	// Build Nomad api client
    37  	nomadClient, err := api.NewClient(api.DefaultConfig())
    38  	require.Nil(err)
    39  	WaitForCluster(t, nomadClient)
    40  
    41  	// Run fabio
    42  	fabioAllocs := e2eutil.RegisterAndWaitForAllocs(t, nomadClient, "../fabio/fabio.nomad", "fabio")
    43  	require.NotEmpty(fabioAllocs)
    44  
    45  	// Run prometheus
    46  	prometheusAllocs := e2eutil.RegisterAndWaitForAllocs(t, nomadClient, "../prometheus/prometheus.nomad", "prometheus")
    47  	require.NotEmpty(prometheusAllocs)
    48  
    49  	// List all job jobFiles in the input directory and run them and wait for allocations
    50  	var jobFiles []string
    51  
    52  	root := "input/"
    53  	err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
    54  		if strings.HasSuffix(path, ".nomad") {
    55  			jobFiles = append(jobFiles, path)
    56  		}
    57  		return nil
    58  	})
    59  	require.Nil(err)
    60  	for _, file := range jobFiles {
    61  		uuid := uuid.Generate()
    62  		jobId := "metrics" + uuid[0:8]
    63  		fmt.Println("Registering ", file)
    64  		allocs := e2eutil.RegisterAndWaitForAllocs(t, nomadClient, file, jobId)
    65  		require.NotEmpty(allocs)
    66  	}
    67  
    68  	// Get a client node IP address
    69  	nodesAPI := nomadClient.Nodes()
    70  	nodes, _, err := nodesAPI.List(nil)
    71  	require.Nil(err)
    72  	for _, node := range nodes {
    73  		nodeDetails, _, err := nodesAPI.Info(node.ID, nil)
    74  		require.Nil(err)
    75  		clientPublicIP := nodeDetails.Attributes["unique.platform.aws.public-ipv4"]
    76  		fmt.Printf("Prometheus Metrics available at http://%s:9999\n", clientPublicIP)
    77  	}
    78  
    79  }