mirror of
https://github.com/edgelesssys/constellation.git
synced 2024-09-07 18:12:01 +00:00
operator: fix gRPC dialing over UDS (#3201)
* operator: add test for gRPC connection over UDS
This commit is contained in:
parent
e0c5acf2f3
commit
3db3db3bf2
@ -1,4 +1,5 @@
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_library")
|
||||
load("//bazel/go:go_test.bzl", "go_test")
|
||||
|
||||
go_library(
|
||||
name = "upgrade",
|
||||
@ -13,3 +14,15 @@ go_library(
|
||||
"@org_golang_google_grpc//credentials/insecure",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "upgrade_test",
|
||||
srcs = ["upgrade_test.go"],
|
||||
embed = [":upgrade"],
|
||||
deps = [
|
||||
"//internal/versions/components",
|
||||
"//upgrade-agent/upgradeproto",
|
||||
"@com_github_stretchr_testify//require",
|
||||
"@org_golang_google_grpc//:grpc",
|
||||
],
|
||||
)
|
||||
|
@ -21,24 +21,25 @@ import (
|
||||
|
||||
// Client is a client for the upgrade agent.
|
||||
type Client struct {
|
||||
addr string
|
||||
dialer Dialer
|
||||
}
|
||||
|
||||
// NewClient creates a new upgrade agent client.
|
||||
// NewClient creates a new upgrade agent client connecting to the default upgrade-agent Unix socket.
|
||||
func NewClient() *Client {
|
||||
return newClientWithAddress(mainconstants.UpgradeAgentMountPath)
|
||||
}
|
||||
|
||||
func newClientWithAddress(addr string) *Client {
|
||||
return &Client{
|
||||
addr: "unix:" + addr,
|
||||
dialer: &net.Dialer{},
|
||||
}
|
||||
}
|
||||
|
||||
// Upgrade upgrades the Constellation node to the given Kubernetes version.
|
||||
func (c *Client) Upgrade(ctx context.Context, kubernetesComponents components.Components, WantedKubernetesVersion string) error {
|
||||
conn, err := grpc.NewClient(mainconstants.UpgradeAgentMountPath, grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
grpc.WithContextDialer(
|
||||
func(ctx context.Context, addr string) (net.Conn, error) {
|
||||
return c.dialer.DialContext(ctx, "unix", addr)
|
||||
},
|
||||
))
|
||||
conn, err := grpc.NewClient(c.addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to dial: %w", err)
|
||||
}
|
||||
|
@ -0,0 +1,52 @@
|
||||
/*
|
||||
Copyright (c) Edgeless Systems GmbH
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package upgrade
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/edgelesssys/constellation/v2/internal/versions/components"
|
||||
"github.com/edgelesssys/constellation/v2/upgrade-agent/upgradeproto"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// TestGRPCDialer is a regression test to ensure the upgrade client can connect to a UDS.
|
||||
func TestGRPCDialer(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
dir := t.TempDir()
|
||||
sockAddr := filepath.Join(dir, "test.socket")
|
||||
|
||||
upgradeAgent := &fakeUpgradeAgent{}
|
||||
grpcServer := grpc.NewServer()
|
||||
upgradeproto.RegisterUpdateServer(grpcServer, upgradeAgent)
|
||||
|
||||
listener, err := net.Listen("unix", sockAddr)
|
||||
require.NoError(err)
|
||||
go grpcServer.Serve(listener)
|
||||
t.Cleanup(grpcServer.Stop)
|
||||
|
||||
fileInfo, err := os.Stat(sockAddr)
|
||||
require.NoError(err)
|
||||
require.Equal(os.ModeSocket, fileInfo.Mode()&os.ModeType)
|
||||
|
||||
upgradeClient := newClientWithAddress(sockAddr)
|
||||
require.NoError(upgradeClient.Upgrade(context.Background(), []*components.Component{}, "v1.29.6"))
|
||||
}
|
||||
|
||||
type fakeUpgradeAgent struct {
|
||||
upgradeproto.UnimplementedUpdateServer
|
||||
}
|
||||
|
||||
func (s *fakeUpgradeAgent) ExecuteUpdate(_ context.Context, _ *upgradeproto.ExecuteUpdateRequest) (*upgradeproto.ExecuteUpdateResponse, error) {
|
||||
return &upgradeproto.ExecuteUpdateResponse{}, nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user