1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
// Copyright (c) 2019-2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package instance
import (
"bufio"
"encoding/json"
"fmt"
"net"
"strconv"
"testing"
"time"
"github.com/sylabs/singularity/v4/e2e/internal/e2e"
)
const instanceStartPort = 11372
type instance struct {
Image string `json:"img"`
Instance string `json:"instance"`
Pid int `json:"pid"`
}
type instanceList struct {
Instances []instance `json:"instances"`
}
//nolint:unparam
func (c *ctx) stopInstance(t *testing.T, instance string, stopArgs ...string) (stdout string, stderr string, success bool) {
args := stopArgs
if instance != "" {
args = append(args, instance)
}
c.env.RunSingularity(
t,
e2e.WithProfile(c.profile),
e2e.WithCommand("instance stop"),
e2e.WithArgs(args...),
e2e.PostRun(func(t *testing.T) {
success = !t.Failed()
}),
e2e.ExpectExit(0, e2e.GetStreams(&stdout, &stderr)),
)
c.expectInstance(t, instance, 0)
return
}
//nolint:unparam
func (c *ctx) execInstance(t *testing.T, instance string, execArgs ...string) (stdout string, stderr string, success bool) {
args := []string{"instance://" + instance}
args = append(args, execArgs...)
c.env.RunSingularity(
t,
e2e.WithProfile(c.profile),
e2e.WithCommand("exec"),
e2e.WithArgs(args...),
e2e.PostRun(func(t *testing.T) {
success = !t.Failed()
}),
e2e.ExpectExit(0, e2e.GetStreams(&stdout, &stderr)),
)
return
}
// Check if there is the number of expected instances with the provided name.
func (c *ctx) expectInstance(t *testing.T, name string, nb int) {
listInstancesFn := func(t *testing.T, r *e2e.SingularityCmdResult) {
var instances instanceList
if err := json.Unmarshal([]byte(r.Stdout), &instances); err != nil {
t.Errorf("Error while decoding JSON from 'instance list': %v", err)
}
if nb != len(instances.Instances) {
t.Errorf("%d instance %q found, expected %d", len(instances.Instances), name, nb)
}
}
c.env.RunSingularity(
t,
e2e.WithProfile(c.profile),
e2e.WithCommand("instance list"),
e2e.WithArgs([]string{"--json", name}...),
e2e.ExpectExit(0, listInstancesFn),
)
}
// Sends a deterministic message to an echo server and expects the same, or a
// reversed, message in response.
func echo(t *testing.T, port int, reverse bool) {
const (
message = "b40cbeaaea293f7e8bd40fb61f389cfca9823467\n"
reversedMessage = "7643289acfc983f16bf04db8e7f392aeaaebc04b\n"
)
expectResponse := message
if reverse {
expectResponse = reversedMessage
}
// give it some time for responding, attempt 10 times by
// waiting 100 millisecond between each try
for retries := 0; ; retries++ {
sock, sockErr := net.Dial("tcp", "127.0.0.1:"+strconv.Itoa(port))
if sockErr != nil && retries < 10 {
time.Sleep(100 * time.Millisecond)
continue
} else if sockErr != nil {
t.Errorf("Failed to dial echo server: %v", sockErr)
return
}
fmt.Fprint(sock, message)
response, responseErr := bufio.NewReader(sock).ReadString('\n')
if responseErr != nil || response != expectResponse {
t.Errorf("Bad response: err = %v, response %q != %q", responseErr, response, expectResponse)
}
break
}
}
|