Skip to content

Commit

Permalink
Adds a testcase when cloud-init should fail to remove agent.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosNihelton committed Aug 26, 2024
1 parent 8fb8f2f commit 6ed6be5
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions windows-agent/internal/cloudinit/cloudinit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ hostagent_uid = landscapeUID1234
badLandscape bool

// Break writing to file
breakDir bool
breakTempFile bool
breakFile bool
breakDir bool
breakTempFile bool
breakFile bool
breakRemovingFile bool
}{
"Success": {},
"Without hostagent UID": {skipHostAgentUID: true},
Expand All @@ -110,6 +111,7 @@ hostagent_uid = landscapeUID1234
"Without Landscape [client] section": {landscapeNoClientSection: true},
"With empty contents": {skipProToken: true, skipLandscapeConf: true},

"Error to remove existing agent.yaml": {skipProToken: true, skipLandscapeConf: true, breakRemovingFile: true},
"Error obtaining pro token": {breakSubscription: true},
"Error obtaining Landscape config": {breakLandscape: true},
"Error with erroneous Landscape config": {badLandscape: true},
Expand Down Expand Up @@ -176,6 +178,11 @@ hostagent_uid = landscapeUID1234
require.NoError(t, os.WriteFile(dir, nil, 0600), "Setup: could not create file to mess with cloud-init directory")
}

if tc.breakRemovingFile {
_ = os.RemoveAll(path)
require.NoError(t, os.MkdirAll(path, 0750), "Creating the directory that breaks removing agent.yaml should not fail")
require.NoError(t, os.WriteFile(filepath.Join(path, "child.txt"), nil, 0600), "Setup: could not create file to mess with agent.yaml")
}
ci.Update(ctx)

// Assert that the file was updated (success case) or that the old one remains (error case)
Expand All @@ -184,6 +191,11 @@ hostagent_uid = landscapeUID1234
return
}

if tc.breakRemovingFile {
require.DirExists(t, path, "There should be a directory instead of agent.yaml")
return
}

if tc.skipProToken && tc.skipLandscapeConf {
// empty file should not exist
require.NoFileExists(t, path, "There should not be cloud-init agent file without useful contents")
Expand Down

0 comments on commit 6ed6be5

Please sign in to comment.