Skip to content

Commit

Permalink
ut
Browse files Browse the repository at this point in the history
Signed-off-by: hejianpeng <hejianpeng2@huawei.com>
  • Loading branch information
zirain committed Feb 23, 2023
1 parent dc0c064 commit c9a55c1
Show file tree
Hide file tree
Showing 7 changed files with 6,003 additions and 26 deletions.
34 changes: 24 additions & 10 deletions internal/cmd/egctl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,28 @@ func runAllConfig(c *cobra.Command, args []string) error {
return fmt.Errorf("pod namespace is required")
}

out, err := extractConfigDump(types.NamespacedName{
fw, err := portForwarder(types.NamespacedName{
Namespace: podNamespace,
Name: podName,
})
if err != nil {
return err
}
if err := fw.Start(); err != nil {
return err
}
defer fw.Stop()

if output == "yaml" {
out, err = yaml.JSONToYAML(out)
if err != nil {
return err
}
out, err := extractConfigDump(fw, output)
if err != nil {
return err
}

_, err = fmt.Fprintln(c.OutOrStdout(), string(out))
return err
}

func extractConfigDump(nn types.NamespacedName) ([]byte, error) {
func portForwarder(nn types.NamespacedName) (kube.PortForwarder, error) {
c, err := kube.NewCLIClient(options.DefaultConfigFlags.ToRawKubeConfigLoader())
if err != nil {
return nil, fmt.Errorf("build CLI client fail: %w", err)
Expand All @@ -132,12 +135,23 @@ func extractConfigDump(nn types.NamespacedName) ([]byte, error) {
return nil, err
}

if err := fw.Start(); err != nil {
return fw, nil
}

func extractConfigDump(fw kube.PortForwarder, output string) ([]byte, error) {
out, err := configDumpRequest(fw.Address())
if err != nil {
return nil, err
}
defer fw.Stop()

return configDumpRequest(fw.Address())
if output == "yaml" {
out, err = yaml.JSONToYAML(out)
if err != nil {
return nil, err
}
}

return out, nil
}

func configDumpRequest(address string) ([]byte, error) {
Expand Down
105 changes: 105 additions & 0 deletions internal/cmd/egctl/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.

package egctl

import (
"fmt"
"log"
"net"
"net/http"
"os"
"path"
"testing"

kube "github.com/envoyproxy/gateway/internal/kubernetes"
netutil "github.com/envoyproxy/gateway/internal/utils/net"
"github.com/stretchr/testify/assert"
)

var _ kube.PortForwarder = &fakePortForwarder{}

type fakePortForwarder struct {
responseBody []byte
localPort int
l net.Listener
mux *http.ServeMux
}

func newFakePortForwarder(b []byte) (kube.PortForwarder, error) {
p, err := netutil.LocalAvailablePort()
if err != nil {
return nil, err
}

fw := &fakePortForwarder{
responseBody: b,
localPort: p,
mux: http.NewServeMux(),
}
fw.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write(fw.responseBody)
})

return fw, nil
}

func (fw *fakePortForwarder) Start() error {
l, err := net.Listen("tcp", fw.Address())
if err != nil {
return err
}
fw.l = l

go func() {
if err := http.Serve(l, fw.mux); err != nil {
log.Fatal(err)
}
}()

return nil
}

func (fw *fakePortForwarder) Stop() {}

func (fw *fakePortForwarder) Address() string {
return fmt.Sprintf("localhost:%d", fw.localPort)
}

func TestExtractConfigDump(t *testing.T) {
fw, err := newFakePortForwarder(readConfig("in.json"))
assert.NoError(t, err)
err = fw.Start()
assert.NoError(t, err)

cases := []struct {
output string
expected string
}{
{
output: "json",
expected: "out.json",
},
{
output: "yaml",
expected: "out.yaml",
},
}

for _, tc := range cases {
t.Run(tc.output, func(t *testing.T) {
got, err := extractConfigDump(fw, tc.output)
assert.NoError(t, err)
assert.Equal(t, string(readConfig(tc.expected)), string(got))
})
}

fw.Stop()
}

func readConfig(filename string) []byte {
b, _ := os.ReadFile(path.Join("testdata", "config", filename))
return b
}
Loading

0 comments on commit c9a55c1

Please sign in to comment.