github.com/pdmccormick/importable-docker-buildx@v0.0.0-20240426161518-e47091289030/driver/kubernetes/factory_test.go (about) 1 package kubernetes 2 3 import ( 4 "testing" 5 6 "github.com/docker/buildx/driver" 7 "github.com/docker/buildx/driver/bkimage" 8 "github.com/stretchr/testify/require" 9 v1 "k8s.io/api/core/v1" 10 "k8s.io/client-go/rest" 11 ) 12 13 type mockKubeClientConfig struct { 14 clientConfig *rest.Config 15 namespace string 16 } 17 18 func (r *mockKubeClientConfig) ClientConfig() (*rest.Config, error) { 19 return r.clientConfig, nil 20 } 21 22 func (r *mockKubeClientConfig) Namespace() (string, bool, error) { 23 return r.namespace, true, nil 24 } 25 26 func TestFactory_processDriverOpts(t *testing.T) { 27 kcc := mockKubeClientConfig{ 28 clientConfig: &rest.Config{}, 29 } 30 31 cfg := driver.InitConfig{ 32 Name: driver.BuilderName("test"), 33 KubeClientConfig: &kcc, 34 } 35 f := factory{} 36 37 t.Run( 38 "ValidOptions", func(t *testing.T) { 39 cfg.DriverOpts = map[string]string{ 40 "namespace": "test-ns", 41 "image": "test:latest", 42 "replicas": "2", 43 "requests.cpu": "100m", 44 "requests.memory": "32Mi", 45 "limits.cpu": "200m", 46 "limits.memory": "64Mi", 47 "rootless": "true", 48 "nodeselector": "selector1=value1,selector2=value2", 49 "tolerations": "key=tolerationKey1,value=tolerationValue1,operator=Equal,effect=NoSchedule,tolerationSeconds=60;key=tolerationKey2,operator=Exists", 50 "annotations": "example.com/expires-after=annotation1,example.com/other=annotation2", 51 "labels": "example.com/owner=label1,example.com/other=label2", 52 "loadbalance": "random", 53 "qemu.install": "true", 54 "qemu.image": "qemu:latest", 55 "default-load": "true", 56 } 57 r, loadbalance, ns, defaultLoad, err := f.processDriverOpts(cfg.Name, "test", cfg) 58 59 nodeSelectors := map[string]string{ 60 "selector1": "value1", 61 "selector2": "value2", 62 } 63 64 ts := int64(60) 65 tolerations := []v1.Toleration{ 66 { 67 Key: "tolerationKey1", 68 Operator: v1.TolerationOpEqual, 69 Value: "tolerationValue1", 70 Effect: v1.TaintEffectNoSchedule, 71 TolerationSeconds: &ts, 72 }, 73 { 74 Key: "tolerationKey2", 75 Operator: v1.TolerationOpExists, 76 }, 77 } 78 79 customAnnotations := map[string]string{ 80 "example.com/expires-after": "annotation1", 81 "example.com/other": "annotation2", 82 } 83 84 customLabels := map[string]string{ 85 "example.com/owner": "label1", 86 "example.com/other": "label2", 87 } 88 89 require.NoError(t, err) 90 91 require.Equal(t, "test-ns", ns) 92 require.Equal(t, "test:latest", r.Image) 93 require.Equal(t, 2, r.Replicas) 94 require.Equal(t, "100m", r.RequestsCPU) 95 require.Equal(t, "32Mi", r.RequestsMemory) 96 require.Equal(t, "200m", r.LimitsCPU) 97 require.Equal(t, "64Mi", r.LimitsMemory) 98 require.True(t, r.Rootless) 99 require.Equal(t, nodeSelectors, r.NodeSelector) 100 require.Equal(t, customAnnotations, r.CustomAnnotations) 101 require.Equal(t, customLabels, r.CustomLabels) 102 require.Equal(t, tolerations, r.Tolerations) 103 require.Equal(t, LoadbalanceRandom, loadbalance) 104 require.True(t, r.Qemu.Install) 105 require.Equal(t, "qemu:latest", r.Qemu.Image) 106 require.True(t, defaultLoad) 107 }, 108 ) 109 110 t.Run( 111 "NoOptions", func(t *testing.T) { 112 cfg.DriverOpts = map[string]string{} 113 114 r, loadbalance, ns, defaultLoad, err := f.processDriverOpts(cfg.Name, "test", cfg) 115 116 require.NoError(t, err) 117 118 require.Equal(t, "test", ns) 119 require.Equal(t, bkimage.DefaultImage, r.Image) 120 require.Equal(t, 1, r.Replicas) 121 require.Equal(t, "", r.RequestsCPU) 122 require.Equal(t, "", r.RequestsMemory) 123 require.Equal(t, "", r.LimitsCPU) 124 require.Equal(t, "", r.LimitsMemory) 125 require.False(t, r.Rootless) 126 require.Empty(t, r.NodeSelector) 127 require.Empty(t, r.CustomAnnotations) 128 require.Empty(t, r.CustomLabels) 129 require.Empty(t, r.Tolerations) 130 require.Equal(t, LoadbalanceSticky, loadbalance) 131 require.False(t, r.Qemu.Install) 132 require.Equal(t, bkimage.QemuImage, r.Qemu.Image) 133 require.False(t, defaultLoad) 134 }, 135 ) 136 137 t.Run( 138 "RootlessOverride", func(t *testing.T) { 139 cfg.DriverOpts = map[string]string{ 140 "rootless": "true", 141 "loadbalance": "sticky", 142 } 143 144 r, loadbalance, ns, defaultLoad, err := f.processDriverOpts(cfg.Name, "test", cfg) 145 146 require.NoError(t, err) 147 148 require.Equal(t, "test", ns) 149 require.Equal(t, bkimage.DefaultRootlessImage, r.Image) 150 require.Equal(t, 1, r.Replicas) 151 require.Equal(t, "", r.RequestsCPU) 152 require.Equal(t, "", r.RequestsMemory) 153 require.Equal(t, "", r.LimitsCPU) 154 require.Equal(t, "", r.LimitsMemory) 155 require.True(t, r.Rootless) 156 require.Empty(t, r.NodeSelector) 157 require.Empty(t, r.CustomAnnotations) 158 require.Empty(t, r.CustomLabels) 159 require.Empty(t, r.Tolerations) 160 require.Equal(t, LoadbalanceSticky, loadbalance) 161 require.False(t, r.Qemu.Install) 162 require.Equal(t, bkimage.QemuImage, r.Qemu.Image) 163 require.False(t, defaultLoad) 164 }, 165 ) 166 167 t.Run( 168 "InvalidReplicas", func(t *testing.T) { 169 cfg.DriverOpts = map[string]string{ 170 "replicas": "invalid", 171 } 172 _, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg) 173 require.Error(t, err) 174 }, 175 ) 176 177 t.Run( 178 "InvalidRootless", func(t *testing.T) { 179 cfg.DriverOpts = map[string]string{ 180 "rootless": "invalid", 181 } 182 _, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg) 183 require.Error(t, err) 184 }, 185 ) 186 187 t.Run( 188 "InvalidTolerationKeyword", func(t *testing.T) { 189 cfg.DriverOpts = map[string]string{ 190 "tolerations": "key=foo,value=bar,invalid=foo2", 191 } 192 _, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg) 193 require.Error(t, err) 194 }, 195 ) 196 197 t.Run( 198 "InvalidTolerationSeconds", func(t *testing.T) { 199 cfg.DriverOpts = map[string]string{ 200 "tolerations": "key=foo,value=bar,tolerationSeconds=invalid", 201 } 202 _, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg) 203 require.Error(t, err) 204 }, 205 ) 206 207 t.Run( 208 "InvalidCustomAnnotation", func(t *testing.T) { 209 cfg.DriverOpts = map[string]string{ 210 "annotations": "key,value", 211 } 212 _, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg) 213 require.Error(t, err) 214 }, 215 ) 216 217 t.Run( 218 "InvalidCustomLabel", func(t *testing.T) { 219 cfg.DriverOpts = map[string]string{ 220 "labels": "key=value=foo", 221 } 222 _, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg) 223 require.Error(t, err) 224 }, 225 ) 226 227 t.Run( 228 "InvalidLoadBalance", func(t *testing.T) { 229 cfg.DriverOpts = map[string]string{ 230 "loadbalance": "invalid", 231 } 232 _, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg) 233 require.Error(t, err) 234 }, 235 ) 236 237 t.Run( 238 "InvalidQemuInstall", func(t *testing.T) { 239 cfg.DriverOpts = map[string]string{ 240 "qemu.install": "invalid", 241 } 242 _, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg) 243 require.Error(t, err) 244 }, 245 ) 246 247 t.Run( 248 "InvalidOption", func(t *testing.T) { 249 cfg.DriverOpts = map[string]string{ 250 "invalid": "foo", 251 } 252 _, _, _, _, err := f.processDriverOpts(cfg.Name, "test", cfg) 253 require.Error(t, err) 254 }, 255 ) 256 }