github.com/verrazzano/verrazzano@v1.7.0/authproxy/src/config/config_test.go (about)

     1  // Copyright (c) 2023, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package config
     5  
     6  import (
     7  	"os"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/verrazzano/verrazzano/authproxy/internal/testutil/file"
    13  	"go.uber.org/zap"
    14  )
    15  
    16  // TestInitConfiguration tests the InitConfiguration function
    17  func TestInitConfiguration(t *testing.T) {
    18  	const testServiceURL = "provider.namespace.svc.cluster.local"
    19  	const testExternalURL = "provider.com"
    20  	const testClientID = "unit-test-client-id"
    21  
    22  	// create temporary files with test data and override the filenames
    23  	serviceURLFile, err := file.MakeTempFile(testServiceURL)
    24  	if serviceURLFile != nil {
    25  		defer os.Remove(serviceURLFile.Name())
    26  	}
    27  	assert.NoError(t, err)
    28  
    29  	externalURLFile, err := file.MakeTempFile(testExternalURL)
    30  	if externalURLFile != nil {
    31  		defer os.Remove(externalURLFile.Name())
    32  	}
    33  	assert.NoError(t, err)
    34  
    35  	clientIDFile, err := file.MakeTempFile(testClientID)
    36  	if clientIDFile != nil {
    37  		defer os.Remove(clientIDFile.Name())
    38  	}
    39  	assert.NoError(t, err)
    40  
    41  	// restore the filenames when this test is done
    42  	oldServiceURLFilename := serviceURLFilename
    43  	defer func() { serviceURLFilename = oldServiceURLFilename }()
    44  	serviceURLFilename = serviceURLFile.Name()
    45  
    46  	oldExternalURLFilename := externalURLFilename
    47  	defer func() { externalURLFilename = oldExternalURLFilename }()
    48  	externalURLFilename = externalURLFile.Name()
    49  
    50  	oldClientIDFilename := clientIDFilename
    51  	defer func() { clientIDFilename = oldClientIDFilename }()
    52  	clientIDFilename = clientIDFile.Name()
    53  
    54  	// override the watch interval
    55  	oldWatchInterval := watchInterval
    56  	defer func() { watchInterval = oldWatchInterval }()
    57  	watchInterval = 500 * time.Millisecond
    58  
    59  	// GIVEN initial configuration files
    60  	// WHEN the InitConfiguration function is called
    61  	// THEN the fetched configuration values match the file contents
    62  	err = InitConfiguration(zap.S())
    63  	assert.NoError(t, err)
    64  
    65  	assert.Equal(t, testServiceURL, GetServiceURL())
    66  	assert.Equal(t, testExternalURL, GetExternalURL())
    67  	assert.Equal(t, testClientID, GetClientID())
    68  
    69  	// GIVEN the file contents are changed
    70  	// WHEN we fetch the configuration values
    71  	// THEN the values eventually match the expected updated file contents
    72  	const newTestServiceURL = "new-provider.namespace.svc.cluster.local"
    73  	const newTestExternalURL = "new-provider.com"
    74  	const newTestClientID = "new-unit-test-client-id"
    75  
    76  	// update the file contents and validate that the new values are loaded
    77  	err = os.WriteFile(serviceURLFilename, []byte(newTestServiceURL), 0)
    78  	assert.NoError(t, err)
    79  
    80  	err = os.WriteFile(externalURLFilename, []byte(newTestExternalURL), 0)
    81  	assert.NoError(t, err)
    82  
    83  	err = os.WriteFile(clientIDFilename, []byte(newTestClientID), 0)
    84  	assert.NoError(t, err)
    85  
    86  	eventually(func() bool { return GetServiceURL() == newTestServiceURL })
    87  	eventually(func() bool { return GetExternalURL() == newTestExternalURL })
    88  	eventually(func() bool { return GetClientID() == newTestClientID })
    89  
    90  	// stop the goroutine
    91  	keepWatching.Store(false)
    92  }
    93  
    94  // eventually executes the provided function until it either returns true or exceeds a number of attempts
    95  func eventually(f func() bool) bool {
    96  	for i := 0; i < 10; i++ {
    97  		if f() == true {
    98  			return true
    99  		}
   100  		time.Sleep(watchInterval)
   101  	}
   102  	return false
   103  }