github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/domain/infra/abi/play_test.go (about) 1 package abi 2 3 import ( 4 "bytes" 5 "testing" 6 7 v1 "github.com/hanks177/podman/v4/pkg/k8s.io/api/core/v1" 8 v12 "github.com/hanks177/podman/v4/pkg/k8s.io/apimachinery/pkg/apis/meta/v1" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestReadConfigMapFromFile(t *testing.T) { 13 tests := []struct { 14 name string 15 configMapContent string 16 expectError bool 17 expectedErrorMsg string 18 expected v1.ConfigMap 19 }{ 20 { 21 "ValidConfigMap", 22 ` 23 apiVersion: v1 24 kind: ConfigMap 25 metadata: 26 name: foo 27 data: 28 myvar: foo 29 `, 30 false, 31 "", 32 v1.ConfigMap{ 33 TypeMeta: v12.TypeMeta{ 34 Kind: "ConfigMap", 35 APIVersion: "v1", 36 }, 37 ObjectMeta: v12.ObjectMeta{ 38 Name: "foo", 39 }, 40 Data: map[string]string{ 41 "myvar": "foo", 42 }, 43 }, 44 }, 45 { 46 "InvalidYAML", 47 ` 48 Invalid YAML 49 apiVersion: v1 50 kind: ConfigMap 51 metadata: 52 name: foo 53 data: 54 myvar: foo 55 `, 56 true, 57 "unable to read YAML as Kube ConfigMap", 58 v1.ConfigMap{}, 59 }, 60 { 61 "InvalidKind", 62 ` 63 apiVersion: v1 64 kind: InvalidKind 65 metadata: 66 name: foo 67 data: 68 myvar: foo 69 `, 70 true, 71 "invalid YAML kind", 72 v1.ConfigMap{}, 73 }, 74 } 75 76 for _, test := range tests { 77 test := test 78 t.Run(test.name, func(t *testing.T) { 79 buf := bytes.NewBufferString(test.configMapContent) 80 cm, err := readConfigMapFromFile(buf) 81 82 if test.expectError { 83 assert.Error(t, err) 84 assert.Contains(t, err.Error(), test.expectedErrorMsg) 85 } else { 86 assert.NoError(t, err) 87 assert.Equal(t, test.expected, cm) 88 } 89 }) 90 } 91 } 92 93 func TestGetKubeKind(t *testing.T) { 94 tests := []struct { 95 name string 96 kubeYAML string 97 expectError bool 98 expectedErrorMsg string 99 expected string 100 }{ 101 { 102 "ValidKubeYAML", 103 ` 104 apiVersion: v1 105 kind: Pod 106 `, 107 false, 108 "", 109 "Pod", 110 }, 111 { 112 "InvalidKubeYAML", 113 "InvalidKubeYAML", 114 true, 115 "cannot unmarshal", 116 "", 117 }, 118 } 119 120 for _, test := range tests { 121 test := test 122 t.Run(test.name, func(t *testing.T) { 123 kind, err := getKubeKind([]byte(test.kubeYAML)) 124 if test.expectError { 125 assert.Error(t, err) 126 assert.Contains(t, err.Error(), test.expectedErrorMsg) 127 } else { 128 assert.NoError(t, err) 129 assert.Equal(t, test.expected, kind) 130 } 131 }) 132 } 133 } 134 135 func TestSplitMultiDocYAML(t *testing.T) { 136 tests := []struct { 137 name string 138 kubeYAML string 139 expectError bool 140 expectedErrorMsg string 141 expected int 142 }{ 143 { 144 "ValidNumberOfDocs", 145 ` 146 apiVersion: v1 147 kind: Pod 148 --- 149 apiVersion: v1 150 kind: Pod 151 --- 152 apiVersion: v1 153 kind: Pod 154 `, 155 false, 156 "", 157 3, 158 }, 159 { 160 "InvalidMultiDocYAML", 161 ` 162 apiVersion: v1 163 kind: Pod 164 --- 165 apiVersion: v1 166 kind: Pod 167 - 168 `, 169 true, 170 "multi doc yaml could not be split", 171 0, 172 }, 173 } 174 175 for _, test := range tests { 176 test := test 177 t.Run(test.name, func(t *testing.T) { 178 docs, err := splitMultiDocYAML([]byte(test.kubeYAML)) 179 if test.expectError { 180 assert.Error(t, err) 181 assert.Contains(t, err.Error(), test.expectedErrorMsg) 182 } else { 183 assert.NoError(t, err) 184 assert.Equal(t, test.expected, len(docs)) 185 } 186 }) 187 } 188 }