-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
nehagup
wants to merge
9
commits into
DataDog:main
Choose a base branch
from
nehagup:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
29ecee0
feat(tests): increase unit test coverage for cmd/agent/common and cmd…
nehagup cff0134
Merge branch 'DataDog:main' into main
nehagup 0088b06
Merge branch 'main' into main
nehagup 383f413
Merge branch 'main' into main
nehagup 63bd9ce
Merge branch 'main' into main
nehagup 5851679
Merge branch 'main' into main
nehagup 2f6783c
Merge branch 'DataDog:main' into main
nehagup 92413a1
chore: adding assert and require
nehagup ca5f5a3
Merge branch 'main' into main
nehagup File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
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) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
Could you update the test to use
assert
/require
?There was a problem hiding this comment.
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. :)
There was a problem hiding this comment.
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. :)