2020-03-16 12:53:04 +00:00
|
|
|
package csi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-11-13 14:31:21 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-03-16 12:53:04 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-11-13 14:31:21 +00:00
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-03-16 12:53:04 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
2020-11-13 14:31:21 +00:00
|
|
|
e2e "github.com/hashicorp/nomad/e2e/e2eutil"
|
2020-03-16 12:53:04 +00:00
|
|
|
"github.com/hashicorp/nomad/e2e/framework"
|
2022-03-03 22:00:00 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2020-11-13 14:31:21 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2020-03-16 12:53:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
framework.AddSuites(&framework.TestSuite{
|
|
|
|
Component: "CSI",
|
|
|
|
CanRunLocal: true,
|
|
|
|
Consul: false,
|
|
|
|
Cases: []framework.TestCase{
|
2021-04-07 17:42:26 +00:00
|
|
|
new(CSIControllerPluginEBSTest), // see ebs.go
|
|
|
|
new(CSINodeOnlyPluginEFSTest), // see efs.go
|
2020-03-16 12:53:04 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-13 14:31:21 +00:00
|
|
|
const ns = ""
|
|
|
|
|
2020-11-25 14:05:22 +00:00
|
|
|
var pluginWait = &e2e.WaitConfig{Interval: 5 * time.Second, Retries: 36} // 3min
|
2020-11-13 14:31:21 +00:00
|
|
|
var reapWait = &e2e.WaitConfig{Interval: 5 * time.Second, Retries: 36} // 3min
|
|
|
|
|
2021-04-07 17:42:26 +00:00
|
|
|
// assertNoErrorElseDump calls a non-halting assert on the error and dumps the
|
|
|
|
// plugin logs if it fails.
|
|
|
|
func assertNoErrorElseDump(f *framework.F, err error, msg string, pluginJobIDs []string) {
|
2020-10-14 14:29:33 +00:00
|
|
|
if err != nil {
|
2021-04-07 17:42:26 +00:00
|
|
|
dumpLogs(pluginJobIDs)
|
|
|
|
f.Assert().NoError(err, fmt.Sprintf("%v: %v", msg, err))
|
2020-10-14 14:29:33 +00:00
|
|
|
}
|
2020-03-16 12:53:04 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 17:42:26 +00:00
|
|
|
// requireNoErrorElseDump calls a halting assert on the error and dumps the
|
|
|
|
// plugin logs if it fails.
|
|
|
|
func requireNoErrorElseDump(f *framework.F, err error, msg string, pluginJobIDs []string) {
|
|
|
|
if err != nil {
|
|
|
|
dumpLogs(pluginJobIDs)
|
|
|
|
f.NoError(err, fmt.Sprintf("%v: %v", msg, err))
|
2020-04-03 19:52:58 +00:00
|
|
|
}
|
2020-11-13 14:31:21 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 17:42:26 +00:00
|
|
|
func dumpLogs(pluginIDs []string) error {
|
2020-11-13 14:31:21 +00:00
|
|
|
|
2021-04-07 17:42:26 +00:00
|
|
|
for _, id := range pluginIDs {
|
2020-11-13 14:31:21 +00:00
|
|
|
allocs, err := e2e.AllocsForJob(id, ns)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not find allocs for plugin: %v", err)
|
|
|
|
}
|
|
|
|
for _, alloc := range allocs {
|
|
|
|
allocID := alloc["ID"]
|
|
|
|
out, err := e2e.AllocLogs(allocID, e2e.LogsStdErr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get logs for alloc: %v\n%s", err, out)
|
|
|
|
}
|
|
|
|
_, isCI := os.LookupEnv("CI")
|
|
|
|
if isCI {
|
|
|
|
fmt.Println("--------------------------------------")
|
|
|
|
fmt.Println("allocation logs:", allocID)
|
|
|
|
fmt.Println(out)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
f, err := os.Create(allocID + ".log")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create log file: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = f.WriteString(out)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not write to log file: %v", err)
|
|
|
|
}
|
|
|
|
fmt.Printf("nomad alloc logs written to %s.log\n", allocID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2020-03-16 12:53:04 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 17:42:26 +00:00
|
|
|
// waitForVolumeClaimRelease makes sure we don't try to re-claim a volume
|
|
|
|
// that's in the process of being unpublished. we can't just wait for allocs
|
|
|
|
// to stop, but need to wait for their claims to be released
|
|
|
|
func waitForVolumeClaimRelease(volID string, wc *e2e.WaitConfig) error {
|
|
|
|
var out string
|
|
|
|
var err error
|
|
|
|
testutil.WaitForResultRetries(wc.Retries, func() (bool, error) {
|
|
|
|
time.Sleep(wc.Interval)
|
|
|
|
out, err = e2e.Command("nomad", "volume", "status", volID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
section, err := e2e.GetSection(out, "Allocations")
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return strings.Contains(section, "No allocations placed"), nil
|
|
|
|
}, func(e error) {
|
|
|
|
if e == nil {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
err = fmt.Errorf("alloc claim was not released: %v\n%s", e, out)
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-16 12:53:04 +00:00
|
|
|
// TODO(tgross): replace this w/ AllocFS().Stat() after
|
|
|
|
// https://github.com/hashicorp/nomad/issues/7365 is fixed
|
2020-11-13 14:31:21 +00:00
|
|
|
func readFile(client *api.Client, allocID string, path string) (bytes.Buffer, error) {
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
alloc, _, err := client.Allocations().Info(allocID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return stdout, err
|
|
|
|
}
|
2020-03-16 12:53:04 +00:00
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancelFn()
|
|
|
|
|
2020-11-13 14:31:21 +00:00
|
|
|
_, err = client.Allocations().Exec(ctx,
|
2020-03-16 12:53:04 +00:00
|
|
|
alloc, "task", false,
|
|
|
|
[]string{"cat", path},
|
|
|
|
os.Stdin, &stdout, &stderr,
|
|
|
|
make(chan api.TerminalSize), nil)
|
|
|
|
return stdout, err
|
|
|
|
}
|
2020-04-02 16:16:43 +00:00
|
|
|
|
2020-11-13 14:31:21 +00:00
|
|
|
func waitForPluginStatusMinNodeCount(pluginID string, minCount int, wc *e2e.WaitConfig) error {
|
2020-04-02 16:16:43 +00:00
|
|
|
|
2020-11-13 14:31:21 +00:00
|
|
|
return waitForPluginStatusCompare(pluginID, func(out string) (bool, error) {
|
|
|
|
expected, err := e2e.GetField(out, "Nodes Expected")
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
expectedCount, err := strconv.Atoi(strings.TrimSpace(expected))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if expectedCount < minCount {
|
|
|
|
return false, fmt.Errorf(
|
|
|
|
"expected Nodes Expected >= %d, got %q", minCount, expected)
|
|
|
|
}
|
|
|
|
healthy, err := e2e.GetField(out, "Nodes Healthy")
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if healthy != expected {
|
|
|
|
return false, fmt.Errorf(
|
|
|
|
"expected Nodes Healthy >= %d, got %q", minCount, healthy)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, wc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForPluginStatusControllerCount(pluginID string, count int, wc *e2e.WaitConfig) error {
|
|
|
|
|
|
|
|
return waitForPluginStatusCompare(pluginID, func(out string) (bool, error) {
|
|
|
|
|
|
|
|
expected, err := e2e.GetField(out, "Controllers Expected")
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
expectedCount, err := strconv.Atoi(strings.TrimSpace(expected))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if expectedCount != count {
|
|
|
|
return false, fmt.Errorf(
|
|
|
|
"expected Controllers Expected = %d, got %d", count, expectedCount)
|
|
|
|
}
|
|
|
|
healthy, err := e2e.GetField(out, "Controllers Healthy")
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
healthyCount, err := strconv.Atoi(strings.TrimSpace(healthy))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if healthyCount != count {
|
|
|
|
return false, fmt.Errorf(
|
|
|
|
"expected Controllers Healthy = %d, got %d", count, healthyCount)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
|
|
|
|
}, wc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForPluginStatusCompare(pluginID string, compare func(got string) (bool, error), wc *e2e.WaitConfig) error {
|
|
|
|
var err error
|
|
|
|
testutil.WaitForResultRetries(wc.Retries, func() (bool, error) {
|
|
|
|
time.Sleep(wc.Interval)
|
|
|
|
out, err := e2e.Command("nomad", "plugin", "status", pluginID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return compare(out)
|
|
|
|
}, func(e error) {
|
|
|
|
err = fmt.Errorf("plugin status check failed: %v", e)
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-07 17:42:26 +00:00
|
|
|
// volumeRegister creates or registers a volume spec from a file but with a
|
|
|
|
// unique ID. The caller is responsible for recording that ID for later
|
|
|
|
// cleanup.
|
|
|
|
func volumeRegister(volID, volFilePath, createOrRegister string) error {
|
2020-11-13 14:31:21 +00:00
|
|
|
|
2022-03-04 13:55:36 +00:00
|
|
|
// a CSI RPC to create a volume can take a long time because we
|
|
|
|
// have to wait on the AWS API to provision a disk, but a register
|
|
|
|
// should not because it only has to check the API for compatibility
|
|
|
|
timeout := time.Second * 30
|
|
|
|
if createOrRegister == "create" {
|
|
|
|
timeout = time.Minute * 2
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
cmd := exec.CommandContext(ctx, "nomad", "volume", createOrRegister, "-")
|
2020-11-13 14:31:21 +00:00
|
|
|
stdin, err := cmd.StdinPipe()
|
2020-04-02 16:16:43 +00:00
|
|
|
if err != nil {
|
2020-11-13 14:31:21 +00:00
|
|
|
return fmt.Errorf("could not open stdin?: %w", err)
|
2020-04-02 16:16:43 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 14:31:21 +00:00
|
|
|
content, err := ioutil.ReadFile(volFilePath)
|
2020-04-02 16:16:43 +00:00
|
|
|
if err != nil {
|
2020-11-13 14:31:21 +00:00
|
|
|
return fmt.Errorf("could not open vol file: %w", err)
|
2020-04-02 16:16:43 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 14:31:21 +00:00
|
|
|
// hack off the first line to replace with our unique ID
|
2022-03-03 22:00:00 +00:00
|
|
|
var idRegex = regexp.MustCompile(`(?m)^id[\s]+= ".*"`)
|
2022-03-01 15:15:46 +00:00
|
|
|
volspec := idRegex.ReplaceAllString(string(content),
|
|
|
|
fmt.Sprintf("id = %q", volID))
|
|
|
|
|
2022-03-03 22:00:00 +00:00
|
|
|
// the EBS plugin uses the name as an idempotency token across the
|
|
|
|
// whole AWS account, so it has to be globally unique
|
|
|
|
var nameRegex = regexp.MustCompile(`(?m)^name[\s]+= ".*"`)
|
2022-03-01 15:15:46 +00:00
|
|
|
volspec = nameRegex.ReplaceAllString(volspec,
|
2022-03-03 22:00:00 +00:00
|
|
|
fmt.Sprintf("name = %q", uuid.Generate()))
|
2020-11-13 14:31:21 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer stdin.Close()
|
|
|
|
io.WriteString(stdin, volspec)
|
|
|
|
}()
|
|
|
|
|
|
|
|
out, err := cmd.CombinedOutput()
|
2020-04-02 16:16:43 +00:00
|
|
|
if err != nil {
|
2020-11-13 14:31:21 +00:00
|
|
|
return fmt.Errorf("could not register vol: %w\n%v", err, string(out))
|
2020-04-02 16:16:43 +00:00
|
|
|
}
|
2020-11-13 14:31:21 +00:00
|
|
|
return nil
|
2020-04-02 16:16:43 +00:00
|
|
|
}
|