From 48e3800a04622c9de7b143d06d87ac7cb4d1cca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edu=20G=C3=B3mez=20Escandell?= Date: Tue, 14 Nov 2023 09:54:00 +0100 Subject: [PATCH] Landscape tests: switch to templates 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. --- .../proservices/landscape/landscape_test.go | 56 +++++++++++++------ .../landscape.conf | 3 + .../landscape.conf | 5 +- .../landscape.conf | 5 +- .../landscape.conf | 5 +- .../landscape.conf | 5 +- 6 files changed, 57 insertions(+), 22 deletions(-) create mode 100644 windows-agent/internal/proservices/landscape/testdata/TestConnect/error_when_the_landscape_url_cannot_be_retrieved/landscape.conf diff --git a/windows-agent/internal/proservices/landscape/landscape_test.go b/windows-agent/internal/proservices/landscape/landscape_test.go index 334c9ad66..bb4818e62 100644 --- a/windows-agent/internal/proservices/landscape/landscape_test.go +++ b/windows-agent/internal/proservices/landscape/landscape_test.go @@ -20,6 +20,7 @@ import ( "strings" "sync" "testing" + "text/template" "time" landscapeapi "github.com/canonical/landscape-hostagent-api" @@ -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() @@ -67,8 +73,7 @@ func TestConnect(t *testing.T) { landscapeUIDReadErr bool landscapeUIDWriteErr bool - noLandscapeURL bool - tokenErr bool + tokenErr bool requireCertificate bool breakLandscapeClientConfig bool @@ -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}, @@ -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 @@ -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 @@ -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) @@ -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) @@ -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 ` diff --git a/windows-agent/internal/proservices/landscape/testdata/TestConnect/error_when_the_landscape_url_cannot_be_retrieved/landscape.conf b/windows-agent/internal/proservices/landscape/testdata/TestConnect/error_when_the_landscape_url_cannot_be_retrieved/landscape.conf new file mode 100644 index 000000000..a8a807a95 --- /dev/null +++ b/windows-agent/internal/proservices/landscape/testdata/TestConnect/error_when_the_landscape_url_cannot_be_retrieved/landscape.conf @@ -0,0 +1,3 @@ +[client] +hello=world +tags=wsl \ No newline at end of file diff --git a/windows-agent/internal/proservices/landscape/testdata/TestConnect/error_when_the_ssl_certificate_cannot_be_read/landscape.conf b/windows-agent/internal/proservices/landscape/testdata/TestConnect/error_when_the_ssl_certificate_cannot_be_read/landscape.conf index e3d102467..a931480e2 100644 --- a/windows-agent/internal/proservices/landscape/testdata/TestConnect/error_when_the_ssl_certificate_cannot_be_read/landscape.conf +++ b/windows-agent/internal/proservices/landscape/testdata/TestConnect/error_when_the_ssl_certificate_cannot_be_read/landscape.conf @@ -1,2 +1,5 @@ [client] -ssl_public_key = testdata/TestConnect/this_path_does_not_exist/certificate.pem \ No newline at end of file +ssl_public_key = testdata/TestConnect/this_path_does_not_exist/certificate.pem + +[host] +url = {{ .HostURL }} \ No newline at end of file diff --git a/windows-agent/internal/proservices/landscape/testdata/TestConnect/error_when_the_ssl_certificate_is_not_valid/landscape.conf b/windows-agent/internal/proservices/landscape/testdata/TestConnect/error_when_the_ssl_certificate_is_not_valid/landscape.conf index 8a4f36660..20ee5ea4d 100644 --- a/windows-agent/internal/proservices/landscape/testdata/TestConnect/error_when_the_ssl_certificate_is_not_valid/landscape.conf +++ b/windows-agent/internal/proservices/landscape/testdata/TestConnect/error_when_the_ssl_certificate_is_not_valid/landscape.conf @@ -1,2 +1,5 @@ [client] -ssl_public_key = %CERTPATH%/bad-certificate.pem \ No newline at end of file +ssl_public_key = {{ .CertPath }}/bad-certificate.pem + +[host] +url = {{ .HostURL }} \ No newline at end of file diff --git a/windows-agent/internal/proservices/landscape/testdata/TestConnect/success_with_an_ssl_certificate/landscape.conf b/windows-agent/internal/proservices/landscape/testdata/TestConnect/success_with_an_ssl_certificate/landscape.conf index 2b83a48a2..dd3fcbdec 100644 --- a/windows-agent/internal/proservices/landscape/testdata/TestConnect/success_with_an_ssl_certificate/landscape.conf +++ b/windows-agent/internal/proservices/landscape/testdata/TestConnect/success_with_an_ssl_certificate/landscape.conf @@ -1,2 +1,5 @@ [client] -ssl_public_key = %CERTPATH%/cert.pem \ No newline at end of file +ssl_public_key = {{ .CertPath }}/cert.pem + +[host] +url = {{ .HostURL }} \ No newline at end of file diff --git a/windows-agent/internal/proservices/landscape/testdata/TestConnect/success_with_non-empty_config/landscape.conf b/windows-agent/internal/proservices/landscape/testdata/TestConnect/success_with_non-empty_config/landscape.conf index a8a807a95..fa8035de9 100644 --- a/windows-agent/internal/proservices/landscape/testdata/TestConnect/success_with_non-empty_config/landscape.conf +++ b/windows-agent/internal/proservices/landscape/testdata/TestConnect/success_with_non-empty_config/landscape.conf @@ -1,3 +1,6 @@ [client] hello=world -tags=wsl \ No newline at end of file +tags=wsl + +[host] +url = {{ .HostURL }} \ No newline at end of file