How to use Verb method of client Package

Best Testkube code snippet using client.Verb

scale_test.go

Source:scale_test.go Github

copy

Full Screen

...77 if len(actions) != len(scaleClientExpectedAction) {78 t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))79 }80 for i, verb := range scaleClientExpectedAction {81 if actions[i].GetVerb() != verb {82 t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)83 }84 }85}86func TestReplicationControllerScaleInvalid(t *testing.T) {87 verbsOnError := map[string]*kerrors.StatusError{88 "patch": kerrors.NewInvalid(api.Kind("Status"), "foo", nil),89 }90 scaleClientExpectedAction := []string{"patch"}91 scaleClient := createFakeScaleClient("replicationcontrollers", "foo-v1", 1, verbsOnError)92 scaler := NewScaler(scaleClient)93 count := uint(3)94 name := "foo-v1"95 namespace := "default"96 scaleFunc := ScaleCondition(scaler, nil, namespace, name, count, nil, rcgvr)97 pass, err := scaleFunc()98 if pass {99 t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)100 }101 if err == nil {102 t.Errorf("Expected error on invalid update failure, got %v", err)103 }104 actions := scaleClient.Actions()105 if len(actions) != len(scaleClientExpectedAction) {106 t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))107 }108 for i, verb := range scaleClientExpectedAction {109 if actions[i].GetVerb() != verb {110 t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)111 }112 }113}114func TestReplicationControllerScale(t *testing.T) {115 scaleClientExpectedAction := []string{"patch"}116 scaleClient := createFakeScaleClient("replicationcontrollers", "foo-v1", 2, nil)117 scaler := NewScaler(scaleClient)118 count := uint(3)119 name := "foo-v1"120 err := scaler.Scale("default", name, count, nil, nil, nil, rcgvr)121 if err != nil {122 t.Fatalf("unexpected error occurred = %v while scaling the resource", err)123 }124 actions := scaleClient.Actions()125 if len(actions) != len(scaleClientExpectedAction) {126 t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))127 }128 for i, verb := range scaleClientExpectedAction {129 if actions[i].GetVerb() != verb {130 t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)131 }132 }133}134func TestReplicationControllerScaleFailsPreconditions(t *testing.T) {135 scaleClientExpectedAction := []string{"get"}136 scaleClient := createFakeScaleClient("replicationcontrollers", "foo", 10, nil)137 scaler := NewScaler(scaleClient)138 preconditions := ScalePrecondition{2, ""}139 count := uint(3)140 name := "foo"141 err := scaler.Scale("default", name, count, &preconditions, nil, nil, rcgvr)142 if err == nil {143 t.Fatal("expected to get an error but none was returned")144 }145 actions := scaleClient.Actions()146 if len(actions) != len(scaleClientExpectedAction) {147 t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))148 }149 for i, verb := range scaleClientExpectedAction {150 if actions[i].GetVerb() != verb {151 t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)152 }153 }154}155func TestDeploymentScaleRetry(t *testing.T) {156 verbsOnError := map[string]*kerrors.StatusError{157 "patch": kerrors.NewConflict(api.Resource("Status"), "foo", nil),158 }159 scaleClientExpectedAction := []string{"patch", "get"}160 scaleClient := createFakeScaleClient("deployments", "foo", 2, verbsOnError)161 scaler := NewScaler(scaleClient)162 count := uint(3)163 name := "foo"164 namespace := "default"165 scaleFunc := ScaleCondition(scaler, nil, namespace, name, count, nil, deploygvr)166 pass, err := scaleFunc()167 if pass != false {168 t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)169 }170 if err != nil {171 t.Errorf("Did not expect an error on update failure, got %v", err)172 }173 preconditions := &ScalePrecondition{3, ""}174 scaleFunc = ScaleCondition(scaler, preconditions, namespace, name, count, nil, deploygvr)175 pass, err = scaleFunc()176 if err == nil {177 t.Error("Expected error on precondition failure")178 }179 actions := scaleClient.Actions()180 if len(actions) != len(scaleClientExpectedAction) {181 t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))182 }183 for i, verb := range scaleClientExpectedAction {184 if actions[i].GetVerb() != verb {185 t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)186 }187 }188}189func TestDeploymentScale(t *testing.T) {190 scaleClientExpectedAction := []string{"patch"}191 scaleClient := createFakeScaleClient("deployments", "foo", 2, nil)192 scaler := NewScaler(scaleClient)193 count := uint(3)194 name := "foo"195 err := scaler.Scale("default", name, count, nil, nil, nil, deploygvr)196 if err != nil {197 t.Fatal(err)198 }199 actions := scaleClient.Actions()200 if len(actions) != len(scaleClientExpectedAction) {201 t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))202 }203 for i, verb := range scaleClientExpectedAction {204 if actions[i].GetVerb() != verb {205 t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)206 }207 }208}209func TestDeploymentScaleInvalid(t *testing.T) {210 scaleClientExpectedAction := []string{"patch"}211 verbsOnError := map[string]*kerrors.StatusError{212 "patch": kerrors.NewInvalid(api.Kind("Status"), "foo", nil),213 }214 scaleClient := createFakeScaleClient("deployments", "foo", 2, verbsOnError)215 scaler := NewScaler(scaleClient)216 count := uint(3)217 name := "foo"218 namespace := "default"219 scaleFunc := ScaleCondition(scaler, nil, namespace, name, count, nil, deploygvr)220 pass, err := scaleFunc()221 if pass {222 t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)223 }224 if err == nil {225 t.Errorf("Expected error on invalid update failure, got %v", err)226 }227 actions := scaleClient.Actions()228 if len(actions) != len(scaleClientExpectedAction) {229 t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))230 }231 for i, verb := range scaleClientExpectedAction {232 if actions[i].GetVerb() != verb {233 t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)234 }235 }236}237func TestDeploymentScaleFailsPreconditions(t *testing.T) {238 scaleClientExpectedAction := []string{"get"}239 scaleClient := createFakeScaleClient("deployments", "foo", 10, nil)240 scaler := NewScaler(scaleClient)241 preconditions := ScalePrecondition{2, ""}242 count := uint(3)243 name := "foo"244 err := scaler.Scale("default", name, count, &preconditions, nil, nil, deploygvr)245 if err == nil {246 t.Fatal("exptected to get an error but none was returned")247 }248 actions := scaleClient.Actions()249 if len(actions) != len(scaleClientExpectedAction) {250 t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))251 }252 for i, verb := range scaleClientExpectedAction {253 if actions[i].GetVerb() != verb {254 t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)255 }256 }257}258func TestStatefulSetScale(t *testing.T) {259 scaleClientExpectedAction := []string{"patch"}260 scaleClient := createFakeScaleClient("statefulsets", "foo", 2, nil)261 scaler := NewScaler(scaleClient)262 count := uint(3)263 name := "foo"264 err := scaler.Scale("default", name, count, nil, nil, nil, stsgvr)265 if err != nil {266 t.Fatal(err)267 }268 actions := scaleClient.Actions()269 if len(actions) != len(scaleClientExpectedAction) {270 t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))271 }272 for i, verb := range scaleClientExpectedAction {273 if actions[i].GetVerb() != verb {274 t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)275 }276 }277}278func TestStatefulSetScaleRetry(t *testing.T) {279 scaleClientExpectedAction := []string{"patch", "get"}280 verbsOnError := map[string]*kerrors.StatusError{281 "patch": kerrors.NewConflict(api.Resource("Status"), "foo", nil),282 }283 scaleClient := createFakeScaleClient("statefulsets", "foo", 2, verbsOnError)284 scaler := NewScaler(scaleClient)285 count := uint(3)286 name := "foo"287 namespace := "default"288 scaleFunc := ScaleCondition(scaler, nil, namespace, name, count, nil, stsgvr)289 pass, err := scaleFunc()290 if pass != false {291 t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)292 }293 if err != nil {294 t.Errorf("Did not expect an error on update failure, got %v", err)295 }296 preconditions := &ScalePrecondition{3, ""}297 scaleFunc = ScaleCondition(scaler, preconditions, namespace, name, count, nil, stsgvr)298 pass, err = scaleFunc()299 if err == nil {300 t.Error("Expected error on precondition failure")301 }302 actions := scaleClient.Actions()303 if len(actions) != len(scaleClientExpectedAction) {304 t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))305 }306 for i, verb := range scaleClientExpectedAction {307 if actions[i].GetVerb() != verb {308 t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)309 }310 }311}312func TestStatefulSetScaleInvalid(t *testing.T) {313 scaleClientExpectedAction := []string{"patch"}314 verbsOnError := map[string]*kerrors.StatusError{315 "patch": kerrors.NewInvalid(api.Kind("Status"), "foo", nil),316 }317 scaleClient := createFakeScaleClient("statefulsets", "foo", 2, verbsOnError)318 scaler := NewScaler(scaleClient)319 count := uint(3)320 name := "foo"321 namespace := "default"322 scaleFunc := ScaleCondition(scaler, nil, namespace, name, count, nil, stsgvr)323 pass, err := scaleFunc()324 if pass {325 t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)326 }327 if err == nil {328 t.Errorf("Expected error on invalid update failure, got %v", err)329 }330 actions := scaleClient.Actions()331 if len(actions) != len(scaleClientExpectedAction) {332 t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))333 }334 for i, verb := range scaleClientExpectedAction {335 if actions[i].GetVerb() != verb {336 t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)337 }338 }339}340func TestStatefulSetScaleFailsPreconditions(t *testing.T) {341 scaleClientExpectedAction := []string{"get"}342 scaleClient := createFakeScaleClient("statefulsets", "foo", 10, nil)343 scaler := NewScaler(scaleClient)344 preconditions := ScalePrecondition{2, ""}345 count := uint(3)346 name := "foo"347 err := scaler.Scale("default", name, count, &preconditions, nil, nil, stsgvr)348 if err == nil {349 t.Fatal("expected to get an error but none was returned")350 }351 actions := scaleClient.Actions()352 if len(actions) != len(scaleClientExpectedAction) {353 t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))354 }355 for i, verb := range scaleClientExpectedAction {356 if actions[i].GetVerb() != verb {357 t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)358 }359 }360}361func TestReplicaSetScale(t *testing.T) {362 scaleClientExpectedAction := []string{"patch"}363 scaleClient := createFakeScaleClient("replicasets", "foo", 10, nil)364 scaler := NewScaler(scaleClient)365 count := uint(3)366 name := "foo"367 err := scaler.Scale("default", name, count, nil, nil, nil, rsgvr)368 if err != nil {369 t.Fatal(err)370 }371 actions := scaleClient.Actions()372 if len(actions) != len(scaleClientExpectedAction) {373 t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))374 }375 for i, verb := range scaleClientExpectedAction {376 if actions[i].GetVerb() != verb {377 t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)378 }379 }380}381func TestReplicaSetScaleRetry(t *testing.T) {382 verbsOnError := map[string]*kerrors.StatusError{383 "patch": kerrors.NewConflict(api.Resource("Status"), "foo", nil),384 }385 scaleClientExpectedAction := []string{"patch", "get"}386 scaleClient := createFakeScaleClient("replicasets", "foo", 2, verbsOnError)387 scaler := NewScaler(scaleClient)388 count := uint(3)389 name := "foo"390 namespace := "default"391 scaleFunc := ScaleCondition(scaler, nil, namespace, name, count, nil, rsgvr)392 pass, err := scaleFunc()393 if pass != false {394 t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)395 }396 if err != nil {397 t.Errorf("Did not expect an error on update failure, got %v", err)398 }399 preconditions := &ScalePrecondition{3, ""}400 scaleFunc = ScaleCondition(scaler, preconditions, namespace, name, count, nil, rsgvr)401 pass, err = scaleFunc()402 if err == nil {403 t.Error("Expected error on precondition failure")404 }405 actions := scaleClient.Actions()406 if len(actions) != len(scaleClientExpectedAction) {407 t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))408 }409 for i, verb := range scaleClientExpectedAction {410 if actions[i].GetVerb() != verb {411 t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)412 }413 }414}415func TestReplicaSetScaleInvalid(t *testing.T) {416 verbsOnError := map[string]*kerrors.StatusError{417 "patch": kerrors.NewInvalid(api.Kind("Status"), "foo", nil),418 }419 scaleClientExpectedAction := []string{"patch"}420 scaleClient := createFakeScaleClient("replicasets", "foo", 2, verbsOnError)421 scaler := NewScaler(scaleClient)422 count := uint(3)423 name := "foo"424 namespace := "default"425 scaleFunc := ScaleCondition(scaler, nil, namespace, name, count, nil, rsgvr)426 pass, err := scaleFunc()427 if pass {428 t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)429 }430 if err == nil {431 t.Errorf("Expected error on invalid update failure, got %v", err)432 }433 actions := scaleClient.Actions()434 if len(actions) != len(scaleClientExpectedAction) {435 t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))436 }437 for i, verb := range scaleClientExpectedAction {438 if actions[i].GetVerb() != verb {439 t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)440 }441 }442}443func TestReplicaSetsGetterFailsPreconditions(t *testing.T) {444 scaleClientExpectedAction := []string{"get"}445 scaleClient := createFakeScaleClient("replicasets", "foo", 10, nil)446 scaler := NewScaler(scaleClient)447 preconditions := ScalePrecondition{2, ""}448 count := uint(3)449 name := "foo"450 err := scaler.Scale("default", name, count, &preconditions, nil, nil, rsgvr)451 if err == nil {452 t.Fatal("expected to get an error but non was returned")453 }454 actions := scaleClient.Actions()455 if len(actions) != len(scaleClientExpectedAction) {456 t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))457 }458 for i, verb := range scaleClientExpectedAction {459 if actions[i].GetVerb() != verb {460 t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)461 }462 }463}464// TestGenericScaleSimple exercises GenericScaler.ScaleSimple method465func TestGenericScaleSimple(t *testing.T) {466 // test data467 scaleClient := createFakeScaleClient("deployments", "abc", 5, nil)468 // expected actions469 scaleClientExpectedAction := []string{"patch", "get", "update", "get", "update", "get", "get", "update", "get"}470 // test scenarios471 scenarios := []struct {472 name string473 precondition *ScalePrecondition474 newSize int475 targetGVR schema.GroupVersionResource476 resName string477 scaleGetter scale.ScalesGetter478 expectError bool479 }{480 // scenario 0: scale up the "abc" deployment without precondition481 {482 name: "scale up the \"abc\" deployment without precondition",483 precondition: nil,484 newSize: 10,485 targetGVR: deploygvr,486 resName: "abc",487 scaleGetter: scaleClient,488 },489 // scenario 1: scale up the "abc" deployment490 {491 name: "scale up the \"abc\" deployment",492 precondition: &ScalePrecondition{10, ""},493 newSize: 20,494 targetGVR: deploygvr,495 resName: "abc",496 scaleGetter: scaleClient,497 },498 // scenario 2: scale down the "abc" deployment499 {500 name: "scale down the \"abs\" deployment",501 precondition: &ScalePrecondition{20, ""},502 newSize: 5,503 targetGVR: deploygvr,504 resName: "abc",505 scaleGetter: scaleClient,506 },507 // scenario 3: precondition error, expected size is 1,508 // note that the previous scenario (2) set the size to 5509 {510 name: "precondition error, expected size is 1",511 precondition: &ScalePrecondition{1, ""},512 newSize: 5,513 targetGVR: deploygvr,514 resName: "abc",515 scaleGetter: scaleClient,516 expectError: true,517 },518 // scenario 4: precondition is not validated when the precondition size is set to -1519 {520 name: "precondition is not validated when the size is set to -1",521 precondition: &ScalePrecondition{-1, ""},522 newSize: 5,523 targetGVR: deploygvr,524 resName: "abc",525 scaleGetter: scaleClient,526 },527 // scenario 5: precondition error, resource version mismatch528 {529 name: "precondition error, resource version mismatch",530 precondition: &ScalePrecondition{5, "v1"},531 newSize: 5,532 targetGVR: deploygvr,533 resName: "abc",534 scaleGetter: scaleClient,535 expectError: true,536 },537 }538 // act539 for index, scenario := range scenarios {540 t.Run(fmt.Sprintf("running scenario %d: %s", index+1, scenario.name), func(t *testing.T) {541 target := NewScaler(scenario.scaleGetter)542 resVersion, err := target.ScaleSimple("default", scenario.resName, scenario.precondition, uint(scenario.newSize), scenario.targetGVR)543 if scenario.expectError && err == nil {544 t.Fatal("expected an error but was not returned")545 }546 if !scenario.expectError && err != nil {547 t.Fatalf("unexpected error: %v", err)548 }549 if resVersion != "" {550 t.Fatalf("unexpected resource version returned = %s, wanted = %s", resVersion, "")551 }552 })553 }554 // check actions555 actions := scaleClient.Actions()556 if len(actions) != len(scaleClientExpectedAction) {557 t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))558 }559 for i, verb := range scaleClientExpectedAction {560 if actions[i].GetVerb() != verb {561 t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)562 }563 }564}565// TestGenericScale exercises GenericScaler.Scale method566func TestGenericScale(t *testing.T) {567 // test data568 scaleClient := createFakeScaleClient("deployments", "abc", 5, nil)569 // expected actions570 scaleClientExpectedAction := []string{"patch", "get", "update", "get", "get"}571 // test scenarios572 scenarios := []struct {573 name string574 precondition *ScalePrecondition575 newSize int576 targetGVR schema.GroupVersionResource577 resName string578 scaleGetter scale.ScalesGetter579 waitForReplicas *RetryParams580 expectError bool581 }{582 // scenario 0: scale up the "abc" deployment without precondition583 {584 name: "scale up the \"abc\" deployment without precondition",585 precondition: nil,586 newSize: 10,587 targetGVR: deploygvr,588 resName: "abc",589 scaleGetter: scaleClient,590 },591 // scenario 1: scale up the "abc" deployment592 {593 name: "scale up the \"abc\" deployment",594 precondition: &ScalePrecondition{10, ""},595 newSize: 20,596 targetGVR: deploygvr,597 resName: "abc",598 scaleGetter: scaleClient,599 },600 //scenario 2: a resource name cannot be empty601 {602 name: "a resource name cannot be empty",603 precondition: &ScalePrecondition{10, ""},604 newSize: 20,605 targetGVR: deploygvr,606 resName: "",607 scaleGetter: scaleClient,608 expectError: true,609 },610 // scenario 3: wait for replicas error due to status.Replicas != spec.Replicas611 {612 name: "wait for replicas error due to status.Replicas != spec.Replicas",613 precondition: &ScalePrecondition{10, ""},614 newSize: 20,615 targetGVR: deploygvr,616 resName: "abc",617 scaleGetter: scaleClient,618 waitForReplicas: &RetryParams{time.Duration(5 * time.Second), time.Duration(5 * time.Second)},619 expectError: true,620 },621 }622 // act623 for _, scenario := range scenarios {624 t.Run(scenario.name, func(t *testing.T) {625 target := NewScaler(scenario.scaleGetter)626 err := target.Scale("default", scenario.resName, uint(scenario.newSize), scenario.precondition, nil, scenario.waitForReplicas, scenario.targetGVR)627 if scenario.expectError && err == nil {628 t.Fatal("expected an error but was not returned")629 }630 if !scenario.expectError && err != nil {631 t.Fatalf("unexpected error: %v", err)632 }633 })634 }635 // check actions636 actions := scaleClient.Actions()637 if len(actions) != len(scaleClientExpectedAction) {638 t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))639 }640 for i, verb := range scaleClientExpectedAction {641 if actions[i].GetVerb() != verb {642 t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)643 }644 }645}646func createFakeScaleClient(resource string, resourceName string, replicas int, errorsOnVerb map[string]*kerrors.StatusError) *fakescale.FakeScaleClient {647 shouldReturnAnError := func(verb string) (*kerrors.StatusError, bool) {648 if anError, anErrorExists := errorsOnVerb[verb]; anErrorExists {649 return anError, true650 }651 return &kerrors.StatusError{}, false652 }653 newReplicas := int32(replicas)654 scaleClient := &fakescale.FakeScaleClient{}655 scaleClient.AddReactor("get", resource, func(rawAction testcore.Action) (handled bool, ret runtime.Object, err error) {656 action := rawAction.(testcore.GetAction)657 if action.GetName() != resourceName {658 return true, nil, fmt.Errorf("expected = %s, got = %s", resourceName, action.GetName())659 }660 if anError, should := shouldReturnAnError("get"); should {661 return true, nil, anError662 }...

Full Screen

Full Screen

tags.go

Source:tags.go Github

copy

Full Screen

...19)20var supportedTags = []string{21 "genclient",22 "genclient:nonNamespaced",23 "genclient:noVerbs",24 "genclient:onlyVerbs",25 "genclient:skipVerbs",26 "genclient:noStatus",27 "genclient:readonly",28 "genclient:method",29}30// SupportedVerbs is a list of supported verbs for +onlyVerbs and +skipVerbs.31var SupportedVerbs = []string{32 "create",33 "update",34 "updateStatus",35 "delete",36 "deleteCollection",37 "get",38 "list",39 "watch",40 "patch",41}42// ReadonlyVerbs represents a list of read-only verbs.43var ReadonlyVerbs = []string{44 "get",45 "list",46 "watch",47}48// genClientPrefix is the default prefix for all genclient tags.49const genClientPrefix = "genclient:"50// unsupportedExtensionVerbs is a list of verbs we don't support generating51// extension client functions for.52var unsupportedExtensionVerbs = []string{53 "updateStatus",54 "deleteCollection",55 "watch",56 "delete",57}58// inputTypeSupportedVerbs is a list of verb types that supports overriding the59// input argument type.60var inputTypeSupportedVerbs = []string{61 "create",62 "update",63}64// resultTypeSupportedVerbs is a list of verb types that supports overriding the65// resulting type.66var resultTypeSupportedVerbs = []string{67 "create",68 "update",69 "get",70 "list",71 "patch",72}73// Extensions allows to extend the default set of client verbs74// (CRUD+watch+patch+list+deleteCollection) for a given type with custom defined75// verbs. Custom verbs can have custom input and result types and also allow to76// use a sub-resource in a request instead of top-level resource type.77//78// Example:79//80// +genclient:method=UpdateScale,verb=update,subresource=scale,input=Scale,result=Scale81//82// type ReplicaSet struct { ... }83//84// The 'method=UpdateScale' is the name of the client function.85// The 'verb=update' here means the client function will use 'PUT' action.86// The 'subresource=scale' means we will use SubResource template to generate this client function.87// The 'input' is the input type used for creation (function argument).88// The 'result' (not needed in this case) is the result type returned from the89// client function.90//91type extension struct {92 // VerbName is the name of the custom verb (Scale, Instantiate, etc..)93 VerbName string94 // VerbType is the type of the verb (only verbs from SupportedVerbs are95 // supported)96 VerbType string97 // SubResourcePath defines a path to a sub-resource to use in the request.98 // (optional)99 SubResourcePath string100 // InputTypeOverride overrides the input parameter type for the verb. By101 // default the original type is used. Overriding the input type only works for102 // "create" and "update" verb types. The given type must exists in the same103 // package as the original type.104 // (optional)105 InputTypeOverride string106 // ResultTypeOverride overrides the resulting object type for the verb. By107 // default the original type is used. Overriding the result type works.108 // (optional)109 ResultTypeOverride string110}111// IsSubresource indicates if this extension should generate the sub-resource.112func (e *extension) IsSubresource() bool {113 return len(e.SubResourcePath) > 0114}115// HasVerb checks if the extension matches the given verb.116func (e *extension) HasVerb(verb string) bool {117 return e.VerbType == verb118}119// Input returns the input override package path and the type.120func (e *extension) Input() (string, string) {121 parts := strings.Split(e.InputTypeOverride, ".")122 return parts[len(parts)-1], strings.Join(parts[0:len(parts)-1], ".")123}124// Result returns the result override package path and the type.125func (e *extension) Result() (string, string) {126 parts := strings.Split(e.ResultTypeOverride, ".")127 return parts[len(parts)-1], strings.Join(parts[0:len(parts)-1], ".")128}129// Tags represents a genclient configuration for a single type.130type Tags struct {131 // +genclient132 GenerateClient bool133 // +genclient:nonNamespaced134 NonNamespaced bool135 // +genclient:noStatus136 NoStatus bool137 // +genclient:noVerbs138 NoVerbs bool139 // +genclient:skipVerbs=get,update140 // +genclient:onlyVerbs=create,delete141 SkipVerbs []string142 // +genclient:method=UpdateScale,verb=update,subresource=scale,input=Scale,result=Scale143 Extensions []extension144}145// HasVerb returns true if we should include the given verb in final client interface and146// generate the function for it.147func (t Tags) HasVerb(verb string) bool {148 if len(t.SkipVerbs) == 0 {149 return true150 }151 for _, s := range t.SkipVerbs {152 if verb == s {153 return false154 }155 }156 return true157}158// MustParseClientGenTags calls ParseClientGenTags but instead of returning error it panics.159func MustParseClientGenTags(lines []string) Tags {160 tags, err := ParseClientGenTags(lines)161 if err != nil {162 panic(err.Error())163 }164 return tags165}166// ParseClientGenTags parse the provided genclient tags and validates that no unknown167// tags are provided.168func ParseClientGenTags(lines []string) (Tags, error) {169 ret := Tags{}170 values := types.ExtractCommentTags("+", lines)171 value := []string{}172 value, ret.GenerateClient = values["genclient"]173 // Check the old format and error when used to avoid generating client when //+genclient=false174 if len(value) > 0 && len(value[0]) > 0 {175 return ret, fmt.Errorf("+genclient=%s is invalid, use //+genclient if you want to generate client or omit it when you want to disable generation", value)176 }177 _, ret.NonNamespaced = values[genClientPrefix+"nonNamespaced"]178 // Check the old format and error when used179 if value := values["nonNamespaced"]; len(value) > 0 && len(value[0]) > 0 {180 return ret, fmt.Errorf("+nonNamespaced=%s is invalid, use //+genclient:nonNamespaced instead", value[0])181 }182 _, ret.NoVerbs = values[genClientPrefix+"noVerbs"]183 _, ret.NoStatus = values[genClientPrefix+"noStatus"]184 onlyVerbs := []string{}185 if _, isReadonly := values[genClientPrefix+"readonly"]; isReadonly {186 onlyVerbs = ReadonlyVerbs187 }188 // Check the old format and error when used189 if value := values["readonly"]; len(value) > 0 && len(value[0]) > 0 {190 return ret, fmt.Errorf("+readonly=%s is invalid, use //+genclient:readonly instead", value[0])191 }192 if v, exists := values[genClientPrefix+"skipVerbs"]; exists {193 ret.SkipVerbs = strings.Split(v[0], ",")194 }195 if v, exists := values[genClientPrefix+"onlyVerbs"]; exists || len(onlyVerbs) > 0 {196 if len(v) > 0 {197 onlyVerbs = append(onlyVerbs, strings.Split(v[0], ",")...)198 }199 skipVerbs := []string{}200 for _, m := range SupportedVerbs {201 skip := true202 for _, o := range onlyVerbs {203 if o == m {204 skip = false205 break206 }207 }208 // Check for conflicts209 for _, v := range skipVerbs {210 if v == m {211 return ret, fmt.Errorf("verb %q used both in genclient:skipVerbs and genclient:onlyVerbs", v)212 }213 }214 if skip {215 skipVerbs = append(skipVerbs, m)216 }217 }218 ret.SkipVerbs = skipVerbs219 }220 var err error221 if ret.Extensions, err = parseClientExtensions(values); err != nil {222 return ret, err223 }224 return ret, validateClientGenTags(values)225}226func parseClientExtensions(tags map[string][]string) ([]extension, error) {227 var ret []extension228 for name, values := range tags {229 if !strings.HasPrefix(name, genClientPrefix+"method") {230 continue231 }232 for _, value := range values {233 // the value comes in this form: "Foo,verb=create"234 ext := extension{}235 parts := strings.Split(value, ",")236 if len(parts) == 0 {237 return nil, fmt.Errorf("invalid of empty extension verb name: %q", value)238 }239 // The first part represents the name of the extension240 ext.VerbName = parts[0]241 if len(ext.VerbName) == 0 {242 return nil, fmt.Errorf("must specify a verb name (// +genclient:method=Foo,verb=create)")243 }244 // Parse rest of the arguments245 params := parts[1:]246 for _, p := range params {247 parts := strings.Split(p, "=")248 if len(parts) != 2 {249 return nil, fmt.Errorf("invalid extension tag specification %q", p)250 }251 key, val := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])252 if len(val) == 0 {253 return nil, fmt.Errorf("empty value of %q for %q extension", key, ext.VerbName)254 }255 switch key {256 case "verb":257 ext.VerbType = val258 case "subresource":259 ext.SubResourcePath = val260 case "input":261 ext.InputTypeOverride = val262 case "result":263 ext.ResultTypeOverride = val264 default:265 return nil, fmt.Errorf("unknown extension configuration key %q", key)266 }267 }268 // Validate resulting extension configuration269 if len(ext.VerbType) == 0 {270 return nil, fmt.Errorf("verb type must be specified (use '// +genclient:method=%s,verb=create')", ext.VerbName)271 }272 if len(ext.ResultTypeOverride) > 0 {273 supported := false274 for _, v := range resultTypeSupportedVerbs {275 if ext.VerbType == v {276 supported = true277 break278 }279 }280 if !supported {281 return nil, fmt.Errorf("%s: result type is not supported for %q verbs (supported verbs: %#v)", ext.VerbName, ext.VerbType, resultTypeSupportedVerbs)282 }283 }284 if len(ext.InputTypeOverride) > 0 {285 supported := false286 for _, v := range inputTypeSupportedVerbs {287 if ext.VerbType == v {288 supported = true289 break290 }291 }292 if !supported {293 return nil, fmt.Errorf("%s: input type is not supported for %q verbs (supported verbs: %#v)", ext.VerbName, ext.VerbType, inputTypeSupportedVerbs)294 }295 }296 for _, t := range unsupportedExtensionVerbs {297 if ext.VerbType == t {298 return nil, fmt.Errorf("verb %q is not supported by extension generator", ext.VerbType)299 }300 }301 ret = append(ret, ext)302 }303 }304 return ret, nil305}306// validateTags validates that only supported genclient tags were provided.307func validateClientGenTags(values map[string][]string) error {308 for _, k := range supportedTags {309 delete(values, k)310 }311 for key := range values {312 if strings.HasPrefix(key, strings.TrimSuffix(genClientPrefix, ":")) {...

Full Screen

Full Screen

Verb

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 client := &http.Client{}4 if err != nil {5 panic(err)6 }7 resp, err := client.Do(req)8 if err != nil {9 panic(err)10 }11 fmt.Println(resp.Status)12}

Full Screen

Full Screen

Verb

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 client := &http.Client{}4 if err != nil {5 log.Fatal(err)6 }7 resp, err := client.Do(req)8 if err != nil {9 log.Fatal(err)10 }11 fmt.Println(resp.Status)12}13import (14func main() {15 if err != nil {16 log.Fatal(err)17 }18 fmt.Println(resp.Status)19}20import

Full Screen

Full Screen

Verb

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 client := &http.Client{}4 resp, _ := client.Do(req)5 fmt.Println("Response status:", resp.Status)6 body, _ := ioutil.ReadAll(resp.Body)7 fmt.Println("Response body:", string(body))8 fmt.Println("Response status:", resp.Status)9 body, _ = ioutil.ReadAll(resp.Body)10 fmt.Println("Response body:", string(body))11 fmt.Println("Response status:", resp.Status)12 body, _ = ioutil.ReadAll(resp.Body)13 fmt.Println("Response body:", string(body))14 fmt.Println("Response status:", resp.Status)15 body, _ = ioutil.ReadAll(resp.Body)16 fmt.Println("Response body:", string(body))17 req.Header.Add("If-None-Match", `W/"wyzzy"`)18 resp, _ = client.Do(req)19 fmt.Println("Response status:", resp.Status)20 body, _ = ioutil.ReadAll(resp.Body)21 fmt.Println("Response body:", string(body))22}

Full Screen

Full Screen

Verb

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 client := http.Client{}4 if err != nil {5 panic(err)6 }7 response, err := client.Do(request)8 if err != nil {9 panic(err)10 }11 fmt.Println("Status: ", response.Status)12 fmt.Println("StatusCode: ", response.StatusCode)13 fmt.Println("Proto: ", response.Proto)14 fmt.Println("ProtoMajor: ", response.ProtoMajor)15 fmt.Println("ProtoMinor: ", response.ProtoMinor)16 fmt.Println("Header: ", response.Header)17 fmt.Println("Body: ", response.Body)18 fmt.Println("ContentLength: ", response.ContentLength)19 fmt.Println("TransferEncoding: ", response.TransferEncoding)20 fmt.Println("Close: ", response.Close)21 fmt.Println("Request: ", response.Request)22 fmt.Println("TLS: ", response.TLS)23}24Header: map[Cache-Control:[max-age=604800] Content-Type:[text/html; charset=utf-8] Date:[Thu, 01 Aug 2019 07:58:12 GMT] Etag:["1541025663+ident"] Expires:[Thu, 08 Aug 2019 07:58:12 GMT] Last-Modified:[Fri, 09 Aug 2013 23:54:35 GMT] Server:[ECS (dcb/7E16)] Vary:[Accept-Encoding] X-Cache:[HIT] Content-Length:[60641]]

Full Screen

Full Screen

Verb

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 client := resty.New()4 if err != nil {5 fmt.Println("Error in getting response")6 }7 fmt.Println(resp)8}9{200 OK 200 HTTP/1.1 1 1 map[Cache-Control:[no-cache, no-store, must-revalidate] Content-Type:[application/json] Date:[Thu, 28 May 2020 05:27:18 GMT] Expires:[0] Pragma:[no-cache] Server:[nginx] X-Content-Type-Options:[nosniff] X-Frame-Options:[SAMEORIGIN] X-XSS-Protection:[1; mode=block]] {0xc0000a4f00 0xc0000a4f00} 0xc0000a4f00 0 [] false map[] 0xc0000a4f00 <nil> [] false}10import (11func main() {12 client := resty.New()13 if err != nil {14 fmt.Println("Error in getting response")15 }16 fmt.Println(resp)17}18{200 OK 200 HTTP/1.1 1 1 map[Cache-Control:[no-cache, no-store, must-revalidate] Content-Type:[application/json] Date:[Thu, 28 May 2020 05:28:23 GMT] Expires:[0] Pragma:[no-cache] Server:[nginx] X-Content-Type-Options:[nosniff] X-Frame-Options:[SAMEORIGIN] X-XSS-Protection:[1; mode=block]] {0xc0000a4f00 0xc0000a4f00} 0xc0000a4f00 0 [] false map[] 0xc0000a4f00 <nil> [] false}

Full Screen

Full Screen

Verb

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 client := resty.New()4 if err != nil {5 panic(err)6 }7 fmt.Println("response Status:", resp.Status())8 fmt.Println("response Headers:", resp.Header())9 fmt.Println("response Body:", resp)10}11response Headers: map[Access-Control-Allow-Credentials:[true] Access-Control-Allow-Origin:[*] Connection:[keep-alive] Content-Length:[101] Content-Type:[application/json; charset=utf-8] Date:[Wed, 12 Aug 2020 18:12:47 GMT] Etag:[W/"65-17yyLsLJU1yDrQlSbxbtVn0FkLk"] Via:[1.1 vegur] X-Powered-By:[Express]]12response Body: &{201 Created 201 Created map[Access-Control-Allow-Credentials:[true] Access-Control-Allow-Origin:[*] Connection:[keep-alive] Content-Length:[101] Content-Type:[application/json; charset=utf-8] Date:[Wed, 12 Aug 2020 18:12:47 GMT] Etag:[W/"65-17yyLsLJU1yDrQlSbxbtVn0FkLk"] Via:[1.1 vegur] X-Powered-By:[Express]] 0xc00000e4c0 {0xc0000a0000 0xc0000a0000} 0xc00000e4b0 0xc00000e4c0 0xc00000e4d0 <nil> 0xc00000e4e0 <nil> 0xc00000e4f0 <nil> 0xc00000e500 <nil> 0xc00000e510 <nil> 0xc00000e520 <nil> 0xc

Full Screen

Full Screen

Verb

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 client := &http.Client{}4 if err != nil {5 fmt.Println(err)6 }7 req.Header.Set("X-Custom-Header", "myvalue")8 resp, err := client.Do(req)9 if err != nil {10 fmt.Println(err)11 }12 defer resp.Body.Close()13 fmt.Println("response Status:", resp.Status)14 fmt.Println("response Headers:", resp.Header)15}16import (17func main() {18 client := &http.Client{}19 if err != nil {20 fmt.Println(err)21 }22 req.Header.Set("X-Custom-Header", "myvalue")23 resp, err := client.Do(req)24 if err != nil {25 fmt.Println(err)26 }27 defer resp.Body.Close()28 fmt.Println("response Status:", resp.Status)29 fmt.Println("response Headers:", resp.Header)30}31response Headers: map[Content-Type:[text/plain; charset=utf-8] Date:[Thu, 29 Nov 2018 10:24:30 GMT] Content-Length:[13]]

Full Screen

Full Screen

Verb

Using AI Code Generation

copy

Full Screen

1func main() {2 var client = new(Client)3 var request = new(Request)4 var response = new(Response)5 request.Header = make(map[string]string)6 response = client.Verb(request)7 fmt.Println(response)8}9&{200 OK map[Content-Type:[application/json; charset=utf-8] Date:[Thu, 22 Aug 2019 12:25:12 GMT] Content-Length:[0]] }

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testkube automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful