|
| 1 | +package gitops |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/codefly-dev/cli/pkg/orchestration" |
| 8 | + "github.com/codefly-dev/core/composition" |
| 9 | + "github.com/codefly-dev/core/resources" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "testing" |
| 14 | +) |
| 15 | + |
| 16 | +func hopRouteWorkspace(t *testing.T, visibility string) *resources.Workspace { |
| 17 | + t.Helper() |
| 18 | + files := map[string]string{ |
| 19 | + "workspace.codefly.yaml": "name: routes\nlayout: modules\nmodules:\n - name: platform\n - name: payments\n", |
| 20 | + "configurations/local/hop.env": "MODULES=platform\n", |
| 21 | + "modules/platform/module.codefly.yaml": "name: platform\nservices:\n - name: first\n - name: second\n", |
| 22 | + "modules/platform/module.contract.codefly.yaml": "schema: codefly/module-contract/v1\nprincipal: platform\nnamespaces: [platform]\nqueues: []\nscope_ceilings: []\nbindings: []\n", |
| 23 | + "modules/payments/module.codefly.yaml": "name: payments\nservices:\n - name: worker\n", |
| 24 | + "modules/payments/services/worker/service.codefly.yaml": "name: worker\nversion: 0.0.0\nagent: {kind: runtime::service, name: go-grpc, version: 0.0.16, publisher: codefly.ai}\nworkspace-configuration-dependencies: [hop-routes]\n", |
| 25 | + } |
| 26 | + catalog := composition.APIContractCatalog{Schema: composition.APIContractCatalogSchema, Package: "owner/platform", Version: "1.0.0"} |
| 27 | + manifest := "schema: codefly/module-package/v2\nkind: module-package\nid: owner/platform\nversion: 1.0.0\nminimum-codefly-version: '>=0.3.32 <1.0.0'\nartifact-roots: [contracts, services]\ncontracts: {composition: '>=2.0.0 <3.0.0'}\nservices:\n" |
| 28 | + for _, service := range []string{"first", "second"} { |
| 29 | + path := "contracts/api/" + service + ".json" |
| 30 | + doc := fmt.Sprintf(`{"openapi":"3.0.0","info":{"title":"%s","version":"1.0.0"},"paths":{"/%s":{"get":{"responses":{"200":{"description":"ok"}}}}}}`, service, service) |
| 31 | + digest := composition.APIContractDigest([]byte(doc)) |
| 32 | + files["modules/platform/"+path] = doc |
| 33 | + files["modules/platform/services/"+service+"/service.codefly.yaml"] = "name: " + service + "\nversion: 0.0.0\nagent: {kind: runtime::service, name: go-grpc, version: 0.0.16, publisher: codefly.ai}\nendpoints:\n - name: web\n api: http\n visibility: " + visibility + "\n" + " exposure: none\n" |
| 34 | + manifest += fmt.Sprintf(" - name: %s\n endpoints: [web]\n api-contracts:\n - endpoint: web\n kind: openapi\n package: api\n path: %s\n digest: %s\n", service, path, digest) |
| 35 | + catalog.Endpoints = append(catalog.Endpoints, composition.APIContractEndpoint{Service: service, Endpoint: "web", API: "http", Kind: composition.APIContractKindOpenAPI, Package: "api", Path: path, Digest: digest, Routes: []composition.APIContractRoute{{Method: "GET", Path: "/" + service}}}) |
| 36 | + } |
| 37 | + raw, err := json.Marshal(catalog) |
| 38 | + require.NoError(t, err) |
| 39 | + files["modules/platform/"+composition.APIContractCatalogFileName] = string(raw) |
| 40 | + files["modules/platform/module.package.codefly.yaml"] = manifest |
| 41 | + root := t.TempDir() |
| 42 | + for rel, raw := range files { |
| 43 | + path := filepath.Join(root, rel) |
| 44 | + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0700)) |
| 45 | + require.NoError(t, os.WriteFile(path, []byte(raw), 0600)) |
| 46 | + } |
| 47 | + workspace, err := resources.LoadWorkspaceFromDir(context.Background(), root) |
| 48 | + require.NoError(t, err) |
| 49 | + return workspace |
| 50 | +} |
| 51 | + |
| 52 | +func TestCellSeesGeneratedHopRoutes(t *testing.T) { |
| 53 | + ctx := context.Background() |
| 54 | + ws := hopRouteWorkspace(t, "public") |
| 55 | + env := selectedEnvironment(t, ws, "local") |
| 56 | + provided, _, err := orchestration.WorkspaceConfigurationsForChecking(ctx, ws, env) |
| 57 | + require.NoError(t, err) |
| 58 | + require.NotEmpty(t, provided.Infos) |
| 59 | + consumers, _, err := endpointConsumers(ctx, ws, env) |
| 60 | + require.NoError(t, err) |
| 61 | + require.Equal(t, []string{"payments/worker"}, consumers["platform/first/web"]) |
| 62 | + require.Equal(t, []string{"payments/worker"}, consumers["platform/second/web"]) |
| 63 | +} |
0 commit comments