Skip to content

Commit

Permalink
Landscape tests: Put certificates in a temp directory
Browse files Browse the repository at this point in the history
This way we prevent test coupling.
  • Loading branch information
EduardGomezEscandell committed Nov 14, 2023
1 parent b1a1910 commit 4c021c9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
39 changes: 19 additions & 20 deletions windows-agent/internal/proservices/landscape/landscape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,9 @@ func TestConnect(t *testing.T) {
t.Parallel()
}

certPath := filepath.Join(golden.TestFamilyPath(t), "certificates")
t.Cleanup(func() {
if err := os.RemoveAll(certPath); err != nil {
log.Printf("Failed to remove certificates directory: %v", err)
}
})

err := os.MkdirAll(certPath, 0600)
require.NoError(t, err, "Setup: could not create certificates directory")
certPath := t.TempDir()

err = generateTempCertificate(certPath)
err := generateTempCertificate(certPath)
require.NoError(t, err, "Setup: could not generate certificates")

err = os.WriteFile(filepath.Join(certPath, "bad-certificate.pem"), []byte("This is not a valid certificate."), 0600)
Expand Down Expand Up @@ -113,7 +105,12 @@ func TestConnect(t *testing.T) {
ctx = wsl.WithMock(ctx, wslmock.New())
}

lis, server, mockService := setUpLandscapeMock(t, ctx, "localhost:", tc.requireCertificate)
p := ""
if tc.requireCertificate {
p = certPath
}

lis, server, mockService := setUpLandscapeMock(t, ctx, "localhost:", p)
defer lis.Close()

conf := &mockConfig{
Expand Down Expand Up @@ -145,6 +142,8 @@ func TestConnect(t *testing.T) {
conf.landscapeClientConfig = fmt.Sprintf("[host]\nurl=%q\n\n%s", lis.Addr(), conf.landscapeClientConfig)
}

conf.landscapeClientConfig = strings.ReplaceAll(conf.landscapeClientConfig, "%CERTPATH%", certPath)

if !tc.serverNotAvailable {
//nolint:errcheck // We don't care about these errors
go server.Serve(lis)
Expand Down Expand Up @@ -292,7 +291,7 @@ func TestSendUpdatedInfo(t *testing.T) {
ctx = wsl.WithMock(ctx, mock)
}

lis, server, mockService := setUpLandscapeMock(t, ctx, "localhost:", false)
lis, server, mockService := setUpLandscapeMock(t, ctx, "localhost:", "")

conf := &mockConfig{
proToken: "TOKEN",
Expand Down Expand Up @@ -458,7 +457,7 @@ func TestAutoReconnection(t *testing.T) {
ctx = wsl.WithMock(ctx, mock)
}

lis, server, mockService := setUpLandscapeMock(t, ctx, "localhost:", false)
lis, server, mockService := setUpLandscapeMock(t, ctx, "localhost:", "")
defer lis.Close()
defer server.Stop()

Expand Down Expand Up @@ -510,7 +509,7 @@ func TestAutoReconnection(t *testing.T) {
}, 5*time.Second, 100*time.Millisecond, "Client should have disconnected after the server is stopped")

// Restart server at the same address
lis, server, _ = setUpLandscapeMock(t, ctx, lis.Addr().String(), false)
lis, server, _ = setUpLandscapeMock(t, ctx, lis.Addr().String(), "")
defer lis.Close()

//nolint:errcheck // We don't care
Expand Down Expand Up @@ -591,7 +590,7 @@ func TestReceiveCommands(t *testing.T) {
t.Skip("This test can only run with the mock")
}

lis, server, service := setUpLandscapeMock(t, ctx, "localhost:", false)
lis, server, service := setUpLandscapeMock(t, ctx, "localhost:", "")
defer lis.Close()

//nolint:errcheck // We don't care about these errors
Expand Down Expand Up @@ -831,19 +830,19 @@ func isAppxInstalled(t *testing.T, appxPackage string) bool {
}

//nolint:revive // Context goes after testing.T
func setUpLandscapeMock(t *testing.T, ctx context.Context, addr string, requireCertificate bool) (lis net.Listener, server *grpc.Server, service *landscapemockservice.Service) {
func setUpLandscapeMock(t *testing.T, ctx context.Context, addr string, certPath 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")

var opts []grpc.ServerOption
if requireCertificate {
certPath := filepath.Join(golden.TestFamilyPath(t), "certificates/cert.pem")
keyPath := filepath.Join(golden.TestFamilyPath(t), "certificates/key.pem")
if certPath != "" {
cert := filepath.Join(certPath, "cert.pem")
key := filepath.Join(certPath, "key.pem")

serverCert, err := tls.LoadX509KeyPair(certPath, keyPath)
serverCert, err := tls.LoadX509KeyPair(cert, key)
require.NoError(t, err, "Setup: could not load Landscape mock server credentials")

config := &tls.Config{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[client]
ssl_public_key = testdata/TestConnect/certificates/bad-certificate.pem
ssl_public_key = %CERTPATH%/bad-certificate.pem
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[client]
ssl_public_key = testdata/TestConnect/certificates/cert.pem
ssl_public_key = %CERTPATH%/cert.pem

0 comments on commit 4c021c9

Please sign in to comment.