github.com/argoproj/argo-cd/v3@v3.2.1/cmd/argocd/commands/common_test.go (about)

     1  package commands
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  // Be careful with tabs vs. spaces in the following expected formats. Indents
    13  // should all be spaces, no tabs.
    14  const expectYamlSingle = `bar: ""
    15  baz: foo
    16  foo: bar
    17  `
    18  
    19  const expectJSONSingle = `{
    20    "bar": "",
    21    "baz": "foo",
    22    "foo": "bar"
    23  }
    24  `
    25  
    26  const expectYamlList = `one:
    27    bar: ""
    28    baz: foo
    29    foo: bar
    30  two:
    31    bar: ""
    32    baz: foo
    33    foo: bar
    34  `
    35  
    36  const expectJSONList = `{
    37    "one": {
    38      "bar": "",
    39      "baz": "foo",
    40      "foo": "bar"
    41    },
    42    "two": {
    43      "bar": "",
    44      "baz": "foo",
    45      "foo": "bar"
    46    }
    47  }
    48  `
    49  
    50  // Rather dirty hack to capture stdout from PrintResource() and PrintResourceList()
    51  func captureOutput(f func() error) (string, error) {
    52  	stdout := os.Stdout
    53  	r, w, err := os.Pipe()
    54  	if err != nil {
    55  		return "", err
    56  	}
    57  	os.Stdout = w
    58  	err = f()
    59  	w.Close()
    60  	if err != nil {
    61  		os.Stdout = stdout
    62  		return "", err
    63  	}
    64  	str, err := io.ReadAll(r)
    65  	os.Stdout = stdout
    66  	if err != nil {
    67  		return "", err
    68  	}
    69  	return string(str), err
    70  }
    71  
    72  func Test_PrintResource(t *testing.T) {
    73  	testResource := map[string]string{
    74  		"foo": "bar",
    75  		"bar": "",
    76  		"baz": "foo",
    77  	}
    78  
    79  	str, err := captureOutput(func() error {
    80  		err := PrintResource(testResource, "yaml")
    81  		return err
    82  	})
    83  	require.NoError(t, err)
    84  	assert.YAMLEq(t, expectYamlSingle, str)
    85  
    86  	str, err = captureOutput(func() error {
    87  		err := PrintResource(testResource, "json")
    88  		return err
    89  	})
    90  	require.NoError(t, err)
    91  	assert.JSONEq(t, expectJSONSingle, str)
    92  
    93  	err = PrintResource(testResource, "unknown")
    94  	require.Error(t, err)
    95  }
    96  
    97  func Test_PrintResourceList(t *testing.T) {
    98  	testResource := map[string]map[string]string{
    99  		"one": {
   100  			"foo": "bar",
   101  			"bar": "",
   102  			"baz": "foo",
   103  		},
   104  		"two": {
   105  			"foo": "bar",
   106  			"bar": "",
   107  			"baz": "foo",
   108  		},
   109  	}
   110  
   111  	testResource2 := make([]map[string]string, 0)
   112  	testResource2 = append(testResource2, testResource["one"])
   113  
   114  	str, err := captureOutput(func() error {
   115  		err := PrintResourceList(testResource, "yaml", false)
   116  		return err
   117  	})
   118  	require.NoError(t, err)
   119  	assert.YAMLEq(t, expectYamlList, str)
   120  
   121  	str, err = captureOutput(func() error {
   122  		err := PrintResourceList(testResource, "json", false)
   123  		return err
   124  	})
   125  	require.NoError(t, err)
   126  	assert.JSONEq(t, expectJSONList, str)
   127  
   128  	str, err = captureOutput(func() error {
   129  		err := PrintResourceList(testResource2, "yaml", true)
   130  		return err
   131  	})
   132  	require.NoError(t, err)
   133  	assert.YAMLEq(t, expectYamlSingle, str)
   134  
   135  	str, err = captureOutput(func() error {
   136  		err := PrintResourceList(testResource2, "json", true)
   137  		return err
   138  	})
   139  	require.NoError(t, err)
   140  	assert.JSONEq(t, expectJSONSingle, str)
   141  
   142  	err = PrintResourceList(testResource, "unknown", false)
   143  	require.Error(t, err)
   144  }