Skip to content

Commit

Permalink
fix: move all resources in one namespace for tcp monitor acc tests (#717
Browse files Browse the repository at this point in the history
)

* fix: move all resources in one namespace for tcp monitor acc tests
* add mutex in kibana API client and move tests to same package to run test in single golang process

due-to elastic/kibana#190801
  • Loading branch information
biscout42 authored Aug 21, 2024
1 parent 1c23072 commit 1920946
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package private_location_test
package synthetics_test

// this test is in synthetics_test package, because of https://github.com/elastic/kibana/issues/190801
// having both tests in same package allows to use mutex in kibana API client and workaround the issue

import (
"fmt"
Expand All @@ -21,18 +24,18 @@ provider "elasticstack" {
)

var (
minKibanaVersion = version.Must(version.NewVersion("8.12.0"))
minKibanaPrivateLocationAPIVersion = version.Must(version.NewVersion("8.12.0"))
)

func TestPrivateLocationResource(t *testing.T) {
func TestSyntheticPrivateLocationResource(t *testing.T) {
resourceId := "elasticstack_kibana_synthetics_private_location.test"
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProtoV6ProviderFactories: acctest.Providers,
Steps: []resource.TestStep{
// Create and Read testing
{
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaVersion),
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaPrivateLocationAPIVersion),
Config: testConfig("testacc", "test_policy") + `
resource "elasticstack_kibana_synthetics_private_location" "test" {
label = "pl-test-label"
Expand All @@ -58,7 +61,7 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
},
// ImportState testing
{
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaVersion),
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaPrivateLocationAPIVersion),
ResourceName: resourceId,
ImportState: true,
ImportStateVerify: true,
Expand All @@ -77,7 +80,7 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
},
// Update and Read testing
{
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaVersion),
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaPrivateLocationAPIVersion),
Config: testConfig("default", "test_policy_default") + `
resource "elasticstack_kibana_synthetics_private_location" "test" {
label = "pl-test-label-2"
Expand All @@ -104,7 +107,7 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
},
// Update and Read testing
{
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaVersion),
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaPrivateLocationAPIVersion),
Config: testConfig("default", "test_policy_default") + `
resource "elasticstack_kibana_synthetics_private_location" "test" {
label = "pl-test-label-2"
Expand All @@ -122,7 +125,7 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
},
// Update and Read testing
{
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaVersion),
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaPrivateLocationAPIVersion),
Config: testConfig("default", "test_policy_default") + `
resource "elasticstack_kibana_synthetics_private_location" "test" {
label = "pl-test-label-2"
Expand All @@ -144,7 +147,7 @@ resource "elasticstack_kibana_synthetics_private_location" "test" {
},
// Update and Read testing
{
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaVersion),
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minKibanaPrivateLocationAPIVersion),
Config: testConfig("default", "test_policy_default") + `
resource "elasticstack_kibana_synthetics_private_location" "test" {
label = "pl-test-label-2"
Expand Down
10 changes: 5 additions & 5 deletions internal/kibana/synthetics/acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ resource "elasticstack_kibana_synthetics_monitor" "%s" {
resource "elasticstack_kibana_synthetics_monitor" "%s" {
name = "TestTcpMonitorResource - %s"
space_id = "default"
space_id = "testacc"
schedule = 5
private_locations = [elasticstack_kibana_synthetics_private_location.%s.label]
enabled = true
Expand Down Expand Up @@ -140,7 +140,7 @@ resource "elasticstack_kibana_synthetics_monitor" "%s" {
tcpMonitorUpdated = `
resource "elasticstack_kibana_synthetics_monitor" "%s" {
name = "TestTcpMonitorResource Updated - %s"
space_id = "default"
space_id = "testacc"
schedule = 10
private_locations = [elasticstack_kibana_synthetics_private_location.%s.label]
enabled = false
Expand Down Expand Up @@ -285,7 +285,7 @@ func TestSyntheticMonitorTCPResource(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(tcpMonitorId, "id"),
resource.TestCheckResourceAttr(tcpMonitorId, "name", "TestTcpMonitorResource - "+name),
resource.TestCheckResourceAttr(tcpMonitorId, "space_id", "default"),
resource.TestCheckResourceAttr(tcpMonitorId, "space_id", "testacc"),
resource.TestCheckResourceAttr(tcpMonitorId, "schedule", "5"),
resource.TestCheckResourceAttr(tcpMonitorId, "private_locations.#", "1"),
resource.TestCheckResourceAttrSet(tcpMonitorId, "private_locations.0"),
Expand Down Expand Up @@ -323,7 +323,7 @@ func TestSyntheticMonitorTCPResource(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(tcpMonitorId, "id"),
resource.TestCheckResourceAttr(tcpMonitorId, "name", "TestTcpMonitorResource Updated - "+name),
resource.TestCheckResourceAttr(tcpMonitorId, "space_id", "default"),
resource.TestCheckResourceAttr(tcpMonitorId, "space_id", "testacc"),
resource.TestCheckResourceAttr(tcpMonitorId, "schedule", "10"),
resource.TestCheckResourceAttr(tcpMonitorId, "private_locations.#", "1"),
resource.TestCheckResourceAttrSet(tcpMonitorId, "private_locations.0"),
Expand Down Expand Up @@ -377,7 +377,7 @@ resource "elasticstack_fleet_agent_policy" "%s" {
}
resource "elasticstack_kibana_synthetics_private_location" "%s" {
label = "TestMonitorResource-label-%s"
label = "monitor-pll-%s"
space_id = "testacc"
agent_policy_id = elasticstack_fleet_agent_policy.%s.policy_id
}
Expand Down
1 change: 1 addition & 0 deletions libs/go-kibana-rest/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ services:
ELASTICSEARCH_USERNAME: kibana_system
ELASTICSEARCH_PASSWORD: changeme
XPACK_ENCRYPTEDSAVEDOBJECTS_ENCRYPTIONKEY: min-32-byte-long-strong-encryption-key
# LOGGING_ROOT_LEVEL: debug
links:
- elasticsearch:es
ports:
Expand Down
39 changes: 23 additions & 16 deletions libs/go-kibana-rest/kbapi/api.kibana_synthetics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"sync"
"time"

"github.com/go-resty/resty/v2"
Expand Down Expand Up @@ -47,6 +48,8 @@ const (
ModeAny = "any"
)

var plMu sync.Mutex

type MonitorFields interface {
APIRequest(cfg SyntheticsMonitorConfig) interface{}
}
Expand Down Expand Up @@ -260,16 +263,15 @@ type KibanaSyntheticsPrivateLocationGet func(ctx context.Context, idOrLabel stri
type KibanaSyntheticsPrivateLocationDelete func(ctx context.Context, id string, namespace string) error

func newKibanaSyntheticsPrivateLocationGetFunc(c *resty.Client) KibanaSyntheticsPrivateLocationGet {
return func(ctx context.Context, idOrLabel string, namespace string) (*PrivateLocation, error) {

return func(ctx context.Context, idOrLabel string, _ string) (*PrivateLocation, error) {
if idOrLabel == "" {
return nil, APIError{
Code: 404,
Message: "Private location id or label is empty",
}
}

path := basePathWithId(namespace, privateLocationsSuffix, idOrLabel)
path := basePathWithId("", privateLocationsSuffix, idOrLabel)
log.Debugf("URL to get private locations: %s", path)
resp, err := c.R().SetContext(ctx).Get(path)
if err = handleKibanaError(err, resp); err != nil {
Expand All @@ -279,8 +281,26 @@ func newKibanaSyntheticsPrivateLocationGetFunc(c *resty.Client) KibanaSynthetics
}
}

func newKibanaSyntheticsPrivateLocationCreateFunc(c *resty.Client) KibanaSyntheticsPrivateLocationCreate {
return func(ctx context.Context, pLoc PrivateLocationConfig, namespace string) (*PrivateLocation, error) {
plMu.Lock()
defer plMu.Unlock()

path := basePath(namespace, privateLocationsSuffix)
log.Debugf("URL to create private locations: %s", path)
resp, err := c.R().SetContext(ctx).SetBody(pLoc).Post(path)
if err = handleKibanaError(err, resp); err != nil {
return nil, err
}
return unmarshal(resp, PrivateLocation{})
}
}

func newKibanaSyntheticsPrivateLocationDeleteFunc(c *resty.Client) KibanaSyntheticsPrivateLocationDelete {
return func(ctx context.Context, id string, namespace string) error {
plMu.Lock()
defer plMu.Unlock()

path := basePathWithId(namespace, privateLocationsSuffix, id)
log.Debugf("URL to delete private locations: %s", path)
resp, err := c.R().SetContext(ctx).Delete(path)
Expand Down Expand Up @@ -319,19 +339,6 @@ func newKibanaSyntheticsMonitorDeleteFunc(c *resty.Client) KibanaSyntheticsMonit
}
}

func newKibanaSyntheticsPrivateLocationCreateFunc(c *resty.Client) KibanaSyntheticsPrivateLocationCreate {
return func(ctx context.Context, pLoc PrivateLocationConfig, namespace string) (*PrivateLocation, error) {

path := basePath(namespace, privateLocationsSuffix)
log.Debugf("URL to create private locations: %s", path)
resp, err := c.R().SetContext(ctx).SetBody(pLoc).Post(path)
if err = handleKibanaError(err, resp); err != nil {
return nil, err
}
return unmarshal(resp, PrivateLocation{})
}
}

func newKibanaSyntheticsMonitorUpdateFunc(c *resty.Client) KibanaSyntheticsMonitorUpdate {
return func(ctx context.Context, id MonitorID, config SyntheticsMonitorConfig, fields MonitorFields, namespace string) (*SyntheticsMonitor, error) {

Expand Down
3 changes: 2 additions & 1 deletion libs/go-kibana-rest/kbapi/api.kibana_synthetics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (s *KBAPITestSuite) TestKibanaSyntheticsPrivateLocationAPI() {
testWithPolicy(s.T(), s.client, space, func(policyId string) {

cfg := PrivateLocationConfig{
Label: fmt.Sprintf("TestKibanaSyntheticsPrivateLocationAPI %s", testUuid),
Label: fmt.Sprintf("TestKibanaSyntheticsPrivateLocationAPI-%s", testUuid),
AgentPolicyId: policyId,
Tags: []string{"a", "b"},
Geo: &SyntheticGeoConfig{
Expand All @@ -322,6 +322,7 @@ func (s *KBAPITestSuite) TestKibanaSyntheticsPrivateLocationAPI() {
},
}
created, err := pAPI.Create(ctx, cfg, space)

assert.NoError(s.T(), err)
assert.Equal(s.T(), created.Label, cfg.Label)
assert.Equal(s.T(), created.AgentPolicyId, cfg.AgentPolicyId)
Expand Down

0 comments on commit 1920946

Please sign in to comment.