diff --git a/e2e/scenario_win_test.go b/e2e/scenario_win_test.go index ed2454a3daf..f1a8cd34ca7 100644 --- a/e2e/scenario_win_test.go +++ b/e2e/scenario_win_test.go @@ -1,6 +1,9 @@ package e2e import ( + "context" + "fmt" + "strings" "testing" "github.com/Azure/agentbaker/e2e/config" @@ -16,6 +19,9 @@ func Test_Windows2019Containerd(t *testing.T) { VHD: config.VHDWindows2019Containerd, VMConfigMutator: func(vmss *armcompute.VirtualMachineScaleSet) {}, BootstrapConfigMutator: func(configuration *datamodel.NodeBootstrappingConfiguration) {}, + Validator: func(ctx context.Context, s *Scenario) { + ValidateFileHasContentWindows(ctx, s, "c:/k/kubelet", "--rotate-server-certificates=true") + }, }, }) } @@ -42,7 +48,6 @@ func Test_Windows2022ContainerdGen2(t *testing.T) { VHD: config.VHDWindows2022ContainerdGen2, VMConfigMutator: func(vmss *armcompute.VirtualMachineScaleSet) {}, BootstrapConfigMutator: func(configuration *datamodel.NodeBootstrappingConfiguration) { - }, }, }) @@ -75,3 +80,31 @@ func Test_Windows23H2Gen2(t *testing.T) { }, }) } + +func makeExecutablePowershellCommand(steps []string) string { + stepsWithEchos := make([]string, len(steps)*2) + + for i, s := range steps { + stepsWithEchos[i*2] = fmt.Sprintf("echo '%s'", cleanse(s)) + stepsWithEchos[i*2+1] = s + } + + // quote " quotes and $ vars + joinedCommand := strings.Join(stepsWithEchos, " && ") + quotedCommand := strings.Replace(joinedCommand, "'", "'\"'\"'", -1) + + command := fmt.Sprintf("pwsh -C '%s'", quotedCommand) + + return command +} + +func ValidateFileHasContentWindows(ctx context.Context, s *Scenario, fileName string, contents string) { + steps := []string{ + fmt.Sprintf("dir %[1]s", fileName), + fmt.Sprintf("Get-Content %[1]s", fileName), + fmt.Sprintf("if (Select-String -Path .%s -Pattern \"%s\" -SimpleMatch -Quiet) { return 1 } else { return 0 }", fileName, contents), + } + + command := makeExecutablePowershellCommand(steps) + execOnVMForScenarioValidateExitCode(ctx, s, command, 0, "could not validate file has contents - might mean file does not have contents, might mean something went wrong") +} diff --git a/e2e/validators.go b/e2e/validators.go index 22159bbfddb..ed92c96e105 100644 --- a/e2e/validators.go +++ b/e2e/validators.go @@ -64,7 +64,7 @@ func ValidateFileHasContent(ctx context.Context, s *Scenario, fileName string, c fmt.Sprintf("(sudo cat %[1]s | grep -q -F -e %[2]q)", fileName, contents), } - command := makeExecutableCommand(steps) + command := makeExecutableBashCommand(steps) execOnVMForScenarioValidateExitCode(ctx, s, command, 0, "could not validate file has contents - might mean file does not have contents, might mean something went wrong") } @@ -77,7 +77,7 @@ func ValidateFileExcludesContent(ctx context.Context, s *Scenario, fileName stri fmt.Sprintf("sudo cat %[1]s", fileName), fmt.Sprintf("(sudo cat %[1]s | grep -q -v -F -e %[2]q)", fileName, contents), } - command := makeExecutableCommand(steps) + command := makeExecutableBashCommand(steps) execOnVMForScenarioValidateExitCode(ctx, s, command, 0, "could not validate file excludes contents - might mean file does have contents, might mean something went wrong") } @@ -86,7 +86,7 @@ func cleanse(str string) string { return strings.Replace(str, "'", "", -1) } -func makeExecutableCommand(steps []string) string { +func makeExecutableBashCommand(steps []string) string { stepsWithEchos := make([]string, len(steps)*2) for i, s := range steps { @@ -131,7 +131,7 @@ func ServiceCanRestartValidator(ctx context.Context, s *Scenario, serviceName st "if [[ \"$INITIAL_PID\" == \"$POST_PID\" ]]; then echo PID did not change after restart, failing validator. ; exit 1; fi", } - command := makeExecutableCommand(steps) + command := makeExecutableBashCommand(steps) execOnVMForScenarioValidateExitCode(ctx, s, command, 0, "command to restart service failed") }