Skip to content

Commit

Permalink
Move Landscape helpers to their own file
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardGomezEscandell committed Oct 19, 2023
1 parent 16ab348 commit 9a77e3c
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 79 deletions.
90 changes: 90 additions & 0 deletions end-to-end/landscape_utils_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
79 changes: 0 additions & 79 deletions end-to-end/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,23 @@ package endtoend_test
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/fs"
"log"
"net"
"os"
"os/exec"
"path/filepath"
"strings"
"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) {
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 9a77e3c

Please sign in to comment.