...

Source file src/github.com/linkerd/linkerd2/viz/cmd/edges_test.go

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

     1  package cmd
     2  
     3  import (
     4  	"testing"
     5  
     6  	api "github.com/linkerd/linkerd2/viz/metrics-api"
     7  )
     8  
     9  type edgesParamsExp struct {
    10  	options      *edgesOptions
    11  	resourceType string
    12  	file         string
    13  }
    14  
    15  func TestEdges(t *testing.T) {
    16  	options := newEdgesOptions()
    17  	options.outputFormat = tableOutput
    18  	options.allNamespaces = true
    19  	t.Run("Returns edges", func(t *testing.T) {
    20  		testEdgesCall(edgesParamsExp{
    21  			options:      options,
    22  			resourceType: "deployment",
    23  			file:         "edges_one_output.golden",
    24  		}, t)
    25  	})
    26  
    27  	options.outputFormat = jsonOutput
    28  	t.Run("Returns edges (json)", func(t *testing.T) {
    29  		testEdgesCall(edgesParamsExp{
    30  			options:      options,
    31  			resourceType: "deployment",
    32  			file:         "edges_one_output_json.golden",
    33  		}, t)
    34  	})
    35  
    36  	t.Run("Returns edges (wide)", func(t *testing.T) {
    37  		options.outputFormat = wideOutput
    38  		testEdgesCall(edgesParamsExp{
    39  			options:      options,
    40  			resourceType: "deployment",
    41  			file:         "edges_wide_output.golden",
    42  		}, t)
    43  	})
    44  
    45  	t.Run("Returns an error if outputFormat specified is not wide, table or json", func(t *testing.T) {
    46  		options.outputFormat = "test"
    47  		args := []string{"deployment"}
    48  		expectedError := "--output supports table, json and wide"
    49  
    50  		_, err := buildEdgesRequests(args, options)
    51  		if err == nil || err.Error() != expectedError {
    52  			t.Fatalf("Expected error [%s] instead got [%s]", expectedError, err)
    53  		}
    54  	})
    55  
    56  	t.Run("Returns an error if request includes the resource name", func(t *testing.T) {
    57  		options.outputFormat = tableOutput
    58  		args := []string{"pod/pod-name"}
    59  		expectedError := "Edges cannot be returned for a specific resource name; remove pod-name from query"
    60  
    61  		_, err := buildEdgesRequests(args, options)
    62  		if err == nil || err.Error() != expectedError {
    63  			t.Fatalf("Expected error [%s] instead got [%s]", expectedError, err)
    64  		}
    65  	})
    66  
    67  	t.Run("Returns an error if request is for authority", func(t *testing.T) {
    68  		options.outputFormat = tableOutput
    69  		args := []string{"authority"}
    70  		expectedError := "Resource type is not supported: authority"
    71  
    72  		_, err := buildEdgesRequests(args, options)
    73  		if err == nil || err.Error() != expectedError {
    74  			t.Fatalf("Expected error [%s] instead got [%s]", expectedError, err)
    75  		}
    76  	})
    77  
    78  	t.Run("Returns an error if request is for service", func(t *testing.T) {
    79  		options.outputFormat = tableOutput
    80  		args := []string{"service"}
    81  		expectedError := "Resource type is not supported: service"
    82  
    83  		_, err := buildEdgesRequests(args, options)
    84  		if err == nil || err.Error() != expectedError {
    85  			t.Fatalf("Expected error [%s] instead got [%s]", expectedError, err)
    86  		}
    87  	})
    88  
    89  	t.Run("Returns an error if request is for all resource types", func(t *testing.T) {
    90  		options.outputFormat = tableOutput
    91  		args := []string{"all"}
    92  		expectedError := "Resource type is not supported: all"
    93  
    94  		_, err := buildEdgesRequests(args, options)
    95  		if err == nil || err.Error() != expectedError {
    96  			t.Fatalf("Expected error [%s] instead got [%s]", expectedError, err)
    97  		}
    98  	})
    99  }
   100  
   101  func testEdgesCall(exp edgesParamsExp, t *testing.T) {
   102  	mockClient := &api.MockAPIClient{}
   103  	response := api.GenEdgesResponse(exp.resourceType, "all")
   104  
   105  	mockClient.EdgesResponseToReturn = response
   106  
   107  	args := []string{"deployment"}
   108  	reqs, err := buildEdgesRequests(args, exp.options)
   109  	if err != nil {
   110  		t.Fatalf("Unexpected error: %v", err)
   111  	}
   112  
   113  	resp, err := requestEdgesFromAPI(mockClient, reqs[0])
   114  	if err != nil {
   115  		t.Fatalf("Unexpected error: %v", err)
   116  	}
   117  
   118  	rows := edgesRespToRows(resp)
   119  	output := renderEdgeStats(rows, exp.options)
   120  
   121  	testDataDiffer.DiffTestdata(t, exp.file, output)
   122  }
   123  

View as plain text