...

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

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

     1  package cmd
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/linkerd/linkerd2/pkg/healthcheck"
    11  )
    12  
    13  func TestCheckStatus(t *testing.T) {
    14  	t.Run("Prints expected output", func(t *testing.T) {
    15  		hc := healthcheck.NewHealthChecker(
    16  			[]healthcheck.CategoryID{},
    17  			&healthcheck.Options{},
    18  		)
    19  		hc.AppendCategories(healthcheck.NewCategory("category", []healthcheck.Checker{
    20  			*healthcheck.NewChecker("check1").
    21  				WithCheck(func(context.Context) error {
    22  					return nil
    23  				}),
    24  			*healthcheck.NewChecker("check2").
    25  				WithHintAnchor("hint-anchor").
    26  				WithCheck(func(context.Context) error {
    27  					return fmt.Errorf("This should contain instructions for fail")
    28  				}),
    29  		},
    30  			true,
    31  		))
    32  
    33  		output := bytes.NewBufferString("")
    34  		healthcheck.RunChecks(output, stderr, hc, tableOutput)
    35  
    36  		goldenFileBytes, err := os.ReadFile("testdata/check_output.golden")
    37  		if err != nil {
    38  			t.Fatalf("Unexpected error: %v", err)
    39  		}
    40  
    41  		expectedContent := string(goldenFileBytes)
    42  
    43  		if expectedContent != output.String() {
    44  			t.Fatalf("Expected function to render:\n%s\bbut got:\n%s", expectedContent, output)
    45  		}
    46  	})
    47  
    48  	t.Run("Prints expected output in json", func(t *testing.T) {
    49  		hc := healthcheck.NewHealthChecker(
    50  			[]healthcheck.CategoryID{},
    51  			&healthcheck.Options{},
    52  		)
    53  		hc.AppendCategories(healthcheck.NewCategory("category", []healthcheck.Checker{
    54  			*healthcheck.NewChecker("check1").
    55  				WithCheck(func(context.Context) error {
    56  					return nil
    57  				}),
    58  			*healthcheck.NewChecker("check2").
    59  				WithHintAnchor("hint-anchor").
    60  				WithCheck(func(context.Context) error {
    61  					return fmt.Errorf("This should contain instructions for fail")
    62  				}),
    63  		},
    64  			true,
    65  		))
    66  
    67  		output := bytes.NewBufferString("")
    68  		healthcheck.RunChecks(output, stderr, hc, jsonOutput)
    69  
    70  		goldenFileBytes, err := os.ReadFile("testdata/check_output_json.golden")
    71  		if err != nil {
    72  			t.Fatalf("Unexpected error: %v", err)
    73  		}
    74  
    75  		expectedContent := string(goldenFileBytes)
    76  
    77  		if expectedContent != output.String() {
    78  			t.Fatalf("Expected function to render:\n%s\bbut got:\n%s", expectedContent, output)
    79  		}
    80  	})
    81  }
    82  

View as plain text