v2.31.0-dev6 8056b57

target_metadata_test.go RawHistoryFolder

1package proxy
2
3import (
4	"fmt"
5	"testing"
6
7	"github.com/stretchr/testify/assert"
8
9	outmocks "github.com/bnema/gordon/internal/boundaries/out/mocks"
10	"github.com/bnema/gordon/internal/domain"
11)
12
13func TestResolveTargetMetadata_PortAndProtocolFromLabels(t *testing.T) {
14	runtime := outmocks.NewMockContainerRuntime(t)
15	svc := &Service{runtime: runtime}
16	ctx := testContext()
17
18	runtime.EXPECT().GetImageLabels(ctx, "grpc-app:latest").Return(map[string]string{
19		domain.LabelProxyPort:     "50051",
20		domain.LabelProxyProtocol: "h2c",
21	}, nil)
22
23	meta, err := svc.resolveTargetMetadata(ctx, "grpc-app:latest")
24
25	assert.NoError(t, err)
26	assert.Equal(t, 50051, meta.Port)
27	assert.Equal(t, "h2c", meta.Protocol)
28}
29
30func TestResolveTargetMetadata_DefaultProtocolWhenAbsent(t *testing.T) {
31	runtime := outmocks.NewMockContainerRuntime(t)
32	svc := &Service{runtime: runtime}
33	ctx := testContext()
34
35	runtime.EXPECT().GetImageLabels(ctx, "web:latest").Return(map[string]string{
36		domain.LabelProxyPort: "8080",
37	}, nil)
38
39	meta, err := svc.resolveTargetMetadata(ctx, "web:latest")
40
41	assert.NoError(t, err)
42	assert.Equal(t, 8080, meta.Port)
43	assert.Equal(t, "", meta.Protocol)
44}
45
46func TestResolveTargetMetadata_DeprecatedPortLabel(t *testing.T) {
47	runtime := outmocks.NewMockContainerRuntime(t)
48	svc := &Service{runtime: runtime}
49	ctx := testContext()
50
51	runtime.EXPECT().GetImageLabels(ctx, "old:latest").Return(map[string]string{
52		domain.LabelPort: "3000",
53	}, nil)
54
55	meta, err := svc.resolveTargetMetadata(ctx, "old:latest")
56
57	assert.NoError(t, err)
58	assert.Equal(t, 3000, meta.Port)
59	assert.Equal(t, "", meta.Protocol)
60}
61
62func TestResolveTargetMetadata_ProxyPortWinsOverDeprecated(t *testing.T) {
63	runtime := outmocks.NewMockContainerRuntime(t)
64	svc := &Service{runtime: runtime}
65	ctx := testContext()
66
67	runtime.EXPECT().GetImageLabels(ctx, "dual:latest").Return(map[string]string{
68		domain.LabelProxyPort: "9000",
69		domain.LabelPort:      "3000",
70	}, nil)
71
72	meta, err := svc.resolveTargetMetadata(ctx, "dual:latest")
73
74	assert.NoError(t, err)
75	assert.Equal(t, 9000, meta.Port)
76}
77
78func TestResolveTargetMetadata_FallsBackToExposedPort(t *testing.T) {
79	runtime := outmocks.NewMockContainerRuntime(t)
80	svc := &Service{runtime: runtime}
81	ctx := testContext()
82
83	runtime.EXPECT().GetImageLabels(ctx, "plain:latest").Return(map[string]string{}, nil)
84	runtime.EXPECT().GetImageExposedPorts(ctx, "plain:latest").Return([]int{8080}, nil)
85
86	meta, err := svc.resolveTargetMetadata(ctx, "plain:latest")
87
88	assert.NoError(t, err)
89	assert.Equal(t, 8080, meta.Port)
90	assert.Equal(t, "", meta.Protocol)
91}
92
93func TestResolveTargetMetadata_UnknownProtocolIgnored(t *testing.T) {
94	runtime := outmocks.NewMockContainerRuntime(t)
95	svc := &Service{runtime: runtime}
96	ctx := testContext()
97
98	runtime.EXPECT().GetImageLabels(ctx, "exotic:latest").Return(map[string]string{
99		domain.LabelProxyPort:     "8080",
100		domain.LabelProxyProtocol: "quic",
101	}, nil)
102
103	meta, err := svc.resolveTargetMetadata(ctx, "exotic:latest")
104
105	assert.NoError(t, err)
106	assert.Equal(t, 8080, meta.Port)
107	assert.Equal(t, "", meta.Protocol)
108}
109
110func TestResolveTargetMetadata_NoPortsAvailable(t *testing.T) {
111	runtime := outmocks.NewMockContainerRuntime(t)
112	svc := &Service{runtime: runtime}
113	ctx := testContext()
114
115	runtime.EXPECT().GetImageLabels(ctx, "empty:latest").Return(map[string]string{}, nil)
116	runtime.EXPECT().GetImageExposedPorts(ctx, "empty:latest").Return([]int{}, nil)
117
118	_, err := svc.resolveTargetMetadata(ctx, "empty:latest")
119
120	assert.Error(t, err)
121	assert.Contains(t, err.Error(), "no exposed ports")
122}
123
124func TestResolveTargetMetadata_LabelErrorFallsBackToExposedPort(t *testing.T) {
125	runtime := outmocks.NewMockContainerRuntime(t)
126	svc := &Service{runtime: runtime}
127	ctx := testContext()
128
129	runtime.EXPECT().GetImageLabels(ctx, "broken:latest").Return(nil, fmt.Errorf("image inspect failed"))
130	runtime.EXPECT().GetImageExposedPorts(ctx, "broken:latest").Return([]int{3000}, nil)
131
132	meta, err := svc.resolveTargetMetadata(ctx, "broken:latest")
133
134	assert.NoError(t, err)
135	assert.Equal(t, 3000, meta.Port)
136	assert.Equal(t, "", meta.Protocol)
137}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close