Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] add unit tests for the opampsupervisor #34812

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
add server tests
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
  • Loading branch information
bacherfl committed Aug 22, 2024
commit 55f901c096d6497d457f1e7aa7a28697d7c2da96
81 changes: 81 additions & 0 deletions cmd/opampsupervisor/supervisor/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package supervisor

import (
"context"
"github.com/open-telemetry/opamp-go/protobufs"
serverTypes "github.com/open-telemetry/opamp-go/server/types"
"github.com/stretchr/testify/require"
"net/http"
"testing"
)

func Test_flattenedSettings_toServerSettings(t *testing.T) {
fs := flattenedSettings{
endpoint: "localhost",
}

serverSettings := fs.toServerSettings()

require.Equal(t, "localhost", serverSettings.ListenEndpoint)
require.NotNil(t, serverSettings.Callbacks)
}

func Test_flattenedSettings_OnConnecting(t *testing.T) {
t.Run("accept connection", func(t *testing.T) {
onConnectingFuncCalled := false
fs := flattenedSettings{
onConnectingFunc: func(request *http.Request) (shouldConnect bool, rejectStatusCode int) {
onConnectingFuncCalled = true
return true, 0
},
}

cr := fs.OnConnecting(&http.Request{})

require.True(t, onConnectingFuncCalled)
require.True(t, cr.Accept)
require.NotNil(t, cr.ConnectionCallbacks)
})
t.Run("do not accept connection", func(t *testing.T) {
onConnectingFuncCalled := false
fs := flattenedSettings{
onConnectingFunc: func(request *http.Request) (shouldConnect bool, rejectStatusCode int) {
onConnectingFuncCalled = true
return false, 500
},
}

cr := fs.OnConnecting(&http.Request{})

require.True(t, onConnectingFuncCalled)
require.False(t, cr.Accept)
require.Equal(t, 500, cr.HTTPStatusCode)
})
}

func Test_flattenedSettings_OnMessage(t *testing.T) {
onMessageFuncCalled := false
fs := flattenedSettings{
onMessageFunc: func(conn serverTypes.Connection, message *protobufs.AgentToServer) {
onMessageFuncCalled = true
},
}

sta := fs.OnMessage(context.TODO(), &mockConn{}, &protobufs.AgentToServer{})

require.True(t, onMessageFuncCalled)
require.NotNil(t, sta)
}

func Test_flattenedSettings_OnConnectionClose(t *testing.T) {
onConnectionCloseFuncCalled := false
fs := flattenedSettings{
onConnectionCloseFunc: func(conn serverTypes.Connection) {
onConnectionCloseFuncCalled = true
},
}

fs.OnConnectionClose(&mockConn{})

require.True(t, onConnectionCloseFuncCalled)
}
Loading