-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cloud-init): Implement upcoming cloud-init WSL support (#506)
Cloud-init will, at some point, be able to pick up files from specified locations in the Windows file-system. The plan is for UP4W’s agent to receive cloud-init files from Landscape and put them at the appropriate location. Furthermore, system-wide config (Pro token and Landscape) must be written as well. --- UDENG-2112
- Loading branch information
Showing
41 changed files
with
1,053 additions
and
302 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package endtoend_test | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
landscapeapi "github.com/canonical/landscape-hostagent-api" | ||
"github.com/canonical/ubuntu-pro-for-wsl/common/testutils" | ||
"github.com/stretchr/testify/require" | ||
wsl "github.com/ubuntu/gowsl" | ||
) | ||
|
||
func TestCloudInitIntegration(t *testing.T) { | ||
// TODO: Remove this line when cloud-init support for UP4W is released. | ||
// Follow this PR for more information: https://github.com/canonical/cloud-init/pull/5116 | ||
t.Skip("This test depends on cloud-init support for UP4W being released.") | ||
currentFuncName := t.Name() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
testSetup(t) | ||
defer logWindowsAgentOnError(t) | ||
|
||
landscape := NewLandscape(t, ctx) | ||
writeUbuntuProRegistry(t, "LandscapeConfig", landscape.ClientConfig) | ||
|
||
serverDone := make(chan struct{}) | ||
go func() { | ||
defer close(serverDone) | ||
landscape.Serve() | ||
}() | ||
t.Cleanup(func() { | ||
landscape.Stop() | ||
<-serverDone | ||
}) | ||
defer landscape.LogOnError(t) | ||
|
||
hostname, err := os.Hostname() | ||
require.NoError(t, err, "Setup: could not test machine's hostname") | ||
|
||
proToken := os.Getenv(proTokenEnv) | ||
require.NotEmptyf(t, proToken, "Setup: environment variable %q should contain a valid pro token, but is empty", proTokenEnv) | ||
writeUbuntuProRegistry(t, "UbuntuProToken", proToken) | ||
|
||
cleanup := startAgent(t, ctx, currentFuncName) | ||
defer cleanup() | ||
|
||
info := landscape.RequireReceivedInfo(t, proToken, nil, hostname) | ||
|
||
out, err := os.ReadFile(filepath.Join(testutils.TestFixturePath(t), "user-data.yaml")) | ||
require.NoError(t, err, "Setup: could not read cloud-init file") | ||
cloudInitUserData := string(out) | ||
|
||
err = landscape.service.SendCommand(ctx, info.UID, &landscapeapi.Command{ | ||
Cmd: &landscapeapi.Command_Install_{ | ||
Install: &landscapeapi.Command_Install{ | ||
Id: strings.Split(referenceDistroAppx, ".")[1], // CanonicalGroupLimited.[UbuntuPreview] | ||
Cloudinit: &cloudInitUserData, | ||
}, | ||
}, | ||
}) | ||
require.NoError(t, err, "Setup: could not send install command") | ||
|
||
distro := wsl.NewDistro(ctx, referenceDistro) | ||
|
||
//nolint:errcheck // Nothing we can do about it | ||
defer distro.Unregister() | ||
|
||
require.Eventually(t, func() bool { | ||
ok, err := distro.IsRegistered() | ||
if err != nil { | ||
t.Logf("could not determine if distro is registered: %v", err) | ||
return false | ||
} | ||
return ok | ||
}, time.Minute, time.Second, "Distro should have been registered") | ||
|
||
defer logWslProServiceOnError(t, ctx, distro) | ||
|
||
runCommand(t, ctx, time.Minute, distro, "cloud-init status --wait") | ||
|
||
require.Eventually(t, func() bool { | ||
attached, err := distroIsProAttached(t, ctx, distro) | ||
if err != nil { | ||
t.Logf("could not determine if distro is attached: %v", err) | ||
return false | ||
} | ||
return attached | ||
}, 10*time.Second, time.Second, "distro should have been Pro attached") | ||
|
||
userName := runCommand(t, ctx, 10*time.Second, distro, "whoami") | ||
require.Equal(t, "testuser", userName, "cloud-init should have configured the default user") | ||
|
||
landscape.RequireReceivedInfo(t, proToken, []wsl.Distro{distro}, hostname) | ||
landscape.RequireUninstallCommand(t, ctx, distro, info) | ||
} | ||
|
||
//nolint:revive // t always goes before ctx | ||
func runCommand(t *testing.T, ctx context.Context, timeout time.Duration, distro wsl.Distro, comand string) string { | ||
t.Helper() | ||
|
||
ctx, cancel := context.WithTimeout(ctx, timeout) | ||
defer cancel() | ||
|
||
out, err := distro.Command(ctx, comand).CombinedOutput() | ||
if err == nil { | ||
return string(out) | ||
} | ||
|
||
// We check the context to see if it was a timeout. | ||
// This makes the error message easier to understand. | ||
|
||
select { | ||
case <-ctx.Done(): | ||
require.Fail(t, "Timed out waiting for cloud-init to finish") | ||
default: | ||
} | ||
|
||
require.NoError(t, err, "could not determine if cloud-init is done: %s. Output: %s", out, out) | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
end-to-end/testdata/TestCloudInitIntegration/user-data.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#cloud-config | ||
users: | ||
- name: testuser | ||
gecos: Test User | ||
sudo: ALL=(ALL) NOPASSWD:ALL | ||
groups: [adm,dialout,cdrom,floppy,sudo,audio,dip,video,plugdev,users,netdev] | ||
shell: /bin/bash | ||
write_files: | ||
- path: /etc/wsl.conf | ||
append: true | ||
content: | | ||
[user] | ||
default=testuser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.