|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestClassifyInfrahubEdition(t *testing.T) { |
| 9 | + cases := []struct { |
| 10 | + reference string |
| 11 | + want string |
| 12 | + }{ |
| 13 | + // Community images, various registries/tags/digests. |
| 14 | + {"opsmill/infrahub:stable", infrahubEditionCommunity}, |
| 15 | + {"registry.opsmill.io/opsmill/infrahub:1.5.2", infrahubEditionCommunity}, |
| 16 | + {"infrahub", infrahubEditionCommunity}, |
| 17 | + {"opsmill/infrahub@sha256:deadbeef", infrahubEditionCommunity}, |
| 18 | + {"registry:5000/opsmill/infrahub:1.5.2", infrahubEditionCommunity}, |
| 19 | + // Enterprise images. |
| 20 | + {"opsmill/infrahub-enterprise:stable", infrahubEditionEnterprise}, |
| 21 | + {"registry.opsmill.io/opsmill/infrahub-enterprise:1.5.2", infrahubEditionEnterprise}, |
| 22 | + {"infrahub-enterprise", infrahubEditionEnterprise}, |
| 23 | + {"registry:5000/opsmill/infrahub-enterprise@sha256:abc", infrahubEditionEnterprise}, |
| 24 | + // Helm chart names use the same signal. |
| 25 | + {"infrahub", infrahubEditionCommunity}, |
| 26 | + {"infrahub-enterprise", infrahubEditionEnterprise}, |
| 27 | + // Unrecognized references stay unknown rather than being guessed. |
| 28 | + {"", ""}, |
| 29 | + {"opsmill/infrahub-enterprise-custom:1.0", ""}, |
| 30 | + {"opsmill/some-other-image:1.0", ""}, |
| 31 | + {"neo4j:5.20-enterprise", ""}, |
| 32 | + } |
| 33 | + |
| 34 | + for _, tc := range cases { |
| 35 | + if got := classifyInfrahubEdition(tc.reference); got != tc.want { |
| 36 | + t.Errorf("classifyInfrahubEdition(%q) = %q, want %q", tc.reference, got, tc.want) |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestImageRepository(t *testing.T) { |
| 42 | + cases := []struct { |
| 43 | + reference string |
| 44 | + want string |
| 45 | + }{ |
| 46 | + {"registry.opsmill.io/opsmill/infrahub-enterprise:1.5.2", "infrahub-enterprise"}, |
| 47 | + {"opsmill/infrahub@sha256:deadbeef", "infrahub"}, |
| 48 | + {"registry:5000/opsmill/infrahub:1.5.2", "infrahub"}, |
| 49 | + {"infrahub", "infrahub"}, |
| 50 | + {" opsmill/infrahub:stable ", "infrahub"}, |
| 51 | + {"", ""}, |
| 52 | + } |
| 53 | + |
| 54 | + for _, tc := range cases { |
| 55 | + if got := imageRepository(tc.reference); got != tc.want { |
| 56 | + t.Errorf("imageRepository(%q) = %q, want %q", tc.reference, got, tc.want) |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// editionDetectorBackend stands in for the Docker backend in populateEdition |
| 62 | +// tests: it reports an edition directly via the editionDetector capability. |
| 63 | +type editionDetectorBackend struct { |
| 64 | + bareBackend |
| 65 | + edition string |
| 66 | + err error |
| 67 | +} |
| 68 | + |
| 69 | +func (e *editionDetectorBackend) InfrahubEdition() (string, error) { |
| 70 | + return e.edition, e.err |
| 71 | +} |
| 72 | + |
| 73 | +var _ editionDetector = (*editionDetectorBackend)(nil) |
| 74 | + |
| 75 | +func TestPopulateEdition(t *testing.T) { |
| 76 | + t.Run("docker: detector edition is recorded", func(t *testing.T) { |
| 77 | + manifest := newBundleManifest("20260703_101530", "docker", 100) |
| 78 | + populateEdition(&editionDetectorBackend{ |
| 79 | + bareBackend: bareBackend{name: "docker"}, |
| 80 | + edition: infrahubEditionEnterprise, |
| 81 | + }, manifest) |
| 82 | + if manifest.Edition != infrahubEditionEnterprise { |
| 83 | + t.Errorf("Edition = %q, want %q", manifest.Edition, infrahubEditionEnterprise) |
| 84 | + } |
| 85 | + }) |
| 86 | + |
| 87 | + t.Run("docker: an unknown edition leaves the field unset", func(t *testing.T) { |
| 88 | + manifest := newBundleManifest("20260703_101530", "docker", 100) |
| 89 | + populateEdition(&editionDetectorBackend{ |
| 90 | + bareBackend: bareBackend{name: "docker"}, |
| 91 | + edition: "", |
| 92 | + }, manifest) |
| 93 | + if manifest.Edition != "" { |
| 94 | + t.Errorf("Edition = %q, want empty when detection is inconclusive", manifest.Edition) |
| 95 | + } |
| 96 | + }) |
| 97 | + |
| 98 | + t.Run("docker: a detection error leaves the field unset without failing", func(t *testing.T) { |
| 99 | + manifest := newBundleManifest("20260703_101530", "docker", 100) |
| 100 | + populateEdition(&editionDetectorBackend{ |
| 101 | + bareBackend: bareBackend{name: "docker"}, |
| 102 | + err: errors.New("docker daemon unreachable"), |
| 103 | + }, manifest) |
| 104 | + if manifest.Edition != "" { |
| 105 | + t.Errorf("Edition = %q, want empty when detection errored", manifest.Edition) |
| 106 | + } |
| 107 | + }) |
| 108 | + |
| 109 | + t.Run("kubernetes: edition falls back to the Helm chart name", func(t *testing.T) { |
| 110 | + manifest := newBundleManifest("20260703_101530", "kubernetes", 100) |
| 111 | + manifest.Helm = &HelmRelease{ReleaseName: "infrahub", Chart: "infrahub-enterprise", ChartVersion: "1.2.3"} |
| 112 | + populateEdition(&bareBackend{name: "kubernetes"}, manifest) |
| 113 | + if manifest.Edition != infrahubEditionEnterprise { |
| 114 | + t.Errorf("Edition = %q, want %q from the Helm chart", manifest.Edition, infrahubEditionEnterprise) |
| 115 | + } |
| 116 | + }) |
| 117 | + |
| 118 | + t.Run("no detector and no Helm metadata leaves the field unset", func(t *testing.T) { |
| 119 | + manifest := newBundleManifest("20260703_101530", "docker", 100) |
| 120 | + populateEdition(&bareBackend{name: "docker"}, manifest) |
| 121 | + if manifest.Edition != "" { |
| 122 | + t.Errorf("Edition = %q, want empty when nothing can report it", manifest.Edition) |
| 123 | + } |
| 124 | + }) |
| 125 | +} |
| 126 | + |
| 127 | +func TestEditionFromContainers(t *testing.T) { |
| 128 | + cases := []struct { |
| 129 | + name string |
| 130 | + containers []composePSContainer |
| 131 | + want string |
| 132 | + }{ |
| 133 | + { |
| 134 | + name: "enterprise infrahub-server image", |
| 135 | + containers: []composePSContainer{ |
| 136 | + {Service: "database", Image: "neo4j:5.20-enterprise"}, |
| 137 | + {Service: "infrahub-server", Image: "registry.opsmill.io/opsmill/infrahub-enterprise:1.5.2"}, |
| 138 | + }, |
| 139 | + want: infrahubEditionEnterprise, |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "community infrahub-server image", |
| 143 | + containers: []composePSContainer{ |
| 144 | + {Service: "infrahub-server", Image: "opsmill/infrahub:stable"}, |
| 145 | + }, |
| 146 | + want: infrahubEditionCommunity, |
| 147 | + }, |
| 148 | + { |
| 149 | + name: "service absent", |
| 150 | + containers: []composePSContainer{{Service: "cache", Image: "redis:7"}}, |
| 151 | + want: "", |
| 152 | + }, |
| 153 | + { |
| 154 | + name: "service present but image empty", |
| 155 | + containers: []composePSContainer{{Service: "infrahub-server", Image: ""}}, |
| 156 | + want: "", |
| 157 | + }, |
| 158 | + } |
| 159 | + |
| 160 | + for _, tc := range cases { |
| 161 | + t.Run(tc.name, func(t *testing.T) { |
| 162 | + if got := editionFromContainers("infrahub-server", tc.containers); got != tc.want { |
| 163 | + t.Errorf("editionFromContainers() = %q, want %q", got, tc.want) |
| 164 | + } |
| 165 | + }) |
| 166 | + } |
| 167 | +} |
0 commit comments