github.com/containers/podman/v4@v4.9.4/pkg/bindings/test/manifests_test.go (about)

     1  package bindings_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"net/http"
     7  	"time"
     8  
     9  	podmanRegistry "github.com/containers/podman/v4/hack/podman-registry-go"
    10  	"github.com/containers/podman/v4/pkg/bindings"
    11  	"github.com/containers/podman/v4/pkg/bindings/images"
    12  	"github.com/containers/podman/v4/pkg/bindings/manifests"
    13  	. "github.com/onsi/ginkgo/v2"
    14  	. "github.com/onsi/gomega"
    15  	"github.com/onsi/gomega/gexec"
    16  )
    17  
    18  var _ = Describe("Podman manifests", func() {
    19  	var (
    20  		bt *bindingTest
    21  		s  *gexec.Session
    22  	)
    23  
    24  	BeforeEach(func() {
    25  		bt = newBindingTest()
    26  		bt.RestoreImagesFromCache()
    27  		s = bt.startAPIService()
    28  		time.Sleep(1 * time.Second)
    29  		err := bt.NewConnection()
    30  		Expect(err).ToNot(HaveOccurred())
    31  
    32  	})
    33  
    34  	AfterEach(func() {
    35  		s.Kill()
    36  		bt.cleanup()
    37  	})
    38  
    39  	It("create", func() {
    40  		// create manifest list without images
    41  		id, err := manifests.Create(bt.conn, "quay.io/libpod/foobar:latest", []string{}, nil)
    42  		Expect(err).ToNot(HaveOccurred(), err)
    43  		list, err := manifests.Inspect(bt.conn, id, nil)
    44  		Expect(err).ToNot(HaveOccurred())
    45  
    46  		Expect(list.Manifests).To(BeEmpty())
    47  
    48  		// creating a duplicate should fail as a 500
    49  		_, err = manifests.Create(bt.conn, "quay.io/libpod/foobar:latest", nil, nil)
    50  		Expect(err).To(HaveOccurred())
    51  
    52  		code, _ := bindings.CheckResponseCode(err)
    53  		Expect(code).To(BeNumerically("==", http.StatusInternalServerError))
    54  
    55  		_, errs := images.Remove(bt.conn, []string{id}, nil)
    56  		Expect(errs).To(BeEmpty())
    57  
    58  		// create manifest list with images
    59  		id, err = manifests.Create(bt.conn, "quay.io/libpod/foobar:latest", []string{alpine.name}, nil)
    60  		Expect(err).ToNot(HaveOccurred())
    61  
    62  		list, err = manifests.Inspect(bt.conn, id, nil)
    63  		Expect(err).ToNot(HaveOccurred())
    64  
    65  		Expect(list.Manifests).To(HaveLen(1))
    66  	})
    67  
    68  	It("delete manifest", func() {
    69  		id, err := manifests.Create(bt.conn, "quay.io/libpod/foobar:latest", []string{}, nil)
    70  		Expect(err).ToNot(HaveOccurred(), err)
    71  		list, err := manifests.Inspect(bt.conn, id, nil)
    72  		Expect(err).ToNot(HaveOccurred())
    73  
    74  		Expect(list.Manifests).To(BeEmpty())
    75  
    76  		removeReport, err := manifests.Delete(bt.conn, "quay.io/libpod/foobar:latest")
    77  		Expect(err).ToNot(HaveOccurred())
    78  		Expect(removeReport.Deleted).To(HaveLen(1))
    79  	})
    80  
    81  	It("inspect", func() {
    82  		_, err := manifests.Inspect(bt.conn, "larry", nil)
    83  		Expect(err).To(HaveOccurred())
    84  
    85  		code, _ := bindings.CheckResponseCode(err)
    86  		Expect(code).To(BeNumerically("==", http.StatusNotFound))
    87  	})
    88  
    89  	It("add", func() {
    90  		// add to bogus should 404
    91  		_, err := manifests.Add(bt.conn, "foobar", nil)
    92  		Expect(err).To(HaveOccurred())
    93  
    94  		code, _ := bindings.CheckResponseCode(err)
    95  		Expect(code).To(BeNumerically("==", http.StatusNotFound), err.Error())
    96  
    97  		id, err := manifests.Create(bt.conn, "quay.io/libpod/foobar:latest", []string{}, nil)
    98  		Expect(err).ToNot(HaveOccurred())
    99  
   100  		options := new(manifests.AddOptions).WithImages([]string{alpine.name})
   101  		_, err = manifests.Add(bt.conn, id, options)
   102  		Expect(err).ToNot(HaveOccurred())
   103  
   104  		list, err := manifests.Inspect(bt.conn, id, nil)
   105  		Expect(err).ToNot(HaveOccurred())
   106  
   107  		Expect(list.Manifests).To(HaveLen(1))
   108  
   109  		// add bogus name to existing list should fail
   110  		options.WithImages([]string{"larry"})
   111  		_, err = manifests.Add(bt.conn, id, options)
   112  		Expect(err).To(HaveOccurred())
   113  
   114  		code, _ = bindings.CheckResponseCode(err)
   115  		Expect(code).To(BeNumerically("==", http.StatusBadRequest))
   116  	})
   117  
   118  	It("remove digest", func() {
   119  		// removal on bogus manifest list should be 404
   120  		_, err := manifests.Remove(bt.conn, "larry", "1234", nil)
   121  		Expect(err).To(HaveOccurred())
   122  
   123  		code, _ := bindings.CheckResponseCode(err)
   124  		Expect(code).To(BeNumerically("==", http.StatusNotFound))
   125  
   126  		id, err := manifests.Create(bt.conn, "quay.io/libpod/foobar:latest", []string{alpine.name}, nil)
   127  		Expect(err).ToNot(HaveOccurred())
   128  
   129  		data, err := manifests.Inspect(bt.conn, id, nil)
   130  		Expect(err).ToNot(HaveOccurred())
   131  
   132  		Expect(data.Manifests).To(HaveLen(1))
   133  
   134  		// removal on a good manifest list with a bad digest should be 400
   135  		_, err = manifests.Remove(bt.conn, id, "!234", nil)
   136  		Expect(err).To(HaveOccurred())
   137  
   138  		code, _ = bindings.CheckResponseCode(err)
   139  		Expect(code).To(BeNumerically("==", http.StatusBadRequest))
   140  
   141  		digest := data.Manifests[0].Digest.String()
   142  		_, err = manifests.Remove(bt.conn, id, digest, nil)
   143  		Expect(err).ToNot(HaveOccurred())
   144  
   145  		// removal on good manifest with good digest should work
   146  		data, err = manifests.Inspect(bt.conn, id, nil)
   147  		Expect(err).ToNot(HaveOccurred())
   148  
   149  		Expect(data.Manifests).Should(BeEmpty())
   150  	})
   151  
   152  	It("annotate", func() {
   153  		id, err := manifests.Create(bt.conn, "quay.io/libpod/foobar:latest", []string{}, nil)
   154  		Expect(err).ToNot(HaveOccurred())
   155  
   156  		opts := manifests.AddOptions{Images: []string{"quay.io/libpod/alpine:latest"}}
   157  
   158  		_, err = manifests.Add(bt.conn, id, &opts)
   159  		Expect(err).ToNot(HaveOccurred())
   160  
   161  		data, err := manifests.Inspect(bt.conn, id, nil)
   162  		Expect(err).ToNot(HaveOccurred())
   163  
   164  		Expect(data.Manifests).To(HaveLen(1))
   165  
   166  		digest := data.Manifests[0].Digest.String()
   167  		annoOpts := new(manifests.ModifyOptions).WithOS("foo")
   168  		_, err = manifests.Annotate(bt.conn, id, []string{digest}, annoOpts)
   169  		Expect(err).ToNot(HaveOccurred())
   170  
   171  		list, err := manifests.Inspect(bt.conn, id, nil)
   172  		Expect(err).ToNot(HaveOccurred())
   173  
   174  		Expect(list.Manifests).To(HaveLen(1))
   175  		Expect(list.Manifests[0].Platform.OS).To(Equal("foo"))
   176  	})
   177  
   178  	It("Manifest Push", func() {
   179  		registryOptions := &podmanRegistry.Options{
   180  			PodmanPath: getPodmanBinary(),
   181  		}
   182  		registry, err := podmanRegistry.StartWithOptions(registryOptions)
   183  		Expect(err).ToNot(HaveOccurred())
   184  
   185  		name := "quay.io/libpod/foobar:latest"
   186  		_, err = manifests.Create(bt.conn, name, []string{alpine.name}, nil)
   187  		Expect(err).ToNot(HaveOccurred())
   188  
   189  		var writer bytes.Buffer
   190  		pushOpts := new(images.PushOptions).WithUsername(registry.User).WithPassword(registry.Password).WithAll(true).WithSkipTLSVerify(true).WithProgressWriter(&writer).WithQuiet(false)
   191  		_, err = manifests.Push(bt.conn, name, fmt.Sprintf("localhost:%s/test:latest", registry.Port), pushOpts)
   192  		Expect(err).ToNot(HaveOccurred())
   193  
   194  		output := writer.String()
   195  		Expect(output).To(ContainSubstring("Writing manifest list to image destination"))
   196  		Expect(output).To(ContainSubstring("Storing list signatures"))
   197  	})
   198  })