code.gitea.io/gitea@v1.21.7/tests/integration/api_packages_alpine_test.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package integration
     5  
     6  import (
     7  	"archive/tar"
     8  	"bufio"
     9  	"bytes"
    10  	"compress/gzip"
    11  	"encoding/base64"
    12  	"fmt"
    13  	"io"
    14  	"net/http"
    15  	"testing"
    16  
    17  	"code.gitea.io/gitea/models/db"
    18  	"code.gitea.io/gitea/models/packages"
    19  	"code.gitea.io/gitea/models/unittest"
    20  	user_model "code.gitea.io/gitea/models/user"
    21  	alpine_module "code.gitea.io/gitea/modules/packages/alpine"
    22  	"code.gitea.io/gitea/tests"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestPackageAlpine(t *testing.T) {
    28  	defer tests.PrepareTestEnv(t)()
    29  
    30  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
    31  
    32  	packageName := "gitea-test"
    33  	packageVersion := "1.4.1-r3"
    34  
    35  	base64AlpinePackageContent := `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`
    59  	content, err := base64.StdEncoding.DecodeString(base64AlpinePackageContent)
    60  	assert.NoError(t, err)
    61  
    62  	branches := []string{"v3.16", "v3.17", "v3.18"}
    63  	repositories := []string{"main", "testing"}
    64  
    65  	rootURL := fmt.Sprintf("/api/packages/%s/alpine", user.Name)
    66  
    67  	t.Run("RepositoryKey", func(t *testing.T) {
    68  		defer tests.PrintCurrentTest(t)()
    69  
    70  		req := NewRequest(t, "GET", rootURL+"/key")
    71  		resp := MakeRequest(t, req, http.StatusOK)
    72  
    73  		assert.Equal(t, "application/x-pem-file", resp.Header().Get("Content-Type"))
    74  		assert.Contains(t, resp.Body.String(), "-----BEGIN PUBLIC KEY-----")
    75  	})
    76  
    77  	for _, branch := range branches {
    78  		for _, repository := range repositories {
    79  			t.Run(fmt.Sprintf("[Branch:%s,Repository:%s]", branch, repository), func(t *testing.T) {
    80  				t.Run("Upload", func(t *testing.T) {
    81  					defer tests.PrintCurrentTest(t)()
    82  
    83  					uploadURL := fmt.Sprintf("%s/%s/%s", rootURL, branch, repository)
    84  
    85  					req := NewRequestWithBody(t, "PUT", uploadURL, bytes.NewReader([]byte{}))
    86  					MakeRequest(t, req, http.StatusUnauthorized)
    87  
    88  					req = NewRequestWithBody(t, "PUT", uploadURL, bytes.NewReader([]byte{}))
    89  					AddBasicAuthHeader(req, user.Name)
    90  					MakeRequest(t, req, http.StatusBadRequest)
    91  
    92  					req = NewRequestWithBody(t, "PUT", uploadURL, bytes.NewReader(content))
    93  					AddBasicAuthHeader(req, user.Name)
    94  					MakeRequest(t, req, http.StatusCreated)
    95  
    96  					pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeAlpine)
    97  					assert.NoError(t, err)
    98  					assert.Len(t, pvs, 1)
    99  
   100  					pd, err := packages.GetPackageDescriptor(db.DefaultContext, pvs[0])
   101  					assert.NoError(t, err)
   102  					assert.Nil(t, pd.SemVer)
   103  					assert.IsType(t, &alpine_module.VersionMetadata{}, pd.Metadata)
   104  					assert.Equal(t, packageName, pd.Package.Name)
   105  					assert.Equal(t, packageVersion, pd.Version.Version)
   106  
   107  					pfs, err := packages.GetFilesByVersionID(db.DefaultContext, pvs[0].ID)
   108  					assert.NoError(t, err)
   109  					assert.NotEmpty(t, pfs)
   110  					assert.Condition(t, func() bool {
   111  						seen := false
   112  						expectedFilename := fmt.Sprintf("%s-%s.apk", packageName, packageVersion)
   113  						expectedCompositeKey := fmt.Sprintf("%s|%s|x86_64", branch, repository)
   114  						for _, pf := range pfs {
   115  							if pf.Name == expectedFilename && pf.CompositeKey == expectedCompositeKey {
   116  								if seen {
   117  									return false
   118  								}
   119  								seen = true
   120  
   121  								assert.True(t, pf.IsLead)
   122  
   123  								pfps, err := packages.GetProperties(db.DefaultContext, packages.PropertyTypeFile, pf.ID)
   124  								assert.NoError(t, err)
   125  
   126  								for _, pfp := range pfps {
   127  									switch pfp.Name {
   128  									case alpine_module.PropertyBranch:
   129  										assert.Equal(t, branch, pfp.Value)
   130  									case alpine_module.PropertyRepository:
   131  										assert.Equal(t, repository, pfp.Value)
   132  									case alpine_module.PropertyArchitecture:
   133  										assert.Equal(t, "x86_64", pfp.Value)
   134  									}
   135  								}
   136  							}
   137  						}
   138  						return seen
   139  					})
   140  				})
   141  
   142  				t.Run("Index", func(t *testing.T) {
   143  					defer tests.PrintCurrentTest(t)()
   144  
   145  					url := fmt.Sprintf("%s/%s/%s/x86_64/APKINDEX.tar.gz", rootURL, branch, repository)
   146  
   147  					req := NewRequest(t, "GET", url)
   148  					resp := MakeRequest(t, req, http.StatusOK)
   149  
   150  					assert.Condition(t, func() bool {
   151  						br := bufio.NewReader(resp.Body)
   152  
   153  						gzr, err := gzip.NewReader(br)
   154  						assert.NoError(t, err)
   155  
   156  						for {
   157  							gzr.Multistream(false)
   158  
   159  							tr := tar.NewReader(gzr)
   160  							for {
   161  								hd, err := tr.Next()
   162  								if err == io.EOF {
   163  									break
   164  								}
   165  								assert.NoError(t, err)
   166  
   167  								if hd.Name == "APKINDEX" {
   168  									buf, err := io.ReadAll(tr)
   169  									assert.NoError(t, err)
   170  
   171  									s := string(buf)
   172  
   173  									assert.Contains(t, s, "C:Q1/se1PjO94hYXbfpNR1/61hVORIc=\n")
   174  									assert.Contains(t, s, "P:"+packageName+"\n")
   175  									assert.Contains(t, s, "V:"+packageVersion+"\n")
   176  									assert.Contains(t, s, "A:x86_64\n")
   177  									assert.Contains(t, s, "T:Gitea Test Package\n")
   178  									assert.Contains(t, s, "U:https://gitea.io/\n")
   179  									assert.Contains(t, s, "L:MIT\n")
   180  									assert.Contains(t, s, "S:1353\n")
   181  									assert.Contains(t, s, "I:4096\n")
   182  									assert.Contains(t, s, "o:gitea-test\n")
   183  									assert.Contains(t, s, "m:KN4CK3R <kn4ck3r@gitea.io>\n")
   184  									assert.Contains(t, s, "t:1679498030\n")
   185  
   186  									return true
   187  								}
   188  							}
   189  
   190  							err = gzr.Reset(br)
   191  							if err == io.EOF {
   192  								break
   193  							}
   194  							assert.NoError(t, err)
   195  						}
   196  
   197  						return false
   198  					})
   199  				})
   200  
   201  				t.Run("Download", func(t *testing.T) {
   202  					defer tests.PrintCurrentTest(t)()
   203  
   204  					req := NewRequest(t, "GET", fmt.Sprintf("%s/%s/%s/x86_64/%s-%s.apk", rootURL, branch, repository, packageName, packageVersion))
   205  					MakeRequest(t, req, http.StatusOK)
   206  				})
   207  			})
   208  		}
   209  	}
   210  
   211  	t.Run("Delete", func(t *testing.T) {
   212  		defer tests.PrintCurrentTest(t)()
   213  
   214  		for _, branch := range branches {
   215  			for _, repository := range repositories {
   216  				req := NewRequest(t, "DELETE", fmt.Sprintf("%s/%s/%s/x86_64/%s-%s.apk", rootURL, branch, repository, packageName, packageVersion))
   217  				MakeRequest(t, req, http.StatusUnauthorized)
   218  
   219  				req = NewRequest(t, "DELETE", fmt.Sprintf("%s/%s/%s/x86_64/%s-%s.apk", rootURL, branch, repository, packageName, packageVersion))
   220  				AddBasicAuthHeader(req, user.Name)
   221  				MakeRequest(t, req, http.StatusNoContent)
   222  
   223  				// Deleting the last file of an architecture should remove that index
   224  				req = NewRequest(t, "GET", fmt.Sprintf("%s/%s/%s/x86_64/APKINDEX.tar.gz", rootURL, branch, repository))
   225  				MakeRequest(t, req, http.StatusNotFound)
   226  			}
   227  		}
   228  	})
   229  }