Skip to content

Commit bd0cc24

Browse files
author
Kaan Yalti
committed
enhancement(5235): added TestUpgradeDirectoryCopyErrors test in upgrade_test
enhancement(5235): added test imports in upgrade_test
1 parent ede6b6a commit bd0cc24

File tree

1 file changed

+132
-1
lines changed

1 file changed

+132
-1
lines changed

internal/pkg/agent/application/upgrade/upgrade_test.go

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"crypto/tls"
1010
"fmt"
1111
"io/fs"
12+
"net/http"
13+
"net/http/httptest"
1214
"os"
1315
"path/filepath"
1416
"runtime"
@@ -41,6 +43,7 @@ import (
4143
"github.com/elastic/elastic-agent/pkg/core/logger"
4244
"github.com/elastic/elastic-agent/pkg/core/logger/loggertest"
4345
agtversion "github.com/elastic/elastic-agent/pkg/version"
46+
"github.com/elastic/elastic-agent/testing/mocks/internal_/pkg/agent/application/info"
4447
mocks "github.com/elastic/elastic-agent/testing/mocks/pkg/control/v2/client"
4548
)
4649

@@ -1419,7 +1422,7 @@ func TestCoyActionStore(t *testing.T) {
14191422
},
14201423
expectedError: fs.ErrPermission,
14211424
},
1422-
"should return disk space error if it cannot write the action store files": { // adding this test case to explicitely show that disk space error gets returned
1425+
"should return disk space error if it cannot write the action store files": { // adding this test case to explicitly show that disk space error gets returned
14231426
files: []testFile{
14241427
{name: "action_store", perm: 0o600, content: actionStoreContent},
14251428
{name: "state_yaml", perm: 0o600, content: actionStateStoreYamlContent},
@@ -1573,3 +1576,131 @@ func TestCopyRunDirectory(t *testing.T) {
15731576
})
15741577
}
15751578
}
1579+
1580+
func TestUpgradeDirectoryCopyErrors(t *testing.T) {
1581+
log, _ := loggertest.New("test")
1582+
1583+
initialVersion := agtversion.NewParsedSemVer(1, 2, 3, "SNAPSHOT", "")
1584+
initialArtifactName, initialArchiveFiles := buildArchiveFiles(t, archiveFilesWithMoreComponents, initialVersion, "abcdef")
1585+
1586+
targetVersion := agtversion.NewParsedSemVer(3, 4, 5, "SNAPSHOT", "")
1587+
targetArtifactName, targetArchiveFiles := buildArchiveFiles(t, archiveFilesWithMoreComponents, targetVersion, "ghijkl")
1588+
1589+
mockAgentInfo := info.NewAgent(t)
1590+
mockAgentInfo.On("Version").Return(targetVersion.String())
1591+
1592+
upgradeDetails := details.NewDetails(targetVersion.String(), details.StateRequested, "test")
1593+
1594+
tempUnpacker, err := NewUpgrader(log, &artifact.Config{}, mockAgentInfo) // used only to unpack the initial archive
1595+
require.NoError(t, err)
1596+
1597+
genericError := errors.New("test error")
1598+
1599+
testCases := map[string]struct {
1600+
mockReturnedError error
1601+
expectedError error
1602+
}{
1603+
"should return error if run directory copy fails": {
1604+
mockReturnedError: genericError,
1605+
expectedError: genericError,
1606+
},
1607+
}
1608+
1609+
for _, te := range upgradeErrors.OS_DiskSpaceErrors {
1610+
testCases[fmt.Sprintf("should return error if run directory copy fails with disk space error: %v", te)] = struct {
1611+
mockReturnedError error
1612+
expectedError error
1613+
}{
1614+
mockReturnedError: te,
1615+
expectedError: upgradeErrors.ErrInsufficientDiskSpace,
1616+
}
1617+
}
1618+
1619+
for _, copiedDir := range []string{"action_store", "run_directory"} {
1620+
for name, tc := range testCases {
1621+
t.Run(fmt.Sprintf("when copying %s: %s", copiedDir, name), func(t *testing.T) {
1622+
baseDir := t.TempDir()
1623+
paths.SetTop(baseDir)
1624+
paths.SetDownloads(filepath.Join(baseDir, "downloads")) // ensure the upgrader uses a predictable path for downloads
1625+
initialArchive, err := createArchive(t, initialArtifactName, initialArchiveFiles)
1626+
require.NoError(t, err)
1627+
1628+
t.Logf("Created archive: %s", initialArchive)
1629+
1630+
_, err = tempUnpacker.unpack(initialVersion.String(), initialArchive, paths.Data(), "")
1631+
require.NoError(t, err)
1632+
1633+
// The archive file list does not contain the action store files, so we need to
1634+
// create them
1635+
actionStorePaths := []string{paths.AgentActionStoreFile(), paths.AgentStateStoreYmlFile(), paths.AgentStateStoreFile()}
1636+
for _, path := range actionStorePaths {
1637+
err := os.MkdirAll(filepath.Dir(path), 0o700)
1638+
require.NoError(t, err, "error creating directory %s", filepath.Dir(path))
1639+
1640+
err = os.WriteFile(path, []byte(fmt.Sprintf("initial agent %s content", filepath.Base(path))), 0o600)
1641+
require.NoError(t, err, "error writing to %s", path)
1642+
}
1643+
1644+
targetArchive, err := createArchive(t, targetArtifactName, targetArchiveFiles)
1645+
require.NoError(t, err)
1646+
1647+
t.Logf("Created archive: %s", targetArchive)
1648+
1649+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1650+
http.ServeFile(w, r, targetArchive)
1651+
}))
1652+
t.Cleanup(server.Close)
1653+
1654+
mockCalled := false
1655+
1656+
if copiedDir == "run_directory" {
1657+
initialRunPath := paths.Run()
1658+
require.NoError(t, os.MkdirAll(initialRunPath, 0o755))
1659+
// Archive files do not contain the run directory, so we need to
1660+
// create it and add some files
1661+
for i := 0; i < 3; i++ {
1662+
filePath := filepath.Join(initialRunPath, fmt.Sprintf("file%d.txt", i))
1663+
err := os.WriteFile(filePath, []byte(fmt.Sprintf("content for file %d", i)), 0o600)
1664+
require.NoError(t, err)
1665+
}
1666+
1667+
tmpCopyRunDirFunc := copyRunDirectoryFunc
1668+
t.Cleanup(func() {
1669+
copyRunDirectoryFunc = tmpCopyRunDirFunc
1670+
})
1671+
1672+
copyRunDirectoryFunc = func(log *logger.Logger, oldRunPath string, newRunPath string) error {
1673+
mockCalled = true
1674+
return tc.mockReturnedError
1675+
}
1676+
} else {
1677+
tmpCopyActionStoreFunc := copyActionStoreFunc
1678+
t.Cleanup(func() {
1679+
copyActionStoreFunc = tmpCopyActionStoreFunc
1680+
})
1681+
1682+
copyActionStoreFunc = func(log *logger.Logger, newHome string) error {
1683+
mockCalled = true
1684+
return tc.mockReturnedError
1685+
}
1686+
}
1687+
1688+
config := artifact.Config{
1689+
TargetDirectory: paths.Downloads(),
1690+
SourceURI: server.URL,
1691+
RetrySleepInitDuration: 1 * time.Second,
1692+
HTTPTransportSettings: httpcommon.HTTPTransportSettings{
1693+
Timeout: 1 * time.Second,
1694+
},
1695+
}
1696+
1697+
upgrader, err := NewUpgrader(log, &config, mockAgentInfo)
1698+
require.NoError(t, err)
1699+
1700+
_, err = upgrader.Upgrade(context.Background(), targetVersion.String(), server.URL, nil, upgradeDetails, true, true)
1701+
require.True(t, mockCalled, "mock should be called")
1702+
require.ErrorIs(t, err, tc.expectedError, "expected error mismatch")
1703+
})
1704+
}
1705+
}
1706+
}

0 commit comments

Comments
 (0)