istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/cli/mock_client.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cli 16 17 import ( 18 "context" 19 "fmt" 20 21 "google.golang.org/grpc/credentials" 22 23 "istio.io/istio/pkg/kube" 24 ) 25 26 type MockPortForwarder struct{} 27 28 func (m MockPortForwarder) Start() error { 29 return nil 30 } 31 32 func (m MockPortForwarder) Address() string { 33 return "localhost:3456" 34 } 35 36 func (m MockPortForwarder) Close() { 37 } 38 39 func (m MockPortForwarder) ErrChan() <-chan error { 40 return make(chan error) 41 } 42 43 func (m MockPortForwarder) WaitForStop() { 44 } 45 46 var _ kube.PortForwarder = MockPortForwarder{} 47 48 type MockClient struct { 49 // Results is a map of podName to the results of the expected test on the pod 50 Results map[string][]byte 51 kube.CLIClient 52 } 53 54 func (c MockClient) NewPortForwarder(_, _, _ string, _, _ int) (kube.PortForwarder, error) { 55 return MockPortForwarder{}, nil 56 } 57 58 func (c MockClient) AllDiscoveryDo(_ context.Context, _, _ string) (map[string][]byte, error) { 59 return c.Results, nil 60 } 61 62 func (c MockClient) EnvoyDo(ctx context.Context, podName, podNamespace, method, path string) ([]byte, error) { 63 results, ok := c.Results[podName] 64 if !ok { 65 return nil, fmt.Errorf("unable to retrieve Pod: pods %q not found", podName) 66 } 67 return results, nil 68 } 69 70 func (c MockClient) EnvoyDoWithPort(ctx context.Context, podName, podNamespace, method, path string, port int) ([]byte, error) { 71 results, ok := c.Results[podName] 72 if !ok { 73 return nil, fmt.Errorf("unable to retrieve Pod: pods %q not found", podName) 74 } 75 return results, nil 76 } 77 78 func (c MockClient) CreatePerRPCCredentials(_ context.Context, _, _ string, _ []string, _ int64, 79 ) (credentials.PerRPCCredentials, error) { 80 return nil, nil 81 }