Skip to content

Commit 389dd9f

Browse files
committed
TestAgent_Metrics_SSH
1 parent a51cde9 commit 389dd9f

File tree

1 file changed

+116
-3
lines changed

1 file changed

+116
-3
lines changed

agent/agent_test.go

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"github.com/google/uuid"
2828
"github.com/pion/udp"
2929
"github.com/pkg/sftp"
30+
"github.com/prometheus/client_golang/prometheus"
31+
promgo "github.com/prometheus/client_model/go"
3032
"github.com/spf13/afero"
3133
"github.com/stretchr/testify/assert"
3234
"github.com/stretchr/testify/require"
@@ -1724,7 +1726,7 @@ func (c closeFunc) Close() error {
17241726
return c()
17251727
}
17261728

1727-
func setupAgent(t *testing.T, metadata agentsdk.Manifest, ptyTimeout time.Duration) (
1729+
func setupAgent(t *testing.T, metadata agentsdk.Manifest, ptyTimeout time.Duration, opts ...func(agent.Options) agent.Options) (
17281730
*codersdk.WorkspaceAgentConn,
17291731
*client,
17301732
<-chan *agentsdk.Stats,
@@ -1749,12 +1751,19 @@ func setupAgent(t *testing.T, metadata agentsdk.Manifest, ptyTimeout time.Durati
17491751
statsChan: statsCh,
17501752
coordinator: coordinator,
17511753
}
1752-
closer := agent.New(agent.Options{
1754+
1755+
options := agent.Options{
17531756
Client: c,
17541757
Filesystem: fs,
17551758
Logger: logger.Named("agent"),
17561759
ReconnectingPTYTimeout: ptyTimeout,
1757-
})
1760+
}
1761+
1762+
for _, opt := range opts {
1763+
options = opt(options)
1764+
}
1765+
1766+
closer := agent.New(options)
17581767
t.Cleanup(func() {
17591768
_ = closer.Close()
17601769
})
@@ -1979,3 +1988,107 @@ func tempDirUnixSocket(t *testing.T) string {
19791988

19801989
return t.TempDir()
19811990
}
1991+
1992+
func TestAgent_Metrics_SSH(t *testing.T) {
1993+
t.Parallel()
1994+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
1995+
defer cancel()
1996+
1997+
registry := prometheus.NewRegistry()
1998+
1999+
//nolint:dogsled
2000+
conn, _, _, _, _ := setupAgent(t, agentsdk.Manifest{}, 0, func(o agent.Options) agent.Options {
2001+
o.PrometheusRegistry = registry
2002+
return o
2003+
})
2004+
2005+
sshClient, err := conn.SSHClient(ctx)
2006+
require.NoError(t, err)
2007+
defer sshClient.Close()
2008+
session, err := sshClient.NewSession()
2009+
require.NoError(t, err)
2010+
defer session.Close()
2011+
stdin, err := session.StdinPipe()
2012+
require.NoError(t, err)
2013+
err = session.Shell()
2014+
require.NoError(t, err)
2015+
2016+
expected := []agentsdk.AgentMetric{
2017+
{
2018+
Name: "agent_reconnecting_pty_connections_total",
2019+
Type: agentsdk.AgentMetricTypeCounter,
2020+
Value: 0,
2021+
},
2022+
{
2023+
Name: "agent_sessions_total",
2024+
Type: agentsdk.AgentMetricTypeCounter,
2025+
Value: 1,
2026+
Labels: []agentsdk.AgentMetricLabel{
2027+
{
2028+
Name: "magic_type",
2029+
Value: "ssh",
2030+
},
2031+
{
2032+
Name: "pty",
2033+
Value: "no",
2034+
},
2035+
},
2036+
},
2037+
{
2038+
Name: "agent_ssh_server_failed_connections_total",
2039+
Type: agentsdk.AgentMetricTypeCounter,
2040+
Value: 0,
2041+
},
2042+
{
2043+
Name: "agent_ssh_server_sftp_connections_total",
2044+
Type: agentsdk.AgentMetricTypeCounter,
2045+
Value: 0,
2046+
},
2047+
{
2048+
Name: "agent_ssh_server_sftp_server_errors_total",
2049+
Type: agentsdk.AgentMetricTypeCounter,
2050+
Value: 0,
2051+
},
2052+
}
2053+
2054+
var actual []*promgo.MetricFamily
2055+
assert.Eventually(t, func() bool {
2056+
actual, err = registry.Gather()
2057+
if err != nil {
2058+
return false
2059+
}
2060+
2061+
if len(expected) != len(actual) {
2062+
return false
2063+
}
2064+
2065+
return verifyCollectedMetrics(t, expected, actual)
2066+
}, testutil.WaitLong, testutil.IntervalFast)
2067+
2068+
require.Len(t, actual, len(expected))
2069+
verifyCollectedMetrics(t, expected, actual)
2070+
2071+
_ = stdin.Close()
2072+
err = session.Wait()
2073+
require.NoError(t, err)
2074+
}
2075+
2076+
func verifyCollectedMetrics(t *testing.T, expected []agentsdk.AgentMetric, actual []*promgo.MetricFamily) bool {
2077+
for i, e := range expected {
2078+
require.Equal(t, e.Name, actual[i].GetName())
2079+
require.Equal(t, string(e.Type), strings.ToLower(actual[i].GetType().String()))
2080+
2081+
for _, m := range actual[i].GetMetric() {
2082+
require.Equal(t, e.Value, m.Counter.GetValue())
2083+
2084+
if len(m.GetLabel()) > 0 {
2085+
for j, lbl := range m.GetLabel() {
2086+
require.Equal(t, e.Labels[j].Name, lbl.GetName())
2087+
require.Equal(t, e.Labels[j].Value, lbl.GetValue())
2088+
}
2089+
}
2090+
m.GetLabel()
2091+
}
2092+
}
2093+
return true
2094+
}

0 commit comments

Comments
 (0)