...

Source file src/helm.sh/helm/v3/pkg/getter/ocigetter_test.go

Documentation: helm.sh/helm/v3/pkg/getter

     1  /*
     2  Copyright The Helm Authors.
     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  
    16  package getter
    17  
    18  import (
    19  	"net/http"
    20  	"path/filepath"
    21  	"testing"
    22  	"time"
    23  
    24  	"helm.sh/helm/v3/pkg/registry"
    25  )
    26  
    27  func TestOCIGetter(t *testing.T) {
    28  	g, err := NewOCIGetter(WithURL("oci://example.com"))
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  
    33  	if _, ok := g.(*OCIGetter); !ok {
    34  		t.Fatal("Expected NewOCIGetter to produce an *OCIGetter")
    35  	}
    36  
    37  	cd := "../../testdata"
    38  	join := filepath.Join
    39  	ca, pub, priv := join(cd, "rootca.crt"), join(cd, "crt.pem"), join(cd, "key.pem")
    40  	timeout := time.Second * 5
    41  	transport := &http.Transport{}
    42  	insecureSkipVerifyTLS := false
    43  	plainHTTP := false
    44  
    45  	// Test with options
    46  	g, err = NewOCIGetter(
    47  		WithBasicAuth("I", "Am"),
    48  		WithTLSClientConfig(pub, priv, ca),
    49  		WithTimeout(timeout),
    50  		WithTransport(transport),
    51  		WithInsecureSkipVerifyTLS(insecureSkipVerifyTLS),
    52  		WithPlainHTTP(plainHTTP),
    53  	)
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  
    58  	og, ok := g.(*OCIGetter)
    59  	if !ok {
    60  		t.Fatal("expected NewOCIGetter to produce an *OCIGetter")
    61  	}
    62  
    63  	if og.opts.username != "I" {
    64  		t.Errorf("Expected NewOCIGetter to contain %q as the username, got %q", "I", og.opts.username)
    65  	}
    66  
    67  	if og.opts.password != "Am" {
    68  		t.Errorf("Expected NewOCIGetter to contain %q as the password, got %q", "Am", og.opts.password)
    69  	}
    70  
    71  	if og.opts.certFile != pub {
    72  		t.Errorf("Expected NewOCIGetter to contain %q as the public key file, got %q", pub, og.opts.certFile)
    73  	}
    74  
    75  	if og.opts.keyFile != priv {
    76  		t.Errorf("Expected NewOCIGetter to contain %q as the private key file, got %q", priv, og.opts.keyFile)
    77  	}
    78  
    79  	if og.opts.caFile != ca {
    80  		t.Errorf("Expected NewOCIGetter to contain %q as the CA file, got %q", ca, og.opts.caFile)
    81  	}
    82  
    83  	if og.opts.timeout != timeout {
    84  		t.Errorf("Expected NewOCIGetter to contain %s as Timeout flag, got %s", timeout, og.opts.timeout)
    85  	}
    86  
    87  	if og.opts.transport != transport {
    88  		t.Errorf("Expected NewOCIGetter to contain %p as Transport, got %p", transport, og.opts.transport)
    89  	}
    90  
    91  	if og.opts.plainHTTP != plainHTTP {
    92  		t.Errorf("Expected NewOCIGetter to have plainHTTP as %t, got %t", plainHTTP, og.opts.plainHTTP)
    93  	}
    94  
    95  	if og.opts.insecureSkipVerifyTLS != insecureSkipVerifyTLS {
    96  		t.Errorf("Expected NewOCIGetter to have insecureSkipVerifyTLS as %t, got %t", insecureSkipVerifyTLS, og.opts.insecureSkipVerifyTLS)
    97  	}
    98  
    99  	// Test if setting registryClient is being passed to the ops
   100  	registryClient, err := registry.NewClient()
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  
   105  	g, err = NewOCIGetter(
   106  		WithRegistryClient(registryClient),
   107  	)
   108  	if err != nil {
   109  		t.Fatal(err)
   110  	}
   111  	og, ok = g.(*OCIGetter)
   112  	if !ok {
   113  		t.Fatal("expected NewOCIGetter to produce an *OCIGetter")
   114  	}
   115  
   116  	if og.opts.registryClient != registryClient {
   117  		t.Errorf("Expected NewOCIGetter to contain %p as RegistryClient, got %p", registryClient, og.opts.registryClient)
   118  	}
   119  }
   120  
   121  func TestOCIHTTPTransportReuse(t *testing.T) {
   122  	g := OCIGetter{}
   123  
   124  	_, err := g.newRegistryClient()
   125  
   126  	if err != nil {
   127  		t.Fatal(err)
   128  	}
   129  
   130  	if g.transport == nil {
   131  		t.Fatalf("Expected non nil value for transport")
   132  	}
   133  
   134  	transport1 := g.transport
   135  
   136  	_, err = g.newRegistryClient()
   137  
   138  	if err != nil {
   139  		t.Fatal(err)
   140  	}
   141  
   142  	if g.transport == nil {
   143  		t.Fatalf("Expected non nil value for transport")
   144  	}
   145  
   146  	transport2 := g.transport
   147  
   148  	if transport1 != transport2 {
   149  		t.Fatalf("Expected default transport to be reused")
   150  	}
   151  }
   152  

View as plain text