-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bence Csati <csatib02@gmail.com>
- Loading branch information
Showing
11 changed files
with
296 additions
and
24 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
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,74 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLoadConfig(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
envVars map[string]string | ||
wantConfig *Config | ||
wantError error | ||
}{ | ||
{ | ||
name: "Default values", | ||
envVars: map[string]string{}, | ||
wantConfig: &Config{ | ||
ListenAddress: defaultListenAddress, | ||
TrustedProxies: nil, | ||
Mode: defaultMode, | ||
LogLevel: "info", | ||
JSONLog: false, | ||
LogServerAddress: "", | ||
}, | ||
wantError: nil, | ||
}, | ||
{ | ||
name: "Custom values", | ||
envVars: map[string]string{ | ||
"KPA_LISTEN_ADDRESS": "127.0.0.1:9090", | ||
"KPA_TRUSTED_PROXIES": "192.168.1.1,192.168.1.2", | ||
"KPA_MODE": gin.ReleaseMode, | ||
"KPA_LOG_LEVEL": "debug", | ||
"KPA_JSON_LOG": "true", | ||
"KPA_LOG_SERVER": "logserver.local", | ||
}, | ||
wantConfig: &Config{ | ||
ListenAddress: "127.0.0.1:9090", | ||
TrustedProxies: []string{"192.168.1.1", "192.168.1.2"}, | ||
Mode: gin.ReleaseMode, | ||
LogLevel: "debug", | ||
JSONLog: true, | ||
LogServerAddress: "logserver.local", | ||
}, | ||
wantError: nil, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
ttp := tt | ||
t.Run(ttp.name, func(t *testing.T) { | ||
for envKey, envVal := range ttp.envVars { | ||
os.Setenv(envKey, envVal) | ||
} | ||
t.Cleanup(func() { | ||
os.Clearenv() | ||
}) | ||
|
||
config, err := LoadConfig() | ||
if err != nil { | ||
assert.EqualError(t, ttp.wantError, err.Error(), "Unexpected error message") | ||
} | ||
|
||
if ttp.wantConfig != nil { | ||
assert.Equal(t, ttp.wantConfig, config, "Unexpected config") | ||
} | ||
|
||
}) | ||
} | ||
} |
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,48 @@ | ||
package handlers | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestValidateRequestedFilters(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
inputFilters []string | ||
wantFilters []string | ||
wantError error | ||
}{ | ||
{ | ||
name: "Valid filters", | ||
inputFilters: []string{"namespace", "phase", "labels"}, | ||
wantFilters: []string{"namespace", "phase", "labels"}, | ||
wantError: nil, | ||
}, | ||
{ | ||
name: "Mixed valid and empty filters", | ||
inputFilters: []string{"namespace", "", "phase", " ", "labels"}, | ||
wantFilters: []string{"namespace", "phase", "labels"}, | ||
wantError: nil, | ||
}, | ||
{ | ||
name: "All empty filters", | ||
inputFilters: []string{"", " ", "\t", "\n"}, | ||
wantFilters: nil, | ||
wantError: errors.New("no valid filters provided"), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
ttp := tt | ||
t.Run(ttp.name, func(t *testing.T) { | ||
filters, err := validateRequestedFilters(ttp.inputFilters) | ||
if err != nil { | ||
assert.EqualError(t, ttp.wantError, err.Error(), "Unexpected error message") | ||
} | ||
|
||
assert.Equal(t, ttp.wantFilters, filters, "Unexpected filters") | ||
}) | ||
} | ||
} |
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
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,45 @@ | ||
package autocomplete | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/csatib02/kube-pod-autocomplete/internal/services/autocomplete/model" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var serviceTest = Service{} | ||
|
||
func TestProcessMapSuggestion(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
filterName string | ||
mapData map[string][]string | ||
inputSuggestions []model.Suggestion | ||
wantSuggestions []model.Suggestion | ||
}{ | ||
{ | ||
name: "Valid Map Data", | ||
filterName: "labels", | ||
mapData: map[string][]string{"app": {"nginx", "redis"}}, | ||
inputSuggestions: []model.Suggestion{}, | ||
wantSuggestions: []model.Suggestion{ | ||
{Key: "labels:app", Values: []string{"nginx", "redis"}}, | ||
}, | ||
}, | ||
{ | ||
name: "Empty Map Data", | ||
filterName: "labels", | ||
mapData: map[string][]string{}, | ||
inputSuggestions: []model.Suggestion{}, | ||
wantSuggestions: []model.Suggestion{}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
ttp := tt | ||
t.Run(ttp.name, func(t *testing.T) { | ||
serviceTest.processMapSuggestion(&ttp.inputSuggestions, ttp.filterName, &ttp.mapData) | ||
assert.Equal(t, ttp.wantSuggestions, ttp.inputSuggestions) | ||
}) | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
internal/services/autocomplete/filter/filteroptions_test.go
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,103 @@ | ||
package filter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/csatib02/kube-pod-autocomplete/internal/services/autocomplete/model" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var optionsTest = Options{} | ||
|
||
func TestRemoveDuplicateValues(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
inputsuggestions []model.Suggestion | ||
wantSuggestions []model.Suggestion | ||
}{ | ||
{ | ||
name: "No duplicates", | ||
inputsuggestions: []model.Suggestion{ | ||
{Key: "namespace", Values: []string{"value1", "value2"}}, | ||
{Key: "label", Values: []string{"value3", "value4"}}, | ||
}, | ||
wantSuggestions: []model.Suggestion{ | ||
{Key: "namespace", Values: []string{"value1", "value2"}}, | ||
{Key: "label", Values: []string{"value3", "value4"}}, | ||
}, | ||
}, | ||
{ | ||
name: "With duplicates", | ||
inputsuggestions: []model.Suggestion{ | ||
{Key: "namespace", Values: []string{"value1", "value2", "value1"}}, | ||
{Key: "label", Values: []string{"value3", "value4", "value3"}}, | ||
}, | ||
wantSuggestions: []model.Suggestion{ | ||
{Key: "namespace", Values: []string{"value1", "value2"}}, | ||
{Key: "label", Values: []string{"value3", "value4"}}, | ||
}, | ||
}, | ||
{ | ||
name: "All duplicates", | ||
inputsuggestions: []model.Suggestion{ | ||
{Key: "namespace", Values: []string{"value1", "value1", "value1"}}, | ||
}, | ||
wantSuggestions: []model.Suggestion{ | ||
{Key: "namespace", Values: []string{"value1"}}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
ttp := tt | ||
t.Run(ttp.name, func(t *testing.T) { | ||
optionsTest.RemoveDuplicateValues(&ttp.inputsuggestions) | ||
assert.Equal(t, ttp.wantSuggestions, ttp.inputsuggestions) | ||
}) | ||
} | ||
} | ||
|
||
func TestRemoveIgnoredKeys(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
inputsuggestions []model.Suggestion | ||
wantSuggestions []model.Suggestion | ||
}{ | ||
{ | ||
name: "No ignored keys", | ||
inputsuggestions: []model.Suggestion{ | ||
{Key: "namespace", Values: []string{"value1", "value2"}}, | ||
{Key: "label", Values: []string{"value3", "value4"}}, | ||
}, | ||
wantSuggestions: []model.Suggestion{ | ||
{Key: "namespace", Values: []string{"value1", "value2"}}, | ||
{Key: "label", Values: []string{"value3", "value4"}}, | ||
}, | ||
}, | ||
{ | ||
name: "With ignored keys", | ||
inputsuggestions: []model.Suggestion{ | ||
{Key: "annotations:kubectl.kubernetes.io/last-applied-configuration", Values: []string{"value1"}}, | ||
{Key: "label", Values: []string{"value2"}}, | ||
}, | ||
wantSuggestions: []model.Suggestion{ | ||
{Key: "label", Values: []string{"value2"}}, | ||
}, | ||
}, | ||
{ | ||
name: "All ignored keys", | ||
inputsuggestions: []model.Suggestion{ | ||
{Key: "annotations:kubectl.kubernetes.io/last-applied-configuration", Values: []string{"value1"}}, | ||
}, | ||
wantSuggestions: []model.Suggestion{}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
ttp := tt | ||
t.Run(ttp.name, func(t *testing.T) { | ||
optionsTest.RemoveIgnoredKeys(&ttp.inputsuggestions) | ||
assert.Equal(t, ttp.wantSuggestions, ttp.inputsuggestions) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.