Skip to content

Commit b57eacf

Browse files
authored
Merge pull request #7269 from chrischdi/pr-log-improvements
🌱 Improve logging in test/framework
2 parents f37f4d8 + f91e6b2 commit b57eacf

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

docs/book/src/developer/providers/v1.2-to-v1.3.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@ The default value is 0, meaning that the volume can be detached without any time
4646
- Custer API introduced new [logging guidelines](../../developer/logging.md). All reconcilers in the core repository were updated
4747
to [log the entire object hierarchy](../../developer/logging.md#keyvalue-pairs). It would be great if providers would be adjusted
4848
as well to make it possible to cross-reference log entries across providers (please see CAPD for an infra provider reference implementation).
49+
- The `CreateLogFile` function and `CreateLogFileInput` struct in the E2E test framework for clusterctl has been renamed to `OpenLogFile` and `OpenLogFileInput` because the function will now append to the logfile instead of truncating the content.
50+
- The `Move` function in E2E test framework for clusterctl has been modified to:
51+
* print the `clusterctl move` command including the arguments similar to `Init`.
52+
* log the output to the a `clusterctl-move.log` file at the subdirectory `logs/<namespace>`.

test/framework/clusterctl/client.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"os"
2323
"os/exec"
24+
"path"
2425
"path/filepath"
2526
"strings"
2627

@@ -315,11 +316,17 @@ func Move(ctx context.Context, input MoveInput) {
315316
Expect(input.ClusterctlConfigPath).To(BeAnExistingFile(), "Invalid argument. input.ClusterctlConfigPath must be an existing file when calling Move")
316317
Expect(input.FromKubeconfigPath).To(BeAnExistingFile(), "Invalid argument. input.FromKubeconfigPath must be an existing file when calling Move")
317318
Expect(input.ToKubeconfigPath).To(BeAnExistingFile(), "Invalid argument. input.ToKubeconfigPath must be an existing file when calling Move")
318-
Expect(os.MkdirAll(input.LogFolder, 0750)).To(Succeed(), "Invalid argument. input.LogFolder can't be created for Move")
319+
logDir := path.Join(input.LogFolder, "logs", input.Namespace)
320+
Expect(os.MkdirAll(logDir, 0750)).To(Succeed(), "Invalid argument. input.LogFolder can't be created for Move")
319321

320322
By("Moving workload clusters")
323+
log.Logf("clusterctl move --from-kubeconfig %s --to-kubeconfig %s --namespace %s",
324+
input.FromKubeconfigPath,
325+
input.ToKubeconfigPath,
326+
input.Namespace,
327+
)
321328

322-
clusterctlClient, log := getClusterctlClientWithLogger(input.ClusterctlConfigPath, "clusterctl-move.log", input.LogFolder)
329+
clusterctlClient, log := getClusterctlClientWithLogger(input.ClusterctlConfigPath, "clusterctl-move.log", logDir)
323330
defer log.Close()
324331
options := clusterctlclient.MoveOptions{
325332
FromKubeconfig: clusterctlclient.Kubeconfig{Path: input.FromKubeconfigPath, Context: ""},
@@ -331,7 +338,7 @@ func Move(ctx context.Context, input MoveInput) {
331338
}
332339

333340
func getClusterctlClientWithLogger(configPath, logName, logFolder string) (clusterctlclient.Client, *logger.LogFile) {
334-
log := logger.CreateLogFile(logger.CreateLogFileInput{
341+
log := logger.OpenLogFile(logger.OpenLogFileInput{
335342
LogFolder: logFolder,
336343
Name: logName,
337344
})

test/framework/clusterctl/logger/log_file.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ import (
2727

2828
// Provides a log_file that can be used to store the logs generated by clusterctl actions.
2929

30-
type CreateLogFileInput struct {
30+
type OpenLogFileInput struct {
3131
LogFolder string
3232
Name string
3333
}
3434

35-
func CreateLogFile(input CreateLogFileInput) *LogFile {
35+
func OpenLogFile(input OpenLogFileInput) *LogFile {
3636
filePath := filepath.Join(input.LogFolder, input.Name)
3737
Expect(os.MkdirAll(filepath.Dir(filePath), 0750)).To(Succeed(), "Failed to create log folder %s", filepath.Dir(filePath))
3838

39-
f, err := os.Create(filePath) //nolint:gosec // No security issue: filepath is safe.
39+
f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) //nolint:gosec // No security issue: filepath is safe.
4040
Expect(err).ToNot(HaveOccurred(), "Failed to create log file %s", filePath)
4141

4242
return &LogFile{

0 commit comments

Comments
 (0)