Skip to content

Commit c9c85b3

Browse files
authored
Merge pull request #16 from Hamzenium/Added-Docker-Setup
Added Docker Setup
2 parents d8216f4 + 5c403a9 commit c9c85b3

9 files changed

Lines changed: 67 additions & 289 deletions

File tree

backend.log

Lines changed: 0 additions & 9 deletions
This file was deleted.

backend/image

432 Bytes
Binary file not shown.

backend/main_test.go

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,95 +6,83 @@ import (
66
"net/http"
77
"net/http/httptest"
88
"testing"
9+
10+
"resource-monitor/backend/handlers"
11+
"resource-monitor/backend/monitor"
12+
"resource-monitor/backend/types"
913
)
1014

11-
// Test toggleAlertHandler
1215
func TestToggleAlertHandler(t *testing.T) {
1316
payload := []byte(`{"enable_alerts": true}`)
1417
req := httptest.NewRequest(http.MethodPost, "/toggle-alerts", bytes.NewBuffer(payload))
1518
req.Header.Set("Content-Type", "application/json")
1619
w := httptest.NewRecorder()
1720

18-
toggleAlertHandler(w, req)
21+
handlers.ToggleAlertHandler(w, req)
1922

2023
if w.Code != http.StatusOK {
2124
t.Errorf("Expected status code 200, got %v", w.Code)
2225
}
2326

24-
if !alertEnabled {
25-
t.Errorf("Expected alertEnabled to be true, got %v", alertEnabled)
27+
monitor.Mu.Lock()
28+
defer monitor.Mu.Unlock()
29+
if !monitor.AlertEnabled {
30+
t.Errorf("Expected AlertEnabled to be true, got %v", monitor.AlertEnabled)
2631
}
2732
}
2833

29-
// Test toggleLimitHandler
3034
func TestToggleLimitHandler(t *testing.T) {
3135
payload := []byte(`{"cpu_threshold": 80, "memory_threshold": 70, "disk_threshold": 75}`)
3236
req := httptest.NewRequest(http.MethodPost, "/limit-changer", bytes.NewBuffer(payload))
3337
req.Header.Set("Content-Type", "application/json")
3438
w := httptest.NewRecorder()
3539

36-
toggleLimitHandler(w, req)
40+
handlers.ToggleLimitHandler(w, req)
3741

3842
if w.Code != http.StatusOK {
3943
t.Errorf("Expected status code 200, got %v", w.Code)
4044
}
4145

42-
expectedLimits := SetLimit{
43-
CPUThreshold: 80,
44-
MemoryThreshold: 70,
45-
DiskThreshold: 75,
46-
}
47-
48-
if defaultLimit != expectedLimits {
49-
t.Errorf("Expected limits %+v, got %+v", expectedLimits, defaultLimit)
46+
monitor.Mu.Lock()
47+
defer monitor.Mu.Unlock()
48+
expected := types.SetLimit{CPUThreshold: 80, MemoryThreshold: 70, DiskThreshold: 75}
49+
if monitor.DefaultLimit != expected {
50+
t.Errorf("Expected %+v, got %+v", expected, monitor.DefaultLimit)
5051
}
5152
}
5253

53-
// Test resourceUsageHandler
5454
func TestResourceUsageHandler(t *testing.T) {
5555
req := httptest.NewRequest(http.MethodGet, "/resource-usage", nil)
5656
w := httptest.NewRecorder()
5757

58-
resourceUsageHandler(w, req)
58+
handlers.ResourceUsageHandler(w, req)
5959

6060
if w.Code != http.StatusOK {
6161
t.Errorf("Expected status code 200, got %v", w.Code)
6262
}
6363

64-
var usage ResourceUsage
65-
err := json.NewDecoder(w.Body).Decode(&usage)
66-
if err != nil {
64+
var usage types.ResourceUsage
65+
if err := json.NewDecoder(w.Body).Decode(&usage); err != nil {
6766
t.Errorf("Error decoding response: %v", err)
6867
}
6968

7069
if usage.CPUUsage < 0 || usage.MemoryUsage < 0 || usage.DiskUsage < 0 {
71-
t.Errorf("Expected positive resource usage values, got %+v", usage)
70+
t.Errorf("Expected positive resource values, got %+v", usage)
7271
}
7372
}
7473

75-
// Test sendAlert functionality (mock example)
76-
func TestSendAlert(t *testing.T) {
77-
alertEnabled = true
78-
defaultLimit = SetLimit{
79-
CPUThreshold: 50,
80-
MemoryThreshold: 50,
81-
DiskThreshold: 50,
82-
}
83-
84-
tests := []struct {
85-
resource string
86-
usage float64
87-
alert bool
88-
}{
89-
{"CPU", 60, true},
90-
{"Memory", 40, false},
91-
{"Disk", 55, true},
92-
}
93-
94-
for _, test := range tests {
95-
mu.Lock()
96-
alertEnabled = false // Mocking disabled alert notifications
97-
mu.Unlock()
98-
sendAlert(test.resource, test.usage)
99-
}
100-
}
74+
// func TestSendAlertLogic(t *testing.T) {
75+
// monitor.Mu.Lock()
76+
// monitor.AlertEnabled = true
77+
// monitor.DefaultLimit = types.SetLimit{
78+
// CPUThreshold: 50,
79+
// MemoryThreshold: 50,
80+
// DiskThreshold: 50,
81+
// }
82+
// monitor.Mu.Unlock()
83+
84+
// // These will trigger alerts (but we don't test the actual notification pop-up)
85+
// monitor.SendAlert("CPU", 55)
86+
// monitor.SendAlert("Memory", 30) // Should not trigger alert
87+
// monitor.SendAlert("Disk", 70)
88+
// }

backend/output

-7.55 MB
Binary file not shown.

backend/server.go

Lines changed: 0 additions & 220 deletions
This file was deleted.

docker-compose.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "3.8"
2+
3+
services:
4+
app:
5+
build:
6+
context: .
7+
ports:
8+
- "8080:8080"
9+
container_name: go-app
10+
restart: unless-stopped

dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM alpine:latest
2+
3+
WORKDIR /app
4+
5+
# Copy backend and frontend binaries into the container
6+
COPY backend/image /app/backend
7+
COPY frontend/image /app/frontend
8+
9+
# Expose backend port
10+
EXPOSE 8080
11+
12+
# Start both processes
13+
CMD ["/bin/sh", "-c", "/app/backend & /app/frontend"]

0 commit comments

Comments
 (0)