Skip to content

Commit 4785d12

Browse files
authored
Test automatic stop mechanism (#264)
* Test automatic stop mechanism Also: * Add sleep on test polling loops * Fix unsafe access to event time on Azure * Wait for data to be synchronized before exiting * Fix testing inconsistencies * Fix RCLONE_REMOTE expansion
1 parent 3d74f90 commit 4785d12

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

task/az/resources/resource_virtual_machine_scale_set.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"net"
99
"regexp"
10+
"time"
1011

1112
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-30/compute"
1213

@@ -249,8 +250,12 @@ func (v *VirtualMachineScaleSet) Read(ctx context.Context) error {
249250
}
250251
if scaleSetView.Statuses != nil {
251252
for _, status := range *scaleSetView.Statuses {
253+
statusTime := time.Unix(0, 0)
254+
if status.Time != nil {
255+
statusTime = status.Time.Time
256+
}
252257
v.Attributes.Events = append(v.Attributes.Events, common.Event{
253-
Time: status.Time.Time,
258+
Time: statusTime,
254259
Code: to.String(status.Code),
255260
Description: []string{
256261
string(status.Level),

task/common/machine/script.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ sudo tee /etc/systemd/system/tpi-task.service > /dev/null <<END
6161
[Service]
6262
Type=simple
6363
ExecStart=/usr/bin/tpi-task
64-
ExecStop=/bin/bash -c 'systemctl is-system-running | grep stopping || echo "{\\\\"result\\\\": \\\\"\$SERVICE_RESULT\\\\", \\\\"code\\\\": \\\\"\$EXIT_STATUS\\\\", \\\\"status\\\\": \\\\"\$EXIT_CODE\\\\"}" > "$TPI_LOG_DIRECTORY/status-$TPI_MACHINE_IDENTITY" && rclone copy "$TPI_LOG_DIRECTORY" "$RCLONE_REMOTE/reports"'
64+
ExecStop=/bin/bash -c 'systemctl is-system-running | grep stopping || echo "{\\\\"result\\\\": \\\\"\$SERVICE_RESULT\\\\", \\\\"code\\\\": \\\\"\$EXIT_STATUS\\\\", \\\\"status\\\\": \\\\"\$EXIT_CODE\\\\"}" > "$TPI_LOG_DIRECTORY/status-$TPI_MACHINE_IDENTITY" && rclone copy "$TPI_LOG_DIRECTORY" "\$RCLONE_REMOTE/reports"'
6565
ExecStopPost=/usr/bin/tpi-task-shutdown
6666
Environment=HOME=/root
6767
EnvironmentFile=/tmp/tpi-environment

task/task_test.go

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package task
33
import (
44
"context"
55
"os"
6+
"strings"
67
"testing"
78
"time"
89

@@ -48,8 +49,8 @@ func TestTask(t *testing.T) {
4849
}
4950

5051
t.Run(string(provider), func(t *testing.T) {
51-
oldData := gofakeit.Phrase()
52-
newData := gofakeit.Phrase()
52+
oldData := gofakeit.UUID()
53+
newData := gofakeit.UUID()
5354

5455
dataDirectory := t.TempDir()
5556
dataFile := filepath.Join(dataDirectory, "data")
@@ -75,20 +76,21 @@ func TestTask(t *testing.T) {
7576
Environment: common.Environment{
7677
Image: "ubuntu",
7778
Script: `#!/bin/bash
79+
echo "$ENVIRONMENT_VARIABLE_DATA" | tee --append data
80+
sleep 60
7881
cat data
79-
echo "$ENVIRONMENT_VARIABLE_DATA" | tee data
8082
`,
8183
Variables: map[string]*string{
8284
"ENVIRONMENT_VARIABLE_DATA": &newData,
8385
},
84-
Directory: dataDirectory,
85-
Timeout: 10 * time.Minute,
86+
Directory: dataDirectory,
87+
DirectoryOut: dataDirectory,
88+
Timeout: 10 * time.Minute,
8689
},
8790
Firewall: common.Firewall{
8891
Ingress: common.FirewallRule{
8992
Ports: &[]uint16{22},
9093
},
91-
// Egress: everything open.
9294
},
9395
Spot: common.SpotEnabled,
9496
Parallelism: 1,
@@ -120,38 +122,30 @@ func TestTask(t *testing.T) {
120122
require.Nil(t, err)
121123

122124
for _, log := range logs {
123-
if assert.Contains(t, log, oldData) &&
124-
assert.Contains(t, log, newData) {
125+
if strings.Contains(log, oldData) &&
126+
strings.Contains(log, newData) {
125127
break loop
126128
}
127129
}
130+
131+
time.Sleep(10 * time.Second)
128132
}
129133

130134
if provider == common.ProviderK8S {
131135
require.Equal(t, newTask.Start(ctx), common.NotImplementedError)
132136
require.Equal(t, newTask.Stop(ctx), common.NotImplementedError)
133-
} else {
134-
require.Nil(t, newTask.Stop(ctx))
135-
require.Nil(t, newTask.Stop(ctx))
136-
137-
for assert.Nil(t, newTask.Read(ctx)) {
138-
status, err := newTask.Status(ctx)
139-
require.Nil(t, err)
140-
if status[common.StatusCodeActive] > 0 {
141-
break
142-
}
143-
}
137+
}
144138

145-
require.Nil(t, newTask.Start(ctx))
146-
require.Nil(t, newTask.Start(ctx))
139+
for assert.Nil(t, newTask.Read(ctx)) {
140+
status, err := newTask.Status(ctx)
141+
require.Nil(t, err)
147142

148-
for assert.Nil(t, newTask.Read(ctx)) {
149-
status, err := newTask.Status(ctx)
150-
require.Nil(t, err)
151-
if status[common.StatusCodeActive] == 0 {
152-
break
153-
}
143+
if status[common.StatusCodeActive] == 0 &&
144+
status[common.StatusCodeSucceeded] > 0 {
145+
break
154146
}
147+
148+
time.Sleep(10 * time.Second)
155149
}
156150

157151
require.Nil(t, newTask.Delete(ctx))

0 commit comments

Comments
 (0)