File: lfstest-count-tests.go

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (285 lines) | stat: -rw-r--r-- 7,565 bytes parent folder | download
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main

import (
	"context"
	"fmt"
	"io"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strconv"
	"strings"
	"time"
)

var (
	// countFile is the path to a file (relative to the $LFSTEST_DIR) who's
	// contents is the number of actively-running integration tests.
	countFile = "test_count"
	// lockFile is the path to a file (relative to the $LFSTEST_DIR) who's
	// presence indicates that another invocation of the lfstest-count-tests
	// program is modifying the test_count.
	lockFile = "test_count.lock"

	// lockAcquireTimeout is the maximum amount of time that we will wait
	// for lockFile to become available (and thus the amount of time that we
	// will wait in order to acquire the lock).
	lockAcquireTimeout = 5 * time.Second

	// errCouldNotAcquire indicates that the program could not acquire the
	// lock needed to modify the test_count. It is a fatal error.
	errCouldNotAcquire = fmt.Errorf("could not acquire lock, dying")
	// errNegativeCount indicates that the count in test_count was negative,
	// which is unexpected and makes this script behave in an undefined
	// fashion
	errNegativeCount = fmt.Errorf("unexpected negative count")
)

// countFn is a type signature that all functions who wish to modify the
// test_count must inhabit.
//
// The first and only formal parameter is the current number of running tests
// found in test_count after acquiring the lock.
//
// The returned tuple indicates (1) the new number that should be written to
// test_count, and (2) if there was an error in computing that value. If err is
// non-nil, the program will exit and test_count will not be updated.
type countFn func(int) (int, error)

func main() {
	if len(os.Args) > 2 {
		fmt.Fprintf(os.Stderr,
			"usage: %s [increment|decrement]\n", os.Args[0])
		os.Exit(1)
	}

	ctx, cancel := context.WithTimeout(
		context.Background(), lockAcquireTimeout)
	defer cancel()

	if err := acquire(ctx); err != nil {
		fatal(err)
	}
	defer func() {
		if err := release(); err != nil {
			fmt.Fprintf(os.Stderr, "unable to release lock file: %s\n", err)
		}
	}()

	if len(os.Args) == 1 {
		// Calling with no arguments indicates that we simply want to
		// read the contents of test_count.
		callWithCount(func(n int) (int, error) {
			fmt.Fprintf(os.Stdout, "%d\n", n)
			return n, nil
		})
		return
	}

	var err error

	switch strings.ToLower(os.Args[1]) {
	case "increment":
		err = callWithCount(func(n int) (int, error) {
			if n > 0 {
				// If n>1, it is therefore true that a
				// lfstest-gitserver invocation is already
				// running.
				//
				// Hence, let's do nothing here other than
				// increase the count.
				return n + 1, nil
			}

			// The lfstest-gitserver invocation (see: below) does
			// not itself create a gitserver.log in the appropriate
			// directory. Thus, let's create it ourselves instead.
			log, err := os.Create(fmt.Sprintf(
				"%s/gitserver.log", os.Getenv("LFSTEST_DIR")))
			if err != nil {
				return n, err
			}

			// The executable name depends on the X environment
			// variable, which is set in script/cibuild.
			var cmd *exec.Cmd
			if runtime.GOOS == "windows" {
				cmd = exec.Command("lfstest-gitserver.exe")
			} else {
				cmd = exec.Command("lfstest-gitserver")
			}

			// The following are ported from the old
			// test/testhelpers.sh, and comprise the requisite
			// environment variables needed to run
			// lfstest-gitserver.
			cmd.Env = append(os.Environ(),
				fmt.Sprintf("LFSTEST_URL=%s", os.Getenv("LFSTEST_URL")),
				fmt.Sprintf("LFSTEST_SSL_URL=%s", os.Getenv("LFSTEST_SSL_URL")),
				fmt.Sprintf("LFSTEST_CLIENT_CERT_URL=%s", os.Getenv("LFSTEST_CLIENT_CERT_URL")),
				fmt.Sprintf("LFSTEST_DIR=%s", os.Getenv("LFSTEST_DIR")),
				fmt.Sprintf("LFSTEST_CERT=%s", os.Getenv("LFSTEST_CERT")),
				fmt.Sprintf("LFSTEST_CLIENT_CERT=%s", os.Getenv("LFSTEST_CLIENT_CERT")),
				fmt.Sprintf("LFSTEST_CLIENT_KEY=%s", os.Getenv("LFSTEST_CLIENT_KEY")),
			)
			cmd.Stdout = log
			cmd.Stderr = log

			// Start performs a fork/execve, hence we can abandon
			// this process once it has started.
			if err := cmd.Start(); err != nil {
				return n, err
			}
			return 1, nil
		})
	case "decrement":
		err = callWithCount(func(n int) (int, error) {
			if n > 1 {
				// If there is at least two tests running, we
				// need not shutdown a lfstest-gitserver
				// instance.
				return n - 1, nil
			}

			// Otherwise, we need to POST to /shutdown, which will
			// cause the lfstest-gitserver to abort itself.
			url, err := os.ReadFile(os.Getenv("LFS_URL_FILE"))
			if err == nil {
				_, err = http.Post(string(url)+"/shutdown",
					"application/text",
					strings.NewReader(time.Now().String()))
			}

			return 0, nil
		})
	}

	if err != nil {
		fatal(err)
	}
}

var (
	// acquireTick is the constant time that one tick (i.e., one attempt at
	// acquiring the lock) should last.
	acquireTick = 10 * time.Millisecond
)

// acquire acquires the lock file necessary to perform updates to test_count,
// and returns an error if that lock cannot be acquired.
func acquire(ctx context.Context) error {
	path, err := path(lockFile)
	if err != nil {
		return err
	}

	tick := time.NewTicker(acquireTick)
	defer tick.Stop()

	for {
		select {
		case <-tick.C:
			// Try every tick of the above ticker before giving up
			// and trying again.
			f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
			if err == nil || !os.IsExist(err) {
				if err == nil {
					f.Close()
				}
				return err
			}
		case <-ctx.Done():
			// If the context.Context above has reached its
			// deadline, we must give up.
			return errCouldNotAcquire
		}
	}
}

// release releases the lock file so that another process can take over, or
// returns an error.
func release() error {
	path, err := path(lockFile)
	if err != nil {
		return err
	}
	return os.Remove(path)
}

// callWithCount calls the given countFn with the current count in test_count,
// and updates it with what the function returns.
//
// If the function produced an error, that will be returned instead.
func callWithCount(fn countFn) error {
	path, err := path(countFile)
	if err != nil {
		return err
	}

	f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0666)
	if err != nil {
		return err
	}

	contents, err := io.ReadAll(f)
	if err != nil {
		return err
	}

	var n int = 0
	if len(contents) != 0 {
		n, err = strconv.Atoi(string(contents))
		if err != nil {
			return err
		}

		if n < 0 {
			return errNegativeCount
		}
	}

	after, err := fn(n)
	if err != nil {
		return err
	}

	// We want to write over the contents in the file, so "truncate" the
	// file to a length of 0, and then seek to the beginning of the file to
	// update the write head.
	if err := f.Truncate(0); err != nil {
		return err
	}
	if _, err := f.Seek(0, io.SeekStart); err != nil {
		return err
	}

	if _, err := fmt.Fprintf(f, "%d", after); err != nil {
		return err
	}
	return nil
}

// path returns an absolute path corresponding to any given path relative to the
// 't' directory of the current checkout of Git LFS.
func path(s string) (string, error) {
	p := filepath.Join(filepath.Dir(os.Getenv("LFSTEST_DIR")), s)
	if err := os.MkdirAll(filepath.Dir(p), 0777); err != nil {
		return "", err
	}
	return p, nil
}

// fatal reports the given error (if non-nil), and then dies. If the error was
// nil, nothing happens.
func fatal(err error) {
	if err == nil {
		return
	}
	if err := release(); err != nil {
		fmt.Fprintf(os.Stderr, "fatal: while dying, got: %s\n", err)
	}
	fmt.Fprintf(os.Stderr, "fatal: %s\n", err)
	os.Exit(1)
}