...

Source file src/github.com/linkerd/linkerd2/cli/cmd/metrics_diagnostics_util_test.go

Documentation: github.com/linkerd/linkerd2/cli/cmd

     1  package cmd
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"io"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/linkerd/linkerd2/pkg/k8s"
    12  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    13  )
    14  
    15  func TestGetAllContainersWithPort(t *testing.T) {
    16  	tests := []struct {
    17  		ns         string
    18  		name       string
    19  		k8sConfigs []string
    20  		err        error
    21  	}{
    22  		{
    23  			"pod-ns",
    24  			"pod-name",
    25  			[]string{`apiVersion: v1
    26  kind: Pod
    27  metadata:
    28    name: pod-name
    29    namespace: pod-ns
    30  status:
    31    phase: Stopped
    32  spec:
    33    containers:
    34    - name: linkerd-proxy
    35      ports:
    36      - name: linkerd-admin
    37        port: 123`,
    38  			},
    39  			errors.New("pod not running: pod-name"),
    40  		},
    41  	}
    42  
    43  	ctx := context.Background()
    44  	for i, test := range tests {
    45  		test := test // pin
    46  		t.Run(fmt.Sprintf("%d: getAllContainersWithPort returns expected result", i), func(t *testing.T) {
    47  			k8sClient, err := k8s.NewFakeAPI(test.k8sConfigs...)
    48  			if err != nil {
    49  				t.Fatalf("Unexpected error %s", err)
    50  			}
    51  			pod, err := k8sClient.CoreV1().Pods(test.ns).Get(ctx, test.name, metav1.GetOptions{})
    52  			if err != nil {
    53  				t.Fatalf("Unexpected error %s", err)
    54  			}
    55  			_, err = getAllContainersWithPort(*pod, "admin-http")
    56  			if err != nil || test.err != nil {
    57  				if (err == nil && test.err != nil) ||
    58  					(err != nil && test.err == nil) ||
    59  					(err.Error() != test.err.Error()) {
    60  					t.Fatalf("Unexpected error (Expected: %s, Got: %s)", test.err, err)
    61  				}
    62  			}
    63  		})
    64  	}
    65  }
    66  
    67  func Test_obfuscateMetrics(t *testing.T) {
    68  	tests := []struct {
    69  		inputFileName  string
    70  		goldenFileName string
    71  		wantErr        bool
    72  	}{
    73  		{
    74  			inputFileName:  "obfuscate-diagnostics-proxy-metrics.input",
    75  			goldenFileName: "obfuscate-diagnostics-proxy-metrics.golden",
    76  			wantErr:        false,
    77  		},
    78  	}
    79  	for i, tc := range tests {
    80  		tc := tc
    81  		t.Run(fmt.Sprintf("%d: %s", i, tc.inputFileName), func(t *testing.T) {
    82  			file, err := os.Open("testdata/" + tc.inputFileName)
    83  			if err != nil {
    84  				t.Errorf("error opening test input file: %v\n", err)
    85  			}
    86  
    87  			fileBytes, err := io.ReadAll(file)
    88  			if err != nil {
    89  				t.Errorf("error reading test input file: %v\n", err)
    90  			}
    91  
    92  			got, err := obfuscateMetrics(fileBytes)
    93  			if (err != nil) != tc.wantErr {
    94  				t.Errorf("obfuscateMetrics() error = %v, wantErr %v", err, tc.wantErr)
    95  				return
    96  			}
    97  			testDataDiffer.DiffTestdata(t, tc.goldenFileName, string(got))
    98  		})
    99  	}
   100  }
   101  

View as plain text