Skip to content

Commit

Permalink
support tags on integration tests ESS deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
pchila committed Oct 6, 2023
1 parent 3bbbd06 commit 283ccf6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
3 changes: 2 additions & 1 deletion pkg/testing/ess/create_deployment_request.tmpl.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
},
"name": "{{ .request.Name }}",
"metadata": {
"system_owned": false
"system_owned": false,
"tags": {{ json .request.Tags }}
}
}
21 changes: 19 additions & 2 deletions pkg/testing/ess/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@ import (
_ "embed"
"encoding/json"
"fmt"
"html/template"
"net/http"
"net/url"
"strings"
"text/template"
"time"

"gopkg.in/yaml.v2"
)

type Tag struct {
Key string `json:"key"`
Value string `json:"value"`
}

type CreateDeploymentRequest struct {
Name string `json:"name"`
Region string `json:"region"`
Version string `json:"version"`
Tags []Tag `json:"tags"`
}

type CreateDeploymentResponse struct {
Expand Down Expand Up @@ -324,7 +330,9 @@ func generateCreateDeploymentRequestBody(req CreateDeploymentRequest) ([]byte, e
return nil, fmt.Errorf("creating request template context: %w", err)
}

tpl, err := template.New("create_deployment_request").Parse(createDeploymentRequestTemplate)
tpl, err := template.New("create_deployment_request").
Funcs(template.FuncMap{"json": jsonMarshal}).
Parse(createDeploymentRequestTemplate)
if err != nil {
return nil, fmt.Errorf("unable to parse deployment creation template: %w", err)
}
Expand All @@ -337,6 +345,15 @@ func generateCreateDeploymentRequestBody(req CreateDeploymentRequest) ([]byte, e
return bBuf.Bytes(), nil
}

func jsonMarshal(in any) (string, error) {
jsonBytes, err := json.Marshal(in)
if err != nil {
return "", err
}

return fmt.Sprintf("%s", jsonBytes), nil

Check failure on line 354 in pkg/testing/ess/deployment.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

S1025: the argument's underlying type is a slice of bytes, should use a simple conversion instead of fmt.Sprintf (gosimple)
}

func createDeploymentTemplateContext(csp string, req CreateDeploymentRequest) (map[string]any, error) {
cspSpecificContext, err := loadCspValues(csp)
if err != nil {
Expand Down
16 changes: 13 additions & 3 deletions pkg/testing/ess/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (p *provisioner) Provision(ctx context.Context, requests []runner.StackRequ
for _, r := range requests {
// allow up to 2 minutes for each create request
createCtx, createCancel := context.WithTimeout(ctx, 2*time.Minute)
resp, err := p.createDeployment(createCtx, r, map[string]string{"elastic-agent-integration-tests": "true"})
resp, err := p.createDeployment(createCtx, r, map[string]string{"elastic-agent-integration-tests": "true", "team": "elastic-agent"})
createCancel()
if err != nil {
return nil, err
Expand Down Expand Up @@ -131,18 +131,28 @@ func (p *provisioner) Clean(ctx context.Context, stacks []runner.Stack) error {
return nil
}

func (p *provisioner) createDeployment(ctx context.Context, r runner.StackRequest, _ map[string]string) (*CreateDeploymentResponse, error) {
func (p *provisioner) createDeployment(ctx context.Context, r runner.StackRequest, tags map[string]string) (*CreateDeploymentResponse, error) {
ctx, cancel := context.WithTimeout(ctx, 1*time.Minute)
defer cancel()

p.logger.Logf("Creating stack %s (%s)", r.Version, r.ID)
name := fmt.Sprintf("%s-%s", strings.Replace(p.cfg.Identifier, ".", "-", -1), r.ID)

// prepare tags
tagArray := make([]Tag, 0, len(tags))
for k, v := range tags {
tagArray = append(tagArray, Tag{
Key: k,
Value: v,
})
}

createDeploymentRequest := CreateDeploymentRequest{
Name: name,
Region: p.cfg.Region,
Version: r.Version,
Tags: tagArray,
}
//TODO handle tags

resp, err := p.client.CreateDeployment(ctx, createDeploymentRequest)
if err != nil {
Expand Down

0 comments on commit 283ccf6

Please sign in to comment.