|
| 1 | +// Copyright 2022 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package integrations |
| 6 | + |
| 7 | +import ( |
| 8 | + "archive/tar" |
| 9 | + "bytes" |
| 10 | + "compress/gzip" |
| 11 | + "fmt" |
| 12 | + "net/http" |
| 13 | + "testing" |
| 14 | + "time" |
| 15 | + |
| 16 | + "code.gitea.io/gitea/models/db" |
| 17 | + "code.gitea.io/gitea/models/packages" |
| 18 | + "code.gitea.io/gitea/models/unittest" |
| 19 | + user_model "code.gitea.io/gitea/models/user" |
| 20 | + helm_module "code.gitea.io/gitea/modules/packages/helm" |
| 21 | + "code.gitea.io/gitea/modules/setting" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "gopkg.in/yaml.v2" |
| 25 | +) |
| 26 | + |
| 27 | +func TestPackageHelm(t *testing.T) { |
| 28 | + defer prepareTestEnv(t)() |
| 29 | + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User) |
| 30 | + |
| 31 | + packageName := "test-chart" |
| 32 | + packageVersion := "1.0.3" |
| 33 | + packageAuthor := "KN4CK3R" |
| 34 | + packageDescription := "Gitea Test Package" |
| 35 | + |
| 36 | + filename := fmt.Sprintf("%s-%s.tgz", packageName, packageVersion) |
| 37 | + |
| 38 | + chartContent := `apiVersion: v2 |
| 39 | +description: ` + packageDescription + ` |
| 40 | +name: ` + packageName + ` |
| 41 | +type: application |
| 42 | +version: ` + packageVersion + ` |
| 43 | +maintainers: |
| 44 | +- name: ` + packageAuthor + ` |
| 45 | +dependencies: |
| 46 | +- name: dep1 |
| 47 | + repository: https://example.com/ |
| 48 | + version: 1.0.0` |
| 49 | + |
| 50 | + var buf bytes.Buffer |
| 51 | + zw := gzip.NewWriter(&buf) |
| 52 | + archive := tar.NewWriter(zw) |
| 53 | + archive.WriteHeader(&tar.Header{ |
| 54 | + Name: fmt.Sprintf("%s/Chart.yaml", packageName), |
| 55 | + Mode: 0o600, |
| 56 | + Size: int64(len(chartContent)), |
| 57 | + }) |
| 58 | + archive.Write([]byte(chartContent)) |
| 59 | + archive.Close() |
| 60 | + zw.Close() |
| 61 | + content := buf.Bytes() |
| 62 | + |
| 63 | + url := fmt.Sprintf("/api/packages/%s/helm", user.Name) |
| 64 | + |
| 65 | + t.Run("Upload", func(t *testing.T) { |
| 66 | + defer PrintCurrentTest(t)() |
| 67 | + |
| 68 | + uploadURL := url + "/api/charts" |
| 69 | + |
| 70 | + req := NewRequestWithBody(t, "POST", uploadURL, bytes.NewReader(content)) |
| 71 | + req = AddBasicAuthHeader(req, user.Name) |
| 72 | + MakeRequest(t, req, http.StatusCreated) |
| 73 | + |
| 74 | + pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeHelm) |
| 75 | + assert.NoError(t, err) |
| 76 | + assert.Len(t, pvs, 1) |
| 77 | + |
| 78 | + pd, err := packages.GetPackageDescriptor(db.DefaultContext, pvs[0]) |
| 79 | + assert.NoError(t, err) |
| 80 | + assert.NotNil(t, pd.SemVer) |
| 81 | + assert.IsType(t, &helm_module.Metadata{}, pd.Metadata) |
| 82 | + assert.Equal(t, packageName, pd.Package.Name) |
| 83 | + assert.Equal(t, packageVersion, pd.Version.Version) |
| 84 | + |
| 85 | + pfs, err := packages.GetFilesByVersionID(db.DefaultContext, pvs[0].ID) |
| 86 | + assert.NoError(t, err) |
| 87 | + assert.Len(t, pfs, 1) |
| 88 | + assert.Equal(t, filename, pfs[0].Name) |
| 89 | + assert.True(t, pfs[0].IsLead) |
| 90 | + |
| 91 | + pb, err := packages.GetBlobByID(db.DefaultContext, pfs[0].BlobID) |
| 92 | + assert.NoError(t, err) |
| 93 | + assert.Equal(t, int64(len(content)), pb.Size) |
| 94 | + |
| 95 | + req = NewRequestWithBody(t, "POST", uploadURL, bytes.NewReader(content)) |
| 96 | + req = AddBasicAuthHeader(req, user.Name) |
| 97 | + MakeRequest(t, req, http.StatusCreated) |
| 98 | + }) |
| 99 | + |
| 100 | + t.Run("Download", func(t *testing.T) { |
| 101 | + defer PrintCurrentTest(t)() |
| 102 | + |
| 103 | + checkDownloadCount := func(count int64) { |
| 104 | + pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeHelm) |
| 105 | + assert.NoError(t, err) |
| 106 | + assert.Len(t, pvs, 1) |
| 107 | + assert.Equal(t, count, pvs[0].DownloadCount) |
| 108 | + } |
| 109 | + |
| 110 | + checkDownloadCount(0) |
| 111 | + |
| 112 | + req := NewRequest(t, "GET", fmt.Sprintf("%s/%s", url, filename)) |
| 113 | + req = AddBasicAuthHeader(req, user.Name) |
| 114 | + resp := MakeRequest(t, req, http.StatusOK) |
| 115 | + |
| 116 | + assert.Equal(t, content, resp.Body.Bytes()) |
| 117 | + |
| 118 | + checkDownloadCount(1) |
| 119 | + }) |
| 120 | + |
| 121 | + t.Run("Index", func(t *testing.T) { |
| 122 | + defer PrintCurrentTest(t)() |
| 123 | + |
| 124 | + req := NewRequest(t, "GET", fmt.Sprintf("%s/index.yaml", url)) |
| 125 | + req = AddBasicAuthHeader(req, user.Name) |
| 126 | + resp := MakeRequest(t, req, http.StatusOK) |
| 127 | + |
| 128 | + type ChartVersion struct { |
| 129 | + helm_module.Metadata `yaml:",inline"` |
| 130 | + URLs []string `yaml:"urls"` |
| 131 | + Created time.Time `yaml:"created,omitempty"` |
| 132 | + Removed bool `yaml:"removed,omitempty"` |
| 133 | + Digest string `yaml:"digest,omitempty"` |
| 134 | + } |
| 135 | + |
| 136 | + type ServerInfo struct { |
| 137 | + ContextPath string `yaml:"contextPath,omitempty"` |
| 138 | + } |
| 139 | + |
| 140 | + type Index struct { |
| 141 | + APIVersion string `yaml:"apiVersion"` |
| 142 | + Entries map[string][]*ChartVersion `yaml:"entries"` |
| 143 | + Generated time.Time `yaml:"generated,omitempty"` |
| 144 | + ServerInfo *ServerInfo `yaml:"serverInfo,omitempty"` |
| 145 | + } |
| 146 | + |
| 147 | + var result Index |
| 148 | + assert.NoError(t, yaml.NewDecoder(resp.Body).Decode(&result)) |
| 149 | + assert.NotEmpty(t, result.Entries) |
| 150 | + assert.Contains(t, result.Entries, packageName) |
| 151 | + |
| 152 | + cvs := result.Entries[packageName] |
| 153 | + assert.Len(t, cvs, 1) |
| 154 | + |
| 155 | + cv := cvs[0] |
| 156 | + assert.Equal(t, packageName, cv.Name) |
| 157 | + assert.Equal(t, packageVersion, cv.Version) |
| 158 | + assert.Equal(t, packageDescription, cv.Description) |
| 159 | + assert.Len(t, cv.Maintainers, 1) |
| 160 | + assert.Equal(t, packageAuthor, cv.Maintainers[0].Name) |
| 161 | + assert.Len(t, cv.Dependencies, 1) |
| 162 | + assert.ElementsMatch(t, []string{fmt.Sprintf("%s%s/%s", setting.AppURL, url[1:], filename)}, cv.URLs) |
| 163 | + |
| 164 | + assert.Equal(t, url, result.ServerInfo.ContextPath) |
| 165 | + }) |
| 166 | +} |
0 commit comments