github.com/argoproj/argo-cd/v3@v3.2.1/util/config/reader_test.go (about)

     1  package config
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io"
     8  	"net"
     9  	"net/http"
    10  	"os"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestUnmarshalLocalFile(t *testing.T) {
    18  	const (
    19  		field1 = "Hello, world!"
    20  		field2 = 42
    21  	)
    22  	sentinel := fmt.Sprintf("---\nfield1: %q\nfield2: %d", field1, field2)
    23  
    24  	file, err := os.CreateTemp(os.TempDir(), "")
    25  	if err != nil {
    26  		panic(err)
    27  	}
    28  	defer func() {
    29  		_ = os.Remove(file.Name())
    30  	}()
    31  
    32  	_, _ = file.WriteString(sentinel)
    33  	_ = file.Sync()
    34  
    35  	var testStruct struct {
    36  		Field1 string
    37  		Field2 int
    38  	}
    39  	err = UnmarshalLocalFile(file.Name(), &testStruct)
    40  	require.NoError(t, err, "Could not unmarshal test data")
    41  
    42  	if testStruct.Field1 != field1 || testStruct.Field2 != field2 {
    43  		t.Errorf("Test data did not match! Expected {%s %d} but got: %v", field1, field2, testStruct)
    44  	}
    45  }
    46  
    47  func TestUnmarshal(t *testing.T) {
    48  	const (
    49  		field1 = "Hello, world!"
    50  		field2 = 42
    51  	)
    52  	sentinel := fmt.Sprintf("---\nfield1: %q\nfield2: %d", field1, field2)
    53  
    54  	var testStruct struct {
    55  		Field1 string
    56  		Field2 int
    57  	}
    58  	err := Unmarshal([]byte(sentinel), &testStruct)
    59  	require.NoError(t, err, "Could not unmarshal test data")
    60  
    61  	if testStruct.Field1 != field1 || testStruct.Field2 != field2 {
    62  		t.Errorf("Test data did not match! Expected {%s %d} but got: %v", field1, field2, testStruct)
    63  	}
    64  }
    65  
    66  func TestUnmarshalRemoteFile(t *testing.T) {
    67  	const (
    68  		field1 = "Hello, world!"
    69  		field2 = 42
    70  	)
    71  	sentinel := fmt.Sprintf("---\nfield1: %q\nfield2: %d", field1, field2)
    72  
    73  	serve := func(c chan<- string) {
    74  		// listen on first available dynamic (unprivileged) port
    75  		listener, err := net.Listen("tcp", ":0")
    76  		if err != nil {
    77  			panic(err)
    78  		}
    79  
    80  		// send back the address so that it can be used
    81  		c <- listener.Addr().String()
    82  
    83  		http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
    84  			// return the sentinel text at root URL
    85  			fmt.Fprint(w, sentinel)
    86  		})
    87  
    88  		panic(http.Serve(listener, nil))
    89  	}
    90  
    91  	c := make(chan string, 1)
    92  
    93  	// run a local webserver to test data retrieval
    94  	go serve(c)
    95  
    96  	address := <-c
    97  	t.Logf("Listening at address: %s", address)
    98  
    99  	data, err := ReadRemoteFile("http://" + address)
   100  	assert.Equal(t, string(data), sentinel, "Test data did not match (err = %v)! Expected %q and received %q", err, sentinel, string(data))
   101  
   102  	var testStruct struct {
   103  		Field1 string
   104  		Field2 int
   105  	}
   106  	err = UnmarshalRemoteFile("http://"+address, &testStruct)
   107  	require.NoError(t, err, "Could not unmarshal test data")
   108  
   109  	if testStruct.Field1 != field1 || testStruct.Field2 != field2 {
   110  		t.Errorf("Test data did not match! Expected {%s %d} but got: %v", field1, field2, testStruct)
   111  	}
   112  }
   113  
   114  func TestUnmarshalReader(t *testing.T) {
   115  	type testStruct struct {
   116  		Value string
   117  	}
   118  	value := "test-reader"
   119  	instance := testStruct{value}
   120  	data, err := json.Marshal(instance)
   121  	require.NoError(t, err)
   122  	var reader io.Reader = bytes.NewReader(data)
   123  	err = UnmarshalReader(reader, &instance)
   124  	require.NoError(t, err)
   125  	assert.Equal(t, value, instance.Value)
   126  }