istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/writer/envoy/configdump/ecds_test.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 configdump
    16  
    17  import (
    18  	"bytes"
    19  	"encoding/json"
    20  	"os"
    21  	"testing"
    22  
    23  	"istio.io/istio/pilot/test/util"
    24  	"istio.io/istio/pkg/test/util/assert"
    25  )
    26  
    27  func TestPrintEcdsSummary(t *testing.T) {
    28  	gotOut := &bytes.Buffer{}
    29  	cw := &ConfigWriter{Stdout: gotOut}
    30  	cd, _ := os.ReadFile("testdata/ecds/configdump.json")
    31  	cw.Prime(cd)
    32  	err := cw.PrintEcdsSummary()
    33  	assert.NoError(t, err)
    34  
    35  	util.CompareContent(t, gotOut.Bytes(), "testdata/ecds/output.txt")
    36  }
    37  
    38  func TestPrintEcdsYaml(t *testing.T) {
    39  	gotOut := &bytes.Buffer{}
    40  	cw := &ConfigWriter{Stdout: gotOut}
    41  	cd, _ := os.ReadFile("testdata/ecds/configdump.json")
    42  	cw.Prime(cd)
    43  	err := cw.PrintEcds("yaml")
    44  	assert.NoError(t, err)
    45  
    46  	util.CompareContent(t, gotOut.Bytes(), "testdata/ecds/output.yaml")
    47  }
    48  
    49  func TestPrintEcdsJSON(t *testing.T) {
    50  	gotOut := &bytes.Buffer{}
    51  	cw := &ConfigWriter{Stdout: gotOut}
    52  	cd, _ := os.ReadFile("testdata/ecds/configdump.json")
    53  	cw.Prime(cd)
    54  	err := cw.PrintEcds("json")
    55  	assert.NoError(t, err)
    56  
    57  	// protojson opt out of whitespace randomization, see more details: https://github.com/golang/protobuf/issues/1082
    58  	var rm json.RawMessage = gotOut.Bytes()
    59  	jsonOutput, err := json.MarshalIndent(rm, "", "    ")
    60  	if err != nil {
    61  		assert.NoError(t, err)
    62  	}
    63  	jsonOutput = append(jsonOutput, '\n')
    64  
    65  	util.CompareContent(t, jsonOutput, "testdata/ecds/output.json")
    66  }