Skip to content

Commit 0dadc8c

Browse files
author
Moritz Clasmeier
committed
ClusterType Tests
1 parent 2e2b206 commit 0dadc8c

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package types
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
"gopkg.in/yaml.v3"
9+
)
10+
11+
func TestClusterTypeMarshalYAML(t *testing.T) {
12+
tests := []struct {
13+
clusterType ClusterType
14+
expected string
15+
}{
16+
{ClusterTypeUnknown, "Unknown"},
17+
{ClusterTypeInfraGKE, "InfraGKE"},
18+
{ClusterTypeInfraOpenShift4, "InfraOpenShift4"},
19+
{ClusterTypeOpenShift4, "OpenShift4"},
20+
{ClusterTypeKind, "Kind"},
21+
{ClusterTypeMinikube, "Minikube"},
22+
{ClusterTypeK3s, "K3s"},
23+
{ClusterTypeCRC, "CRC"},
24+
}
25+
for _, tt := range tests {
26+
t.Run(tt.expected, func(t *testing.T) {
27+
out, err := yaml.Marshal(tt.clusterType)
28+
require.NoError(t, err)
29+
assert.Equal(t, tt.expected+"\n", string(out))
30+
})
31+
}
32+
}
33+
34+
func TestClusterTypeUnmarshalYAML(t *testing.T) {
35+
tests := []struct {
36+
input string
37+
expected ClusterType
38+
}{
39+
{"InfraGKE", ClusterTypeInfraGKE},
40+
{"InfraOpenShift4", ClusterTypeInfraOpenShift4},
41+
{"OpenShift4", ClusterTypeOpenShift4},
42+
{"Kind", ClusterTypeKind},
43+
{"Minikube", ClusterTypeMinikube},
44+
{"K3s", ClusterTypeK3s},
45+
{"CRC", ClusterTypeCRC},
46+
{"Unknown", ClusterTypeUnknown},
47+
}
48+
for _, tt := range tests {
49+
t.Run(tt.input, func(t *testing.T) {
50+
var ct ClusterType
51+
err := yaml.Unmarshal([]byte(tt.input), &ct)
52+
require.NoError(t, err)
53+
assert.Equal(t, tt.expected, ct)
54+
})
55+
}
56+
}
57+
58+
func TestClusterTypeUnmarshalYAML_Invalid(t *testing.T) {
59+
var ct ClusterType
60+
err := yaml.Unmarshal([]byte("bogus"), &ct)
61+
assert.ErrorContains(t, err, "unknown cluster type identifier")
62+
}
63+
64+
func TestClusterTypeRoundTrip(t *testing.T) {
65+
for _, ct := range AllClusterTypes() {
66+
t.Run(ct.String(), func(t *testing.T) {
67+
out, err := yaml.Marshal(ct)
68+
require.NoError(t, err)
69+
70+
var parsed ClusterType
71+
require.NoError(t, yaml.Unmarshal(out, &parsed))
72+
assert.Equal(t, ct, parsed)
73+
})
74+
}
75+
}

0 commit comments

Comments
 (0)