sigs.k8s.io/cluster-api@v1.7.1/internal/goproxy/goproxy_test.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package goproxy
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"net/http"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/blang/semver/v4"
    27  	. "github.com/onsi/gomega"
    28  
    29  	goproxytest "sigs.k8s.io/cluster-api/internal/goproxy/test"
    30  )
    31  
    32  func TestClient_GetVersions(t *testing.T) {
    33  	retryableOperationInterval = 200 * time.Millisecond
    34  	retryableOperationTimeout = 1 * time.Second
    35  
    36  	scheme, host, muxGoproxy, teardownGoproxy := goproxytest.NewFakeGoproxy()
    37  	clientGoproxy := NewClient(scheme, host)
    38  	defer teardownGoproxy()
    39  
    40  	// setup an handler for returning 2 fake releases
    41  	muxGoproxy.HandleFunc("/github.com/o/r1/@v/list", func(w http.ResponseWriter, r *http.Request) {
    42  		goproxytest.HTTPTestMethod(t, r, "GET")
    43  		fmt.Fprint(w, "v1.1.0\n")
    44  		fmt.Fprint(w, "v0.2.0\n")
    45  	})
    46  
    47  	// setup an handler for returning 2 fake releases for v1
    48  	muxGoproxy.HandleFunc("/github.com/o/r2/@v/list", func(w http.ResponseWriter, r *http.Request) {
    49  		goproxytest.HTTPTestMethod(t, r, "GET")
    50  		fmt.Fprint(w, "v1.1.0\n")
    51  		fmt.Fprint(w, "v0.2.0\n")
    52  	})
    53  	// setup an handler for returning 2 fake releases for v2
    54  	muxGoproxy.HandleFunc("/github.com/o/r2/v2/@v/list", func(w http.ResponseWriter, r *http.Request) {
    55  		goproxytest.HTTPTestMethod(t, r, "GET")
    56  		fmt.Fprint(w, "v2.0.1\n")
    57  		fmt.Fprint(w, "v2.0.0\n")
    58  	})
    59  
    60  	tests := []struct {
    61  		name         string
    62  		gomodulePath string
    63  		want         semver.Versions
    64  		wantErr      bool
    65  	}{
    66  		{
    67  			"No versions",
    68  			"github.com/o/doesntexist",
    69  			nil,
    70  			true,
    71  		},
    72  		{
    73  			"Two versions < v2",
    74  			"github.com/o/r1",
    75  			semver.Versions{
    76  				semver.MustParse("0.2.0"),
    77  				semver.MustParse("1.1.0"),
    78  			},
    79  			false,
    80  		},
    81  		{
    82  			"Multiple versiosn including > v1",
    83  			"github.com/o/r2",
    84  			semver.Versions{
    85  				semver.MustParse("0.2.0"),
    86  				semver.MustParse("1.1.0"),
    87  				semver.MustParse("2.0.0"),
    88  				semver.MustParse("2.0.1"),
    89  			},
    90  			false,
    91  		},
    92  	}
    93  	for _, tt := range tests {
    94  		t.Run(tt.name, func(t *testing.T) {
    95  			ctx := context.Background()
    96  			g := NewWithT(t)
    97  
    98  			got, err := clientGoproxy.GetVersions(ctx, tt.gomodulePath)
    99  			if tt.wantErr {
   100  				g.Expect(err).To(HaveOccurred())
   101  				return
   102  			}
   103  			g.Expect(err).ToNot(HaveOccurred())
   104  
   105  			g.Expect(got).To(BeEquivalentTo(tt.want))
   106  		})
   107  	}
   108  }
   109  
   110  func Test_GetGoproxyHost(t *testing.T) {
   111  	retryableOperationInterval = 200 * time.Millisecond
   112  	retryableOperationTimeout = 1 * time.Second
   113  
   114  	tests := []struct {
   115  		name       string
   116  		envvar     string
   117  		wantScheme string
   118  		wantHost   string
   119  		wantErr    bool
   120  	}{
   121  		{
   122  			name:       "defaulting",
   123  			envvar:     "",
   124  			wantScheme: "https",
   125  			wantHost:   "proxy.golang.org",
   126  			wantErr:    false,
   127  		},
   128  		{
   129  			name:       "direct falls back to empty strings",
   130  			envvar:     "direct",
   131  			wantScheme: "",
   132  			wantHost:   "",
   133  			wantErr:    false,
   134  		},
   135  		{
   136  			name:       "off falls back to empty strings",
   137  			envvar:     "off",
   138  			wantScheme: "",
   139  			wantHost:   "",
   140  			wantErr:    false,
   141  		},
   142  		{
   143  			name:       "other goproxy",
   144  			envvar:     "foo.bar.de",
   145  			wantScheme: "https",
   146  			wantHost:   "foo.bar.de",
   147  			wantErr:    false,
   148  		},
   149  		{
   150  			name:       "other goproxy comma separated, return first",
   151  			envvar:     "foo.bar,foobar.barfoo",
   152  			wantScheme: "https",
   153  			wantHost:   "foo.bar",
   154  			wantErr:    false,
   155  		},
   156  		{
   157  			name:       "other goproxy including https scheme",
   158  			envvar:     "https://foo.bar",
   159  			wantScheme: "https",
   160  			wantHost:   "foo.bar",
   161  			wantErr:    false,
   162  		},
   163  		{
   164  			name:       "other goproxy including http scheme",
   165  			envvar:     "http://foo.bar",
   166  			wantScheme: "http",
   167  			wantHost:   "foo.bar",
   168  			wantErr:    false,
   169  		},
   170  	}
   171  	for _, tt := range tests {
   172  		t.Run(tt.name, func(t *testing.T) {
   173  			g := NewWithT(t)
   174  			gotScheme, gotHost, err := GetSchemeAndHost(tt.envvar)
   175  			if tt.wantErr {
   176  				g.Expect(err).To(HaveOccurred())
   177  				return
   178  			}
   179  			g.Expect(err).ToNot(HaveOccurred())
   180  
   181  			g.Expect(gotScheme).To(Equal(tt.wantScheme))
   182  			g.Expect(gotHost).To(Equal(tt.wantHost))
   183  		})
   184  	}
   185  }