-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompose_test.go
More file actions
136 lines (119 loc) · 4.28 KB
/
Copy pathcompose_test.go
File metadata and controls
136 lines (119 loc) · 4.28 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
package compose
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLoadComposeSpecInterpolatesFilesAndEnv(t *testing.T) {
dir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(dir, "otelcol.yaml"), []byte("receivers: {}\n"), 0644))
t.Setenv("SIGNOZ_ACCESS_TOKEN", "secret-token")
composePath := filepath.Join(dir, "hypeman.compose.yaml")
require.NoError(t, os.WriteFile(composePath, []byte(`
version: 1
name: hypeship-otel
services:
otelcol:
image: otel/opentelemetry-collector-contrib:0.108.0
cmd: ["--config=env:OTELCOL_CONFIG"]
env:
OTELCOL_CONFIG: ${file:otelcol.yaml}
SIGNOZ_ACCESS_TOKEN: ${env:SIGNOZ_ACCESS_TOKEN}
`), 0644))
spec, err := loadComposeSpec(composePath)
require.NoError(t, err)
service := spec.Services["otelcol"]
assert.Equal(t, "receivers: {}\n", service.Env["OTELCOL_CONFIG"])
assert.Equal(t, "secret-token", service.Env["SIGNOZ_ACCESS_TOKEN"])
}
func TestBuildComposeInstanceInputIncludesFuturePolicyFields(t *testing.T) {
service := composeServiceSpec{
Image: "otel/opentelemetry-collector-contrib:0.108.0",
Cmd: []string{"--config=env:OTELCOL_CONFIG"},
Env: map[string]string{
"OTELCOL_CONFIG": "receivers: {}\n",
},
Resources: composeResourcesSpec{
Vcpus: 8,
Memory: "4GB",
BandwidthUpload: "300Mbps",
BandwidthDownload: "300Mbps",
},
Restart: &composeRestartSpec{
Policy: "on-failure",
Backoff: "5s",
MaxAttempts: 10,
StableAfter: "10m",
},
Health: &composeCheckSpec{
HTTP: &composeHTTPCheckSpec{Port: 13133, Path: "/", ExpectedStatus: 200},
Interval: "10s",
Timeout: "2s",
FailureThreshold: 3,
},
}
input := buildComposeInstanceInput("hypeship-otel-otelcol", service)
assert.Equal(t, "hypeship-otel-otelcol", input["name"])
assert.Equal(t, service.Image, input["image"])
assert.Equal(t, []string{"--config=env:OTELCOL_CONFIG"}, input["cmd"])
assert.Equal(t, "4GB", input["size"])
assert.Equal(t, 8, input["vcpus"])
assert.Equal(t, map[string]any{
"backoff": "5s",
"max_attempts": 10,
"policy": "on_failure",
"stable_after": "10m",
}, input["restart_policy"])
assert.Equal(t, service.Health, input["health_check"])
assert.Equal(t, map[string]any{
"bandwidth_download": "300Mbps",
"bandwidth_upload": "300Mbps",
}, input["network"])
}
func TestDesiredResourcesUseDeterministicNamesAndTags(t *testing.T) {
runner := Runner{
spec: composeSpec{
Version: 1,
Name: "hypeship-otel",
Services: map[string]composeServiceSpec{
"otelcol": {
Image: "otel/opentelemetry-collector-contrib:0.108.0",
Ingress: []composeIngressRuleSpec{
{Hostname: "otel.example.com", HostPort: 443, TargetPort: 4318, TLS: true},
},
},
},
},
}
instances, ingresses, images, err := runner.desiredResources()
require.NoError(t, err)
require.Equal(t, []string{"otel/opentelemetry-collector-contrib:0.108.0"}, images)
require.Len(t, instances, 1)
assert.Equal(t, "hypeship-otel-otelcol", instances[0].Name)
assert.Equal(t, composeResourceInstance, instances[0].Input["tags"].(map[string]string)[composeTagResource])
assert.NotEmpty(t, instances[0].Input["tags"].(map[string]string)[composeTagHash])
require.Len(t, ingresses, 1)
assert.Equal(t, "hypeship-otel-otelcol-0", ingresses[0].Name)
assert.Equal(t, composeResourceIngress, ingresses[0].Input.Tags[composeTagResource])
assert.Equal(t, "hypeship-otel-otelcol", ingresses[0].Input.Rules[0].Target.Instance)
assert.Equal(t, int64(4318), ingresses[0].Input.Rules[0].Target.Port)
}
func TestValidateComposeSpecRejectsInvalidNames(t *testing.T) {
err := validateComposeSpec(&composeSpec{
Version: 1,
Name: "BadName",
Services: map[string]composeServiceSpec{
"api": {Image: "alpine:latest"},
},
})
require.EqualError(t, err, "compose name must contain only lowercase letters, digits, and dashes")
}
func TestConflictBlockers(t *testing.T) {
blockers := conflictBlockers([]Action{
{Action: "create", Type: "image", Name: "alpine:latest"},
{Action: "conflict", Type: "instance", Name: "app-api", Reason: "name exists without compose ownership"},
})
require.Equal(t, []string{" instance app-api: name exists without compose ownership"}, blockers)
}