Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
Add basic tests for the Linux integration (#924)
Browse files Browse the repository at this point in the history
* Add basic tests for the Linux integration

* fix little bits
  • Loading branch information
jalvz authored Mar 24, 2021
1 parent d20811a commit 76cc23a
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 34 deletions.
25 changes: 9 additions & 16 deletions cli/services/kibana.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
package services

import (
"bytes"
"context"
"encoding/json"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -59,22 +61,12 @@ func (k *KibanaClient) withURL(path string) *KibanaClient {
}

// AddIntegrationToPolicy sends a POST request to add an integration to a policy
func (k *KibanaClient) AddIntegrationToPolicy(packageName string, name string, title string, description string, version string, policyID string) (string, error) {
payload := `{
"name":"` + name + `",
"description":"` + description + `",
"namespace":"default",
"policy_id":"` + policyID + `",
"enabled":true,
"output_id":"",
"inputs":[],
"package":{
"name":"` + packageName + `",
"title":"` + title + `",
"version":"` + version + `"
}
}`

func (k *KibanaClient) AddIntegrationToPolicy(policy interface{}) (string, error) {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(&policy); err != nil {
return "", err
}
payload := buf.String()
k.withURL(ingestManagerIntegrationPoliciesURL)

postReq := createDefaultHTTPRequest(k.getURL())
Expand All @@ -87,6 +79,7 @@ func (k *KibanaClient) AddIntegrationToPolicy(packageName string, name string, t
"error": err,
"url": k.getURL(),
"payload": payload,
"policy": policy,
}).Error("Could not add integration to configuration")
return "", err
}
Expand Down
21 changes: 21 additions & 0 deletions e2e/_suites/fleet/features/linux_integration.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@linux_integration
Feature: Linux Integration
Scenarios for Linux integration

@ingest
Scenario Outline: Adding the Linux Integration to an Agent ...
Given a "<os>" agent is deployed to Fleet with "tar" installer
And the agent is listed in Fleet as "online"
When the "Linux" integration is "added" in the policy
Then the "Linux" datasource is shown in the policy as added
And a Linux data stream exists with some data

@centos
Examples: Centos
| os |
| centos |

@debian
Examples: Debian
| os |
| debian |
78 changes: 76 additions & 2 deletions e2e/_suites/fleet/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func (fts *FleetTestSuite) contributeSteps(s *godog.ScenarioContext) {
s.Step(`^the "([^"]*)" process is "([^"]*)" on the host$`, fts.processStateChangedOnTheHost)
s.Step(`^the file system Agent folder is empty$`, fts.theFileSystemAgentFolderIsEmpty)
s.Step(`^certs are installed$`, fts.installCerts)
s.Step(`^a Linux data stream exists with some data$`, fts.checkDataStream)

// endpoint steps
s.Step(`^the "([^"]*)" integration is "([^"]*)" in the policy$`, fts.theIntegrationIsOperatedInThePolicy)
Expand Down Expand Up @@ -698,11 +699,10 @@ func (fts *FleetTestSuite) theIntegrationIsOperatedInThePolicy(packageName strin
return err
}

integration, err := getIntegration(name, version)
fts.Integration, err = getIntegration(name, version)
if err != nil {
return err
}
fts.Integration = integration

integrationPolicyID, err := addIntegrationToPolicy(fts.Integration, fts.PolicyID)
if err != nil {
Expand Down Expand Up @@ -1127,6 +1127,80 @@ func (fts *FleetTestSuite) upgradeAgent(version string) error {
return nil
}

func (fts *FleetTestSuite) checkDataStream() error {
query := map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
"filter": []interface{}{
map[string]interface{}{
"exists": map[string]interface{}{
"field": "linux.memory.page_stats",
},
},
map[string]interface{}{
"exists": map[string]interface{}{
"field": "elastic_agent",
},
},
map[string]interface{}{
"range": map[string]interface{}{
"@timestamp": map[string]interface{}{
"gte": "now-1m",
},
},
},
map[string]interface{}{
"term": map[string]interface{}{
"data_stream.type": "metrics",
},
},
map[string]interface{}{
"term": map[string]interface{}{
"data_stream.dataset": "linux.memory",
},
},
map[string]interface{}{
"term": map[string]interface{}{
"data_stream.namespace": "default",
},
},
map[string]interface{}{
"term": map[string]interface{}{
"event.dataset": "linux.memory",
},
},
map[string]interface{}{
"term": map[string]interface{}{
"agent.type": "metricbeat",
},
},
map[string]interface{}{
"term": map[string]interface{}{
"metricset.period": 1000,
},
},
map[string]interface{}{
"term": map[string]interface{}{
"service.type": "linux",
},
},
},
},
},
}

indexName := "metrics-linux.memory-default"

_, err := e2e.WaitForNumberOfHits(context.Background(), indexName, query, 1, time.Minute)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Warn(e2e.WaitForIndices())
}

return err
}

// checkFleetConfiguration checks that Fleet configuration is not missing
// any requirements and is read. To achieve it, a GET request is executed
func checkFleetConfiguration() error {
Expand Down
93 changes: 77 additions & 16 deletions e2e/_suites/fleet/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"github.com/google/uuid"
"strings"

"github.com/Jeffail/gabs/v2"
Expand All @@ -17,18 +18,78 @@ const elasticEnpointIntegrationTitle = "Endpoint Security"
// IntegrationPackage used to share information about a integration
type IntegrationPackage struct {
packageConfigID string `json:"packageConfigId"`
name string `json:"name"`
title string `json:"title"`
version string `json:"version"`
Name string `json:"name"`
Title string `json:"title"`
Version string `json:"version"`
json *gabs.Container // json representation of the integration
}

// Policy is a policy
type Policy struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Namespace string `json:"namespace"`
Enabled bool `json:"enabled"`
AgentPolicyID string `json:"policy_id"`
OutputID string `json:"output_id"`
Inputs []Input `json:"inputs"`
Package IntegrationPackage `json:"package"`
}

// Input is a policy input
type Input struct {
Type string `json:"type"`
Enabled bool `json:"enabled"`
Streams []interface{} `json:"streams"`
Vars map[string]Var `json:"vars,omitempty"`
}

// Var is an input var
type Var struct {
Value interface{} `json:"value"`
Type string `json:"type"`
}

// addIntegrationToPolicy sends a POST request to Fleet adding an integration to a configuration
func addIntegrationToPolicy(integrationPackage IntegrationPackage, policyID string) (string, error) {
name := integrationPackage.name + "-test-name"
description := integrationPackage.title + "-test-description"

body, err := kibanaClient.AddIntegrationToPolicy(integrationPackage.name, name, integrationPackage.title, description, integrationPackage.version, policyID)
policy := Policy{
AgentPolicyID: policyID,
Name: integrationPackage.Name + "-test-name",
Description: integrationPackage.Title + "-test-description",
Namespace: "default",
Enabled: true,
Package: integrationPackage,
Inputs: []Input{},
}

if policy.Package.Name == "linux" {
policy.Inputs = []Input{
{
Type: "linux/metrics",
Enabled: true,
Streams: []interface{}{
map[string]interface{}{
"id": "linux/metrics-linux.memory-" + uuid.New().String(),
"enabled": true,
"data_stream": map[string]interface{}{
"dataset": "linux.memory",
"type": "metrics",
},
},
},
Vars: map[string]Var{
"period": {
Value: "1s",
Type: "string",
},
},
},
}
}

body, err := kibanaClient.AddIntegrationToPolicy(policy)
if err != nil {
return "", err
}
Expand All @@ -47,8 +108,8 @@ func addIntegrationToPolicy(integrationPackage IntegrationPackage, policyID stri
log.WithFields(log.Fields{
"policyID": policyID,
"integrationConfigurationID": integrationConfigurationID,
"integration": integrationPackage.name,
"version": integrationPackage.version,
"integration": integrationPackage.Name,
"version": integrationPackage.Version,
}).Info("Integration added to the configuration")

return integrationConfigurationID, nil
Expand All @@ -63,9 +124,9 @@ func deleteIntegrationFromPolicy(integrationPackage IntegrationPackage, policyID

log.WithFields(log.Fields{
"policyID": policyID,
"integration": integrationPackage.name,
"integration": integrationPackage.Name,
"packageConfigId": integrationPackage.packageConfigID,
"version": integrationPackage.version,
"version": integrationPackage.Version,
}).Info("Integration deleted from the configuration")

return nil
Expand All @@ -89,9 +150,9 @@ func getIntegration(packageName string, version string) (IntegrationPackage, err

response := jsonParsed.Path("response")
integrationPackage := IntegrationPackage{
name: response.Path("name").Data().(string),
title: response.Path("title").Data().(string),
version: response.Path("latestVersion").Data().(string),
Name: response.Path("name").Data().(string),
Title: response.Path("title").Data().(string),
Version: response.Path("latestVersion").Data().(string),
}

return integrationPackage, nil
Expand Down Expand Up @@ -120,9 +181,9 @@ func getIntegrationFromAgentPolicy(packageName string, agentPolicyID string) (In
if title == packageName {
integrationPackage := IntegrationPackage{
packageConfigID: packagePolicy.Path("id").Data().(string),
name: packagePolicy.Path("package.name").Data().(string),
title: title,
version: packagePolicy.Path("package.version").Data().(string),
Name: packagePolicy.Path("package.name").Data().(string),
Title: title,
Version: packagePolicy.Path("package.version").Data().(string),
json: packagePolicy,
}

Expand Down

0 comments on commit 76cc23a

Please sign in to comment.