How to use APIVersion method of client Package

Best Testkube code snippet using client.APIVersion

exec_test.go

Source:exec_test.go Github

copy

Full Screen

...105 {Name: "3", Value: "4"},106 {Name: "5", Value: "6"},107 {Name: "7", Value: "8"},108 },109 APIVersion: "client.authentication.k8s.io/v1alpha1",110 ProvideClusterInfo: true,111 }112 c1c := &clientauthentication.Cluster{113 Server: "foo",114 TLSServerName: "bar",115 CertificateAuthorityData: []byte("baz"),116 Config: &runtime.Unknown{117 TypeMeta: runtime.TypeMeta{118 APIVersion: "",119 Kind: "",120 },121 Raw: []byte(`{"apiVersion":"group/v1","kind":"PluginConfig","spec":{"audience":"snorlax"}}`),122 ContentEncoding: "",123 ContentType: "application/json",124 },125 }126 c2 := &api.ExecConfig{127 Command: "foo-bar",128 Args: []string{"1", "2"},129 Env: []api.ExecEnvVar{130 {Name: "3", Value: "4"},131 {Name: "5", Value: "6"},132 {Name: "7", Value: "8"},133 },134 APIVersion: "client.authentication.k8s.io/v1alpha1",135 ProvideClusterInfo: true,136 }137 c2c := &clientauthentication.Cluster{138 Server: "foo",139 TLSServerName: "bar",140 CertificateAuthorityData: []byte("baz"),141 Config: &runtime.Unknown{142 TypeMeta: runtime.TypeMeta{143 APIVersion: "",144 Kind: "",145 },146 Raw: []byte(`{"apiVersion":"group/v1","kind":"PluginConfig","spec":{"audience":"snorlax"}}`),147 ContentEncoding: "",148 ContentType: "application/json",149 },150 }151 c3 := &api.ExecConfig{152 Command: "foo-bar",153 Args: []string{"1", "2"},154 Env: []api.ExecEnvVar{155 {Name: "3", Value: "4"},156 {Name: "5", Value: "6"},157 },158 APIVersion: "client.authentication.k8s.io/v1alpha1",159 }160 c3c := &clientauthentication.Cluster{161 Server: "foo",162 TLSServerName: "bar",163 CertificateAuthorityData: []byte("baz"),164 Config: &runtime.Unknown{165 TypeMeta: runtime.TypeMeta{166 APIVersion: "",167 Kind: "",168 },169 Raw: []byte(`{"apiVersion":"group/v1","kind":"PluginConfig","spec":{"audience":"snorlax"}}`),170 ContentEncoding: "",171 ContentType: "application/json",172 },173 }174 c4 := &api.ExecConfig{175 Command: "foo-bar",176 Args: []string{"1", "2"},177 Env: []api.ExecEnvVar{178 {Name: "3", Value: "4"},179 {Name: "5", Value: "6"},180 },181 APIVersion: "client.authentication.k8s.io/v1alpha1",182 }183 c4c := &clientauthentication.Cluster{184 Server: "foo",185 TLSServerName: "bar",186 CertificateAuthorityData: []byte("baz"),187 Config: &runtime.Unknown{188 TypeMeta: runtime.TypeMeta{189 APIVersion: "",190 Kind: "",191 },192 Raw: []byte(`{"apiVersion":"group/v1","kind":"PluginConfig","spec":{"audience":"panda"}}`),193 ContentEncoding: "",194 ContentType: "application/json",195 },196 }197 // c5/c5c should be the same as c4/c4c, except c5 has ProvideClusterInfo set to true.198 c5 := &api.ExecConfig{199 Command: "foo-bar",200 Args: []string{"1", "2"},201 Env: []api.ExecEnvVar{202 {Name: "3", Value: "4"},203 {Name: "5", Value: "6"},204 },205 APIVersion: "client.authentication.k8s.io/v1alpha1",206 ProvideClusterInfo: true,207 }208 c5c := &clientauthentication.Cluster{209 Server: "foo",210 TLSServerName: "bar",211 CertificateAuthorityData: []byte("baz"),212 Config: &runtime.Unknown{213 TypeMeta: runtime.TypeMeta{214 APIVersion: "",215 Kind: "",216 },217 Raw: []byte(`{"apiVersion":"group/v1","kind":"PluginConfig","spec":{"audience":"panda"}}`),218 ContentEncoding: "",219 ContentType: "application/json",220 },221 }222 // c6 should be the same as c4, except c6 is passed with a nil cluster223 c6 := &api.ExecConfig{224 Command: "foo-bar",225 Args: []string{"1", "2"},226 Env: []api.ExecEnvVar{227 {Name: "3", Value: "4"},228 {Name: "5", Value: "6"},229 },230 APIVersion: "client.authentication.k8s.io/v1alpha1",231 }232 // c7 should be the same as c6, except c7 has stdin marked as unavailable233 c7 := &api.ExecConfig{234 Command: "foo-bar",235 Args: []string{"1", "2"},236 Env: []api.ExecEnvVar{237 {Name: "3", Value: "4"},238 {Name: "5", Value: "6"},239 },240 APIVersion: "client.authentication.k8s.io/v1alpha1",241 StdinUnavailable: true,242 }243 key1 := cacheKey(c1, c1c)244 key2 := cacheKey(c2, c2c)245 key3 := cacheKey(c3, c3c)246 key4 := cacheKey(c4, c4c)247 key5 := cacheKey(c5, c5c)248 key6 := cacheKey(c6, nil)249 key7 := cacheKey(c7, nil)250 if key1 != key2 {251 t.Error("key1 and key2 didn't match")252 }253 if key1 == key3 {254 t.Error("key1 and key3 matched")255 }256 if key2 == key3 {257 t.Error("key2 and key3 matched")258 }259 if key3 == key4 {260 t.Error("key3 and key4 matched")261 }262 if key4 == key5 {263 t.Error("key3 and key4 matched")264 }265 if key6 == key4 {266 t.Error("key6 and key4 matched")267 }268 if key6 == key7 {269 t.Error("key6 and key7 matched")270 }271}272func compJSON(t *testing.T, got, want []byte) {273 t.Helper()274 gotJSON := &bytes.Buffer{}275 wantJSON := &bytes.Buffer{}276 if err := json.Indent(gotJSON, got, "", " "); err != nil {277 t.Errorf("got invalid JSON: %v", err)278 }279 if err := json.Indent(wantJSON, want, "", " "); err != nil {280 t.Errorf("want invalid JSON: %v", err)281 }282 g := strings.TrimSpace(gotJSON.String())283 w := strings.TrimSpace(wantJSON.String())284 if g != w {285 t.Errorf("wanted %q, got %q", w, g)286 }287}288func TestRefreshCreds(t *testing.T) {289 tests := []struct {290 name string291 config api.ExecConfig292 stdinUnavailable bool293 exitCode int294 cluster *clientauthentication.Cluster295 output string296 isTerminal bool297 response *clientauthentication.Response298 wantInput string299 wantCreds credentials300 wantExpiry time.Time301 wantErr bool302 wantErrSubstr string303 }{304 {305 name: "basic-request",306 config: api.ExecConfig{307 APIVersion: "client.authentication.k8s.io/v1alpha1",308 InteractiveMode: api.IfAvailableExecInteractiveMode,309 },310 wantInput: `{311 "kind":"ExecCredential",312 "apiVersion":"client.authentication.k8s.io/v1alpha1",313 "spec": {}314 }`,315 output: `{316 "kind": "ExecCredential",317 "apiVersion": "client.authentication.k8s.io/v1alpha1",318 "status": {319 "token": "foo-bar"320 }321 }`,322 wantCreds: credentials{token: "foo-bar"},323 },324 {325 name: "interactive",326 config: api.ExecConfig{327 APIVersion: "client.authentication.k8s.io/v1alpha1",328 InteractiveMode: api.IfAvailableExecInteractiveMode,329 },330 isTerminal: true,331 wantInput: `{332 "kind":"ExecCredential",333 "apiVersion":"client.authentication.k8s.io/v1alpha1",334 "spec": {335 "interactive": true336 }337 }`,338 output: `{339 "kind": "ExecCredential",340 "apiVersion": "client.authentication.k8s.io/v1alpha1",341 "status": {342 "token": "foo-bar"343 }344 }`,345 wantCreds: credentials{token: "foo-bar"},346 },347 {348 name: "response",349 config: api.ExecConfig{350 APIVersion: "client.authentication.k8s.io/v1alpha1",351 InteractiveMode: api.IfAvailableExecInteractiveMode,352 },353 response: &clientauthentication.Response{354 Header: map[string][]string{355 "WWW-Authenticate": {`Basic realm="Access to the staging site", charset="UTF-8"`},356 },357 Code: 401,358 },359 wantInput: `{360 "kind":"ExecCredential",361 "apiVersion":"client.authentication.k8s.io/v1alpha1",362 "spec": {363 "response": {364 "header": {365 "WWW-Authenticate": [366 "Basic realm=\"Access to the staging site\", charset=\"UTF-8\""367 ]368 },369 "code": 401370 }371 }372 }`,373 output: `{374 "kind": "ExecCredential",375 "apiVersion": "client.authentication.k8s.io/v1alpha1",376 "status": {377 "token": "foo-bar"378 }379 }`,380 wantCreds: credentials{token: "foo-bar"},381 },382 {383 name: "expiry",384 config: api.ExecConfig{385 APIVersion: "client.authentication.k8s.io/v1alpha1",386 InteractiveMode: api.IfAvailableExecInteractiveMode,387 },388 wantInput: `{389 "kind":"ExecCredential",390 "apiVersion":"client.authentication.k8s.io/v1alpha1",391 "spec": {}392 }`,393 output: `{394 "kind": "ExecCredential",395 "apiVersion": "client.authentication.k8s.io/v1alpha1",396 "status": {397 "token": "foo-bar",398 "expirationTimestamp": "2006-01-02T15:04:05Z"399 }400 }`,401 wantExpiry: time.Date(2006, 01, 02, 15, 04, 05, 0, time.UTC),402 wantCreds: credentials{token: "foo-bar"},403 },404 {405 name: "no-group-version",406 config: api.ExecConfig{407 APIVersion: "client.authentication.k8s.io/v1alpha1",408 InteractiveMode: api.IfAvailableExecInteractiveMode,409 },410 wantInput: `{411 "kind":"ExecCredential",412 "apiVersion":"client.authentication.k8s.io/v1alpha1",413 "spec": {}414 }`,415 output: `{416 "kind": "ExecCredential",417 "status": {418 "token": "foo-bar"419 }420 }`,421 wantErr: true,422 },423 {424 name: "no-status",425 config: api.ExecConfig{426 APIVersion: "client.authentication.k8s.io/v1alpha1",427 InteractiveMode: api.IfAvailableExecInteractiveMode,428 },429 wantInput: `{430 "kind":"ExecCredential",431 "apiVersion":"client.authentication.k8s.io/v1alpha1",432 "spec": {}433 }`,434 output: `{435 "kind": "ExecCredential",436 "apiVersion":"client.authentication.k8s.io/v1alpha1"437 }`,438 wantErr: true,439 },440 {441 name: "no-creds",442 config: api.ExecConfig{443 APIVersion: "client.authentication.k8s.io/v1alpha1",444 InteractiveMode: api.IfAvailableExecInteractiveMode,445 },446 wantInput: `{447 "kind":"ExecCredential",448 "apiVersion":"client.authentication.k8s.io/v1alpha1",449 "spec": {}450 }`,451 output: `{452 "kind": "ExecCredential",453 "apiVersion":"client.authentication.k8s.io/v1alpha1",454 "status": {}455 }`,456 wantErr: true,457 },458 {459 name: "TLS credentials",460 config: api.ExecConfig{461 APIVersion: "client.authentication.k8s.io/v1alpha1",462 InteractiveMode: api.IfAvailableExecInteractiveMode,463 },464 wantInput: `{465 "kind":"ExecCredential",466 "apiVersion":"client.authentication.k8s.io/v1alpha1",467 "spec": {}468 }`,469 output: fmt.Sprintf(`{470 "kind": "ExecCredential",471 "apiVersion": "client.authentication.k8s.io/v1alpha1",472 "status": {473 "clientKeyData": %q,474 "clientCertificateData": %q475 }476 }`, keyData, certData),477 wantCreds: credentials{cert: validCert},478 },479 {480 name: "bad TLS credentials",481 config: api.ExecConfig{482 APIVersion: "client.authentication.k8s.io/v1alpha1",483 InteractiveMode: api.IfAvailableExecInteractiveMode,484 },485 wantInput: `{486 "kind":"ExecCredential",487 "apiVersion":"client.authentication.k8s.io/v1alpha1",488 "spec": {}489 }`,490 output: `{491 "kind": "ExecCredential",492 "apiVersion": "client.authentication.k8s.io/v1alpha1",493 "status": {494 "clientKeyData": "foo",495 "clientCertificateData": "bar"496 }497 }`,498 wantErr: true,499 },500 {501 name: "cert but no key",502 config: api.ExecConfig{503 APIVersion: "client.authentication.k8s.io/v1alpha1",504 InteractiveMode: api.IfAvailableExecInteractiveMode,505 },506 wantInput: `{507 "kind":"ExecCredential",508 "apiVersion":"client.authentication.k8s.io/v1alpha1",509 "spec": {}510 }`,511 output: fmt.Sprintf(`{512 "kind": "ExecCredential",513 "apiVersion": "client.authentication.k8s.io/v1alpha1",514 "status": {515 "clientCertificateData": %q516 }517 }`, certData),518 wantErr: true,519 },520 {521 name: "beta-basic-request",522 config: api.ExecConfig{523 APIVersion: "client.authentication.k8s.io/v1beta1",524 InteractiveMode: api.IfAvailableExecInteractiveMode,525 },526 wantInput: `{527 "kind": "ExecCredential",528 "apiVersion": "client.authentication.k8s.io/v1beta1",529 "spec": {530 "interactive": false531 }532 }`,533 output: `{534 "kind": "ExecCredential",535 "apiVersion": "client.authentication.k8s.io/v1beta1",536 "status": {537 "token": "foo-bar"538 }539 }`,540 wantCreds: credentials{token: "foo-bar"},541 },542 {543 name: "beta-basic-request-with-never-interactive-mode",544 config: api.ExecConfig{545 APIVersion: "client.authentication.k8s.io/v1beta1",546 InteractiveMode: api.NeverExecInteractiveMode,547 },548 wantInput: `{549 "kind": "ExecCredential",550 "apiVersion": "client.authentication.k8s.io/v1beta1",551 "spec": {552 "interactive": false553 }554 }`,555 output: `{556 "kind": "ExecCredential",557 "apiVersion": "client.authentication.k8s.io/v1beta1",558 "status": {559 "token": "foo-bar"560 }561 }`,562 wantCreds: credentials{token: "foo-bar"},563 },564 {565 name: "beta-basic-request-with-never-interactive-mode-and-stdin-unavailable",566 config: api.ExecConfig{567 APIVersion: "client.authentication.k8s.io/v1beta1",568 InteractiveMode: api.NeverExecInteractiveMode,569 StdinUnavailable: true,570 },571 wantInput: `{572 "kind": "ExecCredential",573 "apiVersion": "client.authentication.k8s.io/v1beta1",574 "spec": {575 "interactive": false576 }577 }`,578 output: `{579 "kind": "ExecCredential",580 "apiVersion": "client.authentication.k8s.io/v1beta1",581 "status": {582 "token": "foo-bar"583 }584 }`,585 wantCreds: credentials{token: "foo-bar"},586 },587 {588 name: "beta-basic-request-with-if-available-interactive-mode",589 config: api.ExecConfig{590 APIVersion: "client.authentication.k8s.io/v1beta1",591 InteractiveMode: api.IfAvailableExecInteractiveMode,592 },593 wantInput: `{594 "kind": "ExecCredential",595 "apiVersion": "client.authentication.k8s.io/v1beta1",596 "spec": {597 "interactive": false598 }599 }`,600 output: `{601 "kind": "ExecCredential",602 "apiVersion": "client.authentication.k8s.io/v1beta1",603 "status": {604 "token": "foo-bar"605 }606 }`,607 wantCreds: credentials{token: "foo-bar"},608 },609 {610 name: "beta-basic-request-with-if-available-interactive-mode-and-stdin-unavailable",611 config: api.ExecConfig{612 APIVersion: "client.authentication.k8s.io/v1beta1",613 InteractiveMode: api.IfAvailableExecInteractiveMode,614 StdinUnavailable: true,615 },616 wantInput: `{617 "kind": "ExecCredential",618 "apiVersion": "client.authentication.k8s.io/v1beta1",619 "spec": {620 "interactive": false621 }622 }`,623 output: `{624 "kind": "ExecCredential",625 "apiVersion": "client.authentication.k8s.io/v1beta1",626 "status": {627 "token": "foo-bar"628 }629 }`,630 wantCreds: credentials{token: "foo-bar"},631 },632 {633 name: "beta-basic-request-with-if-available-interactive-mode-and-terminal",634 config: api.ExecConfig{635 APIVersion: "client.authentication.k8s.io/v1beta1",636 InteractiveMode: api.IfAvailableExecInteractiveMode,637 },638 isTerminal: true,639 wantInput: `{640 "kind": "ExecCredential",641 "apiVersion": "client.authentication.k8s.io/v1beta1",642 "spec": {643 "interactive": true644 }645 }`,646 output: `{647 "kind": "ExecCredential",648 "apiVersion": "client.authentication.k8s.io/v1beta1",649 "status": {650 "token": "foo-bar"651 }652 }`,653 wantCreds: credentials{token: "foo-bar"},654 },655 {656 name: "beta-basic-request-with-if-available-interactive-mode-and-terminal-and-stdin-unavailable",657 config: api.ExecConfig{658 APIVersion: "client.authentication.k8s.io/v1beta1",659 InteractiveMode: api.IfAvailableExecInteractiveMode,660 StdinUnavailable: true,661 },662 isTerminal: true,663 wantInput: `{664 "kind": "ExecCredential",665 "apiVersion": "client.authentication.k8s.io/v1beta1",666 "spec": {667 "interactive": false668 }669 }`,670 output: `{671 "kind": "ExecCredential",672 "apiVersion": "client.authentication.k8s.io/v1beta1",673 "status": {674 "token": "foo-bar"675 }676 }`,677 wantCreds: credentials{token: "foo-bar"},678 },679 {680 name: "beta-basic-request-with-always-interactive-mode",681 config: api.ExecConfig{682 APIVersion: "client.authentication.k8s.io/v1beta1",683 InteractiveMode: api.AlwaysExecInteractiveMode,684 },685 wantErr: true,686 wantErrSubstr: "exec plugin cannot support interactive mode: standard input is not a terminal",687 },688 {689 name: "beta-basic-request-with-always-interactive-mode-and-terminal-and-stdin-unavailable",690 config: api.ExecConfig{691 APIVersion: "client.authentication.k8s.io/v1beta1",692 InteractiveMode: api.AlwaysExecInteractiveMode,693 StdinUnavailable: true,694 },695 isTerminal: true,696 wantErr: true,697 wantErrSubstr: "exec plugin cannot support interactive mode: standard input is unavailable",698 },699 {700 name: "beta-basic-request-with-always-interactive-mode-and-terminal-and-stdin-unavailable-with-message",701 config: api.ExecConfig{702 APIVersion: "client.authentication.k8s.io/v1beta1",703 InteractiveMode: api.AlwaysExecInteractiveMode,704 StdinUnavailable: true,705 StdinUnavailableMessage: "some message",706 },707 isTerminal: true,708 wantErr: true,709 wantErrSubstr: "exec plugin cannot support interactive mode: standard input is unavailable: some message",710 },711 {712 name: "beta-basic-request-with-always-interactive-mode-and-terminal",713 config: api.ExecConfig{714 APIVersion: "client.authentication.k8s.io/v1beta1",715 InteractiveMode: api.AlwaysExecInteractiveMode,716 },717 isTerminal: true,718 wantInput: `{719 "kind": "ExecCredential",720 "apiVersion": "client.authentication.k8s.io/v1beta1",721 "spec": {722 "interactive": true723 }724 }`,725 output: `{726 "kind": "ExecCredential",727 "apiVersion": "client.authentication.k8s.io/v1beta1",728 "status": {729 "token": "foo-bar"730 }731 }`,732 wantCreds: credentials{token: "foo-bar"},733 },734 {735 name: "beta-expiry",736 config: api.ExecConfig{737 APIVersion: "client.authentication.k8s.io/v1beta1",738 InteractiveMode: api.IfAvailableExecInteractiveMode,739 },740 wantInput: `{741 "kind": "ExecCredential",742 "apiVersion": "client.authentication.k8s.io/v1beta1",743 "spec": {744 "interactive": false745 }746 }`,747 output: `{748 "kind": "ExecCredential",749 "apiVersion": "client.authentication.k8s.io/v1beta1",750 "status": {751 "token": "foo-bar",752 "expirationTimestamp": "2006-01-02T15:04:05Z"753 }754 }`,755 wantExpiry: time.Date(2006, 01, 02, 15, 04, 05, 0, time.UTC),756 wantCreds: credentials{token: "foo-bar"},757 },758 {759 name: "beta-no-group-version",760 config: api.ExecConfig{761 APIVersion: "client.authentication.k8s.io/v1beta1",762 InteractiveMode: api.IfAvailableExecInteractiveMode,763 },764 output: `{765 "kind": "ExecCredential",766 "status": {767 "token": "foo-bar"768 }769 }`,770 wantErr: true,771 },772 {773 name: "beta-no-status",774 config: api.ExecConfig{775 APIVersion: "client.authentication.k8s.io/v1beta1",776 InteractiveMode: api.IfAvailableExecInteractiveMode,777 },778 output: `{779 "kind": "ExecCredential",780 "apiVersion":"client.authentication.k8s.io/v1beta1"781 }`,782 wantErr: true,783 },784 {785 name: "beta-no-token",786 config: api.ExecConfig{787 APIVersion: "client.authentication.k8s.io/v1beta1",788 InteractiveMode: api.IfAvailableExecInteractiveMode,789 },790 output: `{791 "kind": "ExecCredential",792 "apiVersion":"client.authentication.k8s.io/v1beta1",793 "status": {}794 }`,795 wantErr: true,796 },797 {798 name: "unknown-binary",799 config: api.ExecConfig{800 APIVersion: "client.authentication.k8s.io/v1beta1",801 Command: "does not exist",802 InstallHint: "some install hint",803 InteractiveMode: api.IfAvailableExecInteractiveMode,804 },805 wantErr: true,806 wantErrSubstr: "some install hint",807 },808 {809 name: "binary-fails",810 config: api.ExecConfig{811 APIVersion: "client.authentication.k8s.io/v1beta1",812 InteractiveMode: api.IfAvailableExecInteractiveMode,813 },814 exitCode: 73,815 wantErr: true,816 wantErrSubstr: "73",817 },818 {819 name: "alpha-with-cluster-is-ignored",820 config: api.ExecConfig{821 APIVersion: "client.authentication.k8s.io/v1alpha1",822 InteractiveMode: api.IfAvailableExecInteractiveMode,823 },824 cluster: &clientauthentication.Cluster{825 Server: "foo",826 TLSServerName: "bar",827 CertificateAuthorityData: []byte("baz"),828 Config: &runtime.Unknown{829 TypeMeta: runtime.TypeMeta{830 APIVersion: "",831 Kind: "",832 },833 Raw: []byte(`{"apiVersion":"group/v1","kind":"PluginConfig","spec":{"audience":"panda"}}`),834 ContentEncoding: "",835 ContentType: "application/json",836 },837 },838 response: &clientauthentication.Response{839 Header: map[string][]string{840 "WWW-Authenticate": {`Basic realm="Access to the staging site", charset="UTF-8"`},841 },842 Code: 401,843 },844 wantInput: `{845 "kind":"ExecCredential",846 "apiVersion":"client.authentication.k8s.io/v1alpha1",847 "spec": {848 "response": {849 "header": {850 "WWW-Authenticate": [851 "Basic realm=\"Access to the staging site\", charset=\"UTF-8\""852 ]853 },854 "code": 401855 }856 }857 }`,858 output: `{859 "kind": "ExecCredential",860 "apiVersion": "client.authentication.k8s.io/v1alpha1",861 "status": {862 "token": "foo-bar"863 }864 }`,865 wantCreds: credentials{token: "foo-bar"},866 },867 {868 name: "beta-with-cluster-and-provide-cluster-info-is-serialized",869 config: api.ExecConfig{870 APIVersion: "client.authentication.k8s.io/v1beta1",871 ProvideClusterInfo: true,872 InteractiveMode: api.IfAvailableExecInteractiveMode,873 },874 cluster: &clientauthentication.Cluster{875 Server: "foo",876 TLSServerName: "bar",877 CertificateAuthorityData: []byte("baz"),878 Config: &runtime.Unknown{879 TypeMeta: runtime.TypeMeta{880 APIVersion: "",881 Kind: "",882 },883 Raw: []byte(`{"apiVersion":"group/v1","kind":"PluginConfig","spec":{"audience":"snorlax"}}`),884 ContentEncoding: "",885 ContentType: "application/json",886 },887 },888 response: &clientauthentication.Response{889 Header: map[string][]string{890 "WWW-Authenticate": {`Basic realm="Access to the staging site", charset="UTF-8"`},891 },892 Code: 401,893 },894 wantInput: `{895 "kind":"ExecCredential",896 "apiVersion":"client.authentication.k8s.io/v1beta1",897 "spec": {898 "cluster": {899 "server": "foo",900 "tls-server-name": "bar",901 "certificate-authority-data": "YmF6",902 "config": {903 "apiVersion": "group/v1",904 "kind": "PluginConfig",905 "spec": {906 "audience": "snorlax"907 }908 }909 },910 "interactive": false911 }912 }`,913 output: `{914 "kind": "ExecCredential",915 "apiVersion": "client.authentication.k8s.io/v1beta1",916 "status": {917 "token": "foo-bar"918 }919 }`,920 wantCreds: credentials{token: "foo-bar"},921 },922 {923 name: "beta-with-cluster-and-without-provide-cluster-info-is-not-serialized",924 config: api.ExecConfig{925 APIVersion: "client.authentication.k8s.io/v1beta1",926 InteractiveMode: api.IfAvailableExecInteractiveMode,927 },928 cluster: &clientauthentication.Cluster{929 Server: "foo",930 TLSServerName: "bar",931 CertificateAuthorityData: []byte("baz"),932 Config: &runtime.Unknown{933 TypeMeta: runtime.TypeMeta{934 APIVersion: "",935 Kind: "",936 },937 Raw: []byte(`{"apiVersion":"group/v1","kind":"PluginConfig","spec":{"audience":"snorlax"}}`),938 ContentEncoding: "",939 ContentType: "application/json",940 },941 },942 response: &clientauthentication.Response{943 Header: map[string][]string{944 "WWW-Authenticate": {`Basic realm="Access to the staging site", charset="UTF-8"`},945 },946 Code: 401,947 },948 wantInput: `{949 "kind":"ExecCredential",950 "apiVersion":"client.authentication.k8s.io/v1beta1",951 "spec": {952 "interactive": false953 }954 }`,955 output: `{956 "kind": "ExecCredential",957 "apiVersion": "client.authentication.k8s.io/v1beta1",958 "status": {959 "token": "foo-bar"960 }961 }`,962 wantCreds: credentials{token: "foo-bar"},963 },964 {965 name: "v1-basic-request",966 config: api.ExecConfig{967 APIVersion: "client.authentication.k8s.io/v1",968 InteractiveMode: api.IfAvailableExecInteractiveMode,969 },970 wantInput: `{971 "kind": "ExecCredential",972 "apiVersion": "client.authentication.k8s.io/v1",973 "spec": {974 "interactive": false975 }976 }`,977 output: `{978 "kind": "ExecCredential",979 "apiVersion": "client.authentication.k8s.io/v1",980 "status": {981 "token": "foo-bar"982 }983 }`,984 wantCreds: credentials{token: "foo-bar"},985 },986 {987 name: "v1-with-missing-interactive-mode",988 config: api.ExecConfig{989 APIVersion: "client.authentication.k8s.io/v1",990 },991 wantErr: true,992 wantErrSubstr: `exec plugin cannot support interactive mode: unknown interactiveMode: ""`,993 },994 }995 for _, test := range tests {996 t.Run(test.name, func(t *testing.T) {997 c := test.config998 if c.Command == "" {999 c.Command = "./testdata/test-plugin.sh"1000 c.Env = append(c.Env, api.ExecEnvVar{1001 Name: "TEST_OUTPUT",1002 Value: test.output,1003 })1004 c.Env = append(c.Env, api.ExecEnvVar{1005 Name: "TEST_EXIT_CODE",1006 Value: strconv.Itoa(test.exitCode),1007 })1008 }1009 a, err := newAuthenticator(newCache(), func(_ int) bool { return test.isTerminal }, &c, test.cluster)1010 if err != nil {1011 t.Fatal(err)1012 }1013 stderr := &bytes.Buffer{}1014 a.stderr = stderr1015 a.environ = func() []string { return nil }1016 if err := a.refreshCredsLocked(test.response); err != nil {1017 if !test.wantErr {1018 t.Errorf("get token %v", err)1019 } else if !strings.Contains(err.Error(), test.wantErrSubstr) {1020 t.Errorf("expected error with substring '%v' got '%v'", test.wantErrSubstr, err.Error())1021 }1022 return1023 }1024 if test.wantErr {1025 t.Fatal("expected error getting token")1026 }1027 if !reflect.DeepEqual(a.cachedCreds, &test.wantCreds) {1028 t.Errorf("expected credentials %+v got %+v", &test.wantCreds, a.cachedCreds)1029 }1030 if !a.exp.Equal(test.wantExpiry) {1031 t.Errorf("expected expiry %v got %v", test.wantExpiry, a.exp)1032 }1033 if test.wantInput == "" {1034 if got := strings.TrimSpace(stderr.String()); got != "" {1035 t.Errorf("expected no input parameters, got %q", got)1036 }1037 return1038 }1039 compJSON(t, stderr.Bytes(), []byte(test.wantInput))1040 })1041 }1042}1043func TestRoundTripper(t *testing.T) {1044 wantToken := ""1045 n := time.Now()1046 now := func() time.Time { return n }1047 env := []string{""}1048 environ := func() []string {1049 s := make([]string, len(env))1050 copy(s, env)1051 return s1052 }1053 setOutput := func(s string) {1054 env[0] = "TEST_OUTPUT=" + s1055 }1056 handler := func(w http.ResponseWriter, r *http.Request) {1057 gotToken := ""1058 parts := strings.Split(r.Header.Get("Authorization"), " ")1059 if len(parts) > 1 && strings.EqualFold(parts[0], "bearer") {1060 gotToken = parts[1]1061 }1062 if wantToken != gotToken {1063 http.Error(w, "Unauthorized", http.StatusUnauthorized)1064 return1065 }1066 fmt.Fprintln(w, "ok")1067 }1068 server := httptest.NewServer(http.HandlerFunc(handler))1069 c := api.ExecConfig{1070 Command: "./testdata/test-plugin.sh",1071 APIVersion: "client.authentication.k8s.io/v1alpha1",1072 InteractiveMode: api.IfAvailableExecInteractiveMode,1073 }1074 a, err := newAuthenticator(newCache(), func(_ int) bool { return false }, &c, nil)1075 if err != nil {1076 t.Fatal(err)1077 }1078 a.environ = environ1079 a.now = now1080 a.stderr = ioutil.Discard1081 tc := &transport.Config{}1082 if err := a.UpdateTransportConfig(tc); err != nil {1083 t.Fatal(err)1084 }1085 client := http.Client{1086 Transport: tc.WrapTransport(http.DefaultTransport),1087 }1088 get := func(t *testing.T, statusCode int) {1089 t.Helper()1090 resp, err := client.Get(server.URL)1091 if err != nil {1092 t.Fatal(err)1093 }1094 defer resp.Body.Close()1095 if resp.StatusCode != statusCode {1096 t.Errorf("wanted status %d got %d", statusCode, resp.StatusCode)1097 }1098 }1099 setOutput(`{1100 "kind": "ExecCredential",1101 "apiVersion": "client.authentication.k8s.io/v1alpha1",1102 "status": {1103 "token": "token1"1104 }1105 }`)1106 wantToken = "token1"1107 get(t, http.StatusOK)1108 setOutput(`{1109 "kind": "ExecCredential",1110 "apiVersion": "client.authentication.k8s.io/v1alpha1",1111 "status": {1112 "token": "token2"1113 }1114 }`)1115 // Previous token should be cached1116 get(t, http.StatusOK)1117 wantToken = "token2"1118 // Token is still cached, hits unauthorized but causes token to rotate.1119 get(t, http.StatusUnauthorized)1120 // Follow up request uses the rotated token.1121 get(t, http.StatusOK)1122 setOutput(`{1123 "kind": "ExecCredential",1124 "apiVersion": "client.authentication.k8s.io/v1alpha1",1125 "status": {1126 "token": "token3",1127 "expirationTimestamp": "` + now().Add(time.Hour).Format(time.RFC3339Nano) + `"1128 }1129 }`)1130 wantToken = "token3"1131 // Token is still cached, hit's unauthorized but causes rotation to token with an expiry.1132 get(t, http.StatusUnauthorized)1133 get(t, http.StatusOK)1134 // Move time forward 2 hours, "token3" is now expired.1135 n = n.Add(time.Hour * 2)1136 setOutput(`{1137 "kind": "ExecCredential",1138 "apiVersion": "client.authentication.k8s.io/v1alpha1",1139 "status": {1140 "token": "token4",1141 "expirationTimestamp": "` + now().Add(time.Hour).Format(time.RFC3339Nano) + `"1142 }1143 }`)1144 wantToken = "token4"1145 // Old token is expired, should refresh automatically without hitting a 401.1146 get(t, http.StatusOK)1147}1148func TestAuthorizationHeaderPresentCancelsExecAction(t *testing.T) {1149 tests := []struct {1150 name string1151 setTransportConfig func(*transport.Config)1152 }{1153 {1154 name: "bearer token",1155 setTransportConfig: func(config *transport.Config) {1156 config.BearerToken = "token1f"1157 },1158 },1159 {1160 name: "basic auth",1161 setTransportConfig: func(config *transport.Config) {1162 config.Username = "marshmallow"1163 config.Password = "zelda"1164 },1165 },1166 }1167 for _, test := range tests {1168 t.Run(test.name, func(t *testing.T) {1169 a, err := newAuthenticator(newCache(), func(_ int) bool { return false }, &api.ExecConfig{1170 Command: "./testdata/test-plugin.sh",1171 APIVersion: "client.authentication.k8s.io/v1alpha1",1172 }, nil)1173 if err != nil {1174 t.Fatal(err)1175 }1176 // UpdateTransportConfig returns error on existing TLS certificate callback, unless a bearer token is present in the1177 // transport config, in which case it takes precedence1178 cert := func() (*tls.Certificate, error) {1179 return nil, nil1180 }1181 tc := &transport.Config{TLS: transport.TLSConfig{Insecure: true, GetCert: cert}}1182 test.setTransportConfig(tc)1183 if err := a.UpdateTransportConfig(tc); err != nil {1184 t.Error("Expected presence of bearer token in config to cancel exec action")1185 }1186 })1187 }1188}1189func TestTLSCredentials(t *testing.T) {1190 now := time.Now()1191 certPool := x509.NewCertPool()1192 cert, key := genClientCert(t)1193 if !certPool.AppendCertsFromPEM(cert) {1194 t.Fatal("failed to add client cert to CertPool")1195 }1196 server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {1197 fmt.Fprintln(w, "ok")1198 }))1199 server.TLS = &tls.Config{1200 ClientAuth: tls.RequireAndVerifyClientCert,1201 ClientCAs: certPool,1202 }1203 server.StartTLS()1204 defer server.Close()1205 a, err := newAuthenticator(newCache(), func(_ int) bool { return false }, &api.ExecConfig{1206 Command: "./testdata/test-plugin.sh",1207 APIVersion: "client.authentication.k8s.io/v1alpha1",1208 InteractiveMode: api.IfAvailableExecInteractiveMode,1209 }, nil)1210 if err != nil {1211 t.Fatal(err)1212 }1213 var output *clientauthentication.ExecCredential1214 a.environ = func() []string {1215 data, err := runtime.Encode(codecs.LegacyCodec(a.group), output)1216 if err != nil {1217 t.Fatal(err)1218 }1219 return []string{"TEST_OUTPUT=" + string(data)}1220 }1221 a.now = func() time.Time { return now }1222 a.stderr = ioutil.Discard1223 // We're not interested in server's cert, this test is about client cert.1224 tc := &transport.Config{TLS: transport.TLSConfig{Insecure: true}}1225 if err := a.UpdateTransportConfig(tc); err != nil {1226 t.Fatal(err)1227 }1228 get := func(t *testing.T, desc string, wantErr bool) {1229 t.Run(desc, func(t *testing.T) {1230 tlsCfg, err := transport.TLSConfigFor(tc)1231 if err != nil {1232 t.Fatal("TLSConfigFor:", err)1233 }1234 client := http.Client{1235 Transport: &http.Transport{TLSClientConfig: tlsCfg},1236 }1237 resp, err := client.Get(server.URL)1238 switch {1239 case err != nil && !wantErr:1240 t.Errorf("got client.Get error: %q, want nil", err)1241 case err == nil && wantErr:1242 t.Error("got nil client.Get error, want non-nil")1243 }1244 if err == nil {1245 resp.Body.Close()1246 }1247 })1248 }1249 output = &clientauthentication.ExecCredential{1250 Status: &clientauthentication.ExecCredentialStatus{1251 ClientCertificateData: string(cert),1252 ClientKeyData: string(key),1253 ExpirationTimestamp: &v1.Time{now.Add(time.Hour)},1254 },1255 }1256 get(t, "valid TLS cert", false)1257 // Advance time to force re-exec.1258 nCert, nKey := genClientCert(t)1259 now = now.Add(time.Hour * 2)1260 output = &clientauthentication.ExecCredential{1261 Status: &clientauthentication.ExecCredentialStatus{1262 ClientCertificateData: string(nCert),1263 ClientKeyData: string(nKey),1264 ExpirationTimestamp: &v1.Time{now.Add(time.Hour)},1265 },1266 }1267 get(t, "untrusted TLS cert", true)1268 now = now.Add(time.Hour * 2)1269 output = &clientauthentication.ExecCredential{1270 Status: &clientauthentication.ExecCredentialStatus{1271 ClientCertificateData: string(cert),1272 ClientKeyData: string(key),1273 ExpirationTimestamp: &v1.Time{now.Add(time.Hour)},1274 },1275 }1276 get(t, "valid TLS cert again", false)1277}1278func TestConcurrentUpdateTransportConfig(t *testing.T) {1279 n := time.Now()1280 now := func() time.Time { return n }1281 env := []string{""}1282 environ := func() []string {1283 s := make([]string, len(env))1284 copy(s, env)1285 return s1286 }1287 c := api.ExecConfig{1288 Command: "./testdata/test-plugin.sh",1289 APIVersion: "client.authentication.k8s.io/v1alpha1",1290 }1291 a, err := newAuthenticator(newCache(), func(_ int) bool { return false }, &c, nil)1292 if err != nil {1293 t.Fatal(err)1294 }1295 a.environ = environ1296 a.now = now1297 a.stderr = ioutil.Discard1298 stopCh := make(chan struct{})1299 defer close(stopCh)1300 numConcurrent := 21301 for i := 0; i < numConcurrent; i++ {1302 go func() {1303 for {1304 tc := &transport.Config{}1305 a.UpdateTransportConfig(tc)1306 select {1307 case <-stopCh:1308 return1309 default:1310 continue1311 }1312 }1313 }()1314 }1315 time.Sleep(2 * time.Second)1316}1317func TestInstallHintRateLimit(t *testing.T) {1318 tests := []struct {1319 name string1320 threshold int1321 interval time.Duration1322 calls int1323 perCallAdvance time.Duration1324 wantInstallHint int1325 }{1326 {1327 name: "print-up-to-threshold",1328 threshold: 2,1329 interval: time.Second,1330 calls: 10,1331 wantInstallHint: 2,1332 },1333 {1334 name: "after-interval-threshold-resets",1335 threshold: 2,1336 interval: time.Second * 5,1337 calls: 10,1338 perCallAdvance: time.Second,1339 wantInstallHint: 4,1340 },1341 }1342 for _, test := range tests {1343 t.Run(test.name, func(t *testing.T) {1344 c := api.ExecConfig{1345 Command: "does not exist",1346 APIVersion: "client.authentication.k8s.io/v1alpha1",1347 InstallHint: "some install hint",1348 InteractiveMode: api.IfAvailableExecInteractiveMode,1349 }1350 a, err := newAuthenticator(newCache(), func(_ int) bool { return false }, &c, nil)1351 if err != nil {1352 t.Fatal(err)1353 }1354 a.sometimes.threshold = test.threshold1355 a.sometimes.interval = test.interval1356 clock := testingclock.NewFakeClock(time.Now())1357 a.sometimes.clock = clock1358 count := 01359 for i := 0; i < test.calls; i++ {1360 err := a.refreshCredsLocked(&clientauthentication.Response{})...

Full Screen

Full Screen

APIVersion

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cli, err := client.NewEnvClient()4 if err != nil {5 panic(err)6 }7 version, err := cli.ServerVersion(context.Background())8 if err != nil {9 panic(err)10 }11 fmt.Println(version.APIVersion)12}13import (14func main() {15 cli, err := client.NewEnvClient()16 if err != nil {17 panic(err)18 }19 ctx := context.Background()20 container, err := cli.ContainerCreate(ctx, &container.Config{21 Cmd: []string{"sh", "-c", "while true; do echo hello world; sleep 1; done"},22 }, nil, nil, "")23 if err != nil {24 panic(err)25 }26 fmt.Println(container.ID)27 containers, err := cli.ContainerList(ctx, types.ContainerListOptions{})28 if err != nil {29 panic(err)30 }31 for _, container := range containers {32 fmt.Println(container.ID)33 }34 if err := cli.ContainerStart(ctx, container.ID, types.ContainerStartOptions{}); err != nil {35 panic(err)36 }37 if err := cli.ContainerStop(ctx, container.ID, nil); err != nil {38 panic(err)39 }40 inspect, err := cli.ContainerInspect(ctx, container.ID)41 if err != nil {42 panic(err)43 }44 fmt.Println(inspect.State.Pid)45 if err := cli.ContainerPause(ctx, container.ID); err != nil {46 panic(err)47 }48 if err := cli.ContainerUnpause(ctx, container.ID);

Full Screen

Full Screen

APIVersion

Using AI Code Generation

copy

Full Screen

1import (2type HelloPlugin struct {3}4func main() {5 plugin.Start(new(HelloPlugin))6}7func (h *HelloPlugin) Run(context plugin.PluginContext, args []string) {8 h.terminal = terminal.NewStdUI()9 h.terminal.Say("Hello IBM Cloud")10 h.terminal.Say("APIVersion: %s", context.APIVersion())11}12func (h *HelloPlugin) GetMetadata() plugin.PluginMetadata {13 return plugin.PluginMetadata{14 Commands: []plugin.Command{15 {16 HelpText: i18n.T("hello command"),17 UsageDetails: plugin.Usage{18 Usage: i18n.T("{{.CommandName}}"),19 },20 },21 },22 }23}

Full Screen

Full Screen

APIVersion

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 sess, err := session.New()4 if err != nil {5 fmt.Println(err)6 }7 clusterAPI := containerv1.New(sess)8 version, err := clusterAPI.APIVersion()9 if err != nil {10 fmt.Println(err)11 }12 fmt.Println(version)13}14import (15func main() {16 sess, err := session.New()17 if err != nil {18 fmt.Println(err)19 }20 clusterAPI := containerv1.New(sess)21 cluster, err := clusterAPI.Clusters().Get("test", "test")22 if err != nil {

Full Screen

Full Screen

APIVersion

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)4 if err != nil {5 panic(err.Error())6 }7 clientset, err := kubernetes.NewForConfig(config)8 if err != nil {9 panic(err.Error())10 }11 fmt.Println(clientset.Discovery().RESTClient().APIVersion())12}13&APIVersions{types.APIVersions{Versions:[]string{"v1"}, ServerAddressByClientCIDRs:[]types.ServerAddressByClientCIDR(nil)}}

Full Screen

Full Screen

APIVersion

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 client, _ := elasticsearch.NewDefaultClient()4 info, _ := client.Info()5 fmt.Println(info.Version.Number)6}

Full Screen

Full Screen

APIVersion

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 v, err := metadata.APIVersion()4 if err != nil {5 fmt.Println("Error:", err)6 }7 fmt.Println("API version:", v)8}9import (10func main() {11 if err != nil {12 fmt.Println("Error:", err)13 }14 v, err := client.APIVersion()15 if err != nil {16 fmt.Println("Error:", err)17 }18 fmt.Println("API version:", v)19}20import (21func main() {22 projectID, err := metadata.ProjectID()23 if err != nil {24 fmt.Println("Error:", err)25 }26 fmt.Println("Project ID:", projectID)27}

Full Screen

Full Screen

APIVersion

Using AI Code Generation

copy

Full Screen

1import (2type IBMCloudPlugin struct {3}4func main() {5 plugin.Start(new(IBMCloudPlugin))6}7func (p *IBMCloudPlugin) Run(context plugin.PluginContext, args []string) {8 ui := terminal.NewStdUI()9 ui.Say("Hello World")10}11func (p *IBMCloudPlugin) GetMetadata() plugin.PluginMetadata {12 return plugin.PluginMetadata{13 Version: plugin.VersionType{14 },15 Commands: []plugin.Command{16 {17 UsageDetails: plugin.Usage{18 },19 },20 },21 }22}

Full Screen

Full Screen

APIVersion

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("APIVersion is", metadata.APIVersion())4}5import (6func main() {7 fmt.Println("ProjectID is", metadata.ProjectID())8}9import (10func main() {11 fmt.Println("Zone is", metadata.Zone())12}13import (14func main() {15 fmt.Println("InstanceID is", metadata.InstanceID())16}17import (18func main() {19 fmt.Println("InstanceName is", metadata.InstanceName())20}21import (22func main() {23 fmt.Println("Hostname is", metadata.Hostname())24}25import (26func main() {27 attributes, err := metadata.Attributes()28 if err != nil {

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