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 4c021c9 commit a1bea40
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 16 deletions.
44 changes: 32 additions & 12 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 Down Expand Up @@ -129,20 +135,15 @@ func TestConnect(t *testing.T) {
landscapeAgentUID: tc.uid,
}

out, err := os.ReadFile(filepath.Join(golden.TestFixturePath(t), "landscape.conf"))
in, err := os.ReadFile(filepath.Join(golden.TestFixturePath(t), "landscape.conf"))
if errors.Is(err, os.ErrNotExist) {
// This fixture is not compulsory
out = []byte{}
// Use the default conf for testing
in = []byte(defaultLandscapeConfig)
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)
}

conf.landscapeClientConfig = strings.ReplaceAll(conf.landscapeClientConfig, "%CERTPATH%", certPath)
conf.landscapeClientConfig = executeLandscapeConfigTemplate(t, string(in), 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 a1bea40

Please sign in to comment.