Skip to content

Commit

Permalink
test: add unit tests for table and command pkg
Browse files Browse the repository at this point in the history
Signed-off-by: Mario Constanti <github@constanti.de>
  • Loading branch information
bavarianbidi committed Jan 7, 2025
1 parent e66e9f7 commit b47fd26
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 0 deletions.
86 changes: 86 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// SPDX-License-Identifier: MIT

package config

import (
"reflect"
"testing"

"github.com/bavarianbidi/kubectl-dpm/pkg/profile"
)

func TestGenerateConfig(t *testing.T) {
tests := []struct {
name string
configFile string
expectedConfig profile.CustomDebugProfile
wantErr bool
}{
{
name: "valid config file",
configFile: "test_data/test_config.yaml",
expectedConfig: profile.CustomDebugProfile{
Profiles: []profile.Profile{
{
ProfileName: "profile1",
Profile: "test_data/profile1.json",
Image: "busybox",
Namespace: "default",
ImagePullPolicy: "Always",
},
{
ProfileName: "profile2",
Profile: "test_data/profile2.json",
Image: "busybox",
},
{
ProfileName: "profile3",
Profile: "netadmin",
},
{
ProfileName: "profile4",
Profile: "netadmin",
Image: "nicolaka/netshoot:v0.13",
Namespace: "application",
MatchLabels: map[string]string{
"app.kubernetes.io/instance": "app",
},
},
},
},
wantErr: false,
},
{
name: "invalid config file",
configFile: "test_data/invalid_config.yaml",
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Cleanup(func() {
// Reset the ConfigurationFile variable
ConfigurationFile = ""
// Reset the Config variable
profile.Config = profile.CustomDebugProfile{}
})

// Set the ConfigurationFile variable
ConfigurationFile = tt.configFile

// Call GenerateConfig
err := GenerateConfig()
if (err != nil) != tt.wantErr {
t.Errorf("GenerateConfig() error = %v, wantErr %v", err, tt.wantErr)
}

// Check the loaded profiles if no error is expected
if !tt.wantErr {
if !reflect.DeepEqual(profile.Config, tt.expectedConfig) {
t.Errorf("expected profiles: %v, got: %v", tt.expectedConfig, profile.Config.Profiles)
}
}
})
}
}
7 changes: 7 additions & 0 deletions pkg/config/test_data/invalid_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# yaml is not valid to be parsed
prof: # should be profile - see working test_config.yaml
- name: "profile1"
profile: "test_data/profile1.json"
image: "busybox"
namespace: "default"
imagePullPolicy: "Always"
17 changes: 17 additions & 0 deletions pkg/config/test_data/test_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
profiles:
- name: "profile1"
profile: "test_data/profile1.json"
image: "busybox"
namespace: "default"
imagePullPolicy: "Always"
- name: "profile2"
profile: "test_data/profile2.json"
image: "busybox"
- name: "profile3"
profile: "netadmin"
- name: profile4
profile: netadmin
image: nicolaka/netshoot:v0.13
namespace: application
matchLabels:
app.kubernetes.io/instance: app
87 changes: 87 additions & 0 deletions pkg/table/table_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// SPDX-License-Identifier: MIT

package table

import (
"testing"

"github.com/charmbracelet/bubbles/table"

"github.com/bavarianbidi/kubectl-dpm/pkg/profile"
)

func TestGenerateTable(t *testing.T) {
tests := []struct {
name string
profiles []profile.Profile
wide bool
expected []table.Row
}{
{
name: "basic profiles",
profiles: []profile.Profile{
{
ProfileName: "profile1",
Profile: "test_data/profile1.json",
Image: "busybox",
Namespace: "default",
MatchLabels: map[string]string{"app": "test"},
},
{
ProfileName: "profile2",
Profile: "test_data/profile2.json",
Image: "nginx",
Namespace: "kube-system",
MatchLabels: map[string]string{"app": "nginx"},
},
},
wide: false,
expected: []table.Row{
{"profile1", "test_data/profile1.json"},
{"profile2", "test_data/profile2.json"},
},
},
{
name: "wide profiles",
profiles: []profile.Profile{
{
ProfileName: "profile1",
Profile: "test_data/profile1.json",
Image: "busybox",
Namespace: "default",
MatchLabels: map[string]string{"app": "test"},
},
{
ProfileName: "profile2",
Profile: "test_data/profile2.json",
Image: "nginx",
Namespace: "kube-system",
MatchLabels: map[string]string{"app": "nginx"},
},
},
wide: true,
expected: []table.Row{
{"profile1", "test_data/profile1.json", "busybox", "default", `{"app":"test"}`},
{"profile2", "test_data/profile2.json", "nginx", "kube-system", `{"app":"nginx"}`},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := GenerateTable(tt.profiles, tt.wide)

if len(got.Rows()) != len(tt.expected) {
t.Errorf("expected %d rows, got %d", len(tt.expected), len(got.Rows()))
}

for i, row := range got.Rows() {
for j, cell := range row {
if cell != tt.expected[i][j] {
t.Errorf("expected cell %d in row %d to be %v, got %v", j, i, tt.expected[i][j], cell)
}
}
}
})
}
}

0 comments on commit b47fd26

Please sign in to comment.