Skip to content

Commit

Permalink
tests/lib/fakestore/store: do not hardcode port in fakestore unit tests
Browse files Browse the repository at this point in the history
The unit tests hardcode the port number, which occasionally seems to be occupied
by something else when running the whole test suite on GH (most likely tests
from another package). Have the port number be selected by the kernel.

Signed-off-by: Maciej Borzecki <[email protected]>
  • Loading branch information
bboozzoo committed May 10, 2024
1 parent 1b035da commit 212616c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions tests/lib/fakestore/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ func (s *Store) Start() error {
return err
}

s.url = fmt.Sprintf("http://%s", l.Addr())

go s.srv.Serve(l)
return nil
}
Expand Down
24 changes: 20 additions & 4 deletions tests/lib/fakestore/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ package store
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"syscall"
"testing"
"text/template"

Expand All @@ -47,8 +50,6 @@ type storeTestSuite struct {

var _ = Suite(&storeTestSuite{})

var defaultAddr = "localhost:23321"

func getSha(fn string) (string, uint64) {
snapDigest, size, err := asserts.SnapFileSHA3_384(fn)
if err != nil {
Expand All @@ -61,7 +62,7 @@ func (s *storeTestSuite) SetUpTest(c *C) {
topdir := c.MkDir()
err := os.Mkdir(filepath.Join(topdir, "asserts"), 0755)
c.Assert(err, IsNil)
s.store = NewStore(topdir, defaultAddr, false)
s.store = NewStore(topdir, "localhost:0", false)
err = s.store.Start()
c.Assert(err, IsNil)

Expand All @@ -88,7 +89,22 @@ func (s *storeTestSuite) StorePostJSON(path string, content []byte) (*http.Respo
}

func (s *storeTestSuite) TestStoreURL(c *C) {
c.Assert(s.store.URL(), Equals, "http://"+defaultAddr)
u, err := url.Parse(s.store.URL())
c.Assert(err, IsNil)
c.Check(u.Hostname(), Equals, "127.0.0.1")
c.Check(u.Port(), Not(Equals), "")
}

func (s *storeTestSuite) TestStoreListenAddr(c *C) {
u, err := url.Parse(s.store.URL())
c.Assert(err, IsNil)
// use the same address as the fake store started by the tests so that
// this will fail with addr-in-use error
topdir := c.MkDir()
newstore := NewStore(topdir, u.Host, false)
err = newstore.Start()
c.Assert(err, NotNil)
c.Check(errors.Is(err, syscall.EADDRINUSE), Equals, true)
}

func (s *storeTestSuite) TestTrivialGetWorks(c *C) {
Expand Down

0 comments on commit 212616c

Please sign in to comment.