forked from rivosinc/prometheus-slurm-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs_test.go
173 lines (156 loc) · 3.9 KB
/
jobs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// SPDX-FileCopyrightText: 2023 Rivos Inc.
//
// SPDX-License-Identifier: Apache-2.0
package main
import (
"testing"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
)
var MockJobInfoFetcher = &MockFetcher{fixture: "fixtures/squeue_out.json"}
func TestNewJobsController(t *testing.T) {
assert := assert.New(t)
config := &Config{
pollLimit: 10,
traceConf: &TraceConfig{
sharedFetcher: MockJobInfoFetcher,
},
cliOpts: &CliOpts{
fallback: true,
},
}
jc := NewJobsController(config)
assert.NotNil(jc)
}
func TestParseJobMetrics(t *testing.T) {
assert := assert.New(t)
fixture, err := MockJobInfoFetcher.Fetch()
assert.Nil(err)
jms, err := parseJobMetrics(fixture)
assert.Nil(err)
assert.NotEmpty(jms)
// test parse of single job
var job *JobMetric
for _, m := range jms {
if m.JobId == 26515966 {
job = &m
break
}
}
assert.NotNil(job)
assert.Equal(float64(64000), totalAllocMem(&job.JobResources))
}
func TestParseCliFallback(t *testing.T) {
assert := assert.New(t)
fetcher := MockFetcher{fixture: "fixtures/squeue_fallback.txt"}
data, err := fetcher.Fetch()
assert.Nil(err)
metrics, err := parseCliFallback(data)
assert.Nil(err)
assert.NotEmpty(metrics)
}
func TestUserJobMetric(t *testing.T) {
// setup
assert := assert.New(t)
fixture, err := MockJobInfoFetcher.Fetch()
assert.Nil(err)
jms, err := parseJobMetrics(fixture)
assert.Nil(err)
//test
for _, metric := range parseUserJobMetrics(jms) {
assert.Positive(metric.allocCpu)
assert.Positive(metric.allocMemory)
}
}
func TestJobCollect(t *testing.T) {
assert := assert.New(t)
config := &Config{
pollLimit: 10,
traceConf: &TraceConfig{
sharedFetcher: MockJobInfoFetcher,
rate: 10,
},
cliOpts: &CliOpts{},
}
jc := NewJobsController(config)
jobChan := make(chan prometheus.Metric)
go func() {
jc.Collect(jobChan)
close(jobChan)
}()
jobMetrics := make([]prometheus.Metric, 0)
for metric, ok := <-jobChan; ok; metric, ok = <-jobChan {
t.Log(metric.Desc().String())
jobMetrics = append(jobMetrics, metric)
}
assert.NotEmpty(jobMetrics)
}
func TestJobCollect_Fallback(t *testing.T) {
assert := assert.New(t)
config := &Config{
pollLimit: 10,
traceConf: &TraceConfig{
sharedFetcher: &MockFetcher{fixture: "fixtures/squeue_fallback.txt"},
rate: 10,
},
cliOpts: &CliOpts{
fallback: true,
},
}
jc := NewJobsController(config)
jobChan := make(chan prometheus.Metric)
go func() {
jc.Collect(jobChan)
close(jobChan)
}()
jobMetrics := make([]prometheus.Metric, 0)
for metric, ok := <-jobChan; ok; metric, ok = <-jobChan {
t.Log(metric.Desc().String())
jobMetrics = append(jobMetrics, metric)
}
assert.NotEmpty(jobMetrics)
}
func TestParsePartitionJobMetrics(t *testing.T) {
assert := assert.New(t)
fixture, err := MockJobInfoFetcher.Fetch()
assert.Nil(err)
jms, err := parseJobMetrics(fixture)
assert.Nil(err)
partitionJobMetrics := parsePartitionJobMetrics(jms)
assert.Equal(float64(1), partitionJobMetrics["hw-l"].partitionState["RUNNING"])
}
func TestJobDescribe(t *testing.T) {
assert := assert.New(t)
ch := make(chan *prometheus.Desc)
config, err := NewConfig()
assert.Nil(err)
config.SetFetcher(MockJobInfoFetcher)
jc := NewJobsController(config)
jc.fetcher = MockJobInfoFetcher
go func() {
jc.Describe(ch)
close(ch)
}()
descs := make([]*prometheus.Desc, 0)
for desc, ok := <-ch; ok; desc, ok = <-ch {
descs = append(descs, desc)
}
assert.NotEmpty(descs)
}
func TestNAbleTimeJson(t *testing.T) {
assert := assert.New(t)
data := `"2023-09-21T14:31:11"`
var nat NAbleTime
err := nat.UnmarshalJSON([]byte(data))
assert.Nil(err)
assert.True(nat.Equal(time.Date(2023, 9, 21, 14, 31, 11, 0, time.UTC)))
}
func TestNAbleTimeJson_NA(t *testing.T) {
assert := assert.New(t)
data := `"N/A"`
var nat NAbleTime
err := nat.UnmarshalJSON([]byte(data))
assert.Nil(err)
assert.True(nat.Equal(time.Time{}))
}