Skip to content

Commit

Permalink
test(e2e): fix config reloader e2e test
Browse files Browse the repository at this point in the history
It sometimes takes a while for a config map change to actually be
seen from within the pod/container (in particular the config-reloader).
Might be a Docker Desktop thing. Either way, increasing the timeout
makes the test more stable.
  • Loading branch information
basti1302 committed Oct 18, 2024
1 parent 3e74a2a commit 107fbc2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
23 changes: 13 additions & 10 deletions images/configreloader/src/configreloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,14 @@ func initializeHashes(configurationFilePaths []string) error {
for _, configurationFilePath := range configurationFilePaths {
configurationFile, err := os.Open(configurationFilePath)
if err != nil {
return fmt.Errorf("cannot open '%v' configuration file: %w", configurationFilePath, err)
return fmt.Errorf("cannot open configuration file '%v': %w", configurationFilePath, err)
}
defer configurationFile.Close()

configurationFileHash := md5.New()
if _, err := io.Copy(configurationFileHash, configurationFile); err != nil {
return fmt.Errorf("cannot hash '%v' configuration file: %w", configurationFilePath, err)
return fmt.Errorf("cannot hash configuration file '%v': %w", configurationFilePath, err)
}

hashValue := hex.EncodeToString(configurationFileHash.Sum(nil))
configurationFileHashes[configurationFilePath] = hashValue
}
Expand All @@ -131,31 +130,33 @@ func checkConfiguration(
for _, configurationFilePath := range configurationFilePaths {
configurationFile, err := os.Open(configurationFilePath)
if err != nil {
return false, fmt.Errorf("cannot open '%v' configuration file: %w", configurationFilePath, err)
return false, fmt.Errorf("cannot open configuration file '%v': %w", configurationFilePath, err)
}
defer configurationFile.Close()

configurationFileHash := md5.New()
if _, err := io.Copy(configurationFileHash, configurationFile); err != nil {
return false, fmt.Errorf("cannot hash '%v' configuration file: %w", configurationFilePath, err)
return false, fmt.Errorf("cannot hash configuration file '%v': %w", configurationFilePath, err)
}

hashValue := hex.EncodeToString(configurationFileHash.Sum(nil))

if previousConfigurationFileHash, ok := configurationFileHashes[configurationFilePath]; ok {
if previousConfigurationFileHash != hashValue {
log.Printf("configuration file '%v' has been updated\n", configurationFilePath)
updatesConfigurationFilePaths = append(updatesConfigurationFilePaths, configurationFilePath)
// Update hash; we cannot check whether the collector *actually* updated,
// so we optimistically update the internal state anyhow
configurationFileHashes[configurationFilePath] = hashValue
}
} else {
// First time we hash the config file, nothing to do beyond updating
// This is the first time we hash this config file, nothing to do beyond updating. This should actually
// never happen since we initialize the hashes for all config files at startup in initializeHashes and
// config files cannot be added retroactively to a running collector pod.
configurationFileHashes[configurationFilePath] = hashValue
log.Printf("initialized hash for configuration file '%v'", configurationFilePath)
}
}

if len(updatesConfigurationFilePaths) < 1 {
// Nothing to do
// nothing to do
return false, nil
}

Expand Down Expand Up @@ -214,6 +215,7 @@ func parsePidFile(pidFilePath string) (OTelColPid, error) {
return 0, fmt.Errorf("pid file '%v' has an unexpected format: expecting a single integer; found additional content: %v", pidFilePath, scanner.Text())
}

log.Printf("parsed collector pid: %v\n", pid)
return OTelColPid(pid), nil
}

Expand All @@ -227,6 +229,7 @@ func triggerConfigurationReload(collectorPid OTelColPid) error {
return fmt.Errorf("cannot find the process with pid '%v': %w", collectorPid, err)
}

log.Printf("sending SIGHUP signal to the process with pid '%v'\n", collectorPid)
if err := collectorProcess.Signal(syscall.SIGHUP); err != nil {
return fmt.Errorf("an error occurred while sending the SIGHUP signal to the process with pid '%v': %w", collectorPid, err)
}
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,15 @@ func verifyConfigMapContainsString(operatorNamespace string, s string) {
"-o",
"json",
),
20*time.Second,
s,
)
}

func verifyCollectorContainerLogContainsStrings(
operatorNamespace string,
containerName string,
timeout time.Duration,
needles ...string,
) {
verifyCommandOutputContainsStrings(
Expand All @@ -146,6 +148,7 @@ func verifyCollectorContainerLogContainsStrings(
"-c",
containerName,
),
timeout,
needles...,
)
}
7 changes: 7 additions & 0 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,24 +518,31 @@ var _ = Describe("Dash0 Kubernetes Operator", Ordered, func() {
operatorHelmChart,
)

time.Sleep(10 * time.Second)

By("updating the Dash0 monitoring resource endpoint setting")
newEndpoint := "ingress.eu-east-1.aws.dash0-dev.com:4317"
updateEndpointOfDash0MonitoringResource(applicationUnderTestNamespace, newEndpoint)

By("verify that the config map has been updated by the controller")
verifyConfigMapContainsString(operatorNamespace, newEndpoint)

// This step sometimes takes quite a while, for some reason the config map change is not seen immediately
// from within the collector pod/config-reloader container process, although it polls the file every second.
// Thus, we allow a very generous timeout here.
By("verify that the configuration reloader says to have triggered a config change")
verifyCollectorContainerLogContainsStrings(
operatorNamespace,
"configuration-reloader",
90*time.Second,
"Triggering a collector update due to changes to the config files",
)

By("verify that the collector appears to have reloaded its configuration")
verifyCollectorContainerLogContainsStrings(
operatorNamespace,
"opentelemetry-collector",
20*time.Second,
"Received signal from OS",
"Config updated, restart service",
)
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/run_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ func getProjectDir() (string, error) {
return wd, nil
}

func verifyCommandOutputContainsStrings(command *exec.Cmd, needles ...string) {
func verifyCommandOutputContainsStrings(command *exec.Cmd, timeout time.Duration, needles ...string) {
Eventually(func(g Gomega) {
// We cannot run the same exec.Command multiple times, thus we create a new instance each time instead.
haystack, err := run(exec.Command(command.Args[0], command.Args[1:]...), false)
g.Expect(err).ToNot(HaveOccurred())
for _, needle := range needles {
g.Expect(haystack).To(ContainSubstring(needle))
}
}, 20*time.Second, time.Second).Should(Succeed())
}, timeout, time.Second).Should(Succeed())
}

0 comments on commit 107fbc2

Please sign in to comment.