MCPcopy
hub / github.com/kubernetes/client-go / TestDoRequestNewWayFile

Function TestDoRequestNewWayFile

rest/request_test.go:1534–1589  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1532}
1533
1534func TestDoRequestNewWayFile(t *testing.T) {
1535 reqObj := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
1536 reqBodyExpected, err := runtime.Encode(scheme.Codecs.LegacyCodec(v1.SchemeGroupVersion), reqObj)
1537 if err != nil {
1538 t.Errorf("unexpected error: %v", err)
1539 }
1540
1541 file, err := ioutil.TempFile("", "foo")
1542 if err != nil {
1543 t.Errorf("unexpected error: %v", err)
1544 }
1545 defer file.Close()
1546 defer os.Remove(file.Name())
1547
1548 _, err = file.Write(reqBodyExpected)
1549 if err != nil {
1550 t.Errorf("unexpected error: %v", err)
1551 }
1552
1553 expectedObj := &v1.Service{Spec: v1.ServiceSpec{Ports: []v1.ServicePort{{
1554 Protocol: "TCP",
1555 Port: 12345,
1556 TargetPort: intstr.FromInt(12345),
1557 }}}}
1558 expectedBody, _ := runtime.Encode(scheme.Codecs.LegacyCodec(v1.SchemeGroupVersion), expectedObj)
1559 fakeHandler := utiltesting.FakeHandler{
1560 StatusCode: 200,
1561 ResponseBody: string(expectedBody),
1562 T: t,
1563 }
1564 testServer := httptest.NewServer(&fakeHandler)
1565 defer testServer.Close()
1566 c := testRESTClient(t, testServer)
1567 wasCreated := true
1568 obj, err := c.Verb("POST").
1569 Prefix("foo/bar", "baz").
1570 Timeout(time.Second).
1571 Body(file.Name()).
1572 Do().WasCreated(&wasCreated).Get()
1573 if err != nil {
1574 t.Errorf("Unexpected error: %v %#v", err, err)
1575 return
1576 }
1577 if obj == nil {
1578 t.Error("nil obj")
1579 } else if !apiequality.Semantic.DeepDerivative(expectedObj, obj) {
1580 t.Errorf("Expected: %#v, got %#v", expectedObj, obj)
1581 }
1582 if wasCreated {
1583 t.Errorf("expected object was created")
1584 }
1585 tmpStr := string(reqBodyExpected)
1586 requestURL := defaultResourcePathWithPrefix("foo/bar/baz", "", "", "")
1587 requestURL += "?timeout=1s"
1588 fakeHandler.ValidateRequest(t, requestURL, "POST", &tmpStr)
1589}
1590
1591func TestWasCreated(t *testing.T) {

Callers

nothing calls this directly

Calls 15

ValidateRequestMethod · 0.95
testRESTClientFunction · 0.85
WriteMethod · 0.80
WasCreatedMethod · 0.80
BodyMethod · 0.80
TimeoutMethod · 0.80
PrefixMethod · 0.80
ErrorfMethod · 0.65
CloseMethod · 0.65
GetMethod · 0.65
DoMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…