2021-10-01 13:59:55 +00:00
|
|
|
//go:build linux
|
|
|
|
// +build linux
|
2019-01-18 04:32:45 +00:00
|
|
|
|
|
|
|
package nomad09upgrade
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
getter "github.com/hashicorp/go-getter"
|
|
|
|
"github.com/hashicorp/nomad/e2e/e2eutil"
|
2019-01-26 00:51:20 +00:00
|
|
|
"github.com/hashicorp/nomad/e2e/execagent"
|
2019-01-18 04:32:45 +00:00
|
|
|
"github.com/hashicorp/nomad/e2e/framework"
|
|
|
|
"github.com/hashicorp/nomad/helper/discover"
|
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
framework.AddSuites(&framework.TestSuite{
|
|
|
|
Component: "nomad09upgrade",
|
|
|
|
CanRunLocal: true,
|
|
|
|
Cases: []framework.TestCase{
|
|
|
|
&UpgradePathTC{},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2019-01-26 00:51:20 +00:00
|
|
|
nomadVersions = [...]string{
|
2019-01-18 04:32:45 +00:00
|
|
|
"0.8.7",
|
|
|
|
"0.8.6",
|
|
|
|
"0.8.5",
|
|
|
|
"0.8.4",
|
|
|
|
"0.8.3",
|
|
|
|
"0.8.2",
|
|
|
|
"0.8.1",
|
|
|
|
"0.8.0",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type UpgradePathTC struct {
|
|
|
|
framework.TC
|
|
|
|
|
|
|
|
binDir string
|
|
|
|
bin string
|
|
|
|
}
|
|
|
|
|
2019-01-26 00:51:20 +00:00
|
|
|
type upgradeAgents struct {
|
|
|
|
origAgent *execagent.NomadAgent
|
|
|
|
targetAgent *execagent.NomadAgent
|
2019-01-18 04:32:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-26 00:51:20 +00:00
|
|
|
func (tc *UpgradePathTC) newNomadServer(t *testing.T, ver string) (*upgradeAgents, error) {
|
|
|
|
binPath := filepath.Join(tc.binDir, ver, "nomad")
|
|
|
|
srv, err := execagent.NewMixedAgent(binPath)
|
2019-01-18 04:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-26 00:51:20 +00:00
|
|
|
w := testlog.NewWriter(t)
|
|
|
|
srv.Cmd.Stdout = w
|
|
|
|
srv.Cmd.Stderr = w
|
2019-01-18 04:32:45 +00:00
|
|
|
|
2019-01-26 00:51:20 +00:00
|
|
|
// Target should copy everything but the binary to target
|
|
|
|
target, err := execagent.NewMixedAgent(tc.bin)
|
2019-01-18 04:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-26 00:51:20 +00:00
|
|
|
target.Cmd.Stdout = w
|
|
|
|
target.Cmd.Stderr = w
|
2019-01-18 04:32:45 +00:00
|
|
|
|
2019-01-26 00:51:20 +00:00
|
|
|
agents := &upgradeAgents{
|
|
|
|
origAgent: srv,
|
|
|
|
targetAgent: target,
|
2019-01-18 04:32:45 +00:00
|
|
|
}
|
2019-01-26 00:51:20 +00:00
|
|
|
return agents, nil
|
2019-01-18 04:32:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 04:25:27 +00:00
|
|
|
// BeforeAll downloads all of the desired nomad versions to test against
|
2019-01-18 04:32:45 +00:00
|
|
|
func (tc *UpgradePathTC) BeforeAll(f *framework.F) {
|
2019-02-27 13:21:21 +00:00
|
|
|
// Upgrade tests currently fail because the nomad binary isn't found by
|
|
|
|
// discover.NomadExecutable(). Ensure that nomad binary is available
|
|
|
|
// and discoverable and enable this test
|
|
|
|
f.T().Skip("upgrade tests are expected to fail. TODO: Fix")
|
|
|
|
|
2019-01-18 04:32:45 +00:00
|
|
|
bin, err := discover.NomadExecutable()
|
|
|
|
f.NoError(err)
|
|
|
|
tc.bin = bin
|
|
|
|
dir, err := ioutil.TempDir("", "")
|
|
|
|
f.NoError(err)
|
|
|
|
|
|
|
|
tc.binDir = dir
|
|
|
|
for _, ver := range nomadVersions {
|
|
|
|
verBin := filepath.Join(tc.binDir, ver)
|
|
|
|
f.NoError(os.Mkdir(verBin, 0755))
|
|
|
|
f.NoError(
|
|
|
|
getter.Get(verBin, fmt.Sprintf(
|
|
|
|
"https://releases.hashicorp.com/nomad/%s/nomad_%s_linux_amd64.zip",
|
|
|
|
ver, ver,
|
|
|
|
)))
|
|
|
|
f.T().Logf("downloaded nomad version %s to %s", ver, verBin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 04:25:27 +00:00
|
|
|
// AfterAll cleans up the downloaded nomad binaries
|
2019-01-18 04:32:45 +00:00
|
|
|
func (tc *UpgradePathTC) AfterAll(f *framework.F) {
|
|
|
|
os.RemoveAll(tc.binDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *UpgradePathTC) TestRawExecTaskUpgrade(f *framework.F) {
|
|
|
|
for _, ver := range nomadVersions {
|
|
|
|
ver := ver
|
|
|
|
f.T().Run(ver, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2019-01-23 19:44:42 +00:00
|
|
|
tc.testUpgradeForJob(t, ver, "nomad09upgrade/rawexec.nomad")
|
2019-01-18 04:32:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *UpgradePathTC) TestExecTaskUpgrade(f *framework.F) {
|
|
|
|
for _, ver := range nomadVersions {
|
|
|
|
ver := ver
|
|
|
|
f.T().Run(ver, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2019-01-23 19:44:42 +00:00
|
|
|
tc.testUpgradeForJob(t, ver, "nomad09upgrade/exec.nomad")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *UpgradePathTC) TestDockerTaskUpgrade(f *framework.F) {
|
|
|
|
for _, ver := range nomadVersions {
|
|
|
|
ver := ver
|
|
|
|
f.T().Run(ver, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tc.testUpgradeForJob(t, ver, "nomad09upgrade/docker.nomad")
|
2019-01-18 04:32:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *UpgradePathTC) testUpgradeForJob(t *testing.T, ver string, jobfile string) {
|
|
|
|
require := require.New(t)
|
2019-01-23 04:25:27 +00:00
|
|
|
// Start a nomad agent for the given version
|
2019-01-26 00:51:20 +00:00
|
|
|
agents, err := tc.newNomadServer(t, ver)
|
2019-01-18 04:32:45 +00:00
|
|
|
require.NoError(err)
|
|
|
|
t.Logf("launching v%s nomad agent", ver)
|
2019-01-26 00:51:20 +00:00
|
|
|
require.NoError(agents.origAgent.Start())
|
2019-01-23 04:25:27 +00:00
|
|
|
|
|
|
|
// Wait for the agent to be ready
|
2019-01-26 00:51:20 +00:00
|
|
|
client, err := agents.origAgent.Client()
|
2019-01-18 04:32:45 +00:00
|
|
|
require.NoError(err)
|
|
|
|
e2eutil.WaitForNodesReady(t, client, 1)
|
|
|
|
|
2019-01-23 04:25:27 +00:00
|
|
|
// Register a sleep job
|
2019-01-18 04:32:45 +00:00
|
|
|
jobID := "sleep-" + uuid.Generate()[:8]
|
|
|
|
t.Logf("registering exec job with id %s", jobID)
|
2020-01-28 22:33:59 +00:00
|
|
|
e2eutil.RegisterAndWaitForAllocs(t, client, jobfile, jobID, "")
|
2019-01-18 04:32:45 +00:00
|
|
|
allocs, _, err := client.Jobs().Allocations(jobID, false, nil)
|
|
|
|
require.NoError(err)
|
|
|
|
require.Len(allocs, 1)
|
|
|
|
|
2019-01-24 01:54:23 +00:00
|
|
|
// Wait for sleep job to transition to running
|
2019-01-18 04:32:45 +00:00
|
|
|
id := allocs[0].ID
|
|
|
|
e2eutil.WaitForAllocRunning(t, client, id)
|
2019-01-23 04:25:27 +00:00
|
|
|
|
|
|
|
// Stop the agent, leaving the sleep job running
|
2019-01-26 00:51:20 +00:00
|
|
|
require.NoError(agents.origAgent.Stop())
|
2019-01-18 04:32:45 +00:00
|
|
|
|
2019-01-23 04:25:27 +00:00
|
|
|
// Start a nomad agent with the to be tested nomad binary
|
2019-01-18 04:32:45 +00:00
|
|
|
t.Logf("launching test nomad agent")
|
2019-01-26 00:51:20 +00:00
|
|
|
require.NoError(agents.targetAgent.Start())
|
2019-01-23 04:25:27 +00:00
|
|
|
|
|
|
|
// Wait for the agent to be ready
|
2019-01-18 04:32:45 +00:00
|
|
|
e2eutil.WaitForNodesReady(t, client, 1)
|
|
|
|
|
2019-01-23 04:25:27 +00:00
|
|
|
// Make sure the same allocation still exists
|
2019-01-18 04:32:45 +00:00
|
|
|
alloc, _, err := client.Allocations().Info(id, nil)
|
|
|
|
require.NoError(err)
|
2019-01-23 04:25:27 +00:00
|
|
|
// Pull stats from the allocation, testing that new code can interface with
|
|
|
|
// the old stats driver apis
|
2019-01-18 04:32:45 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
stats, err := client.Allocations().Stats(alloc, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return stats.ResourceUsage.MemoryStats.RSS > 0, fmt.Errorf("RSS for task should be greater than 0")
|
|
|
|
}, func(err error) {
|
|
|
|
require.NoError(err)
|
|
|
|
})
|
|
|
|
|
2019-01-23 04:25:27 +00:00
|
|
|
// Deregister the job. This tests that the new code can properly tear down
|
|
|
|
// upgraded allocs
|
2019-01-18 04:32:45 +00:00
|
|
|
_, _, err = client.Jobs().Deregister(alloc.JobID, true, nil)
|
|
|
|
require.NoError(err)
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
j, _, _ := client.Jobs().Info(jobID, nil)
|
|
|
|
if j == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
return false, fmt.Errorf("job with id %q should be purged", jobID)
|
|
|
|
}, func(err error) {
|
|
|
|
require.NoError(err)
|
|
|
|
})
|
2019-01-23 04:25:27 +00:00
|
|
|
|
|
|
|
// Check that the task dir mounts have been removed
|
2019-01-18 04:32:45 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
defer client.System().GarbageCollect()
|
2019-01-23 19:09:49 +00:00
|
|
|
time.Sleep(time.Millisecond * 100)
|
2019-01-18 04:32:45 +00:00
|
|
|
data, err := ioutil.ReadFile("/proc/mounts")
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return !strings.Contains(string(data), id), fmt.Errorf("taskdir mounts should be cleaned up, but found mount for id %q:\n%s", id, string(data))
|
|
|
|
}, func(err error) {
|
|
|
|
require.NoError(err)
|
|
|
|
})
|
2019-01-23 04:25:27 +00:00
|
|
|
|
|
|
|
// Cleanup
|
2019-01-26 00:51:20 +00:00
|
|
|
agents.targetAgent.Stop()
|
|
|
|
agents.targetAgent.Destroy()
|
2019-01-18 04:32:45 +00:00
|
|
|
}
|