Skip to content

Commit

Permalink
Delete policy when test ends
Browse files Browse the repository at this point in the history
  • Loading branch information
ycombinator committed Aug 22, 2020
1 parent e60b3b5 commit 53ce3f7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
6 changes: 1 addition & 5 deletions internal/kibana/ingestmanager/client.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package ingestmanager

import (
"path"
)

type Client struct {
apiBaseUrl string

Expand All @@ -13,7 +9,7 @@ type Client struct {

func NewClient(baseUrl, username, password string) (*Client, error) {
return &Client{
path.Join(baseUrl, "api", "ingest_manager"),
baseUrl + "/api/ingest_manager",
username,
password,
}, nil
Expand Down
19 changes: 17 additions & 2 deletions internal/kibana/ingestmanager/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"io"
"io/ioutil"
"net/http"
"path"
"strings"

"github.com/pkg/errors"

Expand Down Expand Up @@ -47,8 +47,23 @@ func (c *Client) CreatePolicy(p Policy) (*Policy, error) {
return &resp.Item, nil
}

func (c *Client) DeletePolicy(p Policy) error {
reqBody := `{ "agentPolicyId": "` + p.ID + `" }`

statusCode, _, err := c.post("agent_policies/delete", strings.NewReader(reqBody))
if err != nil {
return errors.Wrap(err, "could not delete policy")
}

if statusCode != 200 {
return fmt.Errorf("could not delete policy; API status code = %d", statusCode)
}

return nil
}

func (c *Client) post(resourcePath string, reqBody io.Reader) (int, []byte, error) {
url := path.Join(c.apiBaseUrl, resourcePath)
url := c.apiBaseUrl + "/" + resourcePath
req, err := http.NewRequest(http.MethodPost, url, reqBody)
if err != nil {
return 0, nil, errors.Wrapf(err, "could not create POST request to Ingest Manager resource: %s", resourcePath)
Expand Down
24 changes: 10 additions & 14 deletions internal/testrunner/runners/system/servicedeployer/compose.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package servicedeployer

import (
"fmt"

"github.com/pkg/errors"

"github.com/elastic/elastic-package/internal/common"
"github.com/elastic/elastic-package/internal/logger"
)
Expand All @@ -26,16 +22,16 @@ func NewDockerComposeRunner(ymlPath string) (*DockerComposeRunner, error) {
// SetUp sets up the service and returns any relevant information.
func (r *DockerComposeRunner) SetUp(ctxt common.MapStr) (common.MapStr, error) {
logger.Infof("Setting up service using docker compose runner")
v, err := ctxt.GetValue("docker.compose.network")
if err != nil {
return ctxt, errors.Wrap(err, "could not determine docker compose network to join")
}

network, ok := v.(string)
if !ok {
return ctxt, fmt.Errorf("expected docker compose network name to be a string, got: %v", v)
}
r.network = network
//v, err := ctxt.GetValue("docker.compose.network")
//if err != nil {
// return ctxt, errors.Wrap(err, "could not determine docker compose network to join")
//}
//
//network, ok := v.(string)
//if !ok {
// return ctxt, fmt.Errorf("expected docker compose network name to be a string, got: %v", v)
//}
//r.network = network

// TODO
return ctxt, nil
Expand Down
1 change: 1 addition & 0 deletions internal/testrunner/runners/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (r *runner) run() error {
if err != nil {
return errors.Wrap(err, "could not create policy")
}
defer im.DeletePolicy(*policy)

fmt.Println(policy)

Expand Down
9 changes: 5 additions & 4 deletions internal/testrunner/testrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ func FindTestFolders(packageRootPath string, testType TestType, datasets []strin
}

folders := make([]TestFolder, len(paths))
for _, p := range paths {
parts := filepath.SplitList(p)
pkg := parts[0]
_, pkg := filepath.Split(packageRootPath)
for idx, p := range paths {
relP := strings.TrimPrefix(p, packageRootPath)
parts := strings.Split(relP, string(filepath.Separator))
dataset := parts[2]

folder := TestFolder{
Expand All @@ -63,7 +64,7 @@ func FindTestFolders(packageRootPath string, testType TestType, datasets []strin
dataset,
}

folders = append(folders, folder)
folders[idx] = folder
}

return folders, nil
Expand Down

0 comments on commit 53ce3f7

Please sign in to comment.