diff --git a/windows-agent/internal/cloudinit/cloudinit.go b/windows-agent/internal/cloudinit/cloudinit.go index a5a276764..c84efe8c5 100644 --- a/windows-agent/internal/cloudinit/cloudinit.go +++ b/windows-agent/internal/cloudinit/cloudinit.go @@ -27,17 +27,15 @@ type Config interface { // CloudInit contains necessary data to drop cloud-init user data files for WSL's data source to pick them up. type CloudInit struct { - globalDataDir string - distroDataDir string - conf Config + dataDir string + conf Config } // New creates a CloudInit object and attaches it to the configuration notifier. -func New(ctx context.Context, conf Config, dataDir string) (CloudInit, error) { +func New(ctx context.Context, conf Config, publicDir string) (CloudInit, error) { c := CloudInit{ - globalDataDir: dataDir, - distroDataDir: filepath.Join(dataDir, "landscape"), - conf: conf, + dataDir: filepath.Join(publicDir, ".cloud-init"), + conf: conf, } if err := c.WriteAgentData(ctx); err != nil { @@ -62,7 +60,7 @@ func (c CloudInit) WriteAgentData(ctx context.Context) (err error) { return err } - err = writeFileInDir(c.globalDataDir, "agent.yaml", cloudInit) + err = writeFileInDir(c.dataDir, "agent.yaml", cloudInit) if err != nil { return err } @@ -72,7 +70,7 @@ func (c CloudInit) WriteAgentData(ctx context.Context) (err error) { // WriteDistroData writes cloud-init user data to be used for a distro in particular. func (c CloudInit) WriteDistroData(distroName string, cloudInit string) error { - err := writeFileInDir(c.distroDataDir, distroName+".user-data", []byte(cloudInit)) + err := writeFileInDir(c.dataDir, distroName+".user-data", []byte(cloudInit)) if err != nil { return fmt.Errorf("could not create distro-specific cloud-init file: %v", err) } @@ -109,7 +107,7 @@ func writeFileInDir(dir string, file string, contents []byte) error { func (c CloudInit) RemoveDistroData(distroName string) (err error) { defer decorate.OnError(&err, "could not remove distro-specific cloud-init file") - path := filepath.Join(c.distroDataDir, distroName+".user-data") + path := filepath.Join(c.dataDir, distroName+".user-data") err = os.Remove(path) if errors.Is(err, fs.ErrNotExist) { diff --git a/windows-agent/internal/cloudinit/cloudinit_test.go b/windows-agent/internal/cloudinit/cloudinit_test.go index ff9aac5ea..e10f54be2 100644 --- a/windows-agent/internal/cloudinit/cloudinit_test.go +++ b/windows-agent/internal/cloudinit/cloudinit_test.go @@ -32,13 +32,13 @@ func TestNew(t *testing.T) { ctx := context.Background() - dataDir := t.TempDir() + publicDir := t.TempDir() conf := &mockConfig{ subcriptionErr: tc.breakWriteAgentData, } - _, err := cloudinit.New(ctx, conf, dataDir) + _, err := cloudinit.New(ctx, conf, publicDir) if tc.wantErr { require.Error(t, err, "Cloud-init creation should have returned an error") return @@ -48,7 +48,7 @@ func TestNew(t *testing.T) { require.Len(t, conf.notify, 1, "Cloud-init should have attached a callback to the config") // Assert that the subscribed function works - path := filepath.Join(dataDir, "agent.yaml") + path := filepath.Join(publicDir, ".cloud-init", "agent.yaml") require.NoErrorf(t, os.RemoveAll(path), "Removing the agent cloud-init should not fail") conf.triggerNotify() @@ -122,8 +122,8 @@ url = www.example.com/new/rickroll t.Parallel() ctx := context.Background() - root := t.TempDir() - dir := filepath.Join(root, "cloud-init") + publicDir := t.TempDir() + dir := filepath.Join(publicDir, ".cloud-init") path := filepath.Join(dir, "agent.yaml") conf := &mockConfig{ @@ -132,7 +132,7 @@ url = www.example.com/new/rickroll } // Test a clean filesystem (New calls WriteAgentData internally) - ci, err := cloudinit.New(ctx, conf, dir) + ci, err := cloudinit.New(ctx, conf, publicDir) require.NoError(t, err, "cloudinit.New should return no error") require.FileExists(t, path, "New() should have created an agent cloud-init file") @@ -263,14 +263,14 @@ data: distroName := "CoolDistro" - root := t.TempDir() - dir := filepath.Join(root, "cloud-init") - path := filepath.Join(dir, "landscape", distroName+".user-data") + publicDir := t.TempDir() + dir := filepath.Join(publicDir, ".cloud-init") + path := filepath.Join(dir, distroName+".user-data") conf := &mockConfig{} // Test a clean filesystem (New calls WriteAgentData internally) - ci, err := cloudinit.New(ctx, conf, dir) + ci, err := cloudinit.New(ctx, conf, publicDir) require.NoError(t, err, "Setup: cloud-init New should return no errors") if !tc.noOldData { @@ -343,15 +343,15 @@ func TestRemoveDistroData(t *testing.T) { distroName := "CoolDistro" - root := t.TempDir() - distroDir := filepath.Join(root, "landscape") - path := filepath.Join(distroDir, distroName+".user-data") + publicDir := t.TempDir() + dir := filepath.Join(publicDir, ".cloud-init") + path := filepath.Join(dir, distroName+".user-data") - ci, err := cloudinit.New(ctx, &mockConfig{}, root) + ci, err := cloudinit.New(ctx, &mockConfig{}, publicDir) require.NoError(t, err, "Setup: cloud-init New should return no errors") if !tc.dirDoesNotExist { - require.NoError(t, os.MkdirAll(distroDir, 0700), "Setup: could not set up directory") + require.NoError(t, os.MkdirAll(dir, 0700), "Setup: could not set up directory") if !tc.fileDoesNotExist { require.NoError(t, os.WriteFile(path, []byte("hello, world!"), 0600), "Setup: could not set up directory") }