summaryrefslogtreecommitdiff
path: root/cmd/util_test.go
blob: 5b3a428bca70363107f9450a8fda7bb885bbcab0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main

import (
	"context"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestGetKubeConfig(t *testing.T) {
	tests := []struct {
		name        string
		namespace   string
		configPath  string
		expectError bool
		expectedNS  string
	}{
		{
			name:        "Valid KubeConfig",
			namespace:   "",
			configPath:  "../test/testdata/kubernetes/config",
			expectError: false,
			expectedNS:  "default",
		},
		{
			name:        "Invalid KubeConfig Path",
			namespace:   "",
			configPath:  "invalid/kubernetes/config",
			expectError: true,
		},
		{
			name:        "Valid KubeConfig with Namespace",
			namespace:   "argocd",
			configPath:  "../test/testdata/kubernetes/config",
			expectError: false,
			expectedNS:  "argocd",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			client, err := getKubeConfig(context.TODO(), tt.namespace, tt.configPath)
			if tt.expectError {
				require.Error(t, err)
			} else {
				require.NoError(t, err)
				assert.NotNil(t, client)
				assert.Equal(t, tt.expectedNS, client.KubeClient.Namespace)
			}
		})
	}
}
func TestGetPrintableInterval(t *testing.T) {
	tests := []struct {
		input    time.Duration
		expected string
	}{
		{0, "once"},
		{time.Second, "1s"},
		{time.Minute, "1m0s"},
		{time.Hour, "1h0m0s"},
		{time.Hour + 30*time.Minute, "1h30m0s"},
		{24 * time.Hour, "24h0m0s"},
	}

	for _, test := range tests {
		result := getPrintableInterval(test.input)
		if result != test.expected {
			t.Errorf("For input %v, expected %v, but got %v", test.input, test.expected, result)
		}
	}
}

func TestGetPrintableHealthPort(t *testing.T) {
	testPorts := []struct {
		input    int
		expected string
	}{
		{0, "off"},
		{8080, "8080"},
		{9090, "9090"},
	}

	for _, testPort := range testPorts {
		result := getPrintableHealthPort(testPort.input)

		if result != testPort.expected {
			t.Errorf("For input %v, expected %v, but got %v", testPort.input, testPort.expected, result)
		}
	}

}