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

Improved unit-test coverage of common and command packages by 94% using keploy ai-agent #33823

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
94 changes: 94 additions & 0 deletions cmd/agent/command/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package command

import (
"testing"
"github.com/spf13/cobra"
"os"
"path/filepath"
)


// Test generated using Keploy
func TestGetDefaultCoreBundleParams_ValidGlobalParams_ReturnsExpectedBundleParams(t *testing.T) {
globalParams := &GlobalParams{
ConfFilePath: "/path/to/config",
ExtraConfFilePath: []string{"/path/to/extra/config1", "/path/to/extra/config2"},
FleetPoliciesDirPath: "/path/to/fleet/policies",
}

bundleParams := GetDefaultCoreBundleParams(globalParams)

if bundleParams.ConfigParams.ConfFilePath != globalParams.ConfFilePath {
t.Errorf("Expected ConfFilePath to be %v, got %v", globalParams.ConfFilePath, bundleParams.ConfigParams.ConfFilePath)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of the code base uses assert or require package for this kind of test.

Example:

Suggested change
if bundleParams.ConfigParams.ConfFilePath != globalParams.ConfFilePath {
t.Errorf("Expected ConfFilePath to be %v, got %v", globalParams.ConfFilePath, bundleParams.ConfigParams.ConfFilePath)
}
assert.Len(t, bundleParams.ConfigParams.ConfFilePath. globalParams.ConfFilePath)

Could you update the test to use assert/require ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @hush-hush! thanks for the feedback. Infact that seems to be a better approach, we'll incorporate this in our model. :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let me know if your team has a goal to improve any specific packages' coverage. It's just a click for me - I would love to contribute more. :)


if len(bundleParams.ConfigParams.ExtraConfFilePath) != len(globalParams.ExtraConfFilePath) {
t.Errorf("Expected ExtraConfFilePath length to be %v, got %v", len(globalParams.ExtraConfFilePath), len(bundleParams.ConfigParams.ExtraConfFilePath))
}

if bundleParams.ConfigParams.FleetPoliciesDirPath != globalParams.FleetPoliciesDirPath {
t.Errorf("Expected FleetPoliciesDirPath to be %v, got %v", globalParams.FleetPoliciesDirPath, bundleParams.ConfigParams.FleetPoliciesDirPath)
}
}

// Test generated using Keploy
func TestLogLevelDefaultOff_RegisterAndValue_ReturnsExpectedValue(t *testing.T) {
logLevel := &LogLevelDefaultOff{}
cmd := &cobra.Command{}

logLevel.Register(cmd)

flag := cmd.PersistentFlags().Lookup("log_level")
if flag == nil {
t.Fatalf("Expected 'log_level' flag to be registered")
}

if flag.DefValue != "off" {
t.Errorf("Expected default value of 'log_level' to be 'off', got %v", flag.DefValue)
}

cmd.PersistentFlags().Set("log_level", "debug")
if logLevel.Value() != "debug" {
t.Errorf("Expected log level value to be 'debug', got %v", logLevel.Value())
}
}


// Test generated using Keploy
func TestMakeCommand_ValidSubcommandFactories_CreatesExpectedCommand(t *testing.T) {
subcommandFactory := func(globalParams *GlobalParams) []*cobra.Command {
return []*cobra.Command{
{
Use: "subcommand1",
Short: "This is subcommand1",
},
{
Use: "subcommand2",
Short: "This is subcommand2",
},
}
}

rootCmd := MakeCommand([]SubcommandFactory{subcommandFactory})

if rootCmd.Use != filepath.Base(os.Args[0]) {
t.Errorf("Expected root command Use to be %v, got %v", filepath.Base(os.Args[0]), rootCmd.Use)
}

if rootCmd.PersistentFlags().Lookup("cfgpath") == nil {
t.Errorf("Expected persistent flag 'cfgpath' to be defined")
}

if len(rootCmd.Commands()) != 2 {
t.Errorf("Expected 2 subcommands, got %v", len(rootCmd.Commands()))
}

if rootCmd.Commands()[0].Use != "subcommand1" {
t.Errorf("Expected first subcommand to be 'subcommand1', got %v", rootCmd.Commands()[0].Use)
}

if rootCmd.Commands()[1].Use != "subcommand2" {
t.Errorf("Expected second subcommand to be 'subcommand2', got %v", rootCmd.Commands()[1].Use)
}
}

40 changes: 40 additions & 0 deletions cmd/agent/common/autodiscovery_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package common

import (
"testing"
"github.com/DataDog/datadog-agent/comp/core/autodiscovery/integration"
)


// Test generated using Keploy
func TestFilterInstances_EmptyInstances(t *testing.T) {
instances := []integration.Data{}

validFilter := "name == 'validInstance'"

filteredInstances, errors := filterInstances(instances, validFilter)
if len(filteredInstances) != 0 {
t.Errorf("Expected no instances, got: %v", filteredInstances)
}
if len(errors) != 0 {
t.Errorf("Expected no errors, got: %v", errors)
}
}

// Test generated using Keploy
func TestFilterInstances_InvalidYAMLInstance(t *testing.T) {
instances := []integration.Data{
[]byte(`invalid yaml`),
}

validFilter := "name == 'validInstance'"

filteredInstances, errors := filterInstances(instances, validFilter)
if len(errors) == 0 {
t.Errorf("Expected errors due to invalid YAML, got none")
}
if len(filteredInstances) != 0 {
t.Errorf("Expected no instances due to invalid YAML, got: %v", filteredInstances)
}
}

56 changes: 56 additions & 0 deletions cmd/agent/common/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package common

import (
"testing"
"net/http"
)


// Test generated using Keploy
func TestGetVersion_WritesCorrectJSONResponse(t *testing.T) {
mockWriter := &mockResponseWriter{}
mockRequest := &http.Request{}

GetVersion(mockWriter, mockRequest)

if mockWriter.Header().Get("Content-Type") != "application/json" {
t.Errorf("Expected Content-Type to be application/json, got %s", mockWriter.Header().Get("Content-Type"))
}

if len(mockWriter.body) == 0 {
t.Errorf("Expected response body to be non-empty")
}
}

type mockResponseWriter struct {
header http.Header
body []byte
}

func (m *mockResponseWriter) Header() http.Header {
if m.header == nil {
m.header = http.Header{}
}
return m.header
}

func (m *mockResponseWriter) Write(data []byte) (int, error) {
m.body = append(m.body, data...)
return len(data), nil
}

func (m *mockResponseWriter) WriteHeader(statusCode int) {}

// Test generated using Keploy
func TestNewSettingsClient_ReturnsValidClient(t *testing.T) {
client, err := NewSettingsClient()

if err != nil {
t.Errorf("Expected no error, got %v", err)
}

if client == nil {
t.Errorf("Expected a valid settings.Client, got nil")
}
}