Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: e2e test improvements #1716

Merged
merged 5 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions testenv/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ type Server struct {
PublicIP string
}

func (s Server) WaitForSSH(ctx context.Context, t time.Duration) error {
// TODO(paralta): Implement method to check if SSH port is ready
time.Sleep(t) // nolint:mnd

return nil
}

// Setup AWS test environment from cloud formation template.
// * Create a new CloudFormation stack from template
// (upload template file to S3 is required since the template is larger than 51,200 bytes).
Expand Down Expand Up @@ -130,7 +123,9 @@ func (e *AWSEnv) SetUp(ctx context.Context) error {

func (e *AWSEnv) TearDown(ctx context.Context) error {
// Stop SSH port forwarding
e.sshPortForward.Stop()
if e.sshPortForward != nil {
e.sshPortForward.Stop()
}

// Delete the CloudFormation stack
_, err := e.client.DeleteStack(
Expand Down Expand Up @@ -218,18 +213,9 @@ func New(config *Config, opts ...ConfigOptFn) (*AWSEnv, error) {
// Create AWS S3 client
s3Client := s3.NewFromConfig(cfg)

sshKeyPair := &utils.SSHKeyPair{}
// Load SSH key-pair if provided, generate otherwise
if config.PublicKeyFile != "" && config.PrivateKeyFile != "" {
err = sshKeyPair.Load(config.PrivateKeyFile, config.PublicKeyFile)
if err != nil {
return nil, fmt.Errorf("failed to load ssh key pair: %w", err)
}
} else {
sshKeyPair, err = utils.GenerateSSHKeyPair()
if err != nil {
return nil, fmt.Errorf("failed to generate ssh key pair: %w", err)
}
sshKeyPair, err := utils.LoadOrGenerateAndSaveSSHKeyPair(config.PrivateKeyFile, config.PublicKeyFile, config.WorkDir)
if err != nil {
return nil, fmt.Errorf("failed to get SSH key pair: %w", err)
}

return &AWSEnv{
Expand Down
9 changes: 5 additions & 4 deletions testenv/aws/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ func (e *AWSEnv) afterSetUp(ctx context.Context) error {
return errors.New("infrastructure is not ready")
}

if err = e.server.WaitForSSH(ctx, DefaultSSHPortReadyTimeout); err != nil {
return fmt.Errorf("failed to wait for the SSH port to become ready: %w", err)
}

e.sshPortForwardInput = &utils.SSHForwardInput{
PrivateKey: e.sshKeyPair.PrivateKey,
User: DefaultRemoteUser,
Expand Down Expand Up @@ -118,6 +114,11 @@ func (e *AWSEnv) afterSetUp(ctx context.Context) error {
return fmt.Errorf("failed to create Docker helper: %w", err)
}

err = e.DockerHelper.WaitForDockerReady(ctx)
if err != nil {
return fmt.Errorf("failed to check if Docker client is ready: %w", err)
}

return nil
}

Expand Down
15 changes: 3 additions & 12 deletions testenv/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,9 @@ func New(config *Config, opts ...ConfigOptFn) (*AzureEnv, error) {
return nil, fmt.Errorf("failed to create Azure compute client factory: %w", err)
}

sshKeyPair := &utils.SSHKeyPair{}
// Load SSH key-pair if provided, generate otherwise
if config.PublicKeyFile != "" && config.PrivateKeyFile != "" {
err = sshKeyPair.Load(config.PrivateKeyFile, config.PublicKeyFile)
if err != nil {
return nil, fmt.Errorf("failed to load ssh key pair: %w", err)
}
} else {
sshKeyPair, err = utils.GenerateSSHKeyPair()
if err != nil {
return nil, fmt.Errorf("failed to generate ssh key pair: %w", err)
}
sshKeyPair, err := utils.LoadOrGenerateAndSaveSSHKeyPair(config.PrivateKeyFile, config.PublicKeyFile, config.WorkDir)
if err != nil {
return nil, fmt.Errorf("failed to get SSH key pair: %w", err)
}

return &AzureEnv{
Expand Down
18 changes: 0 additions & 18 deletions testenv/azure/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ func (e *AzureEnv) createAzureResources(ctx context.Context) error {
if err := json.Unmarshal(templateFile, &template); err != nil {
return fmt.Errorf("failed to unmarshal template file: %w", err)
}
// template, err := readTemplateJSON("./../installation/azure/vmclarity.json")
// if err != nil {
// return err
// }

deploymentResp, err := e.deploymentsClient.BeginCreateOrUpdateAtSubscriptionScope(
ctx,
Expand Down Expand Up @@ -120,20 +116,6 @@ func (e *AzureEnv) createAzureResources(ctx context.Context) error {
return nil
}

// func readTemplateJSON(path string) (map[string]interface{}, error) {
// templateFile, err := os.ReadFile(path)
// if err != nil {
// return nil, fmt.Errorf("failed to read template file: %w", err)
// }
//
// template := make(map[string]interface{})
// if err := json.Unmarshal(templateFile, &template); err != nil {
// return nil, fmt.Errorf("failed to unmarshal template file: %w", err)
// }
//
// return template, nil
// }

func (e *AzureEnv) createTestParams() map[string]interface{} {
params := make(map[string]interface{})

Expand Down
19 changes: 7 additions & 12 deletions testenv/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ func (e *GCPEnv) SetUp(ctx context.Context) error {
}

func (e *GCPEnv) TearDown(ctx context.Context) error {
e.sshPortForward.Stop()
// Stop SSH port forwarding
if e.sshPortForward != nil {
e.sshPortForward.Stop()
}

op, err := e.dm.Deployments.Delete(ProjectID, e.envName).Context(ctx).Do()
if err != nil {
Expand Down Expand Up @@ -223,17 +226,9 @@ func New(config *Config, opts ...ConfigOptFn) (*GCPEnv, error) {
return nil, fmt.Errorf("failed to create IAM service: %w", err)
}

sshKeyPair := &utils.SSHKeyPair{}
if config.PublicKeyFile != "" && config.PrivateKeyFile != "" {
err = sshKeyPair.Load(config.PrivateKeyFile, config.PublicKeyFile)
if err != nil {
return nil, fmt.Errorf("failed to load ssh key pair: %w", err)
}
} else {
sshKeyPair, err = utils.GenerateSSHKeyPair()
if err != nil {
return nil, fmt.Errorf("failed to generate ssh key pair: %w", err)
}
sshKeyPair, err := utils.LoadOrGenerateAndSaveSSHKeyPair(config.PrivateKeyFile, config.PublicKeyFile, config.WorkDir)
if err != nil {
return nil, fmt.Errorf("failed to get SSH key pair: %w", err)
}

return &GCPEnv{
Expand Down
28 changes: 28 additions & 0 deletions testenv/utils/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,34 @@ func GenerateSSHKeyPair() (*SSHKeyPair, error) {
}, nil
}

// Load SSH key-pair if provided, generate and save otherwise.
func LoadOrGenerateAndSaveSSHKeyPair(privKeyFile, pubKeyFile, workDir string) (*SSHKeyPair, error) {
var err error
sshKeyPair := &SSHKeyPair{}

if privKeyFile != "" && pubKeyFile != "" {
err = sshKeyPair.Load(privKeyFile, pubKeyFile)
if err != nil {
return nil, fmt.Errorf("failed to load ssh key pair: %w", err)
}
} else {
sshKeyPair, err = GenerateSSHKeyPair()
if err != nil {
return nil, fmt.Errorf("failed to generate ssh key pair: %w", err)
}

privateKeyFile := filepath.Join(workDir, "id_rsa")
publicKeyFile := filepath.Join(workDir, "id_rsa.pub")
if _, err := os.Stat(privateKeyFile); errors.Is(err, os.ErrNotExist) {
if err := sshKeyPair.Save(privateKeyFile, publicKeyFile); err != nil {
return nil, fmt.Errorf("failed to save SSH keys to filesystem: %w", err)
}
}
}

return sshKeyPair, nil
}

type SSHForwardCallback func(ctx context.Context, err error) error

type SSHForwardInput struct {
Expand Down
Loading