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