...

Source file src/github.com/google/go-containerregistry/internal/cmd/edit_test.go

Documentation: github.com/google/go-containerregistry/internal/cmd

     1  // Copyright 2022 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cmd
    16  
    17  import (
    18  	"bytes"
    19  	"io"
    20  	"net/http/httptest"
    21  	"net/url"
    22  	"path"
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/google/go-containerregistry/pkg/crane"
    27  	"github.com/google/go-containerregistry/pkg/registry"
    28  	"github.com/google/go-containerregistry/pkg/v1/random"
    29  )
    30  
    31  func mustRegistry(t *testing.T) (*httptest.Server, string) {
    32  	t.Helper()
    33  	s := httptest.NewServer(registry.New())
    34  	u, err := url.Parse(s.URL)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	return s, u.Host
    39  }
    40  
    41  func TestEditConfig(t *testing.T) {
    42  	reg, host := mustRegistry(t)
    43  	defer reg.Close()
    44  	src := path.Join(host, "crane/edit/config")
    45  
    46  	img, err := random.Image(1024, 1)
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	if err := crane.Push(img, src); err != nil {
    51  		t.Fatal(err)
    52  	}
    53  
    54  	cmd := NewCmdEditConfig(&[]crane.Option{})
    55  	cmd.SetArgs([]string{src})
    56  	cmd.SetIn(strings.NewReader("{}"))
    57  
    58  	if err := cmd.Execute(); err != nil {
    59  		t.Fatal(err)
    60  	}
    61  }
    62  
    63  func TestEditManifest(t *testing.T) {
    64  	reg, host := mustRegistry(t)
    65  	defer reg.Close()
    66  	src := path.Join(host, "crane/edit/manifest")
    67  
    68  	img, err := random.Image(1024, 1)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	if err := crane.Push(img, src); err != nil {
    73  		t.Fatal(err)
    74  	}
    75  
    76  	cmd := NewCmdEditManifest(&[]crane.Option{})
    77  	cmd.SetArgs([]string{src})
    78  	cmd.SetIn(strings.NewReader("{}"))
    79  	if err := cmd.Execute(); err != nil {
    80  		t.Fatal(err)
    81  	}
    82  }
    83  
    84  func TestEditFilesystem(t *testing.T) {
    85  	reg, host := mustRegistry(t)
    86  	defer reg.Close()
    87  	src := path.Join(host, "crane/edit/fs")
    88  
    89  	img, err := random.Image(1024, 1)
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  	if err := crane.Push(img, src); err != nil {
    94  		t.Fatal(err)
    95  	}
    96  
    97  	cmd := NewCmdEditFs(&[]crane.Option{})
    98  	cmd.SetArgs([]string{src})
    99  	cmd.Flags().Set("filename", "/foo/bar")
   100  	cmd.SetIn(strings.NewReader("baz"))
   101  	if err := cmd.Execute(); err != nil {
   102  		t.Fatal(err)
   103  	}
   104  
   105  	img, err = crane.Pull(src)
   106  	if err != nil {
   107  		t.Fatal(err)
   108  	}
   109  
   110  	r, _, err := findFile(img, "/foo/bar")
   111  	if err != nil {
   112  		t.Fatal(err)
   113  	}
   114  
   115  	got, err := io.ReadAll(r)
   116  	if err != nil {
   117  		t.Fatal(err)
   118  	}
   119  
   120  	if !bytes.Equal(got, []byte("baz")) {
   121  		t.Fatalf("got: %s, want %s", got, "baz")
   122  	}
   123  
   124  	// Edit the same file to make sure we can edit existing files.
   125  	cmd = NewCmdEditFs(&[]crane.Option{})
   126  	cmd.SetArgs([]string{src})
   127  	cmd.Flags().Set("filename", "/foo/bar")
   128  	cmd.SetIn(strings.NewReader("quux"))
   129  	if err := cmd.Execute(); err != nil {
   130  		t.Fatal(err)
   131  	}
   132  
   133  	img, err = crane.Pull(src)
   134  	if err != nil {
   135  		t.Fatal(err)
   136  	}
   137  
   138  	r, _, err = findFile(img, "/foo/bar")
   139  	if err != nil {
   140  		t.Fatal(err)
   141  	}
   142  
   143  	got, err = io.ReadAll(r)
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  
   148  	if !bytes.Equal(got, []byte("quux")) {
   149  		t.Fatalf("got: %s, want %s", got, "quux")
   150  	}
   151  }
   152  
   153  func TestFindFile(t *testing.T) {
   154  	img, err := random.Image(1024, 1)
   155  	if err != nil {
   156  		t.Fatal(err)
   157  	}
   158  	r, h, err := findFile(img, "/does-not-exist")
   159  	if err != nil {
   160  		t.Fatal(err)
   161  	}
   162  
   163  	b, err := io.ReadAll(r)
   164  	if err != nil {
   165  		t.Fatal(err)
   166  	}
   167  	if len(b) != 0 {
   168  		t.Errorf("expected empty reader, got: %s", string(b))
   169  	}
   170  
   171  	if h.Name != "/does-not-exist" {
   172  		t.Errorf("tar.Header has wrong name: %v", h)
   173  	}
   174  }
   175  

View as plain text