github.com/quay/claircore@v1.5.28/aws/internal/alas/repomd_test.go (about)

     1  // Copyright 2019 RedHat
     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 alas
    16  
    17  import (
    18  	"encoding/xml"
    19  	"os"
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  )
    25  
    26  func Test_RepoMD_Parse(t *testing.T) {
    27  	path := filepath.Join("testdata", "test_repomd.xml")
    28  	f, err := os.Open(path)
    29  	if err != nil {
    30  		t.Fatalf("failed to open test data: %v", err)
    31  	}
    32  
    33  	repomdRoot := &RepoMD{}
    34  	err = xml.NewDecoder(f).Decode(repomdRoot)
    35  	if err != nil {
    36  		t.Fatalf("failed to parse repomd test data into struct: %v", err)
    37  	}
    38  
    39  	if got, want := len(repomdRoot.RepoList), 6; got != want {
    40  		t.Errorf("bad repo list length: got: %d, want: %d", got, want)
    41  	}
    42  }
    43  
    44  func Test_RepoMD_GetRepo(t *testing.T) {
    45  	tests := []RepoType{PrimaryDB, OtherDB, GroupGZ, Group, FileLists, UpdateInfo}
    46  
    47  	path := filepath.Join("testdata", "test_repomd.xml")
    48  	f, err := os.Open(path)
    49  	if err != nil {
    50  		t.Fatalf("failed to open test data: %v", err)
    51  	}
    52  
    53  	repomdRoot := &RepoMD{}
    54  	err = xml.NewDecoder(f).Decode(repomdRoot)
    55  	if err != nil {
    56  		t.Fatalf("failed to parse repomd test data into struct: %v", err)
    57  	}
    58  
    59  	for _, test := range tests {
    60  		repo, err := repomdRoot.Repo(test, "")
    61  		if err != nil {
    62  			t.Error(err)
    63  		}
    64  		if repo == nil {
    65  			t.Error("return was <nil>")
    66  		}
    67  	}
    68  }
    69  
    70  func Test_RepoMD_FQDN(t *testing.T) {
    71  	tests := []struct {
    72  		repoType     RepoType
    73  		expectedFQDN string
    74  	}{
    75  		{
    76  			PrimaryDB,
    77  			"http://test-mirror/repodata/primary.sqlite.bz2",
    78  		}, {
    79  			OtherDB,
    80  			"http://test-mirror/repodata/other.sqlite.bz2",
    81  		}, {
    82  			GroupGZ,
    83  			"http://test-mirror/repodata/comps.xml.gz",
    84  		}, {
    85  			Group,
    86  			"http://test-mirror/repodata/comps.xml",
    87  		}, {
    88  			FileLists,
    89  			"http://test-mirror/repodata/filelists.sqlite.bz2",
    90  		}, {
    91  			UpdateInfo,
    92  			"http://test-mirror/repodata/updateinfo.xml.gz",
    93  		},
    94  	}
    95  
    96  	path := filepath.Join("testdata", "test_repomd.xml")
    97  	f, err := os.Open(path)
    98  	if err != nil {
    99  		t.Fatalf("failed to open test data: %v", err)
   100  	}
   101  
   102  	repomdRoot := &RepoMD{}
   103  	err = xml.NewDecoder(f).Decode(repomdRoot)
   104  	if err != nil {
   105  		t.Fatalf("failed to parse repomd test data into struct: %v", err)
   106  	}
   107  
   108  	for _, test := range tests {
   109  		repo, err := repomdRoot.Repo(test.repoType, "http://test-mirror/")
   110  		if err != nil {
   111  			t.Error(err)
   112  		}
   113  		if repo == nil {
   114  			t.Error("return was <nil>")
   115  		}
   116  		if t.Failed() {
   117  			continue
   118  		}
   119  		if got, want := test.expectedFQDN, repo.Location.Href; got != want {
   120  			t.Error(cmp.Diff(got, want))
   121  		}
   122  	}
   123  }