-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add unit tests for tarian-pod-agent
- Loading branch information
1 parent
3a6af83
commit e556b08
Showing
15 changed files
with
638 additions
and
71 deletions.
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,55 @@ | ||
package flags | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/pflag" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSetGlobalFlags(t *testing.T) { | ||
// Create a FlagSet for testing | ||
fs := pflag.NewFlagSet("test", pflag.ExitOnError) | ||
|
||
// Initialize global flags | ||
globalFlags := SetGlobalFlags(fs) | ||
|
||
// Test default values | ||
assert.Equal(t, "info", globalFlags.LogLevel) | ||
assert.Equal(t, "text", globalFlags.LogFormatter) | ||
} | ||
|
||
func TestValidateGlobalFlags(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
globalFlags *GlobalFlags | ||
expectedError string | ||
}{ | ||
{ | ||
name: "Valid Flags", | ||
globalFlags: &GlobalFlags{LogLevel: "info", LogFormatter: "text"}, | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "Invalid LogLevel", | ||
globalFlags: &GlobalFlags{LogLevel: "invalid", LogFormatter: "text"}, | ||
expectedError: "invalid log level: invalid", | ||
}, | ||
{ | ||
name: "Invalid LogFormatter", | ||
globalFlags: &GlobalFlags{LogLevel: "info", LogFormatter: "invalid"}, | ||
expectedError: "invalid log formatter: invalid", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.globalFlags.ValidateGlobalFlags() | ||
if tt.expectedError == "" { | ||
assert.NoError(t, err) | ||
} else { | ||
assert.EqualError(t, err, tt.expectedError) | ||
} | ||
}) | ||
} | ||
} |
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
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,123 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/kube-tarian/tarian/cmd/tarian-pod-agent/cmd/flags" | ||
"github.com/kube-tarian/tarian/pkg/log" | ||
"github.com/kube-tarian/tarian/pkg/podagent" | ||
utesting "github.com/kube-tarian/tarian/pkg/testing" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRegisterCommandRun(t *testing.T) { | ||
logger := log.GetLogger() | ||
file := createTestLabelsFile(t, true) | ||
defer os.Remove(file.Name()) | ||
tests := []struct { | ||
name string | ||
expectedErr string | ||
expectedLog string | ||
|
||
podLabelsFile string | ||
|
||
podAgent podagent.Agent | ||
}{ | ||
{ | ||
name: "Run Register mode Successfully", | ||
podAgent: podagent.NewFakePodAgent(logger), | ||
expectedLog: "tarian-pod-agent is running in register mode", | ||
}, | ||
{ | ||
name: "Run Register mode with a empty Labels File", | ||
podAgent: podagent.NewFakePodAgent(logger), | ||
expectedErr: "no labels found in file", | ||
podLabelsFile: file.Name(), | ||
}, | ||
{ | ||
name: "Run register mode with Invalid Labels File", | ||
expectedErr: "failed to open file", | ||
podLabelsFile: "nonexistent_labels.txt", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
cmd := ®isterCommand{ | ||
logger: logger, | ||
podLabelsFile: tt.podLabelsFile, | ||
podAgent: tt.podAgent, | ||
} | ||
|
||
logOutput := []byte{} | ||
cmd.logger.Out = &utesting.LogOutputWriter{Output: &logOutput} | ||
log.MiniLogFormat() | ||
|
||
err := cmd.runRegisterCommand(nil, nil) | ||
|
||
if tt.expectedErr != "" { | ||
assert.Contains(t, err.Error(), tt.expectedErr) | ||
} else { | ||
if !assert.NoError(t, err) { | ||
assert.FailNow(t, "error not expected") | ||
} | ||
} | ||
|
||
if tt.expectedLog != "" { | ||
assert.Contains(t, utesting.CleanLog(string(logOutput)), utesting.CleanLog(tt.expectedLog)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestNewRegisterModeCommand(t *testing.T) { | ||
globalFlags := &flags.GlobalFlags{} | ||
cmd := newRegisterCommand(globalFlags) | ||
|
||
assert.NotNil(t, cmd) | ||
assert.IsType(t, &cobra.Command{}, cmd) | ||
|
||
assert.Equal(t, "register", cmd.Use) | ||
assert.Equal(t, "Register the pod to the Tarian server", cmd.Short) | ||
|
||
testFlags(t, cmd) | ||
} | ||
|
||
func testFlags(t *testing.T, cmd *cobra.Command) { | ||
assert.Equal(t, defaultClusterAgentHost, cmd.Flags().Lookup("host").DefValue) | ||
err := cmd.Flags().Set("host", "localhost") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "localhost", cmd.Flags().Lookup("host").Value.String()) | ||
|
||
assert.Equal(t, defaultClusterAgentPort, cmd.Flags().Lookup("port").DefValue) | ||
err = cmd.Flags().Set("port", "50053") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "50053", cmd.Flags().Lookup("port").Value.String()) | ||
|
||
assert.Equal(t, "", cmd.Flags().Lookup("pod-labels-file").DefValue) | ||
err = cmd.Flags().Set("pod-labels-file", "test_labels.txt") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "test_labels.txt", cmd.Flags().Lookup("pod-labels-file").Value.String()) | ||
|
||
assert.Equal(t, "", cmd.Flags().Lookup("pod-name").DefValue) | ||
err = cmd.Flags().Set("pod-name", "test_pod") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "test_pod", cmd.Flags().Lookup("pod-name").Value.String()) | ||
|
||
assert.Equal(t, "", cmd.Flags().Lookup("pod-uid").DefValue) | ||
err = cmd.Flags().Set("pod-uid", "test_pod_uid") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "test_pod_uid", cmd.Flags().Lookup("pod-uid").Value.String()) | ||
|
||
assert.Equal(t, "", cmd.Flags().Lookup("namespace").DefValue) | ||
err = cmd.Flags().Set("namespace", "test_namespace") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "test_namespace", cmd.Flags().Lookup("namespace").Value.String()) | ||
|
||
assert.Equal(t, "3s", cmd.Flags().Lookup("file-validation-interval").DefValue) | ||
err = cmd.Flags().Set("file-validation-interval", "5s") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "5s", cmd.Flags().Lookup("file-validation-interval").Value.String()) | ||
} |
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
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,36 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
|
||
"github.com/kube-tarian/tarian/pkg/log" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPodAgentRootCommand(t *testing.T) { | ||
t.Run("TestRootCommandVersion", func(t *testing.T) { | ||
stdout := new(bytes.Buffer) | ||
|
||
err := runRootCommand(stdout, []string{"version"}) | ||
if assert.NoError(t, err) { | ||
out, _ := io.ReadAll(stdout) | ||
assert.Contains(t, string(out), "tarian pod agent version:") | ||
} | ||
}) | ||
|
||
t.Run("TestRootCommandInvalidSubcommand", func(t *testing.T) { | ||
stdout := new(bytes.Buffer) | ||
err := runRootCommand(stdout, []string{"invalidStderr-subcommand"}) | ||
assert.EqualError(t, err, `unknown command "invalidStderr-subcommand" for "tarian-pod-agent"`) | ||
}) | ||
} | ||
|
||
func runRootCommand(output *bytes.Buffer, args []string) error { | ||
logger := log.GetLogger() | ||
logger.SetOutput(output) | ||
rootCmd := buildRootCommand(logger) | ||
rootCmd.SetArgs(args) | ||
return rootCmd.Execute() | ||
} |
Oops, something went wrong.