| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package dynamic_test |
| 18 |
|
| 19 |
import ( |
| 20 |
"bufio" |
| 21 |
"context" |
| 22 |
"encoding/json" |
| 23 |
"fmt" |
| 24 |
"net" |
| 25 |
"net/http" |
| 26 |
"net/http/httptest" |
| 27 |
"net/http/httputil" |
| 28 |
"net/url" |
| 29 |
"os" |
| 30 |
"path/filepath" |
| 31 |
"testing" |
| 32 |
"time" |
| 33 |
|
| 34 |
"github.com/google/go-cmp/cmp" |
| 35 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 36 |
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 37 |
"k8s.io/apimachinery/pkg/runtime" |
| 38 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 39 |
"k8s.io/apimachinery/pkg/types" |
| 40 |
"k8s.io/client-go/dynamic" |
| 41 |
"k8s.io/client-go/rest" |
| 42 |
) |
| 43 |
|
| 44 |
func TestGoldenRequest(t *testing.T) { |
| 45 |
for _, tc := range []struct { |
| 46 |
name string |
| 47 |
do func(context.Context, dynamic.Interface) error |
| 48 |
}{ |
| 49 |
{ |
| 50 |
name: "create", |
| 51 |
do: func(ctx context.Context, client dynamic.Interface) error { |
| 52 |
_, err := client.Resource(schema.GroupVersionResource{Group: "flops", Version: "v1alpha1", Resource: "flips"}).Namespace("mops").Create( |
| 53 |
ctx, |
| 54 |
&unstructured.Unstructured{Object: map[string]interface{}{ |
| 55 |
"metadata": map[string]interface{}{"name": "mips"}, |
| 56 |
}}, |
| 57 |
metav1.CreateOptions{FieldValidation: "warn"}, |
| 58 |
"fin", |
| 59 |
) |
| 60 |
return err |
| 61 |
}, |
| 62 |
}, |
| 63 |
{ |
| 64 |
name: "update", |
| 65 |
do: func(ctx context.Context, client dynamic.Interface) error { |
| 66 |
_, err := client.Resource(schema.GroupVersionResource{Group: "flops", Version: "v1alpha1", Resource: "flips"}).Namespace("mops").Update( |
| 67 |
ctx, |
| 68 |
&unstructured.Unstructured{Object: map[string]interface{}{ |
| 69 |
"metadata": map[string]interface{}{"name": "mips"}, |
| 70 |
}}, |
| 71 |
metav1.UpdateOptions{FieldValidation: "warn"}, |
| 72 |
"fin", |
| 73 |
) |
| 74 |
return err |
| 75 |
}, |
| 76 |
}, |
| 77 |
{ |
| 78 |
name: "updatestatus", |
| 79 |
do: func(ctx context.Context, client dynamic.Interface) error { |
| 80 |
_, err := client.Resource(schema.GroupVersionResource{Group: "flops", Version: "v1alpha1", Resource: "flips"}).Namespace("mops").UpdateStatus( |
| 81 |
ctx, |
| 82 |
&unstructured.Unstructured{Object: map[string]interface{}{ |
| 83 |
"metadata": map[string]interface{}{"name": "mips"}, |
| 84 |
}}, |
| 85 |
metav1.UpdateOptions{FieldValidation: "warn"}, |
| 86 |
) |
| 87 |
return err |
| 88 |
}, |
| 89 |
}, |
| 90 |
{ |
| 91 |
name: "delete", |
| 92 |
do: func(ctx context.Context, client dynamic.Interface) error { |
| 93 |
return client.Resource(schema.GroupVersionResource{Group: "flops", Version: "v1alpha1", Resource: "flips"}).Namespace("mops").Delete( |
| 94 |
ctx, |
| 95 |
"mips", |
| 96 |
metav1.DeleteOptions{DryRun: []string{metav1.DryRunAll}}, |
| 97 |
"fin", |
| 98 |
) |
| 99 |
}, |
| 100 |
}, |
| 101 |
{ |
| 102 |
name: "deletecollection", |
| 103 |
do: func(ctx context.Context, client dynamic.Interface) error { |
| 104 |
return client.Resource(schema.GroupVersionResource{Group: "flops", Version: "v1alpha1", Resource: "flips"}).Namespace("mops").DeleteCollection( |
| 105 |
ctx, |
| 106 |
metav1.DeleteOptions{DryRun: []string{metav1.DryRunAll}}, |
| 107 |
metav1.ListOptions{ResourceVersion: "42"}, |
| 108 |
) |
| 109 |
}, |
| 110 |
}, |
| 111 |
{ |
| 112 |
name: "get", |
| 113 |
do: func(ctx context.Context, client dynamic.Interface) error { |
| 114 |
_, err := client.Resource(schema.GroupVersionResource{Group: "flops", Version: "v1alpha1", Resource: "flips"}).Namespace("mops").Get( |
| 115 |
ctx, |
| 116 |
"mips", |
| 117 |
metav1.GetOptions{ResourceVersion: "42"}, |
| 118 |
"fin", |
| 119 |
) |
| 120 |
return err |
| 121 |
}, |
| 122 |
}, |
| 123 |
{ |
| 124 |
name: "list", |
| 125 |
do: func(ctx context.Context, client dynamic.Interface) error { |
| 126 |
_, err := client.Resource(schema.GroupVersionResource{Group: "flops", Version: "v1alpha1", Resource: "flips"}).Namespace("mops").List( |
| 127 |
ctx, |
| 128 |
metav1.ListOptions{ResourceVersion: "42"}, |
| 129 |
) |
| 130 |
return err |
| 131 |
}, |
| 132 |
}, |
| 133 |
{ |
| 134 |
name: "watch", |
| 135 |
do: func(ctx context.Context, client dynamic.Interface) error { |
| 136 |
_, err := client.Resource(schema.GroupVersionResource{Group: "flops", Version: "v1alpha1", Resource: "flips"}).Namespace("mops").Watch( |
| 137 |
ctx, |
| 138 |
metav1.ListOptions{ResourceVersion: "42"}, |
| 139 |
) |
| 140 |
return err |
| 141 |
}, |
| 142 |
}, |
| 143 |
{ |
| 144 |
name: "patch", |
| 145 |
do: func(ctx context.Context, client dynamic.Interface) error { |
| 146 |
_, err := client.Resource(schema.GroupVersionResource{Group: "flops", Version: "v1alpha1", Resource: "flips"}).Namespace("mops").Patch( |
| 147 |
ctx, |
| 148 |
"mips", |
| 149 |
types.StrategicMergePatchType, |
| 150 |
[]byte("{\"foo\":\"bar\"}\n"), |
| 151 |
metav1.PatchOptions{FieldManager: "baz"}, |
| 152 |
"fin", |
| 153 |
) |
| 154 |
return err |
| 155 |
}, |
| 156 |
}, |
| 157 |
{ |
| 158 |
name: "apply", |
| 159 |
do: func(ctx context.Context, client dynamic.Interface) error { |
| 160 |
_, err := client.Resource(schema.GroupVersionResource{Group: "flops", Version: "v1alpha1", Resource: "flips"}).Namespace("mops").Apply( |
| 161 |
ctx, |
| 162 |
"mips", |
| 163 |
&unstructured.Unstructured{Object: map[string]interface{}{ |
| 164 |
"metadata": map[string]interface{}{"name": "mips"}, |
| 165 |
}}, |
| 166 |
metav1.ApplyOptions{Force: true}, |
| 167 |
"fin", |
| 168 |
) |
| 169 |
return err |
| 170 |
}, |
| 171 |
}, |
| 172 |
{ |
| 173 |
name: "applystatus", |
| 174 |
do: func(ctx context.Context, client dynamic.Interface) error { |
| 175 |
_, err := client.Resource(schema.GroupVersionResource{Group: "flops", Version: "v1alpha1", Resource: "flips"}).Namespace("mops").ApplyStatus( |
| 176 |
ctx, |
| 177 |
"mips", |
| 178 |
&unstructured.Unstructured{Object: map[string]interface{}{ |
| 179 |
"metadata": map[string]interface{}{"name": "mips"}, |
| 180 |
}}, |
| 181 |
metav1.ApplyOptions{Force: true}, |
| 182 |
) |
| 183 |
return err |
| 184 |
}, |
| 185 |
}, |
| 186 |
} { |
| 187 |
t.Run(tc.name, func(t *testing.T) { |
| 188 |
handled := make(chan struct{}) |
| 189 |
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 190 |
defer close(handled) |
| 191 |
|
| 192 |
got, err := httputil.DumpRequest(r, true) |
| 193 |
if err != nil { |
| 194 |
t.Fatal(err) |
| 195 |
} |
| 196 |
|
| 197 |
path := filepath.Join("testdata", filepath.FromSlash(t.Name())) |
| 198 |
|
| 199 |
if os.Getenv("UPDATE_DYNAMIC_CLIENT_FIXTURES") == "true" { |
| 200 |
err := os.WriteFile(path, got, os.FileMode(0755)) |
| 201 |
if err != nil { |
| 202 |
t.Fatalf("failed to update fixture: %v", err) |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
want, err := os.ReadFile(path) |
| 207 |
if err != nil { |
| 208 |
t.Fatalf("failed to load fixture: %v", err) |
| 209 |
} |
| 210 |
if diff := cmp.Diff(want, got); diff != "" { |
| 211 |
t.Errorf("unexpected difference from expected bytes:\n%s", diff) |
| 212 |
} |
| 213 |
})) |
| 214 |
defer srv.Close() |
| 215 |
|
| 216 |
client, err := dynamic.NewForConfig(&rest.Config{ |
| 217 |
Host: "example.com", |
| 218 |
UserAgent: "TestGoldenRequest", |
| 219 |
Transport: &http.Transport{ |
| 220 |
|
| 221 |
|
| 222 |
DialContext: func(ctx context.Context, network string, addr string) (net.Conn, error) { |
| 223 |
u, err := url.Parse(srv.URL) |
| 224 |
if err != nil { |
| 225 |
return nil, fmt.Errorf("failed to parse test server url: %w", err) |
| 226 |
} |
| 227 |
return (&net.Dialer{}).DialContext(ctx, "tcp", u.Host) |
| 228 |
}, |
| 229 |
}, |
| 230 |
}) |
| 231 |
if err != nil { |
| 232 |
t.Fatal(err) |
| 233 |
} |
| 234 |
|
| 235 |
ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 236 |
defer cancel() |
| 237 |
if err := tc.do(ctx, client); err != nil { |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
t.Logf("client returned non-nil error: %v", err) |
| 242 |
} |
| 243 |
|
| 244 |
select { |
| 245 |
case <-handled: |
| 246 |
default: |
| 247 |
t.Fatal("no request received") |
| 248 |
} |
| 249 |
}) |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
type RoundTripperFunc func(*http.Request) (*http.Response, error) |
| 254 |
|
| 255 |
func (f RoundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { |
| 256 |
return f(r) |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
func TestGoldenResponse(t *testing.T) { |
| 262 |
for _, tc := range []struct { |
| 263 |
name string |
| 264 |
response string |
| 265 |
do func(t *testing.T, client dynamic.ResourceInterface) interface{} |
| 266 |
}{ |
| 267 |
{ |
| 268 |
name: "create", |
| 269 |
response: "nonlist", |
| 270 |
do: func(t *testing.T, client dynamic.ResourceInterface) interface{} { |
| 271 |
got, err := client.Create(context.Background(), &unstructured.Unstructured{}, metav1.CreateOptions{}) |
| 272 |
if err != nil { |
| 273 |
t.Fatal(err) |
| 274 |
} |
| 275 |
|
| 276 |
return got.UnstructuredContent() |
| 277 |
}, |
| 278 |
}, |
| 279 |
{ |
| 280 |
name: "update", |
| 281 |
response: "nonlist", |
| 282 |
do: func(t *testing.T, client dynamic.ResourceInterface) interface{} { |
| 283 |
got, err := client.Update(context.Background(), &unstructured.Unstructured{Object: map[string]interface{}{"metadata": map[string]interface{}{"name": "name"}}}, metav1.UpdateOptions{}) |
| 284 |
if err != nil { |
| 285 |
t.Fatal(err) |
| 286 |
} |
| 287 |
|
| 288 |
return got.UnstructuredContent() |
| 289 |
}, |
| 290 |
}, |
| 291 |
{ |
| 292 |
name: "updatestatus", |
| 293 |
response: "nonlist", |
| 294 |
do: func(t *testing.T, client dynamic.ResourceInterface) interface{} { |
| 295 |
got, err := client.UpdateStatus(context.Background(), &unstructured.Unstructured{Object: map[string]interface{}{"metadata": map[string]interface{}{"name": "name"}}}, metav1.UpdateOptions{}) |
| 296 |
|
| 297 |
if err != nil { |
| 298 |
t.Fatal(err) |
| 299 |
} |
| 300 |
|
| 301 |
return got.UnstructuredContent() |
| 302 |
}, |
| 303 |
}, |
| 304 |
{ |
| 305 |
name: "get", |
| 306 |
response: "nonlist", |
| 307 |
do: func(t *testing.T, client dynamic.ResourceInterface) interface{} { |
| 308 |
got, err := client.Get(context.Background(), "name", metav1.GetOptions{}) |
| 309 |
if err != nil { |
| 310 |
t.Fatal(err) |
| 311 |
} |
| 312 |
|
| 313 |
return got.UnstructuredContent() |
| 314 |
}, |
| 315 |
}, |
| 316 |
{ |
| 317 |
name: "list", |
| 318 |
response: "list", |
| 319 |
do: func(t *testing.T, client dynamic.ResourceInterface) interface{} { |
| 320 |
got, err := client.List(context.Background(), metav1.ListOptions{}) |
| 321 |
if err != nil { |
| 322 |
t.Fatal(err) |
| 323 |
} |
| 324 |
|
| 325 |
return got.UnstructuredContent() |
| 326 |
|
| 327 |
}, |
| 328 |
}, |
| 329 |
{ |
| 330 |
name: "watch", |
| 331 |
response: "events", |
| 332 |
do: func(t *testing.T, client dynamic.ResourceInterface) interface{} { |
| 333 |
w, err := client.Watch(context.Background(), metav1.ListOptions{}) |
| 334 |
if err != nil { |
| 335 |
t.Fatal(err) |
| 336 |
} |
| 337 |
defer w.Stop() |
| 338 |
|
| 339 |
var got []interface{} |
| 340 |
for e := range w.ResultChan() { |
| 341 |
u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&e) |
| 342 |
if err != nil { |
| 343 |
t.Fatalf("failed to convert watch event to unstructured content: %v", err) |
| 344 |
} |
| 345 |
got = append(got, u) |
| 346 |
} |
| 347 |
|
| 348 |
return got |
| 349 |
}, |
| 350 |
}, |
| 351 |
} { |
| 352 |
parentTestName := t.Name() |
| 353 |
t.Run(tc.name, func(t *testing.T) { |
| 354 |
client, err := dynamic.NewForConfig(&rest.Config{ |
| 355 |
|
| 356 |
Transport: RoundTripperFunc(func(request *http.Request) (*http.Response, error) { |
| 357 |
fd, err := os.Open(filepath.Join("testdata", filepath.FromSlash(parentTestName), "responses", tc.response)) |
| 358 |
if err != nil { |
| 359 |
t.Fatal(err) |
| 360 |
} |
| 361 |
defer func() { |
| 362 |
if err := fd.Close(); err != nil { |
| 363 |
t.Fatal(err) |
| 364 |
} |
| 365 |
}() |
| 366 |
|
| 367 |
response, err := http.ReadResponse(bufio.NewReader(fd), request) |
| 368 |
if err != nil { |
| 369 |
t.Fatal(err) |
| 370 |
} |
| 371 |
return response, nil |
| 372 |
}), |
| 373 |
}) |
| 374 |
if err != nil { |
| 375 |
t.Fatal(err) |
| 376 |
} |
| 377 |
|
| 378 |
got := tc.do(t, client.Resource(schema.GroupVersionResource{})) |
| 379 |
|
| 380 |
path := filepath.Join("testdata", filepath.FromSlash(t.Name())) |
| 381 |
if os.Getenv("UPDATE_DYNAMIC_CLIENT_FIXTURES") == "true" { |
| 382 |
fixture, err := json.Marshal(got) |
| 383 |
if err != nil { |
| 384 |
t.Fatal(err) |
| 385 |
} |
| 386 |
if err := os.WriteFile(path, fixture, os.FileMode(0644)); err != nil { |
| 387 |
t.Fatalf("failed to update fixture: %v", err) |
| 388 |
} |
| 389 |
} |
| 390 |
fixture, err := os.ReadFile(path) |
| 391 |
if err != nil { |
| 392 |
t.Fatal(err) |
| 393 |
} |
| 394 |
|
| 395 |
var want interface{} |
| 396 |
if err := json.Unmarshal(fixture, &want); err != nil { |
| 397 |
t.Fatal(err) |
| 398 |
} |
| 399 |
|
| 400 |
if diff := cmp.Diff(want, got); diff != "" { |
| 401 |
t.Errorf("unexpected diff:\n%s", diff) |
| 402 |
} |
| 403 |
}) |
| 404 |
} |
| 405 |
} |