Repository navigation
Expand file tree
/
Copy pathdirctl_test.go
More file actions
220 lines (177 loc) · 5.65 KB
/
Copy pathdirctl_test.go
File metadata and controls
220 lines (177 loc) · 5.65 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright AGNTCY Contributors (https://github.com/agntcy)
// SPDX-License-Identifier: Apache-2.0
package e2e
import (
"bytes"
_ "embed"
"os"
"path/filepath"
clicmd "github.com/agntcy/dir/cli/cmd"
"github.com/agntcy/dir/e2e/config"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"github.com/opencontainers/go-digest"
)
//go:embed testdata/agent.json
var expectedAgentJSON []byte
var _ = ginkgo.Describe("Running dirctl end-to-end tests using a local single node deployment", func() {
ginkgo.BeforeEach(func() {
if cfg.DeploymentMode != config.DeploymentModeLocal {
ginkgo.Skip("Skipping test, not in local mode")
}
})
// Test params
var tempAgentDigest string
tempAgentDir := os.Getenv("E2E_COMPILE_OUTPUT_DIR")
if tempAgentDir == "" {
tempAgentDir = os.TempDir()
}
tempAgentPath := filepath.Join(tempAgentDir, "agent.json")
ginkgo.Context("agent build", func() {
ginkgo.It("should build an agent", func() {
var outputBuffer bytes.Buffer
compileCmd := clicmd.RootCmd
compileCmd.SetOut(&outputBuffer)
compileCmd.SetArgs([]string{
"build",
"testdata",
})
err := compileCmd.Execute()
gomega.Expect(err).NotTo(gomega.HaveOccurred())
err = os.MkdirAll(filepath.Dir(tempAgentPath), 0o755)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
err = os.WriteFile(tempAgentPath, outputBuffer.Bytes(), 0o600)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
// Compare the output with the expected JSON
equal, err := compareJSONAgents(outputBuffer.Bytes(), expectedAgentJSON)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(equal).To(gomega.BeTrue())
})
})
ginkgo.Context("agent push and pull", func() {
ginkgo.It("should successfully push an agent", func() {
var outputBuffer bytes.Buffer
pushCmd := clicmd.RootCmd
pushCmd.SetOut(&outputBuffer)
pushCmd.SetArgs([]string{
"push",
tempAgentPath,
})
err := pushCmd.Execute()
gomega.Expect(err).NotTo(gomega.HaveOccurred())
tempAgentDigest = outputBuffer.String()
// Ensure the digest is valid
_, err = digest.Parse(tempAgentDigest)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
})
ginkgo.It("should successfully pull an existing agent", func() {
var outputBuffer bytes.Buffer
pullCmd := clicmd.RootCmd
pullCmd.SetOut(&outputBuffer)
pullCmd.SetArgs([]string{
"pull",
tempAgentDigest,
})
err := pullCmd.Execute()
gomega.Expect(err).NotTo(gomega.HaveOccurred())
})
ginkgo.It("should push the same agent again and return the same digest", func() {
var outputBuffer bytes.Buffer
pushCmd := clicmd.RootCmd
pushCmd.SetOut(&outputBuffer)
pushCmd.SetArgs([]string{
"push",
tempAgentPath,
})
err := pushCmd.Execute()
gomega.Expect(err).NotTo(gomega.HaveOccurred())
// Ensure the digests are the same
newAgentDigest := outputBuffer.String()
gomega.Expect(newAgentDigest).To(gomega.Equal(tempAgentDigest))
})
ginkgo.It("should push two different agents and return different digests", func() {
var outputBuffer bytes.Buffer
// Modify the agent file to create a different agent
tempAgentPath2 := filepath.Join(tempAgentDir, "agent2.json")
err := os.WriteFile(tempAgentPath2, []byte(`{"name": "different-agent", "signature": {}}`), 0o600)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
// Push second agent
pushCmd2 := clicmd.RootCmd
pushCmd2.SetOut(&outputBuffer)
pushCmd2.SetArgs([]string{
"push",
tempAgentPath2,
})
err = pushCmd2.Execute()
gomega.Expect(err).NotTo(gomega.HaveOccurred())
newAgentDigest := outputBuffer.String()
// Ensure the digests are different
gomega.Expect(newAgentDigest).NotTo(gomega.Equal(tempAgentDigest))
})
ginkgo.It("should pull a non-existent agent and return an error", func() {
var outputBuffer bytes.Buffer
pullCmd := clicmd.RootCmd
pullCmd.SetOut(&outputBuffer)
pullCmd.SetArgs([]string{
"pull",
"non-existent-digest",
})
err := pullCmd.Execute()
gomega.Expect(err).To(gomega.HaveOccurred())
})
})
ginkgo.Context("agent search", func() {
ginkgo.It("should search for an agent with filters and return the cid", func() {
var outputBuffer bytes.Buffer
searchCmd := clicmd.RootCmd
searchCmd.SetOut(&outputBuffer)
searchCmd.SetArgs([]string{
"search",
"--limit",
"10",
"--offset",
"0",
"--query",
"name=directory.agntcy.org/cisco/marketing-strategy",
"--query",
"version=v1.0.0",
"--query",
"skill-id=10201",
"--query",
"skill-name=Natural Language Processing/Text Completion",
"--query",
"locator=docker-image:https://ghcr.io/agntcy/marketing-strategy",
"--query",
"extension=schema.oasf.agntcy.org/features/runtime/framework:v0.0.0",
})
err := searchCmd.Execute()
gomega.Expect(err).NotTo(gomega.HaveOccurred())
// Check if the output contains the expected CID
gomega.Expect(outputBuffer.String()).To(gomega.Equal("sha256:1beb8653b5bf888274c1e2c3754e096b0242ceee8518c330be3239fa88a4fc80"))
})
})
ginkgo.Context("agent delete", func() {
ginkgo.It("should successfully delete an agent", func() {
var outputBuffer bytes.Buffer
deleteCmd := clicmd.RootCmd
deleteCmd.SetOut(&outputBuffer)
deleteCmd.SetArgs([]string{
"delete",
tempAgentDigest,
})
err := deleteCmd.Execute()
gomega.Expect(err).NotTo(gomega.HaveOccurred())
})
ginkgo.It("should fail to pull a deleted agent", func() {
var outputBuffer bytes.Buffer
pullCmd := clicmd.RootCmd
pullCmd.SetOut(&outputBuffer)
pullCmd.SetArgs([]string{
"pull",
tempAgentDigest,
})
err := pullCmd.Execute()
gomega.Expect(err).To(gomega.HaveOccurred())
})
})
})