Skip to content

Commit

Permalink
Check that provided mirrolist file contains some mirror urls
Browse files Browse the repository at this point in the history
Closes #86
  • Loading branch information
anatol committed Aug 2, 2023
1 parent 4995568 commit 61cf111
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
11 changes: 8 additions & 3 deletions urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bufio"
"fmt"
"log"
"os"
"strings"
Expand Down Expand Up @@ -67,8 +68,12 @@ func (r *Repo) getMirrorlistURLs() ([]string, error) {
}

urls, err := parseMirrorlistURLs(file)
if err == nil {
r.URLs = urls
if err != nil {
return nil, err
}
if len(urls) == 0 {
return nil, fmt.Errorf("mirrorlist file %s contains no mirrors", r.Mirrorlist)
}
return urls, err
r.URLs = urls
return urls, nil
}
35 changes: 21 additions & 14 deletions urls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,7 @@ func TestGetCurrentURLs(t *testing.T) {
temp := t.TempDir()
tmpMirrorfile := path.Join(temp, "tmpMirrorFile")

f, err := os.Create(tmpMirrorfile)
if err == nil {
f.Write([]byte(mirrorlist))
f.Close()
f, err = os.Open(tmpMirrorfile)
}

require.NoError(t, err)
require.NoError(t, os.WriteFile(tmpMirrorfile, []byte(mirrorlist), 0o644))

config := parseConfig([]byte(`
cache_dir: ` + temp + `
Expand All @@ -80,15 +73,29 @@ repos:
`))
archTest := config.Repos["archTest"]
urls := archTest.getUrls()

require.Equal(t, urls, expectedURLs)
require.Equal(t, expectedURLs, archTest.getUrls())

fileInfo, _ := os.Stat(tmpMirrorfile)
expectedModTime := fileInfo.ModTime()
gotModTime := archTest.LastModificationTime
require.Equal(t, gotModTime, expectedModTime)
require.Equal(t, fileInfo.ModTime(), archTest.LastModificationTime)

gotCheckTime := archTest.LastMirrorlistCheck
require.LessOrEqual(t, time.Since(gotCheckTime), 3*time.Second)
}

func TestEmptyMirrorlist(t *testing.T) {
temp := t.TempDir()
tmpMirrorfile := path.Join(temp, "tmpMirrorFile")

require.NoError(t, os.WriteFile(tmpMirrorfile, []byte(""), 0o644))

config := parseConfig([]byte(`
cache_dir: ` + temp + `
repos:
archTest:
mirrorlist: ` + tmpMirrorfile + `
`))
archTest := config.Repos["archTest"]
urls, err := archTest.getMirrorlistURLs()
require.Error(t, err)
require.Nil(t, urls)
}

0 comments on commit 61cf111

Please sign in to comment.