Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky TestReload #2624

Merged
merged 1 commit into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/config/tlscfg/cert_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ func (w *certWatcher) watchChangesLoop(rootCAs, clientCAs *x509.CertPool) {
w.mu.Unlock()
err = e
}
if err != nil {
if err == nil {
w.logger.Info("Loaded modified certificate",
zap.String("certificate", event.Name),
zap.String("event", event.Op.String()))

} else {
w.logger.Error("Failed to load certificate",
zap.String("certificate", event.Name),
zap.String("event", event.Op.String()),
Expand Down
20 changes: 18 additions & 2 deletions pkg/config/tlscfg/cert_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package tlscfg
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -92,9 +93,24 @@ func TestReload(t *testing.T) {
require.NoError(t, err)

waitUntil(func() bool {
return logObserver.FilterField(zap.String("certificate", certFile.Name())).Len() > 0
// Logged when both matching public and private keys are modified in the cert.
// If mismatched keys are present in the cert, the "Failed to load certificate" error will be logged instead.
return logObserver.FilterMessage("Loaded modified certificate").Len() > 0
}, 100, time.Millisecond*200)
assert.True(t, logObserver.FilterField(zap.String("certificate", certFile.Name())).Len() > 0)

// Logged when the cert is modified with the client's public key due to
// a mismatch with the existing server private key.
assert.True(t, logObserver.
FilterMessage("Failed to load certificate").
FilterField(zap.String("certificate", certFile.Name())).Len() > 0,
"Failed to find wanted logs. All logs: "+fmt.Sprint(logObserver.All()))

// Logged when the cert is modified with the client's private key,
// resulting in both public and private keys matching (from the client).
assert.True(t, logObserver.
FilterMessage("Loaded modified certificate").
FilterField(zap.String("certificate", keyFile.Name())).Len() > 0,
"Failed to find wanted logs. All logs: "+fmt.Sprint(logObserver.All()))

cert, err = tls.LoadX509KeyPair(filepath.Clean(clientCert), clientKey)
require.NoError(t, err)
Expand Down