forked from dustin-decker/overseer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
proc_parent_test.go
151 lines (126 loc) · 4.2 KB
/
proc_parent_test.go
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
package overseer
import (
"os"
"sync"
"testing"
"time"
)
// TestOverwriteBinary tests the overwriteBinary function and the withFileLock functionality.
func TestOverwriteBinaryPositivePath(t *testing.T) {
// Create temporary files to simulate binPath and tmpBinPath.
binFile, err := os.CreateTemp("", "overseer_bin")
if err != nil {
t.Fatalf("Failed to create temp bin file: %v", err)
}
defer os.Remove(binFile.Name())
tmpBinFile, err := os.CreateTemp("", "overseer_tmp_bin")
if err != nil {
t.Fatalf("Failed to create temp tmpBin file: %v", err)
}
defer os.Remove(tmpBinFile.Name())
// Write initial content to binPath.
initialContent := []byte("initial binary content")
if _, err := binFile.Write(initialContent); err != nil {
t.Fatalf("Failed to write to bin file: %v", err)
}
binFile.Close()
// Write new content to tmpBinPath.
newContent := []byte("new binary content")
if _, err := tmpBinFile.Write(newContent); err != nil {
t.Fatalf("Failed to write to tmpBin file: %v", err)
}
tmpBinFile.Close()
// Create a parent struct with binPath set to the fake bin file.
mp := &parent{
binPath: binFile.Name(),
}
// Test that overwriteBinary correctly overwrites binPath.
err = mp.overwriteBinary(tmpBinFile.Name())
if err != nil {
t.Fatalf("overwriteBinary failed: %v", err)
}
// Read the content of binPath and verify it matches newContent.
updatedContent, err := os.ReadFile(mp.binPath)
if err != nil {
t.Fatalf("Failed to read updated bin file: %v", err)
}
if string(updatedContent) != string(newContent) {
t.Errorf("binPath content does not match new content. Got: %s, Want: %s", string(updatedContent), string(newContent))
}
}
func TestOverwriteBinarySafety(t *testing.T) {
// Create temporary files to simulate binPath and tmpBinPath.
binFile, err := os.CreateTemp("", "overseer_bin")
if err != nil {
t.Fatalf("Failed to create temp bin file: %v", err)
}
defer os.Remove(binFile.Name())
// Create a parent struct with binPath set to the fake bin file.
mp := &parent{
binPath: binFile.Name(),
}
// Create two parent instances to simulate two processes.
mp1 := &parent{
binPath: binFile.Name(),
}
mp2 := &parent{
binPath: binFile.Name(),
}
// Create new temp files to act as tmpBinPath for the concurrent overwrites.
tmpBinFile1, err := os.CreateTemp("", "overseer_tmp_bin1")
if err != nil {
t.Fatalf("Failed to create temp tmpBin file 1: %v", err)
}
defer os.Remove(tmpBinFile1.Name())
tmpBinFile2, err := os.CreateTemp("", "overseer_tmp_bin2")
if err != nil {
t.Fatalf("Failed to create temp tmpBin file 2: %v", err)
}
defer os.Remove(tmpBinFile2.Name())
// Write different content to tmpBinPath files.
newContent1 := []byte("new binary content 1")
if _, err := tmpBinFile1.Write(newContent1); err != nil {
t.Fatalf("Failed to write to tmpBin file 1: %v", err)
}
tmpBinFile1.Close()
newContent2 := []byte("new binary content 2")
if _, err := tmpBinFile2.Write(newContent2); err != nil {
t.Fatalf("Failed to write to tmpBin file 2: %v", err)
}
tmpBinFile2.Close()
// Use a WaitGroup to synchronize the goroutines.
var wg sync.WaitGroup
wg.Add(2)
// Variables to capture errors from goroutines.
var err1, err2 error
// Start the first overwriteBinary call in a goroutine.
go func() {
defer wg.Done()
err1 = mp1.overwriteBinary(tmpBinFile1.Name())
}()
// Small delay to increase the chance of overlap.
time.Sleep(10 * time.Millisecond)
// Start the second overwriteBinary call in another goroutine.
go func() {
defer wg.Done()
err2 = mp2.overwriteBinary(tmpBinFile2.Name())
}()
// Wait for both goroutines to finish.
wg.Wait()
// Both calls should succeed without errors.
if err1 != nil {
t.Errorf("First overwriteBinary call failed: %v", err1)
}
if err2 != nil {
t.Errorf("Second overwriteBinary call failed: %v", err2)
}
// Verify that the binPath content is one of the new contents.
finalContent, err := os.ReadFile(mp.binPath)
if err != nil {
t.Fatalf("Failed to read final bin file: %v", err)
}
finalContentStr := string(finalContent)
if finalContentStr != string(newContent1) && finalContentStr != string(newContent2) {
t.Errorf("Final binPath content does not match any of the new contents. Got: %s", finalContentStr)
}
}