|  | 
|  | 1 | +package certwatcher | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"os" | 
|  | 5 | +	"path/filepath" | 
|  | 6 | +	"sync" | 
|  | 7 | +	"testing" | 
|  | 8 | +	"time" | 
|  | 9 | + | 
|  | 10 | +	"github.com/stretchr/testify/require" | 
|  | 11 | +) | 
|  | 12 | + | 
|  | 13 | +func TestNewCertWatcher(t *testing.T) { | 
|  | 14 | +	t.Run("success", func(t *testing.T) { | 
|  | 15 | +		tempDir := t.TempDir() | 
|  | 16 | +		certPath := filepath.Join(tempDir, "tls.crt") | 
|  | 17 | +		keyPath := filepath.Join(tempDir, "tls.key") | 
|  | 18 | +		require.NoError(t, os.WriteFile(certPath, []byte("cert"), 0600)) | 
|  | 19 | +		require.NoError(t, os.WriteFile(keyPath, []byte("key"), 0600)) | 
|  | 20 | + | 
|  | 21 | +		cw, err := NewCertWatcher(certPath, keyPath) | 
|  | 22 | +		require.NoError(t, err) | 
|  | 23 | +		require.NotNil(t, cw) | 
|  | 24 | +		require.Len(t, cw.directories, 1) | 
|  | 25 | +	}) | 
|  | 26 | + | 
|  | 27 | +	t.Run("cert path does not exist", func(t *testing.T) { | 
|  | 28 | +		tempDir := t.TempDir() | 
|  | 29 | +		certPath := filepath.Join(tempDir, "tls.crt") | 
|  | 30 | +		keyPath := filepath.Join(tempDir, "tls.key") | 
|  | 31 | +		require.NoError(t, os.WriteFile(keyPath, []byte("key"), 0600)) | 
|  | 32 | + | 
|  | 33 | +		_, err := NewCertWatcher(certPath, keyPath) | 
|  | 34 | +		require.Error(t, err) | 
|  | 35 | +	}) | 
|  | 36 | + | 
|  | 37 | +	t.Run("key path does not exist", func(t *testing.T) { | 
|  | 38 | +		tempDir := t.TempDir() | 
|  | 39 | +		certPath := filepath.Join(tempDir, "tls.crt") | 
|  | 40 | +		keyPath := filepath.Join(tempDir, "tls.key") | 
|  | 41 | +		require.NoError(t, os.WriteFile(certPath, []byte("cert"), 0600)) | 
|  | 42 | + | 
|  | 43 | +		_, err := NewCertWatcher(certPath, keyPath) | 
|  | 44 | +		require.Error(t, err) | 
|  | 45 | +	}) | 
|  | 46 | +} | 
|  | 47 | + | 
|  | 48 | +func TestCertWatcher(t *testing.T) { | 
|  | 49 | +	tempDir := t.TempDir() | 
|  | 50 | +	certPath := filepath.Join(tempDir, "tls.crt") | 
|  | 51 | +	keyPath := filepath.Join(tempDir, "tls.key") | 
|  | 52 | +	require.NoError(t, os.WriteFile(certPath, []byte("cert"), 0600)) | 
|  | 53 | +	require.NoError(t, os.WriteFile(keyPath, []byte("key"), 0600)) | 
|  | 54 | + | 
|  | 55 | +	cw, err := NewCertWatcher(certPath, keyPath) | 
|  | 56 | +	require.NoError(t, err) | 
|  | 57 | +	require.NotNil(t, cw) | 
|  | 58 | + | 
|  | 59 | +	wg := sync.WaitGroup{} | 
|  | 60 | +	wg.Add(1) | 
|  | 61 | +	go func() { | 
|  | 62 | +		defer wg.Done() | 
|  | 63 | +		cw.Run() | 
|  | 64 | +	}() | 
|  | 65 | + | 
|  | 66 | +	// Wait a bit for the watcher to start | 
|  | 67 | +	time.Sleep(100 * time.Millisecond) | 
|  | 68 | + | 
|  | 69 | +	// Update the cert file | 
|  | 70 | +	require.NoError(t, os.WriteFile(certPath, []byte("new cert"), 0600)) | 
|  | 71 | + | 
|  | 72 | +	select { | 
|  | 73 | +	case <-cw.Events(): | 
|  | 74 | +		// All good | 
|  | 75 | +	case <-time.After(5 * time.Second): | 
|  | 76 | +		t.Fatal("timed out waiting for event") | 
|  | 77 | +	} | 
|  | 78 | + | 
|  | 79 | +	// Update the key file | 
|  | 80 | +	require.NoError(t, os.WriteFile(keyPath, []byte("new key"), 0600)) | 
|  | 81 | + | 
|  | 82 | +	select { | 
|  | 83 | +	case <-cw.Events(): | 
|  | 84 | +		// All good | 
|  | 85 | +	case <-time.After(5 * time.Second): | 
|  | 86 | +		t.Fatal("timed out waiting for event") | 
|  | 87 | +	} | 
|  | 88 | + | 
|  | 89 | +	cw.Close() | 
|  | 90 | +	wg.Wait() | 
|  | 91 | +} | 
0 commit comments