diff --git a/end-to-end/landscape_utils_test.go b/end-to-end/landscape_utils_test.go new file mode 100644 index 000000000..2a500aae9 --- /dev/null +++ b/end-to-end/landscape_utils_test.go @@ -0,0 +1,90 @@ +package endtoend_test + +import ( + "context" + "crypto/tls" + "net" + "os" + "testing" + "time" + + landscapeapi "github.com/canonical/landscape-hostagent-api" + "github.com/canonical/ubuntu-pro-for-windows/mocks/landscape/landscapemockservice" + "github.com/stretchr/testify/require" + "github.com/ubuntu/gowsl" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials" +) + +// setUpLandscapeMock sets up the Landscape mock server (but does not start it). It also sets up the registry so the Windows Agent can connect to it. +// +//nolint:revive // Context goes after testing.T +func setUpLandscapeMock(t *testing.T, ctx context.Context, addr string) (lis net.Listener, server *grpc.Server, service *landscapemockservice.Service) { + t.Helper() + + var cfg net.ListenConfig + lis, err := cfg.Listen(ctx, "tcp", addr) + require.NoError(t, err, "Setup: can't listen") + + serverCert, err := tls.LoadX509KeyPair("testdata/cert.pem", "testdata/key.pem") + require.NoError(t, err, "Setup: could not load Landscape mock server credentials") + + config := &tls.Config{ + Certificates: []tls.Certificate{serverCert}, + ClientAuth: tls.NoClientCert, + MinVersion: tls.VersionTLS12, + } + + server = grpc.NewServer(grpc.Creds(credentials.NewTLS(config))) + service = landscapemockservice.New() + landscapeapi.RegisterLandscapeHostAgentServer(server, service) + + out, err := os.ReadFile("testdata/landscape.conf") + require.NoError(t, err, "could not read landscape configuration file") + + writeUbuntuProRegistry(t, "LandscapeClientConfig", string(out)) + writeUbuntuProRegistry(t, "LandscapeAgentURL", lis.Addr().String()) + + return lis, server, service +} + +func validateLandscape(t *testing.T, ctx context.Context, landscapeService *landscapemockservice.Service, wantToken string, wantDistro gowsl.Distro) { + t.Helper() + + require.Len(t, landscapeService.Hosts(), 1, "Landscape should have had one connection") + var info landscapemockservice.HostInfo + for _, inf := range landscapeService.Hosts() { + info = inf + } + + // Validate token + require.Equal(t, wantToken, info.Token, "Landscape did not receive the right pro token") + + // Validate distro + require.Len(t, info.Instances, 1, "Landscape did not receive the right number of distros") + require.Equal(t, wantDistro.Name(), info.Instances[0].ID, "Landscape did not receive the right distro name from the agent") + + // Validate hostname + hostname, err := os.Hostname() + require.NoError(t, err, "could not test machine's hostname") + require.Equal(t, hostname, info.Hostname, "Landscape did not receive the right hostname from the agent") + + // Validate a Landscape command (only if there is any distro) + err = landscapeService.SendCommand(ctx, + info.UID, + &landscapeapi.Command{ + Cmd: &landscapeapi.Command_Uninstall_{ + Uninstall: &landscapeapi.Command_Uninstall{ + Id: wantDistro.Name(), + }, + }, + }) + require.NoError(t, err, "could not send an uninstall command via Landscape") + require.Eventually(t, func() bool { + reg, err := wantDistro.IsRegistered() + if err != nil { + t.Logf("While waiting for Landscape uninstall command to complete: %v", err) + } + return !reg + }, time.Minute, time.Second, "Landcape uninstall command never took effect") +} diff --git a/end-to-end/utils_test.go b/end-to-end/utils_test.go index f1df2bb47..568d8c173 100644 --- a/end-to-end/utils_test.go +++ b/end-to-end/utils_test.go @@ -3,13 +3,11 @@ package endtoend_test import ( "bytes" "context" - "crypto/tls" "encoding/json" "errors" "fmt" "io/fs" "log" - "net" "os" "os/exec" "path/filepath" @@ -17,15 +15,11 @@ import ( "testing" "time" - landscapeapi "github.com/canonical/landscape-hostagent-api" "github.com/canonical/ubuntu-pro-for-windows/common/wsltestutils" - "github.com/canonical/ubuntu-pro-for-windows/mocks/landscape/landscapemockservice" "github.com/stretchr/testify/require" "github.com/ubuntu/gowsl" wsl "github.com/ubuntu/gowsl" "golang.org/x/sys/windows/registry" - "google.golang.org/grpc" - "google.golang.org/grpc/credentials" ) func testSetup(t *testing.T) { @@ -188,79 +182,6 @@ func logWslProServiceJournal(t *testing.T, ctx context.Context, d wsl.Distro) { t.Logf("wsl-pro-service logs:\n%s\n", out) } -// setUpLandscapeMock sets up the Landscape mock server (but does not start it). It also sets up the registry so the Windows Agent can connect to it. -// -//nolint:revive // Context goes after testing.T -func setUpLandscapeMock(t *testing.T, ctx context.Context, addr string) (lis net.Listener, server *grpc.Server, service *landscapemockservice.Service) { - t.Helper() - - var cfg net.ListenConfig - lis, err := cfg.Listen(ctx, "tcp", addr) - require.NoError(t, err, "Setup: can't listen") - - serverCert, err := tls.LoadX509KeyPair("testdata/cert.pem", "testdata/key.pem") - require.NoError(t, err, "Setup: could not load Landscape mock server credentials") - - config := &tls.Config{ - Certificates: []tls.Certificate{serverCert}, - ClientAuth: tls.NoClientCert, - MinVersion: tls.VersionTLS12, - } - - server = grpc.NewServer(grpc.Creds(credentials.NewTLS(config))) - service = landscapemockservice.New() - landscapeapi.RegisterLandscapeHostAgentServer(server, service) - - out, err := os.ReadFile("testdata/landscape.conf") - require.NoError(t, err, "could not read landscape configuration file") - - writeUbuntuProRegistry(t, "LandscapeClientConfig", string(out)) - writeUbuntuProRegistry(t, "LandscapeAgentURL", lis.Addr().String()) - - return lis, server, service -} - -func validateLandscape(t *testing.T, ctx context.Context, landscapeService *landscapemockservice.Service, wantToken string, wantDistro gowsl.Distro) { - t.Helper() - - require.Len(t, landscapeService.Hosts(), 1, "Landscape should have had one connection") - var info landscapemockservice.HostInfo - for _, inf := range landscapeService.Hosts() { - info = inf - } - - // Validate token - require.Equal(t, wantToken, info.Token, "Landscape did not receive the right pro token") - - // Validate distro - require.Len(t, info.Instances, 1, "Landscape did not receive the right number of distros") - require.Equal(t, wantDistro.Name(), info.Instances[0].ID, "Landscape did not receive the right distro name from the agent") - - // Validate hostname - hostname, err := os.Hostname() - require.NoError(t, err, "could not test machine's hostname") - require.Equal(t, hostname, info.Hostname, "Landscape did not receive the right hostname from the agent") - - // Validate a Landscape command (only if there is any distro) - err = landscapeService.SendCommand(ctx, - info.UID, - &landscapeapi.Command{ - Cmd: &landscapeapi.Command_Uninstall_{ - Uninstall: &landscapeapi.Command_Uninstall{ - Id: wantDistro.Name(), - }, - }, - }) - require.NoError(t, err, "could not send an uninstall command via Landscape") - require.Eventually(t, func() bool { - reg, err := wantDistro.IsRegistered() - if err != nil { - t.Logf("While waiting for Landscape uninstall command to complete: %v", err) - } - return !reg - }, time.Minute, time.Second, "Landcape uninstall command never took effect") -} - func writeUbuntuProRegistry(t *testing.T, field string, value string) { t.Helper()