-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommit_create_test.go
More file actions
153 lines (132 loc) · 4.7 KB
/
commit_create_test.go
File metadata and controls
153 lines (132 loc) · 4.7 KB
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
package handlers_test
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/hdresearch/vers-cli/internal/handlers"
)
func TestHandleCommitCreate_WithName(t *testing.T) {
var commitBody map[string]interface{}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch {
case r.Method == http.MethodGet && r.URL.Path == "/api/v1/vm/vm-123/status":
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"vm_id":"vm-123","owner_id":"owner-1","created_at":"2026-01-01T00:00:00Z","state":"running"}`))
case r.Method == http.MethodPost && r.URL.Path == "/api/v1/vm/vm-123/commit":
body, _ := io.ReadAll(r.Body)
json.Unmarshal(body, &commitBody)
w.WriteHeader(http.StatusCreated)
w.Write([]byte(`{"commit_id":"commit-abc"}`))
default:
t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()
a := testApp(server.URL)
res, err := handlers.HandleCommitCreate(context.Background(), a, handlers.CommitCreateReq{
Target: "vm-123",
Name: "my-commit",
Description: "my description",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if res.CommitID != "commit-abc" {
t.Errorf("expected commit ID commit-abc, got %s", res.CommitID)
}
if res.VmID != "vm-123" {
t.Errorf("expected VM ID vm-123, got %s", res.VmID)
}
if res.Name != "my-commit" {
t.Errorf("expected name my-commit, got %s", res.Name)
}
if res.Description != "my description" {
t.Errorf("expected description 'my description', got %s", res.Description)
}
// Verify name and description were sent in the commit request body
if commitBody == nil {
t.Fatal("expected commit request to have a body")
}
if commitBody["name"] != "my-commit" {
t.Errorf("expected body name=my-commit, got %v", commitBody["name"])
}
if commitBody["description"] != "my description" {
t.Errorf("expected body description='my description', got %v", commitBody["description"])
}
}
func TestHandleCommitCreate_WithoutName(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch {
case r.Method == http.MethodGet && r.URL.Path == "/api/v1/vm/vm-123/status":
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"vm_id":"vm-123","owner_id":"owner-1","created_at":"2026-01-01T00:00:00Z","state":"running"}`))
case r.Method == http.MethodPost && r.URL.Path == "/api/v1/vm/vm-123/commit":
w.WriteHeader(http.StatusCreated)
w.Write([]byte(`{"commit_id":"commit-abc"}`))
default:
t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()
a := testApp(server.URL)
res, err := handlers.HandleCommitCreate(context.Background(), a, handlers.CommitCreateReq{
Target: "vm-123",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if res.CommitID != "commit-abc" {
t.Errorf("expected commit ID commit-abc, got %s", res.CommitID)
}
if res.Name != "" {
t.Errorf("expected empty name, got %s", res.Name)
}
}
func TestHandleCommitCreate_NameOnly(t *testing.T) {
var commitBody map[string]interface{}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch {
case r.Method == http.MethodGet && r.URL.Path == "/api/v1/vm/vm-123/status":
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"vm_id":"vm-123","owner_id":"owner-1","created_at":"2026-01-01T00:00:00Z","state":"running"}`))
case r.Method == http.MethodPost && r.URL.Path == "/api/v1/vm/vm-123/commit":
body, _ := io.ReadAll(r.Body)
json.Unmarshal(body, &commitBody)
w.WriteHeader(http.StatusCreated)
w.Write([]byte(`{"commit_id":"commit-abc"}`))
default:
t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()
a := testApp(server.URL)
res, err := handlers.HandleCommitCreate(context.Background(), a, handlers.CommitCreateReq{
Target: "vm-123",
Name: "just-a-name",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if res.Name != "just-a-name" {
t.Errorf("expected name just-a-name, got %s", res.Name)
}
if res.Description != "" {
t.Errorf("expected empty description, got %s", res.Description)
}
// Verify name was sent but description was not
if commitBody["name"] != "just-a-name" {
t.Errorf("expected name in body, got %v", commitBody["name"])
}
if _, hasDesc := commitBody["description"]; hasDesc {
t.Error("description should not be in body when not provided")
}
}