Repository navigation
Expand file tree
/
Copy pathcoolify_test.go
More file actions
133 lines (125 loc) · 4.44 KB
/
Copy pathcoolify_test.go
File metadata and controls
133 lines (125 loc) · 4.44 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
package main
import (
"context"
"log/slog"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
)
func fixtureServer(t *testing.T) *httptest.Server {
t.Helper()
mux := http.NewServeMux()
mux.HandleFunc("/api/v1/applications", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
http.ServeFile(w, r, filepath.Join("testdata", "coolify_applications.json"))
})
mux.HandleFunc("/api/v1/services", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"data": [{"uuid": "bbbbbbbb-1111-2222-3333-444444444444", "name": "example-compose-service", "docker_registry_image_name": null, "docker_registry_image_tag": null}]}`))
})
mux.HandleFunc("/api/v1/services/bbbbbbbb-1111-2222-3333-444444444444", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
http.ServeFile(w, r, filepath.Join("testdata", "coolify_service_detail.json"))
})
mux.HandleFunc("/api/v1/databases", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
http.ServeFile(w, r, filepath.Join("testdata", "coolify_databases.json"))
})
return httptest.NewServer(mux)
}
func TestCoolifyInventory(t *testing.T) {
srv := fixtureServer(t)
defer srv.Close()
inst := &CoolifyInstance{Name: "test", URL: srv.URL, Token: "dummy"}
services, err := coolifyInventory(context.Background(), inst, slog.Default())
if err != nil {
t.Fatal(err)
}
// 3 applications + compose sub-services: searxng + valkey + pg (built-by-nixpacks skipped) + 1 database.
if len(services) != 7 {
t.Fatalf("got %d services, want 7: %+v", len(services), services)
}
for _, s := range services {
if s.Source != "coolify:test" || s.Location != "test" {
t.Errorf("bad identity: %+v", s)
}
}
// app 1: direct image+tag
if s := findSvc(t, services, "example-app"); s.Image != "gitea/gitea" || s.Tag != "1.27.1" {
t.Errorf("app1: %+v", s)
}
// app 2: build-pack base_image+version
if s := findSvc(t, services, "example-buildpack-app"); s.Image != "php" || s.Tag != "8.2-fpm" {
t.Errorf("app2: %+v", s)
}
// app 3: commit-SHA tag must be unmappable, not guessed
if s := findSvc(t, services, "example-commitsha-app"); !s.Unmappable() || s.Tag != "" {
t.Errorf("app3 commit SHA should be unmappable: %+v", s)
}
// db: postgres image
if s := findSvc(t, services, "example-db"); s.Image != "postgres" || s.Tag != "16.4-alpine" {
t.Errorf("db: %+v", s)
}
// compose sub-services from the detail endpoint
want := []struct {
name, image, tag string
unmappable bool
}{
// "image" field with embedded tag (compose style)
{"example-compose-service-searxng", "searxng/searxng", "2026.9.16-461f174b0", false},
// registry name/tag fields (service-application style)
{"example-compose-service-valkey", "valkey/valkey", "8.1-alpine", false},
{"example-compose-service-pg", "postgres", "17-alpine", false},
}
for _, w := range want {
got := findSvc(t, services, w.name)
if got.Image != w.image || got.Tag != w.tag || got.Unmappable() != w.unmappable {
t.Errorf("compose %q = %+v, want %v", w.name, got, w)
}
}
}
func findSvc(t *testing.T, svcs []Service, name string) Service {
t.Helper()
for _, s := range svcs {
if s.Name == name {
return s
}
}
t.Fatalf("service %q not found in %+v", name, svcs)
return Service{}
}
func TestCoolifyEnvName(t *testing.T) {
if got := coolifyEnvName("prod-server-1"); got != "PROD_SERVER_1" {
t.Errorf("coolifyEnvName = %q", got)
}
}
func TestVersionFromTag(t *testing.T) {
cases := []struct {
tag string
version string
ok bool
}{
{"1.27.1", "1.27.1", true},
{"v0.16.22", "0.16.22", true},
{"14.23-alpine", "14.23", true},
{"10.6.11-debian-11-r12", "10.6.11", true},
{"2026.5", "2026.5", true},
{"12.1ubu2604-ls50", "12.1", true}, // linuxserver style
{"10.11.0ubu2404", "10.11.0", true}, // bare base suffix
{"postgresql-v2.16", "2.16", true}, // flavor-prefixed (umami)
{"amd64-v1.2.3", "1.2.3", true}, // arch-prefixed
{"postgresql-latest", "", false}, // prefix + floating = still unmappable
{"nightly-20260914-ls102", "", false},
{"latest", "", false},
{"test", "", false},
{"postgresql-latest", "", false},
{"", "", false},
}
for _, c := range cases {
v, ok := VersionFromTag(c.tag)
if v != c.version || ok != c.ok {
t.Errorf("VersionFromTag(%q) = (%q,%v), want (%q,%v)", c.tag, v, ok, c.version, c.ok)
}
}
}