Skip to content

Commit

Permalink
fix(end-to-end): Fix debian package paths in E2E tests (#428)
Browse files Browse the repository at this point in the history
For some reason the path was not being found by locateWSLProService. I
fixed it by getting rid of this function.

For more context: we were doing a weird mix of keeping track of the path
of the debian pkg in a local variable, and the path to the MSIX in a
global one. Instead, I moved the DEB to the global namespace for
consistency's sake. The code is now more readable.

I also sneak in a fix to the Agent's log finder.

---

UDENG-1860
  • Loading branch information
EduardGomezEscandell authored Dec 1, 2023
2 parents e4b11ec + eeaaee6 commit 398ea7a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 54 deletions.
2 changes: 1 addition & 1 deletion end-to-end/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/canonical/ubuntu-pro-for-windows/common v0.0.0-20230906090052-60fb5d60ada4
github.com/canonical/ubuntu-pro-for-windows/mocks v0.0.0-20231018132816-d78b12b1d065
github.com/stretchr/testify v1.8.4
github.com/ubuntu/decorate v0.0.0-20230905131025-e968fa48a85c
github.com/ubuntu/gowsl v0.0.0-20231004124730-8fd8df02f394
golang.org/x/sys v0.15.0
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -21,6 +20,7 @@ require (
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/ubuntu/decorate v0.0.0-20230905131025-e968fa48a85c // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
86 changes: 34 additions & 52 deletions end-to-end/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"testing"

"github.com/canonical/ubuntu-pro-for-windows/common"
"github.com/ubuntu/decorate"
"github.com/ubuntu/gowsl"
wsl "github.com/ubuntu/gowsl"
"golang.org/x/sys/windows/registry"
Expand All @@ -25,6 +24,9 @@ var (

// msixPath is the path to the Ubuntu Pro For Windows MSIX.
msixPath string

// debPkgPath is the path to the Wsl Pro Service Debian package.
debPkgPath string
)

const (
Expand Down Expand Up @@ -76,20 +78,23 @@ func TestMain(m *testing.M) {
log.Fatalf("Setup: %v\n", err)
}

f := buildProject
if buildPath := os.Getenv(prebuiltPath); buildPath != "" {
f = usePrebuiltProject
buildPath := os.Getenv(prebuiltPath)
if buildPath == "" {
path, err := buildProject(ctx)
if err != nil {
log.Fatalf("Setup: %v\n", err)
}
buildPath = path
}

wslProServiceDebPath, err := f(ctx)
if err != nil {
if err := usePrebuiltProject(buildPath); err != nil {
log.Fatalf("Setup: %v\n", err)
}

log.Printf("MSIX package located at %s", msixPath)
log.Printf("Deb package located at %s", wslProServiceDebPath)
log.Printf("Deb package located at %s", debPkgPath)

path, cleanup, err := generateTestImage(ctx, referenceDistro, wslProServiceDebPath)
path, cleanup, err := generateTestImage(ctx, referenceDistro)
if err != nil {
log.Fatalf("Setup: %v\n", err)
}
Expand All @@ -108,28 +113,34 @@ func TestMain(m *testing.M) {
}
}

func usePrebuiltProject(ctx context.Context) (debPath string, err error) {
buildPath := os.Getenv(prebuiltPath)

func usePrebuiltProject(buildPath string) (err error) {
// Locate the Appx package and store the path in global variable so that we can
// reinstall it before every test
result, err := globSingleResult(filepath.Join(buildPath, "windows-agent", "UbuntuProForWindows_*.msixbundle"))
if err != nil {
return "", fmt.Errorf("could not locate MSIX: %v", err)
return fmt.Errorf("could not locate MSIX: %v", err)
}

msixPath, err = filepath.Abs(result)
if err != nil {
return fmt.Errorf("could not make MSIX path absolute: %v", err)
}
msixPath = result

// Locate WSL-Pro-Service (it'll be installed later into the distros)
debPath = filepath.Join(buildPath, "wsl-pro-service")
_, err = locateWslProServiceDeb(debPath)
path, err := globSingleResult(filepath.Join(buildPath, "wsl-pro-service", "wsl-pro-service_*.deb"))
if err != nil {
return fmt.Errorf("could not locate WSL-Pro-Service: %v", err)
}

debPkgPath, err = filepath.Abs(path)
if err != nil {
return "", fmt.Errorf("could not locate pre-built WSL-Pro-Service: %v", err)
return fmt.Errorf("could not make debian package path absolute: %v", err)
}

return debPath, err
return nil
}

func buildProject(ctx context.Context) (debPath string, err error) {
func buildProject(ctx context.Context) (string, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

Expand All @@ -138,7 +149,7 @@ func buildProject(ctx context.Context) (debPath string, err error) {
return "", fmt.Errorf("could not create temporary directory for build artifacts")
}

debPath = filepath.Join(buildPath, "wsl-pro-service")
debPath := filepath.Join(buildPath, "wsl-pro-service")
winPath := filepath.Join(buildPath, "windows-agent")

if err := os.MkdirAll(debPath, 0600); err != nil {
Expand Down Expand Up @@ -189,15 +200,9 @@ func buildProject(ctx context.Context) (debPath string, err error) {
return "", fmt.Errorf("could not build project: %v", err)
}

// Locate the Appx package and store the path in global variable so that we can
// reinstall it before every test
path, err := globSingleResult(filepath.Join(winPath, "UbuntuProForWindows_*.msixbundle"))
if err != nil {
return "", fmt.Errorf("could not locate Appx: %v", err)
}

log.Println("Project built")
return path, nil

return buildPath, nil
}

// assertAppxInstalled returns an error if the provided Appx is not installed.
Expand All @@ -214,23 +219,6 @@ func assertAppxInstalled(ctx context.Context, appx string) error {
return nil
}

// locateWslProServiceDeb locates the WSL pro service at the repository root and returns its absolute path.
func locateWslProServiceDeb(path string) (debPath string, err error) {
defer decorate.OnError(&err, "could not locate wsl-pro-service deb package")

path, err = globSingleResult(filepath.Join(path, "wsl-pro-service_*.deb"))
if err != nil {
return "", err
}

debPath, err = filepath.Abs(debPath)
if err != nil {
return "", fmt.Errorf("could not make path %q absolute: %v", path, err)
}

return debPath, nil
}

// powershellf is syntax sugar to run powrshell commands.
func powershellf(ctx context.Context, command string, args ...any) *exec.Cmd {
//nolint:gosec // Tainted input is acceptable because all callers have their values hardcoded.
Expand Down Expand Up @@ -294,7 +282,6 @@ func filesToCleanUp() ([]string, error) {
}{
{prefixEnv: "LocalAppData", path: "Ubuntu Pro"},
{prefixEnv: "UserProfile", path: ".ubuntupro"},
{prefixEnv: "UserProfile", path: ".ubuntupro.logs"},
}

var out []string
Expand Down Expand Up @@ -353,7 +340,7 @@ func cleanupRegistry() error {
// generateTestImage fails if the sourceDistro is registered, unless the safety checks are overridden,
// in which case the sourceDistro is removed.
// The source distro is then registered, exported after first boot, and unregistered.
func generateTestImage(ctx context.Context, sourceDistro, wslProServiceDebPath string) (path string, cleanup func(), err error) {
func generateTestImage(ctx context.Context, sourceDistro string) (path string, cleanup func(), err error) {
log.Printf("Setup: Generating test image from %q\n", sourceDistro)
defer log.Printf("Setup: Generated test image from %q\n", sourceDistro)

Expand Down Expand Up @@ -396,12 +383,7 @@ func generateTestImage(ctx context.Context, sourceDistro, wslProServiceDebPath s
// From now on, all cleanups must be deferred because the distro
// must be unregistered before removing the directory it is in.

debPath, err := locateWslProServiceDeb(wslProServiceDebPath)
if err != nil {
return "", nil, err
}

out, err = d.Command(ctx, fmt.Sprintf("DEBIAN_FRONTEND=noninteractive apt install -y $(wslpath -ua '%s')", debPath)).CombinedOutput()
out, err = d.Command(ctx, fmt.Sprintf("DEBIAN_FRONTEND=noninteractive apt install -y $(wslpath -ua '%s')", debPkgPath)).CombinedOutput()
if err != nil {
defer cleanup()
return "", nil, fmt.Errorf("could not install wsl-pro-service: %v. %s", err, out)
Expand Down
2 changes: 1 addition & 1 deletion end-to-end/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func logWindowsAgentOnError(t *testing.T) {
return
}

logsPath := filepath.Join(packageDir, "LocalCache", "Local", common.LocalAppDataDir, ".ubuntupro.log")
logsPath := filepath.Join(packageDir, "LocalCache", "Local", common.LocalAppDataDir, "log")
out, err := os.ReadFile(logsPath)
if err != nil {
t.Logf("could not read Windows Agent's logs at %q: %v", logsPath, err)
Expand Down

0 comments on commit 398ea7a

Please sign in to comment.