github.com/smithx10/nomad@v0.9.1-rc1/e2e/allocstats/allocstats.go (about) 1 package allocstats 2 3 import ( 4 "github.com/hashicorp/nomad/e2e/framework" 5 "github.com/stretchr/testify/require" 6 7 "fmt" 8 9 "time" 10 11 "github.com/hashicorp/nomad/api" 12 "github.com/hashicorp/nomad/e2e/e2eutil" 13 "github.com/hashicorp/nomad/helper/uuid" 14 "github.com/hashicorp/nomad/testutil" 15 ) 16 17 type BasicAllocStatsTest struct { 18 framework.TC 19 jobIds []string 20 } 21 22 func init() { 23 framework.AddSuites(&framework.TestSuite{ 24 Component: "AllocationStats", 25 CanRunLocal: true, 26 Cases: []framework.TestCase{ 27 new(BasicAllocStatsTest), 28 }, 29 }) 30 } 31 32 func (tc *BasicAllocStatsTest) BeforeAll(f *framework.F) { 33 // Ensure cluster has leader before running tests 34 e2eutil.WaitForLeader(f.T(), tc.Nomad()) 35 // Ensure that we have four client nodes in ready state 36 e2eutil.WaitForNodesReady(f.T(), tc.Nomad(), 1) 37 } 38 39 // TestResourceStats is an end to end test for resource utilization 40 // This runs a raw exec job. 41 // TODO(preetha) - add more test cases with more realistic resource utilization 42 func (tc *BasicAllocStatsTest) TestResourceStats(f *framework.F) { 43 nomadClient := tc.Nomad() 44 uuid := uuid.Generate() 45 jobId := "allocstats" + uuid[0:8] 46 tc.jobIds = append(tc.jobIds, jobId) 47 allocs := e2eutil.RegisterAndWaitForAllocs(f.T(), nomadClient, "allocstats/input/raw_exec.nomad", jobId) 48 49 require := require.New(f.T()) 50 require.Len(allocs, 1) 51 52 // Wait till alloc is running 53 allocID := allocs[0].ID 54 e2eutil.WaitForAllocRunning(f.T(), nomadClient, allocID) 55 56 allocsClient := nomadClient.Allocations() 57 58 // Verify allocation resource stats 59 // This job file should result in non zero CPU and Memory stats 60 testutil.WaitForResultRetries(500, func() (bool, error) { 61 time.Sleep(time.Millisecond * 100) 62 allocStatsResp, err := allocsClient.Stats(&api.Allocation{ID: allocID}, nil) 63 if err != nil { 64 return false, fmt.Errorf("unexpected error getting alloc stats: %v", err) 65 } 66 resourceUsage := allocStatsResp.ResourceUsage 67 cpuStatsValid := resourceUsage.CpuStats.TotalTicks > 0 && resourceUsage.CpuStats.Percent > 0 68 memStatsValid := resourceUsage.MemoryStats.RSS > 0 69 return cpuStatsValid && memStatsValid, fmt.Errorf("expected non zero resource usage, but was: %v", resourceUsage) 70 }, func(err error) { 71 f.T().Fatalf("invalid resource usage : %v", err) 72 }) 73 74 } 75 76 func (tc *BasicAllocStatsTest) AfterEach(f *framework.F) { 77 nomadClient := tc.Nomad() 78 jobs := nomadClient.Jobs() 79 // Stop all jobs in test 80 for _, id := range tc.jobIds { 81 jobs.Deregister(id, true, nil) 82 } 83 // Garbage collect 84 nomadClient.System().GarbageCollect() 85 }