summaryrefslogtreecommitdiffstats
path: root/tests/integration/api_packages_conda_test.go
blob: 4625c5854c1976fe9235d125b71ade778119c6b5 (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package integration

import (
	"archive/tar"
	"archive/zip"
	"bytes"
	"fmt"
	"io"
	"net/http"
	"testing"

	"code.gitea.io/gitea/models/db"
	"code.gitea.io/gitea/models/packages"
	"code.gitea.io/gitea/models/unittest"
	user_model "code.gitea.io/gitea/models/user"
	conda_module "code.gitea.io/gitea/modules/packages/conda"
	"code.gitea.io/gitea/modules/zstd"
	"code.gitea.io/gitea/tests"

	"github.com/dsnet/compress/bzip2"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestPackageConda(t *testing.T) {
	defer tests.PrepareTestEnv(t)()

	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})

	packageName := "test_package"
	packageVersion := "1.0.1"

	channel := "test-channel"
	root := fmt.Sprintf("/api/packages/%s/conda", user.Name)

	t.Run("Upload", func(t *testing.T) {
		tarContent := func() []byte {
			var buf bytes.Buffer
			tw := tar.NewWriter(&buf)

			content := []byte(`{"name":"` + packageName + `","version":"` + packageVersion + `","subdir":"noarch","build":"xxx"}`)

			hdr := &tar.Header{
				Name: "info/index.json",
				Mode: 0o600,
				Size: int64(len(content)),
			}
			tw.WriteHeader(hdr)
			tw.Write(content)
			tw.Close()
			return buf.Bytes()
		}()

		t.Run(".tar.bz2", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			var buf bytes.Buffer
			bw, _ := bzip2.NewWriter(&buf, nil)
			io.Copy(bw, bytes.NewReader(tarContent))
			bw.Close()

			filename := fmt.Sprintf("%s-%s.tar.bz2", packageName, packageVersion)

			req := NewRequestWithBody(t, "PUT", root+"/"+filename, bytes.NewReader(buf.Bytes()))
			MakeRequest(t, req, http.StatusUnauthorized)

			req = NewRequestWithBody(t, "PUT", root+"/"+filename, bytes.NewReader(buf.Bytes())).
				AddBasicAuth(user.Name)
			MakeRequest(t, req, http.StatusCreated)

			req = NewRequestWithBody(t, "PUT", root+"/"+filename, bytes.NewReader(buf.Bytes())).
				AddBasicAuth(user.Name)
			MakeRequest(t, req, http.StatusConflict)

			pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeConda)
			require.NoError(t, err)
			assert.Len(t, pvs, 1)

			pd, err := packages.GetPackageDescriptor(db.DefaultContext, pvs[0])
			require.NoError(t, err)
			assert.Nil(t, pd.SemVer)
			assert.IsType(t, &conda_module.VersionMetadata{}, pd.Metadata)
			assert.Equal(t, packageName, pd.Package.Name)
			assert.Equal(t, packageVersion, pd.Version.Version)
			assert.Empty(t, pd.PackageProperties.GetByName(conda_module.PropertyChannel))
		})

		t.Run(".conda", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			var infoBuf bytes.Buffer
			zsw, _ := zstd.NewWriter(&infoBuf)
			io.Copy(zsw, bytes.NewReader(tarContent))
			zsw.Close()

			var buf bytes.Buffer
			zpw := zip.NewWriter(&buf)
			w, _ := zpw.Create("info-x.tar.zst")
			w.Write(infoBuf.Bytes())
			zpw.Close()

			fullName := channel + "/" + packageName
			filename := fmt.Sprintf("%s-%s.conda", packageName, packageVersion)

			req := NewRequestWithBody(t, "PUT", root+"/"+channel+"/"+filename, bytes.NewReader(buf.Bytes()))
			MakeRequest(t, req, http.StatusUnauthorized)

			req = NewRequestWithBody(t, "PUT", root+"/"+channel+"/"+filename, bytes.NewReader(buf.Bytes())).
				AddBasicAuth(user.Name)
			MakeRequest(t, req, http.StatusCreated)

			req = NewRequestWithBody(t, "PUT", root+"/"+channel+"/"+filename, bytes.NewReader(buf.Bytes())).
				AddBasicAuth(user.Name)
			MakeRequest(t, req, http.StatusConflict)

			pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeConda)
			require.NoError(t, err)
			assert.Len(t, pvs, 2)

			pds, err := packages.GetPackageDescriptors(db.DefaultContext, pvs)
			require.NoError(t, err)

			assert.Condition(t, func() bool {
				for _, pd := range pds {
					if pd.Package.Name == fullName {
						return true
					}
				}
				return false
			})

			for _, pd := range pds {
				if pd.Package.Name == fullName {
					assert.Nil(t, pd.SemVer)
					assert.IsType(t, &conda_module.VersionMetadata{}, pd.Metadata)
					assert.Equal(t, fullName, pd.Package.Name)
					assert.Equal(t, packageVersion, pd.Version.Version)
					assert.Equal(t, channel, pd.PackageProperties.GetByName(conda_module.PropertyChannel))
				}
			}
		})
	})

	t.Run("Download", func(t *testing.T) {
		t.Run(".tar.bz2", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			req := NewRequest(t, "GET", fmt.Sprintf("%s/noarch/%s-%s-xxx.tar.bz2", root, packageName, packageVersion))
			MakeRequest(t, req, http.StatusOK)

			req = NewRequest(t, "GET", fmt.Sprintf("%s/%s/noarch/%s-%s-xxx.tar.bz2", root, channel, packageName, packageVersion))
			MakeRequest(t, req, http.StatusNotFound)
		})

		t.Run(".conda", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			req := NewRequest(t, "GET", fmt.Sprintf("%s/noarch/%s-%s-xxx.conda", root, packageName, packageVersion))
			MakeRequest(t, req, http.StatusNotFound)

			req = NewRequest(t, "GET", fmt.Sprintf("%s/%s/noarch/%s-%s-xxx.conda", root, channel, packageName, packageVersion))
			MakeRequest(t, req, http.StatusOK)
		})
	})

	t.Run("EnumeratePackages", func(t *testing.T) {
		type Info struct {
			Subdir string `json:"subdir"`
		}

		type PackageInfo struct {
			Name          string   `json:"name"`
			Version       string   `json:"version"`
			NoArch        string   `json:"noarch"`
			Subdir        string   `json:"subdir"`
			Timestamp     int64    `json:"timestamp"`
			Build         string   `json:"build"`
			BuildNumber   int64    `json:"build_number"`
			Dependencies  []string `json:"depends"`
			License       string   `json:"license"`
			LicenseFamily string   `json:"license_family"`
			HashMD5       string   `json:"md5"`
			HashSHA256    string   `json:"sha256"`
			Size          int64    `json:"size"`
		}

		type RepoData struct {
			Info          Info                    `json:"info"`
			Packages      map[string]*PackageInfo `json:"packages"`
			PackagesConda map[string]*PackageInfo `json:"packages.conda"`
			Removed       map[string]*PackageInfo `json:"removed"`
		}

		req := NewRequest(t, "GET", fmt.Sprintf("%s/noarch/repodata.json", root))
		resp := MakeRequest(t, req, http.StatusOK)
		assert.Equal(t, "application/json", resp.Header().Get("Content-Type"))

		req = NewRequest(t, "GET", fmt.Sprintf("%s/noarch/repodata.json.bz2", root))
		resp = MakeRequest(t, req, http.StatusOK)
		assert.Equal(t, "application/x-bzip2", resp.Header().Get("Content-Type"))

		req = NewRequest(t, "GET", fmt.Sprintf("%s/noarch/current_repodata.json", root))
		resp = MakeRequest(t, req, http.StatusOK)
		assert.Equal(t, "application/json", resp.Header().Get("Content-Type"))

		req = NewRequest(t, "GET", fmt.Sprintf("%s/noarch/current_repodata.json.bz2", root))
		resp = MakeRequest(t, req, http.StatusOK)
		assert.Equal(t, "application/x-bzip2", resp.Header().Get("Content-Type"))

		t.Run(".tar.bz2", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			pv, err := packages.GetVersionByNameAndVersion(db.DefaultContext, user.ID, packages.TypeConda, packageName, packageVersion)
			require.NoError(t, err)

			pd, err := packages.GetPackageDescriptor(db.DefaultContext, pv)
			require.NoError(t, err)

			req := NewRequest(t, "GET", fmt.Sprintf("%s/noarch/repodata.json", root))
			resp := MakeRequest(t, req, http.StatusOK)

			var result RepoData
			DecodeJSON(t, resp, &result)

			assert.Equal(t, "noarch", result.Info.Subdir)
			assert.Empty(t, result.PackagesConda)
			assert.Empty(t, result.Removed)

			filename := fmt.Sprintf("%s-%s-xxx.tar.bz2", packageName, packageVersion)
			assert.Contains(t, result.Packages, filename)
			packageInfo := result.Packages[filename]
			assert.Equal(t, packageName, packageInfo.Name)
			assert.Equal(t, packageVersion, packageInfo.Version)
			assert.Equal(t, "noarch", packageInfo.Subdir)
			assert.Equal(t, "xxx", packageInfo.Build)
			assert.Equal(t, pd.Files[0].Blob.HashMD5, packageInfo.HashMD5)
			assert.Equal(t, pd.Files[0].Blob.HashSHA256, packageInfo.HashSHA256)
			assert.Equal(t, pd.Files[0].Blob.Size, packageInfo.Size)
		})

		t.Run(".conda", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			pv, err := packages.GetVersionByNameAndVersion(db.DefaultContext, user.ID, packages.TypeConda, channel+"/"+packageName, packageVersion)
			require.NoError(t, err)

			pd, err := packages.GetPackageDescriptor(db.DefaultContext, pv)
			require.NoError(t, err)

			req := NewRequest(t, "GET", fmt.Sprintf("%s/%s/noarch/repodata.json", root, channel))
			resp := MakeRequest(t, req, http.StatusOK)

			var result RepoData
			DecodeJSON(t, resp, &result)

			assert.Equal(t, "noarch", result.Info.Subdir)
			assert.Empty(t, result.Packages)
			assert.Empty(t, result.Removed)

			filename := fmt.Sprintf("%s-%s-xxx.conda", packageName, packageVersion)
			assert.Contains(t, result.PackagesConda, filename)
			packageInfo := result.PackagesConda[filename]
			assert.Equal(t, packageName, packageInfo.Name)
			assert.Equal(t, packageVersion, packageInfo.Version)
			assert.Equal(t, "noarch", packageInfo.Subdir)
			assert.Equal(t, "xxx", packageInfo.Build)
			assert.Equal(t, pd.Files[0].Blob.HashMD5, packageInfo.HashMD5)
			assert.Equal(t, pd.Files[0].Blob.HashSHA256, packageInfo.HashSHA256)
			assert.Equal(t, pd.Files[0].Blob.Size, packageInfo.Size)
		})
	})
}