-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Landscape helpers to their own file
- Loading branch information
1 parent
16ab348
commit 9a77e3c
Showing
2 changed files
with
90 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters