|
| 1 | +/* |
| 2 | +Copyright 2026 Flant JSC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package console |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "net" |
| 23 | + "os" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + . "github.com/onsi/ginkgo/v2" |
| 28 | + . "github.com/onsi/gomega" |
| 29 | + "github.com/spf13/cobra" |
| 30 | + k8sfake "k8s.io/client-go/kubernetes/fake" |
| 31 | + |
| 32 | + virtualizationfake "github.com/deckhouse/virtualization/api/client/generated/clientset/versioned/fake" |
| 33 | + virtualizationv1alpha2 "github.com/deckhouse/virtualization/api/client/generated/clientset/versioned/typed/core/v1alpha2" |
| 34 | + "github.com/deckhouse/virtualization/api/client/kubeclient" |
| 35 | +) |
| 36 | + |
| 37 | +type fakeClient struct { |
| 38 | + *k8sfake.Clientset |
| 39 | + virtualizationv1alpha2.VirtualizationV1alpha2Interface |
| 40 | +} |
| 41 | + |
| 42 | +func newFakeClient() *fakeClient { |
| 43 | + return &fakeClient{ |
| 44 | + Clientset: k8sfake.NewSimpleClientset(), |
| 45 | + VirtualizationV1alpha2Interface: virtualizationfake.NewSimpleClientset().VirtualizationV1alpha2(), |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestConsole(t *testing.T) { |
| 50 | + RegisterFailHandler(Fail) |
| 51 | + RunSpecs(t, "Console Command Suite") |
| 52 | +} |
| 53 | + |
| 54 | +var _ = Describe("Console", func() { |
| 55 | + var ( |
| 56 | + oldStdin *os.File |
| 57 | + oldClientAndNamespaceFromContext func(context.Context) (kubeclient.Client, string, bool, error) |
| 58 | + oldConnectFunc func(context.Context, string, string, kubeclient.Client, time.Duration, <-chan []byte, <-chan struct{}) error |
| 59 | + ) |
| 60 | + |
| 61 | + BeforeEach(func() { |
| 62 | + oldStdin = os.Stdin |
| 63 | + oldClientAndNamespaceFromContext = clientAndNamespaceFromContext |
| 64 | + oldConnectFunc = connectFunc |
| 65 | + }) |
| 66 | + |
| 67 | + AfterEach(func() { |
| 68 | + os.Stdin = oldStdin |
| 69 | + clientAndNamespaceFromContext = oldClientAndNamespaceFromContext |
| 70 | + connectFunc = oldConnectFunc |
| 71 | + }) |
| 72 | + |
| 73 | + Describe("Run", func() { |
| 74 | + It("refreshes client before reconnect", func() { |
| 75 | + stdinReader, stdinWriter, err := os.Pipe() |
| 76 | + Expect(err).NotTo(HaveOccurred()) |
| 77 | + DeferCleanup(func() { |
| 78 | + _ = stdinReader.Close() |
| 79 | + }) |
| 80 | + DeferCleanup(func() { |
| 81 | + _ = stdinWriter.Close() |
| 82 | + }) |
| 83 | + os.Stdin = stdinReader |
| 84 | + |
| 85 | + var clientCalls int |
| 86 | + clientAndNamespaceFromContext = func(context.Context) (kubeclient.Client, string, bool, error) { |
| 87 | + clientCalls++ |
| 88 | + return newFakeClient(), "default", false, nil |
| 89 | + } |
| 90 | + |
| 91 | + var connectCalls int |
| 92 | + connectFunc = func(_ context.Context, name, namespace string, _ kubeclient.Client, _ time.Duration, _ <-chan []byte, _ <-chan struct{}) error { |
| 93 | + connectCalls++ |
| 94 | + Expect(namespace).To(Equal("default")) |
| 95 | + Expect(name).To(Equal("test-vm")) |
| 96 | + if connectCalls == 1 { |
| 97 | + return errors.New("temporary error") |
| 98 | + } |
| 99 | + return nil |
| 100 | + } |
| 101 | + |
| 102 | + cmd := &cobra.Command{} |
| 103 | + cmd.SetContext(context.Background()) |
| 104 | + |
| 105 | + go func() { |
| 106 | + _ = stdinWriter.Close() |
| 107 | + }() |
| 108 | + |
| 109 | + err = (&Console{timeout: time.Second}).Run(cmd, []string{"test-vm"}) |
| 110 | + Expect(err).NotTo(HaveOccurred()) |
| 111 | + Expect(connectCalls).To(Equal(2)) |
| 112 | + Expect(clientCalls).To(Equal(2)) |
| 113 | + }) |
| 114 | + }) |
| 115 | + |
| 116 | + Describe("ShouldWaitErr", func() { |
| 117 | + It("returns true for abnormal closure errors", func() { |
| 118 | + Expect(ShouldWaitErr(&net.OpError{Err: errors.New("Internal error")})).To(BeTrue()) |
| 119 | + }) |
| 120 | + }) |
| 121 | +}) |
0 commit comments