github.com/quay/claircore@v1.5.28/aws/client_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"net/url"
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  	"github.com/quay/zlog"
    12  )
    13  
    14  type clientTestcase struct {
    15  	Release  Release
    16  	Serve    string
    17  	Expected []string
    18  }
    19  
    20  func (tc *clientTestcase) Run(ctx context.Context) func(*testing.T) {
    21  	var err error
    22  	want := make([]*url.URL, len(tc.Expected))
    23  	for i, s := range tc.Expected {
    24  		want[i], err = url.Parse(s)
    25  		if err != nil {
    26  			panic(err)
    27  		}
    28  	}
    29  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    30  		http.ServeFile(w, r, tc.Serve)
    31  	}))
    32  	client := Client{
    33  		c:       srv.Client(),
    34  		mirrors: make([]*url.URL, 0),
    35  	}
    36  
    37  	return func(t *testing.T) {
    38  		ctx := zlog.Test(ctx, t)
    39  		t.Cleanup(srv.Close)
    40  
    41  		err := client.getMirrors(ctx, srv.URL)
    42  		if err != nil {
    43  			t.Error(err)
    44  		}
    45  		t.Log(client.mirrors)
    46  		if got := client.mirrors; !cmp.Equal(got, want) {
    47  			t.Error(cmp.Diff(got, want))
    48  		}
    49  	}
    50  }
    51  func TestClientGetMirrors(t *testing.T) {
    52  	ctx := context.Background()
    53  	tests := []clientTestcase{
    54  		{
    55  			Release: AmazonLinux1,
    56  			Expected: []string{
    57  				"http://packages.us-west-2.amazonaws.com/2018.03/updates/c539f2128d87/x86_64",
    58  				"http://packages.us-west-1.amazonaws.com/2018.03/updates/c539f2128d87/x86_64",
    59  				"http://packages.us-east-1.amazonaws.com/2018.03/updates/c539f2128d87/x86_64",
    60  				"http://packages.ap-southeast-1.amazonaws.com/2018.03/updates/c539f2128d87/x86_64",
    61  				"http://packages.ap-northeast-1.amazonaws.com/2018.03/updates/c539f2128d87/x86_64",
    62  				"http://packages.ap-northeast-2.amazonaws.com/2018.03/updates/c539f2128d87/x86_64",
    63  				"http://packages.ap-east-1.amazonaws.com/2018.03/updates/c539f2128d87/x86_64",
    64  				"http://packages.eu-west-1.amazonaws.com/2018.03/updates/c539f2128d87/x86_64",
    65  				"http://packages.eu-central-1.amazonaws.com/2018.03/updates/c539f2128d87/x86_64",
    66  				"http://packages.sa-east-1.amazonaws.com/2018.03/updates/c539f2128d87/x86_64",
    67  				"http://packages.ap-southeast-2.amazonaws.com/2018.03/updates/c539f2128d87/x86_64",
    68  			},
    69  			Serve: "testdata/mirrors_linux1.txt",
    70  		},
    71  		{
    72  			Release: AmazonLinux2,
    73  			Expected: []string{
    74  				"https://cdn.amazonlinux.com/2/core/2.0/x86_64/221a4af09d96ac4e34202cc7bdfa252410419542548cc685dc86ed1c17ca4204",
    75  			},
    76  			Serve: "testdata/mirrors_linux2.txt",
    77  		},
    78  		{
    79  			Release: AmazonLinux2023,
    80  			Expected: []string{
    81  				"https://cdn.amazonlinux.com/al2023/core/guids/46ff4933b89b948580f3b223b826fee3c1830b85885db3f7f90502c0ac99698c/x86_64/",
    82  			},
    83  			Serve: "testdata/mirrors_linux2023.txt",
    84  		},
    85  	}
    86  
    87  	for _, tc := range tests {
    88  		t.Run(string(tc.Release), tc.Run(ctx))
    89  	}
    90  }