code.gitea.io/gitea@v1.19.3/modules/packages/container/metadata_test.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package container
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  
    10  	"code.gitea.io/gitea/modules/packages/container/helm"
    11  
    12  	oci "github.com/opencontainers/image-spec/specs-go/v1"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestParseImageConfig(t *testing.T) {
    17  	description := "Image Description"
    18  	author := "Gitea"
    19  	license := "MIT"
    20  	projectURL := "https://gitea.io"
    21  	repositoryURL := "https://gitea.com/gitea"
    22  	documentationURL := "https://docs.gitea.io"
    23  
    24  	configOCI := `{"config": {"labels": {"` + labelAuthors + `": "` + author + `", "` + labelLicenses + `": "` + license + `", "` + labelURL + `": "` + projectURL + `", "` + labelSource + `": "` + repositoryURL + `", "` + labelDocumentation + `": "` + documentationURL + `", "` + labelDescription + `": "` + description + `"}}, "history": [{"created_by": "do it 1"}, {"created_by": "dummy #(nop) do it 2"}]}`
    25  
    26  	metadata, err := ParseImageConfig(oci.MediaTypeImageManifest, strings.NewReader(configOCI))
    27  	assert.NoError(t, err)
    28  
    29  	assert.Equal(t, TypeOCI, metadata.Type)
    30  	assert.Equal(t, description, metadata.Description)
    31  	assert.ElementsMatch(t, []string{author}, metadata.Authors)
    32  	assert.Equal(t, license, metadata.Licenses)
    33  	assert.Equal(t, projectURL, metadata.ProjectURL)
    34  	assert.Equal(t, repositoryURL, metadata.RepositoryURL)
    35  	assert.Equal(t, documentationURL, metadata.DocumentationURL)
    36  	assert.Equal(t, []string{"do it 1", "do it 2"}, metadata.ImageLayers)
    37  	assert.Equal(
    38  		t,
    39  		map[string]string{
    40  			labelAuthors:       author,
    41  			labelLicenses:      license,
    42  			labelURL:           projectURL,
    43  			labelSource:        repositoryURL,
    44  			labelDocumentation: documentationURL,
    45  			labelDescription:   description,
    46  		},
    47  		metadata.Labels,
    48  	)
    49  	assert.Empty(t, metadata.MultiArch)
    50  
    51  	configHelm := `{"description":"` + description + `", "home": "` + projectURL + `", "sources": ["` + repositoryURL + `"], "maintainers":[{"name":"` + author + `"}]}`
    52  
    53  	metadata, err = ParseImageConfig(helm.ConfigMediaType, strings.NewReader(configHelm))
    54  	assert.NoError(t, err)
    55  
    56  	assert.Equal(t, TypeHelm, metadata.Type)
    57  	assert.Equal(t, description, metadata.Description)
    58  	assert.ElementsMatch(t, []string{author}, metadata.Authors)
    59  	assert.Equal(t, projectURL, metadata.ProjectURL)
    60  	assert.Equal(t, repositoryURL, metadata.RepositoryURL)
    61  }