Skip to content

Commit

Permalink
Landscape tests: switch to templates
Browse files Browse the repository at this point in the history
We were modifying the fixtures too much:
- find and replace for certificate
- appending the hostagent URL section

This meant that the config in the fixture bore little similarity to
the one being used in the end. I switched to templates for the sake of
clarity. Now the fixtures look much more like the final config.
  • Loading branch information
EduardGomezEscandell committed Nov 14, 2023
1 parent f29d7ef commit 48e3800
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 22 deletions.
56 changes: 38 additions & 18 deletions windows-agent/internal/proservices/landscape/landscape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strings"
"sync"
"testing"
"text/template"
"time"

landscapeapi "github.com/canonical/landscape-hostagent-api"
Expand Down Expand Up @@ -47,6 +48,11 @@ func TestMain(m *testing.M) {
defer os.Exit(exit)
}

const defaultLandscapeConfig = `
[host]
url = "{{ .HostURL }}"
`

func TestConnect(t *testing.T) {
if wsl.MockAvailable() {
t.Parallel()
Expand All @@ -67,8 +73,7 @@ func TestConnect(t *testing.T) {
landscapeUIDReadErr bool
landscapeUIDWriteErr bool

noLandscapeURL bool
tokenErr bool
tokenErr bool

requireCertificate bool
breakLandscapeClientConfig bool
Expand All @@ -85,7 +90,7 @@ func TestConnect(t *testing.T) {
"Success with an SSL certificate": {requireCertificate: true},

"Error when the context is cancelled before Connected": {precancelContext: true, wantErr: true},
"Error when the landscape URL cannot be retrieved": {noLandscapeURL: true, wantErr: true},
"Error when the landscape URL cannot be retrieved": {wantErr: true},
"Error when the landscape UID cannot be retrieved": {landscapeUIDReadErr: true, wantErr: true},
"Error when the landscape UID cannot be stored": {landscapeUIDWriteErr: true, wantErr: true},
"Error when the server cannot be reached": {serverNotAvailable: true, wantErr: true},
Expand Down Expand Up @@ -129,20 +134,16 @@ func TestConnect(t *testing.T) {
landscapeAgentUID: tc.uid,
}

out, err := os.ReadFile(filepath.Join(golden.TestFixturePath(t), "landscape.conf"))
if errors.Is(err, os.ErrNotExist) {
// This fixture is not compulsory
out = []byte{}
err = nil
}
require.NoError(t, err, "Setup: could not load landscape config")

conf.landscapeClientConfig = string(out)
if !tc.noLandscapeURL {
conf.landscapeClientConfig = fmt.Sprintf("[host]\nurl=%q\n\n%s", lis.Addr(), conf.landscapeClientConfig)
lconf := defaultLandscapeConfig
if fixture, err := os.ReadFile(filepath.Join(golden.TestFixturePath(t), "landscape.conf")); err != nil {
require.ErrorIs(t, err, os.ErrNotExist, "Setup: could not load landscape config")
// Fixture does not exist: use base Landcape confing
} else {
// Fixture exists: override the Landscape config
lconf = string(fixture)
}

conf.landscapeClientConfig = strings.ReplaceAll(conf.landscapeClientConfig, "%CERTPATH%", certPath)
conf.landscapeClientConfig = executeLandscapeConfigTemplate(t, lconf, certPath, lis.Addr())

if !tc.serverNotAvailable {
//nolint:errcheck // We don't care about these errors
Expand Down Expand Up @@ -295,7 +296,7 @@ func TestSendUpdatedInfo(t *testing.T) {

conf := &mockConfig{
proToken: "TOKEN",
landscapeClientConfig: fmt.Sprintf("[host]\nurl=%q\n", lis.Addr()),
landscapeClientConfig: executeLandscapeConfigTemplate(t, defaultLandscapeConfig, "", lis.Addr()),
}

//nolint:errcheck // We don't care about these errors
Expand Down Expand Up @@ -463,7 +464,7 @@ func TestAutoReconnection(t *testing.T) {

conf := &mockConfig{
proToken: "TOKEN",
landscapeClientConfig: fmt.Sprintf("[host]\nurl=%q\n", lis.Addr()),
landscapeClientConfig: executeLandscapeConfigTemplate(t, defaultLandscapeConfig, "", lis.Addr()),
}

db, err := database.New(ctx, t.TempDir(), conf)
Expand Down Expand Up @@ -599,7 +600,7 @@ func TestReceiveCommands(t *testing.T) {

conf := &mockConfig{
proToken: "TOKEN",
landscapeClientConfig: fmt.Sprintf("[host]\nurl=%q\n", lis.Addr()),
landscapeClientConfig: executeLandscapeConfigTemplate(t, defaultLandscapeConfig, "", lis.Addr()),
}

db, err := database.New(ctx, t.TempDir(), conf)
Expand Down Expand Up @@ -662,6 +663,25 @@ func TestReceiveCommands(t *testing.T) {
}
}

func executeLandscapeConfigTemplate(t *testing.T, in string, certPath string, url net.Addr) string {
t.Helper()

tmpl := template.Must(template.New(t.Name()).Parse(in))

data := struct {
CertPath, HostURL string
}{
CertPath: certPath,
HostURL: url.String(),
}

out := bytes.Buffer{}
err := tmpl.Execute(&out, data)
require.NoError(t, err, "Setup: could not generate Landscape config from template")

return out.String()
}

const (
testAppx = "CanonicalGroupLimited.Ubuntu22.04LTS" // The name of the Appx
testDistroAppx = "Ubuntu-22.04" // The name used in `wsl --install <DISTRO>`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[client]
hello=world
tags=wsl
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[client]
ssl_public_key = testdata/TestConnect/this_path_does_not_exist/certificate.pem
ssl_public_key = testdata/TestConnect/this_path_does_not_exist/certificate.pem

[host]
url = {{ .HostURL }}
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[client]
ssl_public_key = %CERTPATH%/bad-certificate.pem
ssl_public_key = {{ .CertPath }}/bad-certificate.pem

[host]
url = {{ .HostURL }}
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[client]
ssl_public_key = %CERTPATH%/cert.pem
ssl_public_key = {{ .CertPath }}/cert.pem

[host]
url = {{ .HostURL }}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[client]
hello=world
tags=wsl
tags=wsl

[host]
url = {{ .HostURL }}

0 comments on commit 48e3800

Please sign in to comment.