|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "os" |
5 | 6 | "path/filepath" |
6 | 7 | "strings" |
7 | 8 | "testing" |
8 | 9 |
|
9 | 10 | "github.com/codefly-dev/core/agents/services" |
10 | 11 | agenttesting "github.com/codefly-dev/core/agents/testing" |
| 12 | + basev0 "github.com/codefly-dev/core/generated/go/codefly/base/v0" |
11 | 13 | builderv0 "github.com/codefly-dev/core/generated/go/codefly/services/builder/v0" |
| 14 | + "github.com/codefly-dev/core/resources" |
| 15 | + "github.com/codefly-dev/core/wool" |
12 | 16 | "github.com/stretchr/testify/require" |
13 | 17 | "gopkg.in/yaml.v3" |
14 | 18 | ) |
@@ -284,3 +288,48 @@ func TestPreparePodOverlayNormalizesMounts(t *testing.T) { |
284 | 288 | func TestPreparePodOverlayAcceptsNoOverlay(t *testing.T) { |
285 | 289 | require.NoError(t, preparePodOverlay(nil)) |
286 | 290 | } |
| 291 | + |
| 292 | +// renderProfile renders the deployment templates for one output profile through |
| 293 | +// the same core entry point Deploy uses. |
| 294 | +func renderProfile(t *testing.T, profile builderv0.KubernetesOutputProfile) string { |
| 295 | + t.Helper() |
| 296 | + ctx := context.Background() |
| 297 | + identity := &resources.ServiceIdentity{Workspace: "workspace", Module: "module", Name: "example-service", Version: "1.2.3"} |
| 298 | + base := &services.Base{ |
| 299 | + Wool: wool.Get(ctx), |
| 300 | + Identity: identity, |
| 301 | + Information: &services.Information{Service: resources.ToServiceWithCase(identity), Module: resources.ToModuleWithCase(identity)}, |
| 302 | + } |
| 303 | + if services.IsRestrictedOutputProfile(profile) { |
| 304 | + base.SetDockerImage(&resources.DockerImage{Name: "example/service", Digest: "sha256:" + strings.Repeat("a", 64)}) |
| 305 | + } else { |
| 306 | + base.SetDockerImage(resources.NewDockerImage("example/service:1.2.3")) |
| 307 | + } |
| 308 | + builder := &services.BuilderWrapper{Base: base} |
| 309 | + base.Builder = builder |
| 310 | + destination := t.TempDir() |
| 311 | + deployment := &builderv0.KubernetesDeployment{Namespace: "codefly-test", Destination: destination, Profile: profile} |
| 312 | + params := services.DeploymentParameters{ |
| 313 | + ConfigMap: services.EnvironmentMap{"CODEFLY_TEST_VALUE": "value"}, |
| 314 | + Parameters: deploymentTestParameters(t, Parameters{}), |
| 315 | + } |
| 316 | + require.NoError(t, builder.KustomizeDeploy(ctx, &basev0.Environment{Name: "test"}, deployment, deploymentFS, params)) |
| 317 | + return destination |
| 318 | +} |
| 319 | + |
| 320 | +// A restricted render is what `codefly deploy gitops render` produces, and it |
| 321 | +// refuses a cluster-scoped object outside an AppProject contract. The Namespace |
| 322 | +// belongs to the environment that owns it, as in the go-grpc agent; only the |
| 323 | +// ephemeral local apply creates one. |
| 324 | +func TestRestrictedDeploymentLeavesTheNamespaceToTheEnvironment(t *testing.T) { |
| 325 | + restricted := renderProfile(t, builderv0.KubernetesOutputProfile_KUBERNETES_OUTPUT_PROFILE_RESTRICTED_PORTABLE_V1) |
| 326 | + kustomization, err := os.ReadFile(filepath.Join(restricted, "base", "kustomization.yaml")) |
| 327 | + require.NoError(t, err) |
| 328 | + require.NotContains(t, string(kustomization), "namespace.yaml") |
| 329 | + require.Contains(t, string(kustomization), "deployment.yaml") |
| 330 | + |
| 331 | + local := renderProfile(t, builderv0.KubernetesOutputProfile_KUBERNETES_OUTPUT_PROFILE_EPHEMERAL_LOCAL_APPLY_V1) |
| 332 | + kustomization, err = os.ReadFile(filepath.Join(local, "base", "kustomization.yaml")) |
| 333 | + require.NoError(t, err) |
| 334 | + require.Contains(t, string(kustomization), "namespace.yaml") |
| 335 | +} |
0 commit comments