-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer_test.go
More file actions
135 lines (113 loc) · 3.71 KB
/
container_test.go
File metadata and controls
135 lines (113 loc) · 3.71 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
package container
import (
"encoding/hex"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// --- GenerateID ---
func TestContainer_GenerateID_Good(t *testing.T) {
id, err := GenerateID()
require.NoError(t, err)
assert.Len(t, id, 8, "container IDs must be 8 hex characters")
_, err = hex.DecodeString(id)
assert.NoError(t, err, "container ID must be valid hex")
}
func TestContainer_GenerateID_Uniqueness_Bad(t *testing.T) {
seen := make(map[string]bool)
for i := 0; i < 100; i++ {
id, err := GenerateID()
require.NoError(t, err)
assert.False(t, seen[id], "GenerateID produced duplicate id %q", id)
seen[id] = true
}
}
func TestContainer_GenerateID_Repeatability_Ugly(t *testing.T) {
// The contract is non-determinism — two consecutive calls must differ.
a, err := GenerateID()
require.NoError(t, err)
b, err := GenerateID()
require.NoError(t, err)
assert.NotEqual(t, a, b)
}
// --- Status constants ---
func TestContainer_StatusValues_Good(t *testing.T) {
assert.Equal(t, Status("running"), StatusRunning)
assert.Equal(t, Status("stopped"), StatusStopped)
assert.Equal(t, Status("error"), StatusError)
}
func TestContainer_StatusValues_Unknown_Bad(t *testing.T) {
var s Status
assert.Equal(t, Status(""), s, "zero value is empty string, not one of the known states")
}
func TestContainer_StatusValues_Switchable_Ugly(t *testing.T) {
// Exhaustive switch compiles and covers each declared status.
check := func(s Status) string {
switch s {
case StatusRunning:
return "running"
case StatusStopped:
return "stopped"
case StatusError:
return "error"
}
return "unknown"
}
assert.Equal(t, "running", check(StatusRunning))
assert.Equal(t, "stopped", check(StatusStopped))
assert.Equal(t, "error", check(StatusError))
assert.Equal(t, "unknown", check(Status("weird")))
}
// --- ImageFormat constants ---
func TestContainer_ImageFormatConstants_Good(t *testing.T) {
assert.Equal(t, ImageFormat("iso"), FormatISO)
assert.Equal(t, ImageFormat("qcow2"), FormatQCOW2)
assert.Equal(t, ImageFormat("vmdk"), FormatVMDK)
assert.Equal(t, ImageFormat("ami"), FormatAMI)
assert.Equal(t, ImageFormat("raw"), FormatRaw)
assert.Equal(t, ImageFormat("unknown"), FormatUnknown)
}
func TestContainer_ImageFormatConstants_Unique_Bad(t *testing.T) {
seen := map[ImageFormat]bool{}
for _, f := range []ImageFormat{FormatISO, FormatQCOW2, FormatVMDK, FormatAMI, FormatRaw, FormatUnknown} {
assert.False(t, seen[f], "duplicate ImageFormat constant %q", f)
seen[f] = true
}
}
func TestContainer_ImageFormatConstants_String_Ugly(t *testing.T) {
// ImageFormat is a string alias — it must roundtrip via string conversion.
for _, f := range []ImageFormat{FormatISO, FormatQCOW2, FormatVMDK, FormatAMI, FormatRaw, FormatUnknown} {
assert.Equal(t, string(f), string(ImageFormat(string(f))))
}
}
// --- RunOptions / Container struct smoke ---
func TestContainer_RunOptions_Zero_Good(t *testing.T) {
var o RunOptions
assert.Equal(t, "", o.Name)
assert.False(t, o.Detach)
assert.Equal(t, 0, o.Memory)
}
func TestContainer_RunOptions_WithValues_Bad(t *testing.T) {
o := RunOptions{Memory: -1, CPUs: -1, SSHPort: -1}
// The struct is a plain DTO, callers are responsible for validation.
assert.Equal(t, -1, o.Memory)
assert.Equal(t, -1, o.CPUs)
assert.Equal(t, -1, o.SSHPort)
}
func TestContainer_Struct_AllFields_Ugly(t *testing.T) {
c := Container{
ID: "abcdef01",
Name: "demo",
Image: "/tmp/img.iso",
Status: StatusRunning,
PID: 42,
Ports: map[int]int{8080: 80},
Memory: 1024,
CPUs: 2,
SSHPort: 2222,
SSHKey: "/tmp/id_ed25519",
}
assert.Equal(t, StatusRunning, c.Status)
assert.Equal(t, 80, c.Ports[8080])
assert.NotZero(t, c.PID)
}