mirror of
https://github.com/coder/coder.git
synced 2026-06-06 06:28:20 +00:00
bc609d0056
As part of the new resources monitoring logic - more specifically for OOM & OOD Notifications , we need to update the AgentAPI , and the agents logic. This PR aims to do it, and more specifically : We are updating the AgentAPI & TailnetAPI to version 24 to add two new methods in the AgentAPI : - One method to fetch the resources monitoring configuration - One method to push the datapoints for the resources monitoring. Also, this PR adds a new logic on the agent side, with a routine running and ticking - fetching the resources usage each time , but also storing it in a FIFO like queue. Finally, this PR fixes a problem we had with RBAC logic on the resources monitoring model, applying the same logic than we have for similar entities.
93 lines
2.0 KiB
Go
93 lines
2.0 KiB
Go
package resourcesmonitor_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/agent/proto/resourcesmonitor"
|
|
)
|
|
|
|
func TestResourceMonitorQueue(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
pushCount int
|
|
expected []resourcesmonitor.Datapoint
|
|
}{
|
|
{
|
|
name: "Push zero",
|
|
pushCount: 0,
|
|
expected: []resourcesmonitor.Datapoint{},
|
|
},
|
|
{
|
|
name: "Push less than capacity",
|
|
pushCount: 3,
|
|
expected: []resourcesmonitor.Datapoint{
|
|
{Memory: &resourcesmonitor.MemoryDatapoint{Total: 1, Used: 1}},
|
|
{Memory: &resourcesmonitor.MemoryDatapoint{Total: 2, Used: 2}},
|
|
{Memory: &resourcesmonitor.MemoryDatapoint{Total: 3, Used: 3}},
|
|
},
|
|
},
|
|
{
|
|
name: "Push exactly capacity",
|
|
pushCount: 20,
|
|
expected: func() []resourcesmonitor.Datapoint {
|
|
var result []resourcesmonitor.Datapoint
|
|
for i := 1; i <= 20; i++ {
|
|
result = append(result, resourcesmonitor.Datapoint{
|
|
Memory: &resourcesmonitor.MemoryDatapoint{
|
|
Total: int64(i),
|
|
Used: int64(i),
|
|
},
|
|
})
|
|
}
|
|
return result
|
|
}(),
|
|
},
|
|
{
|
|
name: "Push more than capacity",
|
|
pushCount: 25,
|
|
expected: func() []resourcesmonitor.Datapoint {
|
|
var result []resourcesmonitor.Datapoint
|
|
for i := 6; i <= 25; i++ {
|
|
result = append(result, resourcesmonitor.Datapoint{
|
|
Memory: &resourcesmonitor.MemoryDatapoint{
|
|
Total: int64(i),
|
|
Used: int64(i),
|
|
},
|
|
})
|
|
}
|
|
return result
|
|
}(),
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
queue := resourcesmonitor.NewQueue(20)
|
|
for i := 1; i <= tt.pushCount; i++ {
|
|
queue.Push(resourcesmonitor.Datapoint{
|
|
Memory: &resourcesmonitor.MemoryDatapoint{
|
|
Total: int64(i),
|
|
Used: int64(i),
|
|
},
|
|
})
|
|
}
|
|
|
|
if tt.pushCount < 20 {
|
|
require.False(t, queue.IsFull())
|
|
} else {
|
|
require.True(t, queue.IsFull())
|
|
require.Equal(t, 20, len(queue.Items()))
|
|
}
|
|
|
|
require.EqualValues(t, tt.expected, queue.Items())
|
|
})
|
|
}
|
|
}
|