|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025 Cisco and/or its affiliates. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package tests |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/agntcy/csit/integrations/testutils" |
| 14 | + "github.com/google/go-cmp/cmp" |
| 15 | + ginkgo "github.com/onsi/ginkgo/v2" |
| 16 | + "github.com/onsi/gomega" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + buildConfigName = "build.config.yaml" |
| 21 | + expectedModelName = "model.json" |
| 22 | + samplesPath = "../../../samples" |
| 23 | +) |
| 24 | + |
| 25 | +var _ = ginkgo.Describe("Samples build test", func() { |
| 26 | + var ( |
| 27 | + dockerImage string |
| 28 | + samples []string |
| 29 | + ) |
| 30 | + |
| 31 | + samplesDir, err := filepath.Abs(samplesPath) |
| 32 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 33 | + samples, err = FindFilePairs(samplesDir, buildConfigName, expectedModelName) |
| 34 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 35 | + |
| 36 | + dockerImage = fmt.Sprintf("%s/dir-ctl:%s", os.Getenv("IMAGE_REPO"), os.Getenv("DIRECTORY_IMAGE_TAG")) |
| 37 | + |
| 38 | + _, err = fmt.Fprintf(ginkgo.GinkgoWriter, "samples: %v\n", samples) |
| 39 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 40 | + |
| 41 | + for _, entry := range samples { |
| 42 | + entry := entry |
| 43 | + ginkgo.Context(entry, func() { |
| 44 | + var ( |
| 45 | + tempAgentPath string |
| 46 | + mountDest string |
| 47 | + mountString string |
| 48 | + modelConfigFilePath string |
| 49 | + expectedAgentModelFile string |
| 50 | + ) |
| 51 | + |
| 52 | + ginkgo.BeforeEach(func() { |
| 53 | + mountDest := fmt.Sprintf("/%s", filepath.Base(entry)) |
| 54 | + mountString = fmt.Sprintf("%s:%s", entry, mountDest) |
| 55 | + modelConfigFilePath = filepath.Join(mountDest, buildConfigName) |
| 56 | + expectedAgentModelFile = filepath.Join(entry, expectedModelName) |
| 57 | + tempFileName := fmt.Sprintf("%s.json", strings.ReplaceAll(entry, "/", "-")) |
| 58 | + tempAgentPath = filepath.Join(os.TempDir(), tempFileName) |
| 59 | + }) |
| 60 | + |
| 61 | + ginkgo.It("Should compile", func() { |
| 62 | + _, err := fmt.Fprintf(ginkgo.GinkgoWriter, "Compiling agent model: %v\n", entry) |
| 63 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 64 | + _, err = fmt.Fprintf(ginkgo.GinkgoWriter, "tempagent path: %v\n", tempAgentPath) |
| 65 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 66 | + |
| 67 | + dirctlArgs := []string{ |
| 68 | + "build", |
| 69 | + "--config", |
| 70 | + modelConfigFilePath, |
| 71 | + mountDest, |
| 72 | + } |
| 73 | + runner := testutils.NewDockerRunner(dockerImage, mountString, nil) |
| 74 | + outputBuffer, err := runner.Run(dirctlArgs...) |
| 75 | + gomega.Expect(err).NotTo(gomega.HaveOccurred(), outputBuffer.String()) |
| 76 | + |
| 77 | + err = os.WriteFile(tempAgentPath, outputBuffer.Bytes(), 0644) |
| 78 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 79 | + }) |
| 80 | + |
| 81 | + ginkgo.It("Agent model should be the expected", func() { |
| 82 | + var expected, compiled map[string]any |
| 83 | + |
| 84 | + _, err = fmt.Fprintf(ginkgo.GinkgoWriter, "tempagent path: %v\n", tempAgentPath) |
| 85 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 86 | + |
| 87 | + expactedModelJSON, err := os.ReadFile(expectedAgentModelFile) |
| 88 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 89 | + |
| 90 | + // Unmarshal or Decode the JSON to the interface. |
| 91 | + err = json.Unmarshal([]byte(expactedModelJSON), &expected) |
| 92 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 93 | + |
| 94 | + compiledModelJSON, err := os.ReadFile(tempAgentPath) |
| 95 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 96 | + |
| 97 | + // Unmarshal or Decode the JSON to the interface. |
| 98 | + err = json.Unmarshal([]byte(compiledModelJSON), &compiled) |
| 99 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 100 | + |
| 101 | + // Filter "created_at" field and extensions |
| 102 | + filter := cmp.FilterPath(func(p cmp.Path) bool { |
| 103 | + // Ensure the path is deep enough |
| 104 | + if len(p) >= 3 { |
| 105 | + if mapStep, ok := p[len(p)-3].(cmp.MapIndex); ok { |
| 106 | + if key, ok := mapStep.Key().Interface().(string); ok && key == "created_at" || key == "extensions" { |
| 107 | + return true // Ignore these paths |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + return false // Include all other paths |
| 112 | + }, cmp.Ignore()) |
| 113 | + |
| 114 | + gomega.Expect(expected).Should(gomega.BeComparableTo(compiled, filter)) |
| 115 | + gomega.Expect(expected["extensions"]).Should(gomega.ConsistOf(compiled["extensions"])) |
| 116 | + }) |
| 117 | + }) |
| 118 | + } |
| 119 | +}) |
0 commit comments