Skip to content

Commit

Permalink
AET-191 Fix e2e tests to be able to run it in CI (#18224)
Browse files Browse the repository at this point in the history
* Try to run test in CI

* Try to run test in CI

* Test

* Read ssh key from env vars and use it in ssh client

* remove useless []byte conversion

* remove tests used for debug
  • Loading branch information
KevinFairise2 authored Jul 21, 2023
1 parent 410f17f commit 9be0c94
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
8 changes: 4 additions & 4 deletions test/new-e2e/utils/clients/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// GetSSHClient returns an ssh Client for the specified host
func GetSSHClient(user, host, privateKey string, retryInterval time.Duration, maxRetries uint64) (client *ssh.Client, session *ssh.Session, err error) {
func GetSSHClient(user, host string, privateKey []byte, retryInterval time.Duration, maxRetries uint64) (client *ssh.Client, session *ssh.Session, err error) {
err = backoff.Retry(func() error {
client, session, err = getSSHClient(user, host, privateKey)
return err
Expand All @@ -26,11 +26,11 @@ func GetSSHClient(user, host, privateKey string, retryInterval time.Duration, ma
return
}

func getSSHClient(user, host, privateKey string) (*ssh.Client, *ssh.Session, error) {
func getSSHClient(user, host string, privateKey []byte) (*ssh.Client, *ssh.Session, error) {
var auth ssh.AuthMethod

if privateKey != "" {
privateKeyAuth, err := ssh.ParsePrivateKey([]byte(privateKey))
if privateKey != nil {
privateKeyAuth, err := ssh.ParsePrivateKey(privateKey)
if err != nil {
return nil, nil, err
}
Expand Down
23 changes: 20 additions & 3 deletions test/new-e2e/utils/e2e/client/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ package client

import (
"errors"
"os"
"regexp"
"testing"
"time"

"github.com/DataDog/datadog-agent/test/new-e2e/runner"
"github.com/DataDog/datadog-agent/test/new-e2e/runner/parameters"
"github.com/DataDog/test-infra-definitions/components/datadog/agent"
"github.com/DataDog/test-infra-definitions/components/os"
e2eOs "github.com/DataDog/test-infra-definitions/components/os"
"github.com/cenkalti/backoff"
)

Expand All @@ -24,7 +27,7 @@ var _ clientService[agent.ClientData] = (*Agent)(nil)
type Agent struct {
*UpResultDeserializer[agent.ClientData]
*vmClient
os os.OS
os e2eOs.OS
}

// Create a new instance of Agent
Expand All @@ -37,7 +40,21 @@ func NewAgent(installer *agent.Installer) *Agent {
//lint:ignore U1000 Ignore unused function as this function is call using reflection
func (agent *Agent) initService(t *testing.T, data *agent.ClientData) error {
var err error
agent.vmClient, err = newVMClient(t, "", &data.Connection)
var privateSshKey []byte

privateKeyPath, err := runner.GetProfile().ParamStore().GetWithDefault(parameters.PrivateKeyPath, "")
if err != nil {
return err
}

if privateKeyPath != "" {
privateSshKey, err = os.ReadFile(privateKeyPath)
if err != nil {
return err
}
}

agent.vmClient, err = newVMClient(t, privateSshKey, &data.Connection)
return err
}

Expand Down
19 changes: 18 additions & 1 deletion test/new-e2e/utils/e2e/client/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
package client

import (
"os"
"testing"

"github.com/DataDog/datadog-agent/test/new-e2e/runner"
"github.com/DataDog/datadog-agent/test/new-e2e/runner/parameters"
commonvm "github.com/DataDog/test-infra-definitions/components/vm"
)

Expand All @@ -30,6 +33,20 @@ func NewVM(infraVM commonvm.VM) *VM {
//lint:ignore U1000 Ignore unused function as this function is call using reflection
func (vm *VM) initService(t *testing.T, data *commonvm.ClientData) error {
var err error
vm.vmClient, err = newVMClient(t, "", &data.Connection)
var privateSshKey []byte

privateKeyPath, err := runner.GetProfile().ParamStore().GetWithDefault(parameters.PrivateKeyPath, "")
if err != nil {
return err
}

if privateKeyPath != "" {
privateSshKey, err = os.ReadFile(privateKeyPath)
if err != nil {
return err
}
}

vm.vmClient, err = newVMClient(t, privateSshKey, &data.Connection)
return err
}
2 changes: 1 addition & 1 deletion test/new-e2e/utils/e2e/client/vm_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type vmClient struct {
t *testing.T
}

func newVMClient(t *testing.T, sshKey string, connection *utils.Connection) (*vmClient, error) {
func newVMClient(t *testing.T, sshKey []byte, connection *utils.Connection) (*vmClient, error) {
client, _, err := clients.GetSSHClient(
connection.User,
fmt.Sprintf("%s:%d", connection.Host, 22),
Expand Down

0 comments on commit 9be0c94

Please sign in to comment.